algorithm.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. // -- algorithm.hpp -- Boost Lambda Library -----------------------------------
  2. // Copyright (C) 2002 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
  3. // Copyright (C) 2002 Gary Powell (gwpowell@hotmail.com)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // For more information, see http://www.boost.org
  10. #ifndef BOOST_LAMBDA_ALGORITHM_HPP
  11. #define BOOST_LAMBDA_ALGORITHM_HPP
  12. #include "boost/lambda/core.hpp"
  13. #include <algorithm>
  14. #include <iterator> // for iterator_traits
  15. #include <utility> // for std::pair
  16. namespace boost {
  17. namespace lambda {
  18. namespace ll {
  19. // for_each ---------------------------------
  20. struct for_each {
  21. template <class Args>
  22. struct sig {
  23. typedef typename boost::remove_const<
  24. typename boost::tuples::element<3, Args>::type
  25. >::type type;
  26. };
  27. template <class A, class C>
  28. C
  29. operator()(A a, A b, C c) const
  30. { return ::std::for_each(a, b, c); }
  31. };
  32. // find ---------------------------------
  33. struct find {
  34. template <class Args>
  35. struct sig {
  36. typedef typename boost::remove_const<
  37. typename boost::tuples::element<1, Args>::type
  38. >::type type;
  39. };
  40. template <class A, class C>
  41. A
  42. operator()(A a, A b, const C& c) const
  43. { return ::std::find(a, b, c); }
  44. };
  45. // find_if ---------------------------------
  46. struct find_if {
  47. template <class Args>
  48. struct sig {
  49. typedef typename boost::remove_const<
  50. typename boost::tuples::element<1, Args>::type
  51. >::type type;
  52. };
  53. template <class A, class C>
  54. A
  55. operator()(A a, A b, C c) const
  56. { return ::std::find_if(a, b, c); }
  57. };
  58. // find_end ---------------------------------
  59. struct find_end {
  60. template <class Args>
  61. struct sig {
  62. typedef typename boost::remove_const<
  63. typename boost::tuples::element<1, Args>::type
  64. >::type type;
  65. };
  66. template <class A, class C>
  67. A
  68. operator()(A a, A b, C c, C d) const
  69. { return ::std::find_end(a, b, c, d); }
  70. template <class A, class C, class E>
  71. A
  72. operator()(A a, A b, C c, C d, E e) const
  73. { return ::std::find_end(a, b, c, d, e); }
  74. };
  75. // find_first_of ---------------------------------
  76. struct find_first_of {
  77. template <class Args>
  78. struct sig {
  79. typedef typename boost::remove_const<
  80. typename boost::tuples::element<1, Args>::type
  81. >::type type;
  82. };
  83. template <class A, class C>
  84. A
  85. operator()(A a, A b, C c, C d) const
  86. { return ::std::find_first_of(a, b, c, d); }
  87. template <class A, class C, class E>
  88. A
  89. operator()(A a, A b, C c, C d, E e) const
  90. { return ::std::find_first_of(a, b, c, d, e); }
  91. };
  92. // adjacent_find ---------------------------------
  93. struct adjacent_find {
  94. template <class Args>
  95. struct sig {
  96. typedef typename boost::remove_const<
  97. typename boost::tuples::element<1, Args>::type
  98. >::type type;
  99. };
  100. template <class A>
  101. A
  102. operator()(A a, A b) const
  103. { return ::std::adjacent_find(a, b); }
  104. template <class A, class C>
  105. A
  106. operator()(A a, A b, C c) const
  107. { return ::std::adjacent_find(a, b, c); }
  108. };
  109. // count ---------------------------------
  110. struct count {
  111. template <class Args>
  112. struct sig {
  113. typedef typename ::std::iterator_traits<
  114. typename boost::remove_const<
  115. typename boost::tuples::element<1, Args>::type
  116. >::type
  117. >::difference_type type;
  118. };
  119. template <class A, class C >
  120. typename ::std::iterator_traits<A>::difference_type
  121. operator()(A a, A b, const C& c) const
  122. { return ::std::count(a, b, c); }
  123. };
  124. // count_if ---------------------------------
  125. struct count_if {
  126. template <class Args>
  127. struct sig {
  128. typedef typename ::std::iterator_traits<
  129. typename boost::remove_const<
  130. typename boost::tuples::element<1, Args>::type
  131. >::type
  132. >::difference_type type;
  133. };
  134. template <class A, class C >
  135. typename ::std::iterator_traits<A>::difference_type
  136. operator()(A a, A b, C c) const
  137. { return ::std::count_if(a, b, c); }
  138. };
  139. // mismatch ---------------------------------
  140. struct mismatch {
  141. template <class Args>
  142. struct sig {
  143. typedef typename boost::remove_const<
  144. typename boost::tuples::element<1, Args>::type
  145. >::type element1_type;
  146. typedef typename boost::remove_const<
  147. typename boost::tuples::element<3, Args>::type
  148. >::type element2_type;
  149. typedef ::std::pair< element1_type, element2_type > type;
  150. };
  151. template <class A, class C >
  152. ::std::pair<A,C>
  153. operator()(A a, A b, C c) const
  154. { return ::std::mismatch(a, b, c); }
  155. template <class A, class C, class D>
  156. ::std::pair<A,C>
  157. operator()(A a, A b, C c, D d) const
  158. { return ::std::mismatch(a, b, c, d); }
  159. };
  160. // equal ---------------------------------
  161. struct equal {
  162. template <class Args>
  163. struct sig {
  164. typedef bool type;
  165. };
  166. template <class A, class C >
  167. bool
  168. operator()(A a, A b, C c) const
  169. { return ::std::equal(a, b, c); }
  170. template <class A, class C, class D>
  171. bool
  172. operator()(A a, A b, C c, D d) const
  173. { return ::std::equal(a, b, c, d); }
  174. };
  175. // search --------------------------------
  176. struct search {
  177. template <class Args>
  178. struct sig {
  179. typedef typename boost::remove_const<
  180. typename boost::tuples::element<1, Args>::type
  181. >::type type;
  182. };
  183. template <class A, class C>
  184. A
  185. operator()(A a, A b, C c, C d) const
  186. { return std::search(a, b, c, d);}
  187. template <class A, class C, class E>
  188. A
  189. operator()(A a, A b, C c, C d, E e) const
  190. { return std::search(a, b, c, d, e);}
  191. };
  192. // copy ---------------------------------
  193. struct copy {
  194. template <class Args>
  195. struct sig {
  196. typedef typename boost::remove_const<
  197. typename boost::tuples::element<3, Args>::type
  198. >::type type;
  199. };
  200. template <class A, class C>
  201. C
  202. operator()(A a, A b, C c) const
  203. { return ::std::copy(a, b, c); }
  204. };
  205. // copy_backward ---------------------------------
  206. struct copy_backward {
  207. template <class Args>
  208. struct sig {
  209. typedef typename boost::remove_const<
  210. typename boost::tuples::element<3, Args>::type
  211. >::type type;
  212. };
  213. template <class A, class C>
  214. C
  215. operator()(A a, A b, C c) const
  216. { return ::std::copy_backward(a, b, c); }
  217. };
  218. // swap ---------------------------------
  219. struct swap {
  220. template <class Args>
  221. struct sig {
  222. typedef void type;
  223. };
  224. template <class A>
  225. void
  226. operator()(A a, A b) const
  227. { ::std::swap(a, b); }
  228. };
  229. // swap_ranges ---------------------------------
  230. struct swap_ranges {
  231. template <class Args>
  232. struct sig {
  233. typedef typename boost::remove_const<
  234. typename boost::tuples::element<3, Args>::type
  235. >::type type;
  236. };
  237. template <class A, class C>
  238. C
  239. operator()(A a, A b, C c) const
  240. { return ::std::swap_ranges(a, b, c); }
  241. };
  242. // iter_swap ---------------------------------
  243. struct iter_swap {
  244. template <class Args>
  245. struct sig {
  246. typedef void type;
  247. };
  248. template <class A>
  249. void
  250. operator()(A a, A b) const
  251. { ::std::iter_swap(a, b); }
  252. };
  253. // transform --------------------------------
  254. struct transform {
  255. template <class Args>
  256. struct sig {
  257. typedef typename boost::remove_const<
  258. typename boost::tuples::element<
  259. boost::tuples::length<Args>::value - 2,
  260. Args
  261. >::type
  262. >::type type;
  263. };
  264. template <class A, class C, class D>
  265. C
  266. operator()(A a, A b, C c, D d) const
  267. { return std::transform(a, b, c, d);}
  268. template <class A, class C, class D, class E>
  269. D
  270. operator()(A a, A b, C c, D d, E e) const
  271. { return std::transform(a, b, c, d, e);}
  272. };
  273. // replace ---------------------------------
  274. struct replace {
  275. template <class Args>
  276. struct sig {
  277. typedef void type;
  278. };
  279. template <class A, class C>
  280. void
  281. operator()(A a, A b, const C& c, const C& d) const
  282. { ::std::replace(a, b, c, d); }
  283. };
  284. // replace_if ---------------------------------
  285. struct replace_if {
  286. template <class Args>
  287. struct sig {
  288. typedef void type;
  289. };
  290. template <class A, class C, class D>
  291. void
  292. operator()(A a, A b, C c, const D& d) const
  293. { ::std::replace_if(a, b, c, d); }
  294. };
  295. // replace_copy ---------------------------------
  296. struct replace_copy {
  297. template <class Args>
  298. struct sig {
  299. typedef typename boost::remove_const<
  300. typename boost::tuples::element<3, Args>::type
  301. >::type type;
  302. };
  303. template <class A, class C, class D>
  304. C
  305. operator()(A a, A b, C c, const D& d, const D& e) const
  306. { return ::std::replace_copy(a, b, c, d, e); }
  307. };
  308. // replace_copy_if ---------------------------------
  309. struct replace_copy_if {
  310. template <class Args>
  311. struct sig {
  312. typedef typename boost::remove_const<
  313. typename boost::tuples::element<3, Args>::type
  314. >::type type;
  315. };
  316. template <class A, class C, class D, class E>
  317. C
  318. operator()(A a, A b, C c, D d, const E& e) const
  319. { return ::std::replace_copy_if(a, b, c, d, e); }
  320. };
  321. // fill ---------------------------------
  322. struct fill {
  323. template <class Args>
  324. struct sig {
  325. typedef void type;
  326. };
  327. template <class A, class C>
  328. void
  329. operator()(A a, A b, const C& c) const
  330. { ::std::fill(a, b, c); }
  331. };
  332. // fill_n ---------------------------------
  333. struct fill_n {
  334. template <class Args>
  335. struct sig {
  336. typedef void type;
  337. };
  338. template <class A, class B, class C>
  339. void
  340. operator()(A a, B b, const C& c) const
  341. { ::std::fill_n(a, b, c); }
  342. };
  343. // generate ---------------------------------
  344. struct generate {
  345. template <class Args>
  346. struct sig {
  347. typedef void type;
  348. };
  349. template <class A, class C>
  350. void
  351. operator()(A a, A b, C c) const
  352. { ::std::generate(a, b, c); }
  353. };
  354. // generate_n ---------------------------------
  355. struct generate_n {
  356. template <class Args>
  357. struct sig {
  358. typedef void type;
  359. };
  360. template <class A, class B, class C>
  361. void
  362. operator()(A a, B b, C c) const
  363. { ::std::generate_n(a, b, c); }
  364. };
  365. // remove ---------------------------------
  366. struct remove {
  367. template <class Args>
  368. struct sig {
  369. typedef typename boost::remove_const<
  370. typename boost::tuples::element<1, Args>::type
  371. >::type type;
  372. };
  373. template <class A, class C >
  374. A
  375. operator()(A a, A b, const C& c) const
  376. { return ::std::remove(a, b, c); }
  377. };
  378. // remove_if ---------------------------------
  379. struct remove_if {
  380. template <class Args>
  381. struct sig {
  382. typedef typename boost::remove_const<
  383. typename boost::tuples::element<1, Args>::type
  384. >::type type;
  385. };
  386. template <class A, class C >
  387. A
  388. operator()(A a, A b, C c) const
  389. { return ::std::remove_if(a, b, c); }
  390. };
  391. // remove_copy ---------------------------------
  392. struct remove_copy {
  393. template <class Args>
  394. struct sig {
  395. typedef typename boost::remove_const<
  396. typename boost::tuples::element<3, Args>::type
  397. >::type type;
  398. };
  399. template <class A, class C, class D >
  400. C
  401. operator()(A a, A b, C c, const D& d) const
  402. { return ::std::remove_copy(a, b, c, d); }
  403. };
  404. // remove_copy_if ---------------------------------
  405. struct remove_copy_if {
  406. template <class Args>
  407. struct sig {
  408. typedef typename boost::remove_const<
  409. typename boost::tuples::element<3, Args>::type
  410. >::type type;
  411. };
  412. template <class A, class C, class D >
  413. C
  414. operator()(A a, A b, C c, D d) const
  415. { return ::std::remove_copy_if(a, b, c, d); }
  416. };
  417. // unique ---------------------------------
  418. struct unique {
  419. template <class Args>
  420. struct sig {
  421. typedef typename boost::remove_const<
  422. typename boost::tuples::element<1, Args>::type
  423. >::type type;
  424. };
  425. template <class A>
  426. A
  427. operator()(A a, A b) const
  428. { return ::std::unique(a, b); }
  429. template <class A, class C>
  430. A
  431. operator()(A a, A b, C c) const
  432. { return ::std::unique(a, b, c); }
  433. };
  434. // unique_copy ---------------------------------
  435. struct unique_copy {
  436. template <class Args>
  437. struct sig {
  438. typedef typename boost::remove_const<
  439. typename boost::tuples::element<3, Args>::type
  440. >::type type;
  441. };
  442. template <class A, class C >
  443. C
  444. operator()(A a, A b, C c) const
  445. { return ::std::unique_copy(a, b, c); }
  446. template <class A, class C, class D>
  447. C
  448. operator()(A a, A b, C c, D d) const
  449. { return ::std::unique_copy(a, b, c, d); }
  450. };
  451. // reverse ---------------------------------
  452. struct reverse {
  453. template <class Args>
  454. struct sig {
  455. typedef void type;
  456. };
  457. template <class A>
  458. void
  459. operator()(A a, A b) const
  460. { ::std::reverse(a, b); }
  461. };
  462. // reverse_copy ---------------------------------
  463. struct reverse_copy {
  464. template <class Args>
  465. struct sig {
  466. typedef typename boost::remove_const<
  467. typename boost::tuples::element<3, Args>::type
  468. >::type type;
  469. };
  470. template <class A, class C >
  471. C
  472. operator()(A a, A b, C c) const
  473. { return ::std::reverse_copy(a, b, c); }
  474. };
  475. // rotate ---------------------------------
  476. struct rotate {
  477. template <class Args>
  478. struct sig {
  479. typedef void type;
  480. };
  481. template <class A>
  482. void
  483. operator()(A a, A b, A c) const
  484. { ::std::rotate(a, b, c); }
  485. };
  486. // rotate_copy ---------------------------------
  487. struct rotate_copy {
  488. template <class Args>
  489. struct sig {
  490. typedef typename boost::remove_const<
  491. typename boost::tuples::element<3, Args>::type
  492. >::type type;
  493. };
  494. template <class A, class D>
  495. D
  496. operator()(A a, A b, A c, D d) const
  497. { return ::std::rotate_copy(a, b, c, d); }
  498. };
  499. // random_shuffle ---------------------------------
  500. #ifndef BOOST_NO_CXX98_RANDOM_SHUFFLE
  501. struct random_shuffle {
  502. template <class Args>
  503. struct sig {
  504. typedef void type;
  505. };
  506. template <class A>
  507. void
  508. operator()(A a, A b) const
  509. { ::std::random_shuffle(a, b); }
  510. template <class A, class C>
  511. void
  512. operator()(A a, A b, const C& c) const
  513. { ::std::random_shuffle(a, b, c); }
  514. };
  515. #endif
  516. // partition ---------------------------------
  517. struct partition {
  518. template <class Args>
  519. struct sig {
  520. typedef typename boost::remove_const<
  521. typename boost::tuples::element<1, Args>::type
  522. >::type type;
  523. };
  524. template <class A, class C>
  525. A
  526. operator()(A a, A b, C c) const
  527. { return ::std::partition(a, b, c); }
  528. };
  529. // stable_partition ---------------------------------
  530. struct stable_partition {
  531. template <class Args>
  532. struct sig {
  533. typedef typename boost::remove_const<
  534. typename boost::tuples::element<1, Args>::type
  535. >::type type;
  536. };
  537. template <class A, class C>
  538. A
  539. operator()(A a, A b, C c) const
  540. { return ::std::stable_partition(a, b, c); }
  541. };
  542. // sort ---------------------------------
  543. struct sort {
  544. template <class Args>
  545. struct sig {
  546. typedef void type;
  547. };
  548. template <class A>
  549. void
  550. operator()(A a, A b) const
  551. { ::std::sort(a, b); }
  552. template <class A, class C>
  553. void
  554. operator()(A a, A b, C c) const
  555. { ::std::sort(a, b, c); }
  556. };
  557. // stable_sort ---------------------------------
  558. struct stable_sort {
  559. template <class Args>
  560. struct sig {
  561. typedef void type;
  562. };
  563. template <class A>
  564. void
  565. operator()(A a, A b) const
  566. { ::std::stable_sort(a, b); }
  567. template <class A, class C>
  568. void
  569. operator()(A a, A b, C c) const
  570. { ::std::stable_sort(a, b, c); }
  571. };
  572. // partial_sort ---------------------------------
  573. struct partial_sort {
  574. template <class Args>
  575. struct sig {
  576. typedef void type;
  577. };
  578. template <class A>
  579. void
  580. operator()(A a, A b, A c) const
  581. { ::std::partial_sort(a, b, c); }
  582. template <class A, class D>
  583. void
  584. operator()(A a, A b, A c, D d) const
  585. { ::std::partial_sort(a, b, c, d); }
  586. };
  587. // partial_sort_copy ---------------------------------
  588. struct partial_sort_copy {
  589. template <class Args>
  590. struct sig {
  591. typedef typename boost::remove_const<
  592. typename boost::tuples::element<3, Args>::type
  593. >::type type;
  594. };
  595. template <class A, class C>
  596. C
  597. operator()(A a, A b, C c, C d) const
  598. { return ::std::partial_sort_copy(a, b, c, d); }
  599. template <class A, class C, class E >
  600. C
  601. operator()(A a, A b, C c, C d, E e) const
  602. { return ::std::partial_sort_copy(a, b, c, d, e); }
  603. };
  604. // nth_element ---------------------------------
  605. struct nth_element {
  606. template <class Args>
  607. struct sig {
  608. typedef void type;
  609. };
  610. template <class A>
  611. void
  612. operator()(A a, A b, A c) const
  613. { ::std::nth_element(a, b, c); }
  614. template <class A, class D>
  615. void
  616. operator()(A a, A b, A c, D d) const
  617. { ::std::nth_element(a, b, c, d); }
  618. };
  619. // lower_bound ---------------------------------
  620. struct lower_bound {
  621. template <class Args>
  622. struct sig {
  623. typedef typename boost::remove_const<
  624. typename boost::tuples::element<1, Args>::type
  625. >::type type;
  626. };
  627. template <class A, class C>
  628. A
  629. operator()(A a, A b, const C& c) const
  630. { return ::std::lower_bound(a, b, c); }
  631. template <class A, class C, class D>
  632. A
  633. operator()(A a, A b, const C& c, D d) const
  634. { return ::std::lower_bound(a, b, c, d); }
  635. };
  636. // upper_bound ---------------------------------
  637. struct upper_bound {
  638. template <class Args>
  639. struct sig {
  640. typedef typename boost::remove_const<
  641. typename boost::tuples::element<1, Args>::type
  642. >::type type;
  643. };
  644. template <class A, class C>
  645. A
  646. operator()(A a, A b, const C& c) const
  647. { return ::std::upper_bound(a, b, c); }
  648. template <class A, class C, class D>
  649. A
  650. operator()(A a, A b, const C& c, D d) const
  651. { return ::std::upper_bound(a, b, c, d); }
  652. };
  653. // equal_range ---------------------------------
  654. struct equal_range {
  655. template <class Args>
  656. struct sig {
  657. typedef typename boost::remove_const<
  658. typename boost::tuples::element<1, Args>::type
  659. >::type element_type;
  660. typedef ::std::pair< element_type, element_type > type;
  661. };
  662. template <class A, class C>
  663. ::std::pair<A,A>
  664. operator()(A a, A b, const C& c) const
  665. { return ::std::equal_range(a, b, c); }
  666. template <class A, class C, class D>
  667. ::std::pair<A,A>
  668. operator()(A a, A b, const C& c, D d) const
  669. { return ::std::equal_range(a, b, c, d); }
  670. };
  671. // binary_search ---------------------------------
  672. struct binary_search {
  673. template <class Args>
  674. struct sig {
  675. typedef bool type;
  676. };
  677. template <class A, class C >
  678. bool
  679. operator()(A a, A b, const C& c) const
  680. { return ::std::binary_search(a, b, c); }
  681. template <class A, class C, class D>
  682. bool
  683. operator()(A a, A b, const C& c, D d) const
  684. { return ::std::binary_search(a, b, c, d); }
  685. };
  686. // merge --------------------------------
  687. struct merge {
  688. template <class Args>
  689. struct sig {
  690. typedef typename boost::remove_const<
  691. typename boost::tuples::element<5, Args>::type
  692. >::type type;
  693. };
  694. template <class A, class C, class E>
  695. E
  696. operator()(A a, A b, C c, C d, E e) const
  697. { return std::merge(a, b, c, d, e);}
  698. template <class A, class C, class E, class F>
  699. E
  700. operator()(A a, A b, C c, C d, E e, F f) const
  701. { return std::merge(a, b, c, d, e, f);}
  702. };
  703. // inplace_merge ---------------------------------
  704. struct inplace_merge {
  705. template <class Args>
  706. struct sig {
  707. typedef void type;
  708. };
  709. template <class A>
  710. void
  711. operator()(A a, A b, A c) const
  712. { ::std::inplace_merge(a, b, c); }
  713. template <class A, class D>
  714. void
  715. operator()(A a, A b, A c, D d) const
  716. { ::std::inplace_merge(a, b, c, d); }
  717. };
  718. // includes ---------------------------------
  719. struct includes {
  720. template <class Args>
  721. struct sig {
  722. typedef bool type;
  723. };
  724. template <class A, class C>
  725. bool
  726. operator()(A a, A b, C c, C d) const
  727. { return ::std::includes(a, b, c, d); }
  728. template <class A, class C, class E>
  729. bool
  730. operator()(A a, A b, C c, C d, E e) const
  731. { return ::std::includes(a, b, c, d, e); }
  732. };
  733. // set_union --------------------------------
  734. struct set_union {
  735. template <class Args>
  736. struct sig {
  737. typedef typename boost::remove_const<
  738. typename boost::tuples::element<5, Args>::type
  739. >::type type;
  740. };
  741. template <class A, class C, class E>
  742. E
  743. operator()(A a, A b, C c, C d, E e) const
  744. { return std::set_union(a, b, c, d, e);}
  745. template <class A, class C, class E, class F>
  746. E
  747. operator()(A a, A b, C c, C d, E e, F f) const
  748. { return std::set_union(a, b, c, d, e, f);}
  749. };
  750. // set_intersection --------------------------------
  751. struct set_intersection {
  752. template <class Args>
  753. struct sig {
  754. typedef typename boost::remove_const<
  755. typename boost::tuples::element<5, Args>::type
  756. >::type type;
  757. };
  758. template <class A, class C, class E>
  759. E
  760. operator()(A a, A b, C c, C d, E e) const
  761. { return std::set_intersection(a, b, c, d, e);}
  762. template <class A, class C, class E, class F>
  763. E
  764. operator()(A a, A b, C c, C d, E e, F f) const
  765. { return std::set_intersection(a, b, c, d, e, f);}
  766. };
  767. // set_difference --------------------------------
  768. struct set_difference {
  769. template <class Args>
  770. struct sig {
  771. typedef typename boost::remove_const<
  772. typename boost::tuples::element<5, Args>::type
  773. >::type type;
  774. };
  775. template <class A, class C, class E>
  776. E
  777. operator()(A a, A b, C c, C d, E e) const
  778. { return std::set_difference(a, b, c, d, e);}
  779. template <class A, class C, class E, class F>
  780. E
  781. operator()(A a, A b, C c, C d, E e, F f) const
  782. { return std::set_difference(a, b, c, d, e, f);}
  783. };
  784. // set_symmetric_difference --------------------------------
  785. struct set_symmetric_difference {
  786. template <class Args>
  787. struct sig {
  788. typedef typename boost::remove_const<
  789. typename boost::tuples::element<5, Args>::type
  790. >::type type;
  791. };
  792. template <class A, class C, class E>
  793. E
  794. operator()(A a, A b, C c, C d, E e) const
  795. { return std::set_symmetric_difference(a, b, c, d, e);}
  796. template <class A, class C, class E, class F>
  797. E
  798. operator()(A a, A b, C c, C d, E e, F f) const
  799. { return std::set_symmetric_difference(a, b, c, d, e, f);}
  800. };
  801. // push_heap ---------------------------------
  802. struct push_heap {
  803. template <class Args>
  804. struct sig {
  805. typedef void type;
  806. };
  807. template <class A>
  808. void
  809. operator()(A a, A b) const
  810. { ::std::push_heap(a, b); }
  811. template <class A, class C>
  812. void
  813. operator()(A a, A b, C c) const
  814. { ::std::push_heap(a, b, c); }
  815. };
  816. // pop_heap ---------------------------------
  817. struct pop_heap {
  818. template <class Args>
  819. struct sig {
  820. typedef void type;
  821. };
  822. template <class A>
  823. void
  824. operator()(A a, A b) const
  825. { ::std::pop_heap(a, b); }
  826. template <class A, class C>
  827. void
  828. operator()(A a, A b, C c) const
  829. { ::std::pop_heap(a, b, c); }
  830. };
  831. // make_heap ---------------------------------
  832. struct make_heap {
  833. template <class Args>
  834. struct sig {
  835. typedef void type;
  836. };
  837. template <class A>
  838. void
  839. operator()(A a, A b) const
  840. { ::std::make_heap(a, b); }
  841. template <class A, class C>
  842. void
  843. operator()(A a, A b, C c) const
  844. { ::std::make_heap(a, b, c); }
  845. };
  846. // sort_heap ---------------------------------
  847. struct sort_heap {
  848. template <class Args>
  849. struct sig {
  850. typedef void type;
  851. };
  852. template <class A>
  853. void
  854. operator()(A a, A b) const
  855. { ::std::sort_heap(a, b); }
  856. template <class A, class C>
  857. void
  858. operator()(A a, A b, C c) const
  859. { ::std::sort_heap(a, b, c); }
  860. };
  861. // min ---------------------------------
  862. struct min {
  863. template <class Args>
  864. struct sig {
  865. typedef typename boost::remove_const<
  866. typename boost::tuples::element<1, Args>::type
  867. >::type type;
  868. };
  869. template <class A>
  870. A
  871. operator()(const A& a, const A& b) const
  872. { return (::std::min)(a, b); }
  873. template <class A, class C>
  874. A
  875. operator()(const A& a, const A& b, C c) const
  876. { return (::std::min)(a, b, c); }
  877. };
  878. // max ---------------------------------
  879. struct max {
  880. template <class Args>
  881. struct sig {
  882. typedef typename boost::remove_const<
  883. typename boost::tuples::element<1, Args>::type
  884. >::type type;
  885. };
  886. template <class A>
  887. A
  888. operator()(const A& a, const A& b) const
  889. { return (::std::max)(a, b); }
  890. template <class A, class C>
  891. A
  892. operator()(const A& a, const A& b, C c) const
  893. { return (::std::max)(a, b, c); }
  894. };
  895. struct min_element {
  896. template <class Args>
  897. struct sig {
  898. typedef typename boost::remove_const<
  899. typename boost::tuples::element<1, Args>::type
  900. >::type type;
  901. };
  902. template <class A>
  903. A
  904. operator()(A a, A b) const
  905. { return ::std::min_element(a, b); }
  906. template <class A, class C>
  907. A
  908. operator()(A a, A b, C c) const
  909. { return ::std::min_element(a, b, c); }
  910. };
  911. // max_element ---------------------------------
  912. struct max_element {
  913. template <class Args>
  914. struct sig {
  915. typedef typename boost::remove_const<
  916. typename boost::tuples::element<1, Args>::type
  917. >::type type;
  918. };
  919. template <class A>
  920. A
  921. operator()(A a, A b) const
  922. { return ::std::max_element(a, b); }
  923. template <class A, class C>
  924. A
  925. operator()(A a, A b, C c) const
  926. { return ::std::max_element(a, b, c); }
  927. };
  928. // lexicographical_compare ---------------------------------
  929. struct lexicographical_compare {
  930. template <class Args>
  931. struct sig {
  932. typedef bool type;
  933. };
  934. template <class A, class C>
  935. bool
  936. operator()(A a, A b, C c, C d) const
  937. { return ::std::lexicographical_compare(a, b, c, d); }
  938. template <class A, class C, class E>
  939. bool
  940. operator()(A a, A b, C c, C d, E e) const
  941. { return ::std::lexicographical_compare(a, b, c, d, e); }
  942. };
  943. // next_permutation ---------------------------------
  944. struct next_permutation {
  945. template <class Args>
  946. struct sig {
  947. typedef bool type;
  948. };
  949. template <class A>
  950. bool
  951. operator()(A a, A b) const
  952. { return ::std::next_permutation(a, b); }
  953. template <class A, class C >
  954. bool
  955. operator()(A a, A b, C c) const
  956. { return ::std::next_permutation(a, b, c); }
  957. };
  958. // prev_permutation ---------------------------------
  959. struct prev_permutation {
  960. template <class Args>
  961. struct sig {
  962. typedef bool type;
  963. };
  964. template <class A>
  965. bool
  966. operator()(A a, A b) const
  967. { return ::std::prev_permutation(a, b); }
  968. template <class A, class C >
  969. bool
  970. operator()(A a, A b, C c) const
  971. { return ::std::prev_permutation(a, b, c); }
  972. };
  973. } // end of ll namespace
  974. // There is no good way to call an overloaded member function in a
  975. // lambda expression.
  976. // The macro below defines a function object class for calling a
  977. // const_iterator returning member function of a container.
  978. #define CALL_MEMBER(X) \
  979. struct call_##X { \
  980. template <class Args> \
  981. struct sig { \
  982. typedef typename boost::remove_const< \
  983. typename boost::tuples::element<1, Args>::type \
  984. >::type::const_iterator type; \
  985. }; \
  986. \
  987. template<class T> \
  988. typename T::const_iterator \
  989. operator()(const T& t) const \
  990. { \
  991. return t.X(); \
  992. } \
  993. };
  994. // create call_begin and call_end classes
  995. CALL_MEMBER(begin)
  996. CALL_MEMBER(end)
  997. #undef CALL_MEMBER
  998. } // end of lambda namespace
  999. } // end of boost namespace
  1000. #endif