search.hpp 24 KB

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