search.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. //----------------------------------------------------------------------------
  2. /// @file search.hpp
  3. /// @brief
  4. /// @author Copyright (c) 2017 Francisco José Tapia (fjtapia@gmail.com )\n
  5. /// Distributed under the Boost Software License, Version 1.0.\n
  6. /// ( See copy at http://www.boost.org/LICENSE_1_0.txt )
  7. /// @remarks
  8. //-----------------------------------------------------------------------------
  9. #ifndef __BOOST_SORT_COMMON_SEARCH_HPP
  10. #define __BOOST_SORT_COMMON_SEARCH_HPP
  11. #include <ciso646>
  12. #include <cassert>
  13. #include <boost/sort/common/util/traits.hpp>
  14. namespace boost
  15. {
  16. namespace sort
  17. {
  18. namespace common
  19. {
  20. namespace util
  21. {
  22. template<class T>
  23. struct filter_pass
  24. {
  25. typedef T key;
  26. const key & operator()(const T & val) const
  27. {
  28. return val;
  29. };
  30. };
  31. //
  32. //###########################################################################
  33. // ##
  34. // ################################################################ ##
  35. // # # ##
  36. // # I N T E R N A L F U N C T I O N S # ##
  37. // # # ##
  38. // ################################################################ ##
  39. // ##
  40. // I M P O R T A N T ##
  41. // ##
  42. // These functions are not directly callable by the user, are for internal ##
  43. // use only. ##
  44. // These functions don't check the parameters ##
  45. // ##
  46. //###########################################################################
  47. //
  48. //-----------------------------------------------------------------------------
  49. // function : internal_find_first
  50. /// @brief find if a value exist in the range [first, last).
  51. /// Always return as valid iterator in the range [first, last-1]
  52. /// If exist return the iterator to the first occurrence. If don't exist
  53. /// return the first greater than val.
  54. /// If val is greater than the *(last-1), return (last-1)
  55. /// If val is lower than (*first), return first
  56. //
  57. /// @param [in] first : iterator to the first element of the range
  58. /// @param [in] last : iterator to the last element of the range
  59. /// @param [in] val : value to find
  60. /// @param [in] comp : object for to compare two value_t objects
  61. /// @return iterator to the element found,
  62. //-----------------------------------------------------------------------------
  63. template <class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
  64. class Compare = std::less<typename Filter::key> >
  65. inline Iter_t internal_find_first(Iter_t first, Iter_t last,
  66. const typename Filter::key &val,
  67. const Compare & comp = Compare(),
  68. Filter flt = Filter())
  69. {
  70. Iter_t LI = first, LS = last - 1, it_out = first;
  71. while (LI != LS)
  72. {
  73. it_out = LI + ((LS - LI) >> 1);
  74. if (comp(flt(*it_out), val))
  75. LI = it_out + 1;
  76. else LS = it_out;
  77. };
  78. return LS;
  79. };
  80. //
  81. //-----------------------------------------------------------------------------
  82. // function : internal_find_last
  83. /// @brief find if a value exist in the range [first, last).
  84. /// Always return as valid iterator in the range [first, last-1]
  85. /// If exist return the iterator to the last occurrence.
  86. /// If don't exist return the first lower than val.
  87. /// If val is greater than *(last-1) return (last-1).
  88. /// If is lower than the first, return first
  89. //
  90. /// @param [in] first : iterator to the first element of the range
  91. /// @param [in] last : iterator to the last element of the range
  92. /// @param [in] val : value to find
  93. /// @param [in] comp : object for to compare two value_t objects
  94. /// @return iterator to the element found, if not found return last
  95. //-----------------------------------------------------------------------------
  96. template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
  97. class Compare = std::less<typename Filter::key> >
  98. inline Iter_t internal_find_last(Iter_t first, Iter_t last,
  99. const typename Filter::key &val,
  100. const Compare & comp = Compare(), Filter flt =
  101. Filter())
  102. {
  103. Iter_t LI = first, LS = last - 1, it_out = first;
  104. while (LI != LS)
  105. {
  106. it_out = LI + ((LS - LI + 1) >> 1);
  107. if (comp(val, flt(*it_out))) LS = it_out - 1;
  108. else LI = it_out;
  109. };
  110. return LS;
  111. };
  112. //
  113. //###########################################################################
  114. // ##
  115. // ################################################################ ##
  116. // # # ##
  117. // # P U B L I C F U N C T I O N S # ##
  118. // # # ##
  119. // ################################################################ ##
  120. // ##
  121. //###########################################################################
  122. //
  123. //-----------------------------------------------------------------------------
  124. // function : find_first
  125. /// @brief find if a value exist in the range [first, last). If exist return the
  126. /// iterator to the first occurrence. If don't exist return last
  127. //
  128. /// @param [in] first : iterator to the first element of the range
  129. /// @param [in] last : iterator to the last element of the range
  130. /// @param [in] val : value to find
  131. /// @param [in] comp : object for to compare two value_t objects
  132. /// @return iterator to the element found, and if not last
  133. //-----------------------------------------------------------------------------
  134. template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
  135. class Compare = std::less<typename Filter::key> >
  136. inline Iter_t find_first(Iter_t first, Iter_t last,
  137. const typename Filter::key &val,
  138. const Compare & comp = Compare(),
  139. Filter flt = Filter())
  140. {
  141. assert((last - first) >= 0);
  142. if (first == last) return last;
  143. Iter_t LS = internal_find_first(first, last, val, comp, flt);
  144. return (comp(flt(*LS), val) or comp(val, flt(*LS))) ? last : LS;
  145. };
  146. //
  147. //-----------------------------------------------------------------------------
  148. // function : find_last
  149. /// @brief find if a value exist in the range [first, last). If exist return the
  150. /// iterator to the last occurrence. If don't exist return last
  151. //
  152. /// @param [in] first : iterator to the first element of the range
  153. /// @param [in] last : iterator to the last element of the range
  154. /// @param [in] val : value to find
  155. /// @param [in] comp : object for to compare two value_t objects
  156. /// @return iterator to the element found, if not found return last
  157. //-----------------------------------------------------------------------------
  158. template <class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
  159. class Compare = std::less<typename Filter::key> >
  160. inline Iter_t find_last(Iter_t first, Iter_t last,
  161. const typename Filter::key &val,
  162. const Compare & comp = Compare(),
  163. Filter flt = Filter())
  164. {
  165. assert((last - first) >= 0);
  166. if (last == first) return last;
  167. Iter_t LS = internal_find_last(first, last, val, comp, flt);
  168. return (comp(flt(*LS), val) or comp(val, flt(*LS))) ? last : LS;
  169. };
  170. //----------------------------------------------------------------------------
  171. // function : lower_bound
  172. /// @brief Returns an iterator pointing to the first element in the range
  173. /// [first, last) that is not less than (i.e. greater or equal to) val.
  174. /// @param [in] last : iterator to the last element of the range
  175. /// @param [in] val : value to find
  176. /// @param [in] comp : object for to compare two value_t objects
  177. /// @return iterator to the element found
  178. //-----------------------------------------------------------------------------
  179. template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
  180. class Compare = std::less<typename Filter::key> >
  181. inline Iter_t lower_bound(Iter_t first, Iter_t last,
  182. const typename Filter::key &val,
  183. const Compare & comp = Compare(),
  184. Filter flt = Filter())
  185. {
  186. assert((last - first) >= 0);
  187. if (last == first) return last;
  188. Iter_t itaux = internal_find_first(first, last, val, comp, flt);
  189. return (itaux == (last - 1) and comp(flt(*itaux), val)) ? last : itaux;
  190. };
  191. //----------------------------------------------------------------------------
  192. // function :upper_bound
  193. /// @brief return the first element greather than val.If don't exist
  194. /// return last
  195. //
  196. /// @param [in] first : iterator to the first element of the range
  197. /// @param [in] last : iterator to the last element of the range
  198. /// @param [in] val : value to find
  199. /// @param [in] comp : object for to compare two value_t objects
  200. /// @return iterator to the element found
  201. /// @remarks
  202. //-----------------------------------------------------------------------------
  203. template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
  204. class Compare = std::less<typename Filter::key> >
  205. inline Iter_t upper_bound(Iter_t first, Iter_t last,
  206. const typename Filter::key &val,
  207. const Compare & comp = Compare(),
  208. Filter flt = Filter())
  209. {
  210. assert((last - first) >= 0);
  211. if (last == first) return last;
  212. Iter_t itaux = internal_find_last(first, last, val, comp, flt);
  213. return (itaux == first and comp(val, flt(*itaux))) ? itaux : itaux + 1;
  214. }
  215. ;
  216. //----------------------------------------------------------------------------
  217. // function :equal_range
  218. /// @brief return a pair of lower_bound and upper_bound with the value val.If
  219. /// don't exist return last in the two elements of the pair
  220. //
  221. /// @param [in] first : iterator to the first element of the range
  222. /// @param [in] last : iterator to the last element of the range
  223. /// @param [in] val : value to find
  224. /// @param [in] comp : object for to compare two value_t objects
  225. /// @return pair of iterators
  226. //-----------------------------------------------------------------------------
  227. template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
  228. class Compare = std::less<typename Filter::key> >
  229. inline std::pair<Iter_t, Iter_t> equal_range(Iter_t first, Iter_t last,
  230. const typename Filter::key &val,
  231. const Compare & comp = Compare(),
  232. Filter flt = Filter())
  233. {
  234. return std::make_pair(lower_bound(first, last, val, comp, flt),
  235. upper_bound(first, last, val, comp, flt));
  236. };
  237. //
  238. //-----------------------------------------------------------------------------
  239. // function : insert_first
  240. /// @brief find if a value exist in the range [first, last). If exist return the
  241. /// iterator to the first occurrence. If don't exist return last
  242. //
  243. /// @param [in] first : iterator to the first element of the range
  244. /// @param [in] last : iterator to the last element of the range
  245. /// @param [in] val : value to find
  246. /// @param [in] comp : object for to compare two value_t objects
  247. /// @return iterator to the element found, and if not last
  248. //-----------------------------------------------------------------------------
  249. template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
  250. class Compare = std::less<typename Filter::key> >
  251. inline Iter_t insert_first(Iter_t first, Iter_t last,
  252. const typename Filter::key &val,
  253. const Compare & comp = Compare(), Filter flt =
  254. Filter())
  255. {
  256. return lower_bound(first, last, val, comp, flt);
  257. };
  258. //
  259. //-----------------------------------------------------------------------------
  260. // function : insert_last
  261. /// @brief find if a value exist in the range [first, last). If exist return the
  262. /// iterator to the last occurrence. If don't exist return last
  263. //
  264. /// @param [in] first : iterator to the first element of the range
  265. /// @param [in] last : iterator to the last element of the range
  266. /// @param [in] val : value to find
  267. /// @param [in] comp : object for to compare two value_t objects
  268. /// @return iterator to the element found, if not found return last
  269. //-----------------------------------------------------------------------------
  270. template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,
  271. class Compare = std::less<typename Filter::key> >
  272. inline Iter_t insert_last(Iter_t first, Iter_t last,
  273. const typename Filter::key &val,
  274. const Compare & comp = Compare(), Filter flt =
  275. Filter())
  276. {
  277. return upper_bound(first, last, val, comp, flt);
  278. };
  279. /*
  280. //
  281. //###########################################################################
  282. // ##
  283. // ################################################################ ##
  284. // # # ##
  285. // # I N T E R N A L F U N C T I O N S # ##
  286. // # # ##
  287. // ################################################################ ##
  288. // ##
  289. // I M P O R T A N T ##
  290. // ##
  291. // These functions are not directly callable by the user, are for internal ##
  292. // use only. ##
  293. // These functions don't check the parameters ##
  294. // ##
  295. //###########################################################################
  296. //
  297. //-----------------------------------------------------------------------------
  298. // function : internal_find_first
  299. /// @brief find if a value exist in the range [first, last).
  300. /// Always return as valid iterator in the range [first, last-1]
  301. /// If exist return the iterator to the first occurrence. If don't exist
  302. /// return the first greater than val.
  303. /// If val is greater than the *(last-1), return (last-1)
  304. /// If val is lower than (*first), return first
  305. //
  306. /// @param [in] first : iterator to the first element of the range
  307. /// @param [in] last : iterator to the last element of the range
  308. /// @param [in] val : value to find
  309. /// @param [in] comp : object for to compare two value_t objects
  310. /// @return iterator to the element found,
  311. //-----------------------------------------------------------------------------
  312. template < class Iter_t, class Compare = compare_iter<Iter_t> >
  313. inline Iter_t internal_find_first ( Iter_t first, Iter_t last,
  314. const value_iter<Iter_t> &val,
  315. const Compare & comp= Compare() )
  316. {
  317. Iter_t LI = first , LS = last - 1, it_out = first;
  318. while ( LI != LS)
  319. { it_out = LI + ( (LS - LI) >> 1);
  320. if ( comp ( *it_out, val)) LI = it_out + 1 ; else LS = it_out ;
  321. };
  322. return LS ;
  323. };
  324. //
  325. //-----------------------------------------------------------------------------
  326. // function : internal_find_last
  327. /// @brief find if a value exist in the range [first, last).
  328. /// Always return as valid iterator in the range [first, last-1]
  329. /// If exist return the iterator to the last occurrence.
  330. /// If don't exist return the first lower than val.
  331. /// If val is greater than *(last-1) return (last-1).
  332. /// If is lower than the first, return first
  333. //
  334. /// @param [in] first : iterator to the first element of the range
  335. /// @param [in] last : iterator to the last element of the range
  336. /// @param [in] val : value to find
  337. /// @param [in] comp : object for to compare two value_t objects
  338. /// @return iterator to the element found, if not found return last
  339. //-----------------------------------------------------------------------------
  340. template < class Iter_t, class Compare = compare_iter<Iter_t> >
  341. inline Iter_t internal_find_last ( Iter_t first, Iter_t last ,
  342. const value_iter<Iter_t> &val,
  343. const Compare &comp= Compare() )
  344. {
  345. Iter_t LI = first , LS = last - 1, it_out = first ;
  346. while ( LI != LS)
  347. { it_out = LI + ( (LS - LI + 1) >> 1);
  348. if ( comp (val, *it_out)) LS = it_out - 1 ; else LI = it_out ;
  349. };
  350. return LS ;
  351. };
  352. //
  353. //###########################################################################
  354. // ##
  355. // ################################################################ ##
  356. // # # ##
  357. // # P U B L I C F U N C T I O N S # ##
  358. // # # ##
  359. // ################################################################ ##
  360. // ##
  361. //###########################################################################
  362. //
  363. //-----------------------------------------------------------------------------
  364. // function : find_first
  365. /// @brief find if a value exist in the range [first, last). If exist return the
  366. /// iterator to the first occurrence. If don't exist return last
  367. //
  368. /// @param [in] first : iterator to the first element of the range
  369. /// @param [in] last : iterator to the last element of the range
  370. /// @param [in] val : value to find
  371. /// @param [in] comp : object for to compare two value_t objects
  372. /// @return iterator to the element found, and if not last
  373. //-----------------------------------------------------------------------------
  374. template < class Iter_t, class Compare = compare_iter<Iter_t> >
  375. inline Iter_t find_first ( Iter_t first, Iter_t last,
  376. const value_iter<Iter_t> &val,
  377. Compare comp = Compare() )
  378. {
  379. assert ( (last - first) >= 0 );
  380. if ( first == last) return last ;
  381. Iter_t LS = internal_find_first ( first, last, val, comp);
  382. return (comp (*LS, val) or comp (val, *LS))?last:LS;
  383. };
  384. //
  385. //-----------------------------------------------------------------------------
  386. // function : find_last
  387. /// @brief find if a value exist in the range [first, last). If exist return the
  388. /// iterator to the last occurrence. If don't exist return last
  389. //
  390. /// @param [in] first : iterator to the first element of the range
  391. /// @param [in] last : iterator to the last element of the range
  392. /// @param [in] val : value to find
  393. /// @param [in] comp : object for to compare two value_t objects
  394. /// @return iterator to the element found, if not found return last
  395. //-----------------------------------------------------------------------------
  396. template < class Iter_t, class Compare = compare_iter<Iter_t> >
  397. inline Iter_t find_last ( Iter_t first, Iter_t last ,
  398. const value_iter<Iter_t> &val,
  399. Compare comp = Compare())
  400. {
  401. assert ( (last - first ) >= 0 );
  402. if ( last == first ) return last ;
  403. Iter_t LS = internal_find_last (first, last, val, comp);
  404. return (comp (*LS, val) or comp (val, *LS))?last:LS ;
  405. };
  406. //----------------------------------------------------------------------------
  407. // function : lower_bound
  408. /// @brief Returns an iterator pointing to the first element in the range
  409. /// [first, last) that is not less than (i.e. greater or equal to) val.
  410. /// @param [in] last : iterator to the last element of the range
  411. /// @param [in] val : value to find
  412. /// @param [in] comp : object for to compare two value_t objects
  413. /// @return iterator to the element found
  414. //-----------------------------------------------------------------------------
  415. template < class Iter_t, class Compare = compare_iter<Iter_t> >
  416. inline Iter_t lower_bound ( Iter_t first, Iter_t last ,
  417. const value_iter<Iter_t> &val,
  418. Compare &comp = Compare() )
  419. {
  420. assert ( (last - first ) >= 0 );
  421. if ( last == first ) return last ;
  422. Iter_t itaux = internal_find_first( first, last, val,comp);
  423. return (itaux == (last - 1) and comp (*itaux, val))?last: itaux;
  424. };
  425. //----------------------------------------------------------------------------
  426. // function :upper_bound
  427. /// @brief return the first element greather than val.If don't exist
  428. /// return last
  429. //
  430. /// @param [in] first : iterator to the first element of the range
  431. /// @param [in] last : iterator to the last element of the range
  432. /// @param [in] val : value to find
  433. /// @param [in] comp : object for to compare two value_t objects
  434. /// @return iterator to the element found
  435. /// @remarks
  436. //-----------------------------------------------------------------------------
  437. template < class Iter_t, class Compare = compare_iter<Iter_t> >
  438. inline Iter_t upper_bound ( Iter_t first, Iter_t last ,
  439. const value_iter<Iter_t> &val,
  440. Compare &comp = Compare() )
  441. {
  442. assert ( (last - first ) >= 0 );
  443. if ( last == first ) return last ;
  444. Iter_t itaux = internal_find_last( first, last, val,comp);
  445. return ( itaux == first and comp (val,*itaux))? itaux: itaux + 1;
  446. };
  447. //----------------------------------------------------------------------------
  448. // function :equal_range
  449. /// @brief return a pair of lower_bound and upper_bound with the value val.If
  450. /// don't exist return last in the two elements of the pair
  451. //
  452. /// @param [in] first : iterator to the first element of the range
  453. /// @param [in] last : iterator to the last element of the range
  454. /// @param [in] val : value to find
  455. /// @param [in] comp : object for to compare two value_t objects
  456. /// @return pair of iterators
  457. //-----------------------------------------------------------------------------
  458. template < class Iter_t, class Compare = compare_iter<Iter_t> >
  459. inline std::pair<Iter_t, Iter_t> equal_range ( Iter_t first, Iter_t last ,
  460. const value_iter<Iter_t> &val,
  461. Compare &comp = Compare() )
  462. {
  463. return std::make_pair(lower_bound(first, last, val,comp),
  464. upper_bound(first, last, val,comp));
  465. };
  466. //
  467. //-----------------------------------------------------------------------------
  468. // function : insert_first
  469. /// @brief find if a value exist in the range [first, last). If exist return the
  470. /// iterator to the first occurrence. If don't exist return last
  471. //
  472. /// @param [in] first : iterator to the first element of the range
  473. /// @param [in] last : iterator to the last element of the range
  474. /// @param [in] val : value to find
  475. /// @param [in] comp : object for to compare two value_t objects
  476. /// @return iterator to the element found, and if not last
  477. //-----------------------------------------------------------------------------
  478. template < class Iter_t, class Compare = compare_iter<Iter_t> >
  479. inline Iter_t insert_first ( Iter_t first, Iter_t last,
  480. const value_iter<Iter_t> &val,
  481. Compare comp = Compare() )
  482. {
  483. return lower_bound (first, last, val, comp);
  484. };
  485. //
  486. //-----------------------------------------------------------------------------
  487. // function : insert_last
  488. /// @brief find if a value exist in the range [first, last). If exist return the
  489. /// iterator to the last occurrence. If don't exist return last
  490. //
  491. /// @param [in] first : iterator to the first element of the range
  492. /// @param [in] last : iterator to the last element of the range
  493. /// @param [in] val : value to find
  494. /// @param [in] comp : object for to compare two value_t objects
  495. /// @return iterator to the element found, if not found return last
  496. //-----------------------------------------------------------------------------
  497. template < class Iter_t, class Compare = compare_iter<Iter_t> >
  498. inline Iter_t insert_last ( Iter_t first, Iter_t last ,
  499. const value_iter<Iter_t> &val,
  500. Compare comp = Compare())
  501. {
  502. return upper_bound (first, last, val, comp);
  503. };
  504. */
  505. //
  506. //****************************************************************************
  507. };// End namespace util
  508. };// End namespace common
  509. };// End namespace sort
  510. };// End namespace boost
  511. //****************************************************************************
  512. //
  513. #endif