algorithm.hpp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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. struct random_shuffle {
  501. template <class Args>
  502. struct sig {
  503. typedef void type;
  504. };
  505. template <class A>
  506. void
  507. operator()(A a, A b) const
  508. { ::std::random_shuffle(a, b); }
  509. template <class A, class C>
  510. void
  511. operator()(A a, A b, const C& c) const
  512. { ::std::random_shuffle(a, b, c); }
  513. };
  514. // partition ---------------------------------
  515. struct partition {
  516. template <class Args>
  517. struct sig {
  518. typedef typename boost::remove_const<
  519. typename boost::tuples::element<1, Args>::type
  520. >::type type;
  521. };
  522. template <class A, class C>
  523. A
  524. operator()(A a, A b, C c) const
  525. { return ::std::partition(a, b, c); }
  526. };
  527. // stable_partition ---------------------------------
  528. struct stable_partition {
  529. template <class Args>
  530. struct sig {
  531. typedef typename boost::remove_const<
  532. typename boost::tuples::element<1, Args>::type
  533. >::type type;
  534. };
  535. template <class A, class C>
  536. A
  537. operator()(A a, A b, C c) const
  538. { return ::std::stable_partition(a, b, c); }
  539. };
  540. // sort ---------------------------------
  541. struct sort {
  542. template <class Args>
  543. struct sig {
  544. typedef void type;
  545. };
  546. template <class A>
  547. void
  548. operator()(A a, A b) const
  549. { ::std::sort(a, b); }
  550. template <class A, class C>
  551. void
  552. operator()(A a, A b, C c) const
  553. { ::std::sort(a, b, c); }
  554. };
  555. // stable_sort ---------------------------------
  556. struct stable_sort {
  557. template <class Args>
  558. struct sig {
  559. typedef void type;
  560. };
  561. template <class A>
  562. void
  563. operator()(A a, A b) const
  564. { ::std::stable_sort(a, b); }
  565. template <class A, class C>
  566. void
  567. operator()(A a, A b, C c) const
  568. { ::std::stable_sort(a, b, c); }
  569. };
  570. // partial_sort ---------------------------------
  571. struct partial_sort {
  572. template <class Args>
  573. struct sig {
  574. typedef void type;
  575. };
  576. template <class A>
  577. void
  578. operator()(A a, A b, A c) const
  579. { ::std::partial_sort(a, b, c); }
  580. template <class A, class D>
  581. void
  582. operator()(A a, A b, A c, D d) const
  583. { ::std::partial_sort(a, b, c, d); }
  584. };
  585. // partial_sort_copy ---------------------------------
  586. struct partial_sort_copy {
  587. template <class Args>
  588. struct sig {
  589. typedef typename boost::remove_const<
  590. typename boost::tuples::element<3, Args>::type
  591. >::type type;
  592. };
  593. template <class A, class C>
  594. C
  595. operator()(A a, A b, C c, C d) const
  596. { return ::std::partial_sort_copy(a, b, c, d); }
  597. template <class A, class C, class E >
  598. C
  599. operator()(A a, A b, C c, C d, E e) const
  600. { return ::std::partial_sort_copy(a, b, c, d, e); }
  601. };
  602. // nth_element ---------------------------------
  603. struct nth_element {
  604. template <class Args>
  605. struct sig {
  606. typedef void type;
  607. };
  608. template <class A>
  609. void
  610. operator()(A a, A b, A c) const
  611. { ::std::nth_element(a, b, c); }
  612. template <class A, class D>
  613. void
  614. operator()(A a, A b, A c, D d) const
  615. { ::std::nth_element(a, b, c, d); }
  616. };
  617. // lower_bound ---------------------------------
  618. struct lower_bound {
  619. template <class Args>
  620. struct sig {
  621. typedef typename boost::remove_const<
  622. typename boost::tuples::element<1, Args>::type
  623. >::type type;
  624. };
  625. template <class A, class C>
  626. A
  627. operator()(A a, A b, const C& c) const
  628. { return ::std::lower_bound(a, b, c); }
  629. template <class A, class C, class D>
  630. A
  631. operator()(A a, A b, const C& c, D d) const
  632. { return ::std::lower_bound(a, b, c, d); }
  633. };
  634. // upper_bound ---------------------------------
  635. struct upper_bound {
  636. template <class Args>
  637. struct sig {
  638. typedef typename boost::remove_const<
  639. typename boost::tuples::element<1, Args>::type
  640. >::type type;
  641. };
  642. template <class A, class C>
  643. A
  644. operator()(A a, A b, const C& c) const
  645. { return ::std::upper_bound(a, b, c); }
  646. template <class A, class C, class D>
  647. A
  648. operator()(A a, A b, const C& c, D d) const
  649. { return ::std::upper_bound(a, b, c, d); }
  650. };
  651. // equal_range ---------------------------------
  652. struct equal_range {
  653. template <class Args>
  654. struct sig {
  655. typedef typename boost::remove_const<
  656. typename boost::tuples::element<1, Args>::type
  657. >::type element_type;
  658. typedef ::std::pair< element_type, element_type > type;
  659. };
  660. template <class A, class C>
  661. ::std::pair<A,A>
  662. operator()(A a, A b, const C& c) const
  663. { return ::std::equal_range(a, b, c); }
  664. template <class A, class C, class D>
  665. ::std::pair<A,A>
  666. operator()(A a, A b, const C& c, D d) const
  667. { return ::std::equal_range(a, b, c, d); }
  668. };
  669. // binary_search ---------------------------------
  670. struct binary_search {
  671. template <class Args>
  672. struct sig {
  673. typedef bool type;
  674. };
  675. template <class A, class C >
  676. bool
  677. operator()(A a, A b, const C& c) const
  678. { return ::std::binary_search(a, b, c); }
  679. template <class A, class C, class D>
  680. bool
  681. operator()(A a, A b, const C& c, D d) const
  682. { return ::std::binary_search(a, b, c, d); }
  683. };
  684. // merge --------------------------------
  685. struct merge {
  686. template <class Args>
  687. struct sig {
  688. typedef typename boost::remove_const<
  689. typename boost::tuples::element<5, Args>::type
  690. >::type type;
  691. };
  692. template <class A, class C, class E>
  693. E
  694. operator()(A a, A b, C c, C d, E e) const
  695. { return std::merge(a, b, c, d, e);}
  696. template <class A, class C, class E, class F>
  697. E
  698. operator()(A a, A b, C c, C d, E e, F f) const
  699. { return std::merge(a, b, c, d, e, f);}
  700. };
  701. // inplace_merge ---------------------------------
  702. struct inplace_merge {
  703. template <class Args>
  704. struct sig {
  705. typedef void type;
  706. };
  707. template <class A>
  708. void
  709. operator()(A a, A b, A c) const
  710. { ::std::inplace_merge(a, b, c); }
  711. template <class A, class D>
  712. void
  713. operator()(A a, A b, A c, D d) const
  714. { ::std::inplace_merge(a, b, c, d); }
  715. };
  716. // includes ---------------------------------
  717. struct includes {
  718. template <class Args>
  719. struct sig {
  720. typedef bool type;
  721. };
  722. template <class A, class C>
  723. bool
  724. operator()(A a, A b, C c, C d) const
  725. { return ::std::includes(a, b, c, d); }
  726. template <class A, class C, class E>
  727. bool
  728. operator()(A a, A b, C c, C d, E e) const
  729. { return ::std::includes(a, b, c, d, e); }
  730. };
  731. // set_union --------------------------------
  732. struct set_union {
  733. template <class Args>
  734. struct sig {
  735. typedef typename boost::remove_const<
  736. typename boost::tuples::element<5, Args>::type
  737. >::type type;
  738. };
  739. template <class A, class C, class E>
  740. E
  741. operator()(A a, A b, C c, C d, E e) const
  742. { return std::set_union(a, b, c, d, e);}
  743. template <class A, class C, class E, class F>
  744. E
  745. operator()(A a, A b, C c, C d, E e, F f) const
  746. { return std::set_union(a, b, c, d, e, f);}
  747. };
  748. // set_intersection --------------------------------
  749. struct set_intersection {
  750. template <class Args>
  751. struct sig {
  752. typedef typename boost::remove_const<
  753. typename boost::tuples::element<5, Args>::type
  754. >::type type;
  755. };
  756. template <class A, class C, class E>
  757. E
  758. operator()(A a, A b, C c, C d, E e) const
  759. { return std::set_intersection(a, b, c, d, e);}
  760. template <class A, class C, class E, class F>
  761. E
  762. operator()(A a, A b, C c, C d, E e, F f) const
  763. { return std::set_intersection(a, b, c, d, e, f);}
  764. };
  765. // set_difference --------------------------------
  766. struct set_difference {
  767. template <class Args>
  768. struct sig {
  769. typedef typename boost::remove_const<
  770. typename boost::tuples::element<5, Args>::type
  771. >::type type;
  772. };
  773. template <class A, class C, class E>
  774. E
  775. operator()(A a, A b, C c, C d, E e) const
  776. { return std::set_difference(a, b, c, d, e);}
  777. template <class A, class C, class E, class F>
  778. E
  779. operator()(A a, A b, C c, C d, E e, F f) const
  780. { return std::set_difference(a, b, c, d, e, f);}
  781. };
  782. // set_symmetric_difference --------------------------------
  783. struct set_symmetric_difference {
  784. template <class Args>
  785. struct sig {
  786. typedef typename boost::remove_const<
  787. typename boost::tuples::element<5, Args>::type
  788. >::type type;
  789. };
  790. template <class A, class C, class E>
  791. E
  792. operator()(A a, A b, C c, C d, E e) const
  793. { return std::set_symmetric_difference(a, b, c, d, e);}
  794. template <class A, class C, class E, class F>
  795. E
  796. operator()(A a, A b, C c, C d, E e, F f) const
  797. { return std::set_symmetric_difference(a, b, c, d, e, f);}
  798. };
  799. // push_heap ---------------------------------
  800. struct push_heap {
  801. template <class Args>
  802. struct sig {
  803. typedef void type;
  804. };
  805. template <class A>
  806. void
  807. operator()(A a, A b) const
  808. { ::std::push_heap(a, b); }
  809. template <class A, class C>
  810. void
  811. operator()(A a, A b, C c) const
  812. { ::std::push_heap(a, b, c); }
  813. };
  814. // pop_heap ---------------------------------
  815. struct pop_heap {
  816. template <class Args>
  817. struct sig {
  818. typedef void type;
  819. };
  820. template <class A>
  821. void
  822. operator()(A a, A b) const
  823. { ::std::pop_heap(a, b); }
  824. template <class A, class C>
  825. void
  826. operator()(A a, A b, C c) const
  827. { ::std::pop_heap(a, b, c); }
  828. };
  829. // make_heap ---------------------------------
  830. struct make_heap {
  831. template <class Args>
  832. struct sig {
  833. typedef void type;
  834. };
  835. template <class A>
  836. void
  837. operator()(A a, A b) const
  838. { ::std::make_heap(a, b); }
  839. template <class A, class C>
  840. void
  841. operator()(A a, A b, C c) const
  842. { ::std::make_heap(a, b, c); }
  843. };
  844. // sort_heap ---------------------------------
  845. struct sort_heap {
  846. template <class Args>
  847. struct sig {
  848. typedef void type;
  849. };
  850. template <class A>
  851. void
  852. operator()(A a, A b) const
  853. { ::std::sort_heap(a, b); }
  854. template <class A, class C>
  855. void
  856. operator()(A a, A b, C c) const
  857. { ::std::sort_heap(a, b, c); }
  858. };
  859. // min ---------------------------------
  860. struct min {
  861. template <class Args>
  862. struct sig {
  863. typedef typename boost::remove_const<
  864. typename boost::tuples::element<1, Args>::type
  865. >::type type;
  866. };
  867. template <class A>
  868. A
  869. operator()(const A& a, const A& b) const
  870. { return (::std::min)(a, b); }
  871. template <class A, class C>
  872. A
  873. operator()(const A& a, const A& b, C c) const
  874. { return (::std::min)(a, b, c); }
  875. };
  876. // max ---------------------------------
  877. struct max {
  878. template <class Args>
  879. struct sig {
  880. typedef typename boost::remove_const<
  881. typename boost::tuples::element<1, Args>::type
  882. >::type type;
  883. };
  884. template <class A>
  885. A
  886. operator()(const A& a, const A& b) const
  887. { return (::std::max)(a, b); }
  888. template <class A, class C>
  889. A
  890. operator()(const A& a, const A& b, C c) const
  891. { return (::std::max)(a, b, c); }
  892. };
  893. struct min_element {
  894. template <class Args>
  895. struct sig {
  896. typedef typename boost::remove_const<
  897. typename boost::tuples::element<1, Args>::type
  898. >::type type;
  899. };
  900. template <class A>
  901. A
  902. operator()(A a, A b) const
  903. { return ::std::min_element(a, b); }
  904. template <class A, class C>
  905. A
  906. operator()(A a, A b, C c) const
  907. { return ::std::min_element(a, b, c); }
  908. };
  909. // max_element ---------------------------------
  910. struct max_element {
  911. template <class Args>
  912. struct sig {
  913. typedef typename boost::remove_const<
  914. typename boost::tuples::element<1, Args>::type
  915. >::type type;
  916. };
  917. template <class A>
  918. A
  919. operator()(A a, A b) const
  920. { return ::std::max_element(a, b); }
  921. template <class A, class C>
  922. A
  923. operator()(A a, A b, C c) const
  924. { return ::std::max_element(a, b, c); }
  925. };
  926. // lexicographical_compare ---------------------------------
  927. struct lexicographical_compare {
  928. template <class Args>
  929. struct sig {
  930. typedef bool type;
  931. };
  932. template <class A, class C>
  933. bool
  934. operator()(A a, A b, C c, C d) const
  935. { return ::std::lexicographical_compare(a, b, c, d); }
  936. template <class A, class C, class E>
  937. bool
  938. operator()(A a, A b, C c, C d, E e) const
  939. { return ::std::lexicographical_compare(a, b, c, d, e); }
  940. };
  941. // next_permutation ---------------------------------
  942. struct next_permutation {
  943. template <class Args>
  944. struct sig {
  945. typedef bool type;
  946. };
  947. template <class A>
  948. bool
  949. operator()(A a, A b) const
  950. { return ::std::next_permutation(a, b); }
  951. template <class A, class C >
  952. bool
  953. operator()(A a, A b, C c) const
  954. { return ::std::next_permutation(a, b, c); }
  955. };
  956. // prev_permutation ---------------------------------
  957. struct prev_permutation {
  958. template <class Args>
  959. struct sig {
  960. typedef bool type;
  961. };
  962. template <class A>
  963. bool
  964. operator()(A a, A b) const
  965. { return ::std::prev_permutation(a, b); }
  966. template <class A, class C >
  967. bool
  968. operator()(A a, A b, C c) const
  969. { return ::std::prev_permutation(a, b, c); }
  970. };
  971. } // end of ll namespace
  972. // There is no good way to call an overloaded member function in a
  973. // lambda expression.
  974. // The macro below defines a function object class for calling a
  975. // const_iterator returning member function of a container.
  976. #define CALL_MEMBER(X) \
  977. struct call_##X { \
  978. template <class Args> \
  979. struct sig { \
  980. typedef typename boost::remove_const< \
  981. typename boost::tuples::element<1, Args>::type \
  982. >::type::const_iterator type; \
  983. }; \
  984. \
  985. template<class T> \
  986. typename T::const_iterator \
  987. operator()(const T& t) const \
  988. { \
  989. return t.X(); \
  990. } \
  991. };
  992. // create call_begin and call_end classes
  993. CALL_MEMBER(begin)
  994. CALL_MEMBER(end)
  995. #undef CALL_MEMBER
  996. } // end of lambda namespace
  997. } // end of boost namespace
  998. #endif