123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 |
- //----------------------------------------------------------------------------
- /// @file search.hpp
- /// @brief
- /// @author Copyright (c) 2017 Francisco José Tapia (fjtapia@gmail.com )\n
- /// Distributed under the Boost Software License, Version 1.0.\n
- /// ( See copy at http://www.boost.org/LICENSE_1_0.txt )
- /// @remarks
- //-----------------------------------------------------------------------------
- #ifndef __BOOST_SORT_COMMON_SEARCH_HPP
- #define __BOOST_SORT_COMMON_SEARCH_HPP
- #include <ciso646>
- #include <cassert>
- #include <boost/sort/common/util/traits.hpp>
- namespace boost
- {
- namespace sort
- {
- namespace common
- {
- namespace util
- {
- template<class T>
- struct filter_pass
- {
- typedef T key;
- const key & operator()(const T & val) const
- {
- return val;
- };
- };
- //
- //###########################################################################
- // ##
- // ################################################################ ##
- // # # ##
- // # I N T E R N A L F U N C T I O N S # ##
- // # # ##
- // ################################################################ ##
- // ##
- // I M P O R T A N T ##
- // ##
- // These functions are not directly callable by the user, are for internal ##
- // use only. ##
- // These functions don't check the parameters ##
- // ##
- //###########################################################################
- //
- //-----------------------------------------------------------------------------
- // function : internal_find_first
- /// @brief find if a value exist in the range [first, last).
- /// Always return as valid iterator in the range [first, last-1]
- /// If exist return the iterator to the first occurrence. If don't exist
- /// return the first greater than val.
- /// If val is greater than the *(last-1), return (last-1)
- /// If val is lower than (*first), return first
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found,
- //-----------------------------------------------------------------------------
- template <class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
- class Compare = std::less<typename Filter::key> >
- inline Iter_t internal_find_first(Iter_t first, Iter_t last,
- const typename Filter::key &val,
- const Compare & comp = Compare(),
- Filter flt = Filter())
- {
- Iter_t LI = first, LS = last - 1, it_out = first;
- while (LI != LS)
- {
- it_out = LI + ((LS - LI) >> 1);
- if (comp(flt(*it_out), val))
- LI = it_out + 1;
- else LS = it_out;
- };
- return LS;
- };
- //
- //-----------------------------------------------------------------------------
- // function : internal_find_last
- /// @brief find if a value exist in the range [first, last).
- /// Always return as valid iterator in the range [first, last-1]
- /// If exist return the iterator to the last occurrence.
- /// If don't exist return the first lower than val.
- /// If val is greater than *(last-1) return (last-1).
- /// If is lower than the first, return first
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found, if not found return last
- //-----------------------------------------------------------------------------
- template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
- class Compare = std::less<typename Filter::key> >
- inline Iter_t internal_find_last(Iter_t first, Iter_t last,
- const typename Filter::key &val,
- const Compare & comp = Compare(), Filter flt =
- Filter())
- {
- Iter_t LI = first, LS = last - 1, it_out = first;
- while (LI != LS)
- {
- it_out = LI + ((LS - LI + 1) >> 1);
- if (comp(val, flt(*it_out))) LS = it_out - 1;
- else LI = it_out;
- };
- return LS;
- };
- //
- //###########################################################################
- // ##
- // ################################################################ ##
- // # # ##
- // # P U B L I C F U N C T I O N S # ##
- // # # ##
- // ################################################################ ##
- // ##
- //###########################################################################
- //
- //-----------------------------------------------------------------------------
- // function : find_first
- /// @brief find if a value exist in the range [first, last). If exist return the
- /// iterator to the first occurrence. If don't exist return last
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found, and if not last
- //-----------------------------------------------------------------------------
- template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
- class Compare = std::less<typename Filter::key> >
- inline Iter_t find_first(Iter_t first, Iter_t last,
- const typename Filter::key &val,
- const Compare & comp = Compare(),
- Filter flt = Filter())
- {
- assert((last - first) >= 0);
- if (first == last) return last;
- Iter_t LS = internal_find_first(first, last, val, comp, flt);
- return (comp(flt(*LS), val) or comp(val, flt(*LS))) ? last : LS;
- };
- //
- //-----------------------------------------------------------------------------
- // function : find_last
- /// @brief find if a value exist in the range [first, last). If exist return the
- /// iterator to the last occurrence. If don't exist return last
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found, if not found return last
- //-----------------------------------------------------------------------------
- template <class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
- class Compare = std::less<typename Filter::key> >
- inline Iter_t find_last(Iter_t first, Iter_t last,
- const typename Filter::key &val,
- const Compare & comp = Compare(),
- Filter flt = Filter())
- {
- assert((last - first) >= 0);
- if (last == first) return last;
- Iter_t LS = internal_find_last(first, last, val, comp, flt);
- return (comp(flt(*LS), val) or comp(val, flt(*LS))) ? last : LS;
- };
- //----------------------------------------------------------------------------
- // function : lower_bound
- /// @brief Returns an iterator pointing to the first element in the range
- /// [first, last) that is not less than (i.e. greater or equal to) val.
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found
- //-----------------------------------------------------------------------------
- template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
- class Compare = std::less<typename Filter::key> >
- inline Iter_t lower_bound(Iter_t first, Iter_t last,
- const typename Filter::key &val,
- const Compare & comp = Compare(),
- Filter flt = Filter())
- {
- assert((last - first) >= 0);
- if (last == first) return last;
- Iter_t itaux = internal_find_first(first, last, val, comp, flt);
- return (itaux == (last - 1) and comp(flt(*itaux), val)) ? last : itaux;
- };
- //----------------------------------------------------------------------------
- // function :upper_bound
- /// @brief return the first element greather than val.If don't exist
- /// return last
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found
- /// @remarks
- //-----------------------------------------------------------------------------
- template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
- class Compare = std::less<typename Filter::key> >
- inline Iter_t upper_bound(Iter_t first, Iter_t last,
- const typename Filter::key &val,
- const Compare & comp = Compare(),
- Filter flt = Filter())
- {
- assert((last - first) >= 0);
- if (last == first) return last;
- Iter_t itaux = internal_find_last(first, last, val, comp, flt);
- return (itaux == first and comp(val, flt(*itaux))) ? itaux : itaux + 1;
- }
- ;
- //----------------------------------------------------------------------------
- // function :equal_range
- /// @brief return a pair of lower_bound and upper_bound with the value val.If
- /// don't exist return last in the two elements of the pair
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return pair of iterators
- //-----------------------------------------------------------------------------
- template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
- class Compare = std::less<typename Filter::key> >
- inline std::pair<Iter_t, Iter_t> equal_range(Iter_t first, Iter_t last,
- const typename Filter::key &val,
- const Compare & comp = Compare(),
- Filter flt = Filter())
- {
- return std::make_pair(lower_bound(first, last, val, comp, flt),
- upper_bound(first, last, val, comp, flt));
- };
- //
- //-----------------------------------------------------------------------------
- // function : insert_first
- /// @brief find if a value exist in the range [first, last). If exist return the
- /// iterator to the first occurrence. If don't exist return last
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found, and if not last
- //-----------------------------------------------------------------------------
- template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
- class Compare = std::less<typename Filter::key> >
- inline Iter_t insert_first(Iter_t first, Iter_t last,
- const typename Filter::key &val,
- const Compare & comp = Compare(), Filter flt =
- Filter())
- {
- return lower_bound(first, last, val, comp, flt);
- };
- //
- //-----------------------------------------------------------------------------
- // function : insert_last
- /// @brief find if a value exist in the range [first, last). If exist return the
- /// iterator to the last occurrence. If don't exist return last
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found, if not found return last
- //-----------------------------------------------------------------------------
- template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
- class Compare = std::less<typename Filter::key> >
- inline Iter_t insert_last(Iter_t first, Iter_t last,
- const typename Filter::key &val,
- const Compare & comp = Compare(), Filter flt =
- Filter())
- {
- return upper_bound(first, last, val, comp, flt);
- };
- /*
- //
- //###########################################################################
- // ##
- // ################################################################ ##
- // # # ##
- // # I N T E R N A L F U N C T I O N S # ##
- // # # ##
- // ################################################################ ##
- // ##
- // I M P O R T A N T ##
- // ##
- // These functions are not directly callable by the user, are for internal ##
- // use only. ##
- // These functions don't check the parameters ##
- // ##
- //###########################################################################
- //
- //-----------------------------------------------------------------------------
- // function : internal_find_first
- /// @brief find if a value exist in the range [first, last).
- /// Always return as valid iterator in the range [first, last-1]
- /// If exist return the iterator to the first occurrence. If don't exist
- /// return the first greater than val.
- /// If val is greater than the *(last-1), return (last-1)
- /// If val is lower than (*first), return first
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found,
- //-----------------------------------------------------------------------------
- template < class Iter_t, class Compare = compare_iter<Iter_t> >
- inline Iter_t internal_find_first ( Iter_t first, Iter_t last,
- const value_iter<Iter_t> &val,
- const Compare & comp= Compare() )
- {
- Iter_t LI = first , LS = last - 1, it_out = first;
- while ( LI != LS)
- { it_out = LI + ( (LS - LI) >> 1);
- if ( comp ( *it_out, val)) LI = it_out + 1 ; else LS = it_out ;
- };
- return LS ;
- };
- //
- //-----------------------------------------------------------------------------
- // function : internal_find_last
- /// @brief find if a value exist in the range [first, last).
- /// Always return as valid iterator in the range [first, last-1]
- /// If exist return the iterator to the last occurrence.
- /// If don't exist return the first lower than val.
- /// If val is greater than *(last-1) return (last-1).
- /// If is lower than the first, return first
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found, if not found return last
- //-----------------------------------------------------------------------------
- template < class Iter_t, class Compare = compare_iter<Iter_t> >
- inline Iter_t internal_find_last ( Iter_t first, Iter_t last ,
- const value_iter<Iter_t> &val,
- const Compare &comp= Compare() )
- {
- Iter_t LI = first , LS = last - 1, it_out = first ;
- while ( LI != LS)
- { it_out = LI + ( (LS - LI + 1) >> 1);
- if ( comp (val, *it_out)) LS = it_out - 1 ; else LI = it_out ;
- };
- return LS ;
- };
- //
- //###########################################################################
- // ##
- // ################################################################ ##
- // # # ##
- // # P U B L I C F U N C T I O N S # ##
- // # # ##
- // ################################################################ ##
- // ##
- //###########################################################################
- //
- //-----------------------------------------------------------------------------
- // function : find_first
- /// @brief find if a value exist in the range [first, last). If exist return the
- /// iterator to the first occurrence. If don't exist return last
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found, and if not last
- //-----------------------------------------------------------------------------
- template < class Iter_t, class Compare = compare_iter<Iter_t> >
- inline Iter_t find_first ( Iter_t first, Iter_t last,
- const value_iter<Iter_t> &val,
- Compare comp = Compare() )
- {
- assert ( (last - first) >= 0 );
- if ( first == last) return last ;
- Iter_t LS = internal_find_first ( first, last, val, comp);
- return (comp (*LS, val) or comp (val, *LS))?last:LS;
- };
- //
- //-----------------------------------------------------------------------------
- // function : find_last
- /// @brief find if a value exist in the range [first, last). If exist return the
- /// iterator to the last occurrence. If don't exist return last
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found, if not found return last
- //-----------------------------------------------------------------------------
- template < class Iter_t, class Compare = compare_iter<Iter_t> >
- inline Iter_t find_last ( Iter_t first, Iter_t last ,
- const value_iter<Iter_t> &val,
- Compare comp = Compare())
- {
- assert ( (last - first ) >= 0 );
- if ( last == first ) return last ;
- Iter_t LS = internal_find_last (first, last, val, comp);
- return (comp (*LS, val) or comp (val, *LS))?last:LS ;
- };
- //----------------------------------------------------------------------------
- // function : lower_bound
- /// @brief Returns an iterator pointing to the first element in the range
- /// [first, last) that is not less than (i.e. greater or equal to) val.
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found
- //-----------------------------------------------------------------------------
- template < class Iter_t, class Compare = compare_iter<Iter_t> >
- inline Iter_t lower_bound ( Iter_t first, Iter_t last ,
- const value_iter<Iter_t> &val,
- Compare &comp = Compare() )
- {
- assert ( (last - first ) >= 0 );
- if ( last == first ) return last ;
- Iter_t itaux = internal_find_first( first, last, val,comp);
- return (itaux == (last - 1) and comp (*itaux, val))?last: itaux;
- };
- //----------------------------------------------------------------------------
- // function :upper_bound
- /// @brief return the first element greather than val.If don't exist
- /// return last
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found
- /// @remarks
- //-----------------------------------------------------------------------------
- template < class Iter_t, class Compare = compare_iter<Iter_t> >
- inline Iter_t upper_bound ( Iter_t first, Iter_t last ,
- const value_iter<Iter_t> &val,
- Compare &comp = Compare() )
- {
- assert ( (last - first ) >= 0 );
- if ( last == first ) return last ;
- Iter_t itaux = internal_find_last( first, last, val,comp);
- return ( itaux == first and comp (val,*itaux))? itaux: itaux + 1;
- };
- //----------------------------------------------------------------------------
- // function :equal_range
- /// @brief return a pair of lower_bound and upper_bound with the value val.If
- /// don't exist return last in the two elements of the pair
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return pair of iterators
- //-----------------------------------------------------------------------------
- template < class Iter_t, class Compare = compare_iter<Iter_t> >
- inline std::pair<Iter_t, Iter_t> equal_range ( Iter_t first, Iter_t last ,
- const value_iter<Iter_t> &val,
- Compare &comp = Compare() )
- {
- return std::make_pair(lower_bound(first, last, val,comp),
- upper_bound(first, last, val,comp));
- };
- //
- //-----------------------------------------------------------------------------
- // function : insert_first
- /// @brief find if a value exist in the range [first, last). If exist return the
- /// iterator to the first occurrence. If don't exist return last
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found, and if not last
- //-----------------------------------------------------------------------------
- template < class Iter_t, class Compare = compare_iter<Iter_t> >
- inline Iter_t insert_first ( Iter_t first, Iter_t last,
- const value_iter<Iter_t> &val,
- Compare comp = Compare() )
- {
- return lower_bound (first, last, val, comp);
- };
- //
- //-----------------------------------------------------------------------------
- // function : insert_last
- /// @brief find if a value exist in the range [first, last). If exist return the
- /// iterator to the last occurrence. If don't exist return last
- //
- /// @param [in] first : iterator to the first element of the range
- /// @param [in] last : iterator to the last element of the range
- /// @param [in] val : value to find
- /// @param [in] comp : object for to compare two value_t objects
- /// @return iterator to the element found, if not found return last
- //-----------------------------------------------------------------------------
- template < class Iter_t, class Compare = compare_iter<Iter_t> >
- inline Iter_t insert_last ( Iter_t first, Iter_t last ,
- const value_iter<Iter_t> &val,
- Compare comp = Compare())
- {
- return upper_bound (first, last, val, comp);
- };
- */
- //
- //****************************************************************************
- };// End namespace util
- };// End namespace common
- };// End namespace sort
- };// End namespace boost
- //****************************************************************************
- //
- #endif
|