segmented_iterator_range.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*=============================================================================
  2. Copyright (c) 2011 Eric Niebler
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #if !defined(BOOST_FUSION_SEGMENTED_ITERATOR_RANGE_HPP_INCLUDED)
  7. #define BOOST_FUSION_SEGMENTED_ITERATOR_RANGE_HPP_INCLUDED
  8. #include <boost/fusion/support/config.hpp>
  9. #include <boost/detail/workaround.hpp>
  10. #include <boost/mpl/assert.hpp>
  11. #include <boost/type_traits/add_const.hpp>
  12. #include <boost/type_traits/remove_reference.hpp>
  13. #include <boost/fusion/support/tag_of.hpp>
  14. #include <boost/fusion/sequence/intrinsic/begin.hpp>
  15. #include <boost/fusion/sequence/intrinsic/end.hpp>
  16. #include <boost/fusion/iterator/next.hpp>
  17. #include <boost/fusion/iterator/deref.hpp>
  18. #include <boost/fusion/sequence/intrinsic/segments.hpp>
  19. #include <boost/fusion/algorithm/transformation/push_back.hpp>
  20. #include <boost/fusion/algorithm/transformation/push_front.hpp>
  21. #include <boost/fusion/iterator/equal_to.hpp>
  22. #include <boost/fusion/container/list/detail/reverse_cons.hpp>
  23. #include <boost/fusion/iterator/detail/segment_sequence.hpp>
  24. #include <boost/fusion/support/is_sequence.hpp>
  25. #include <boost/utility/enable_if.hpp>
  26. // Invariants:
  27. // - Each segmented iterator has a stack
  28. // - Each value in the stack is an iterator range
  29. // - The range at the top of the stack points to values
  30. // - All other ranges point to ranges
  31. // - The front of each range in the stack (besides the
  32. // topmost) is the range above it
  33. namespace boost { namespace fusion
  34. {
  35. template <typename First, typename Last>
  36. struct iterator_range;
  37. namespace result_of
  38. {
  39. template <typename Sequence, typename T>
  40. struct push_back;
  41. template <typename Sequence, typename T>
  42. struct push_front;
  43. }
  44. template <typename Sequence, typename T>
  45. BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  46. inline typename
  47. lazy_enable_if<
  48. traits::is_sequence<Sequence>
  49. , result_of::push_back<Sequence const, T>
  50. >::type
  51. push_back(Sequence const& seq, T const& x);
  52. template <typename Sequence, typename T>
  53. BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  54. inline typename
  55. lazy_enable_if<
  56. traits::is_sequence<Sequence>
  57. , result_of::push_front<Sequence const, T>
  58. >::type
  59. push_front(Sequence const& seq, T const& x);
  60. }}
  61. namespace boost { namespace fusion { namespace detail
  62. {
  63. //auto make_segment_sequence_front(stack_begin)
  64. //{
  65. // switch (size(stack_begin))
  66. // {
  67. // case 1:
  68. // return nil_;
  69. // case 2:
  70. // // car(cdr(stack_begin)) is a range over values.
  71. // assert(end(front(car(stack_begin))) == end(car(cdr(stack_begin))));
  72. // return iterator_range(begin(car(cdr(stack_begin))), end(front(car(stack_begin))));
  73. // default:
  74. // // car(cdr(stack_begin)) is a range over segments. We replace the
  75. // // front with a view that is restricted.
  76. // assert(end(segments(front(car(stack_begin)))) == end(car(cdr(stack_begin))));
  77. // return segment_sequence(
  78. // push_front(
  79. // // The following could be a segment_sequence. It then gets wrapped
  80. // // in a single_view, and push_front puts it in a join_view with the
  81. // // following iterator_range.
  82. // iterator_range(next(begin(car(cdr(stack_begin)))), end(segments(front(car(stack_begin))))),
  83. // make_segment_sequence_front(cdr(stack_begin))));
  84. // }
  85. //}
  86. template <typename Stack, std::size_t Size = Stack::size::value>
  87. struct make_segment_sequence_front
  88. {
  89. // assert(end(segments(front(car(stack_begin)))) == end(car(cdr(stack_begin))));
  90. BOOST_MPL_ASSERT((
  91. result_of::equal_to<
  92. typename result_of::end<
  93. typename remove_reference<
  94. typename add_const<
  95. typename result_of::segments<
  96. typename remove_reference<
  97. typename add_const<
  98. typename result_of::deref<
  99. typename Stack::car_type::begin_type
  100. >::type
  101. >::type
  102. >::type
  103. >::type
  104. >::type
  105. >::type
  106. >::type
  107. , typename Stack::cdr_type::car_type::end_type
  108. >));
  109. typedef
  110. iterator_range<
  111. typename result_of::next<
  112. typename Stack::cdr_type::car_type::begin_type
  113. >::type
  114. , typename result_of::end<
  115. typename remove_reference<
  116. typename add_const<
  117. typename result_of::segments<
  118. typename remove_reference<
  119. typename add_const<
  120. typename result_of::deref<
  121. typename Stack::car_type::begin_type
  122. >::type
  123. >::type
  124. >::type
  125. >::type
  126. >::type
  127. >::type
  128. >::type
  129. >
  130. rest_type;
  131. typedef
  132. make_segment_sequence_front<typename Stack::cdr_type>
  133. recurse;
  134. typedef
  135. segment_sequence<
  136. typename result_of::push_front<
  137. rest_type const
  138. , typename recurse::type
  139. >::type
  140. >
  141. type;
  142. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  143. static type call(Stack const& stack)
  144. {
  145. //return segment_sequence(
  146. // push_front(
  147. // iterator_range(next(begin(car(cdr(stack_begin)))), end(segments(front(car(stack_begin))))),
  148. // make_segment_sequence_front(cdr(stack_begin))));
  149. return type(
  150. fusion::push_front(
  151. rest_type(fusion::next(stack.cdr.car.first), fusion::end(fusion::segments(*stack.car.first)))
  152. , recurse::call(stack.cdr)));
  153. }
  154. };
  155. template <typename Stack>
  156. struct make_segment_sequence_front<Stack, 2>
  157. {
  158. // assert(end(front(car(stack_begin))) == end(car(cdr(stack_begin))));
  159. BOOST_MPL_ASSERT((
  160. result_of::equal_to<
  161. typename result_of::end<
  162. typename remove_reference<
  163. typename add_const<
  164. typename result_of::deref<
  165. typename Stack::car_type::begin_type
  166. >::type
  167. >::type
  168. >::type
  169. >::type
  170. , typename Stack::cdr_type::car_type::end_type
  171. >));
  172. typedef
  173. iterator_range<
  174. typename Stack::cdr_type::car_type::begin_type
  175. , typename result_of::end<
  176. typename remove_reference<
  177. typename add_const<
  178. typename result_of::deref<
  179. typename Stack::car_type::begin_type
  180. >::type
  181. >::type
  182. >::type
  183. >::type
  184. >
  185. type;
  186. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  187. static type call(Stack const& stack)
  188. {
  189. // return iterator_range(begin(car(cdr(stack_begin))), end(front(car(stack_begin))));
  190. return type(stack.cdr.car.first, fusion::end(*stack.car.first));
  191. }
  192. };
  193. template <typename Stack>
  194. struct make_segment_sequence_front<Stack, 1>
  195. {
  196. typedef typename Stack::cdr_type type; // nil_
  197. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  198. static type call(Stack const &stack)
  199. {
  200. return stack.cdr;
  201. }
  202. };
  203. //auto make_segment_sequence_back(stack_end)
  204. //{
  205. // switch (size(stack_end))
  206. // {
  207. // case 1:
  208. // return nil_;
  209. // case 2:
  210. // // car(cdr(stack_back)) is a range over values.
  211. // assert(end(front(car(stack_end))) == end(car(cdr(stack_end))));
  212. // return iterator_range(begin(front(car(stack_end))), begin(car(cdr(stack_end))));
  213. // default:
  214. // // car(cdr(stack_begin)) is a range over segments. We replace the
  215. // // back with a view that is restricted.
  216. // assert(end(segments(front(car(stack_end)))) == end(car(cdr(stack_end))));
  217. // return segment_sequence(
  218. // push_back(
  219. // iterator_range(begin(segments(front(car(stack_end)))), begin(car(cdr(stack_end)))),
  220. // make_segment_sequence_back(cdr(stack_end))));
  221. // }
  222. //}
  223. template <typename Stack, std::size_t Size = Stack::size::value>
  224. struct make_segment_sequence_back
  225. {
  226. // assert(end(segments(front(car(stack_begin)))) == end(car(cdr(stack_begin))));
  227. BOOST_MPL_ASSERT((
  228. result_of::equal_to<
  229. typename result_of::end<
  230. typename remove_reference<
  231. typename add_const<
  232. typename result_of::segments<
  233. typename remove_reference<
  234. typename add_const<
  235. typename result_of::deref<
  236. typename Stack::car_type::begin_type
  237. >::type
  238. >::type
  239. >::type
  240. >::type
  241. >::type
  242. >::type
  243. >::type
  244. , typename Stack::cdr_type::car_type::end_type
  245. >));
  246. typedef
  247. iterator_range<
  248. typename result_of::begin<
  249. typename remove_reference<
  250. typename add_const<
  251. typename result_of::segments<
  252. typename remove_reference<
  253. typename add_const<
  254. typename result_of::deref<
  255. typename Stack::car_type::begin_type
  256. >::type
  257. >::type
  258. >::type
  259. >::type
  260. >::type
  261. >::type
  262. >::type
  263. , typename Stack::cdr_type::car_type::begin_type
  264. >
  265. rest_type;
  266. typedef
  267. make_segment_sequence_back<typename Stack::cdr_type>
  268. recurse;
  269. typedef
  270. segment_sequence<
  271. typename result_of::push_back<
  272. rest_type const
  273. , typename recurse::type
  274. >::type
  275. >
  276. type;
  277. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  278. static type call(Stack const& stack)
  279. {
  280. // return segment_sequence(
  281. // push_back(
  282. // iterator_range(begin(segments(front(car(stack_end)))), begin(car(cdr(stack_end)))),
  283. // make_segment_sequence_back(cdr(stack_end))));
  284. return type(
  285. fusion::push_back(
  286. rest_type(fusion::begin(fusion::segments(*stack.car.first)), stack.cdr.car.first)
  287. , recurse::call(stack.cdr)));
  288. }
  289. };
  290. template <typename Stack>
  291. struct make_segment_sequence_back<Stack, 2>
  292. {
  293. // assert(end(front(car(stack_end))) == end(car(cdr(stack_end))));
  294. BOOST_MPL_ASSERT((
  295. result_of::equal_to<
  296. typename result_of::end<
  297. typename remove_reference<
  298. typename add_const<
  299. typename result_of::deref<
  300. typename Stack::car_type::begin_type
  301. >::type
  302. >::type
  303. >::type
  304. >::type
  305. , typename Stack::cdr_type::car_type::end_type
  306. >));
  307. typedef
  308. iterator_range<
  309. typename result_of::begin<
  310. typename remove_reference<
  311. typename add_const<
  312. typename result_of::deref<
  313. typename Stack::car_type::begin_type
  314. >::type
  315. >::type
  316. >::type
  317. >::type
  318. , typename Stack::cdr_type::car_type::begin_type
  319. >
  320. type;
  321. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  322. static type call(Stack const& stack)
  323. {
  324. // return iterator_range(begin(front(car(stack_end))), begin(car(cdr(stack_end))));
  325. return type(fusion::begin(*stack.car.first), stack.cdr.car.first);
  326. }
  327. };
  328. template <typename Stack>
  329. struct make_segment_sequence_back<Stack, 1>
  330. {
  331. typedef typename Stack::cdr_type type; // nil_
  332. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  333. static type call(Stack const& stack)
  334. {
  335. return stack.cdr;
  336. }
  337. };
  338. //auto make_segmented_range_reduce(stack_begin, stack_end)
  339. //{
  340. // if (size(stack_begin) == 1 && size(stack_end) == 1)
  341. // {
  342. // return segment_sequence(
  343. // single_view(
  344. // iterator_range(begin(car(stack_begin)), begin(car(stack_end)))));
  345. // }
  346. // else
  347. // {
  348. // // We are in the case where both begin_stack and/or end_stack have
  349. // // more than one element. Throw away any part of the tree where
  350. // // begin and end refer to the same segment.
  351. // if (begin(car(stack_begin)) == begin(car(stack_end)))
  352. // {
  353. // return make_segmented_range_reduce(cdr(stack_begin), cdr(stack_end));
  354. // }
  355. // else
  356. // {
  357. // // We are in the case where begin_stack and end_stack (a) have
  358. // // more than one element each, and (b) they point to different
  359. // // segments. We must construct a segmented sequence.
  360. // return segment_sequence(
  361. // push_back(
  362. // push_front(
  363. // iterator_range(
  364. // fusion::next(begin(car(stack_begin))),
  365. // begin(car(stack_end))), // a range of (possibly segmented) ranges.
  366. // make_segment_sequence_front(stack_begin)), // should be a (possibly segmented) range.
  367. // make_segment_sequence_back(stack_end))); // should be a (possibly segmented) range.
  368. // }
  369. // }
  370. //}
  371. template <
  372. typename StackBegin
  373. , typename StackEnd
  374. , int StackBeginSize = StackBegin::size::value
  375. , int StackEndSize = StackEnd::size::value>
  376. struct make_segmented_range_reduce;
  377. template <
  378. typename StackBegin
  379. , typename StackEnd
  380. , bool SameSegment
  381. #if !(BOOST_WORKAROUND(BOOST_GCC, >= 40000) && BOOST_WORKAROUND(BOOST_GCC, < 40200))
  382. = result_of::equal_to<
  383. typename StackBegin::car_type::begin_type
  384. , typename StackEnd::car_type::begin_type
  385. >::type::value
  386. #endif
  387. >
  388. struct make_segmented_range_reduce2
  389. {
  390. typedef
  391. iterator_range<
  392. typename result_of::next<
  393. typename StackBegin::car_type::begin_type
  394. >::type
  395. , typename StackEnd::car_type::begin_type
  396. >
  397. rest_type;
  398. typedef
  399. segment_sequence<
  400. typename result_of::push_back<
  401. typename result_of::push_front<
  402. rest_type const
  403. , typename make_segment_sequence_front<StackBegin>::type
  404. >::type const
  405. , typename make_segment_sequence_back<StackEnd>::type
  406. >::type
  407. >
  408. type;
  409. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  410. static type call(StackBegin stack_begin, StackEnd stack_end)
  411. {
  412. //return segment_sequence(
  413. // push_back(
  414. // push_front(
  415. // iterator_range(
  416. // fusion::next(begin(car(stack_begin))),
  417. // begin(car(stack_end))), // a range of (possibly segmented) ranges.
  418. // make_segment_sequence_front(stack_begin)), // should be a (possibly segmented) range.
  419. // make_segment_sequence_back(stack_end))); // should be a (possibly segmented) range.
  420. return type(
  421. fusion::push_back(
  422. fusion::push_front(
  423. rest_type(fusion::next(stack_begin.car.first), stack_end.car.first)
  424. , make_segment_sequence_front<StackBegin>::call(stack_begin))
  425. , make_segment_sequence_back<StackEnd>::call(stack_end)));
  426. }
  427. };
  428. template <typename StackBegin, typename StackEnd>
  429. struct make_segmented_range_reduce2<StackBegin, StackEnd, true>
  430. {
  431. typedef
  432. make_segmented_range_reduce<
  433. typename StackBegin::cdr_type
  434. , typename StackEnd::cdr_type
  435. >
  436. impl;
  437. typedef
  438. typename impl::type
  439. type;
  440. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  441. static type call(StackBegin stack_begin, StackEnd stack_end)
  442. {
  443. return impl::call(stack_begin.cdr, stack_end.cdr);
  444. }
  445. };
  446. template <typename StackBegin, typename StackEnd, int StackBeginSize, int StackEndSize>
  447. struct make_segmented_range_reduce
  448. : make_segmented_range_reduce2<StackBegin, StackEnd
  449. #if BOOST_WORKAROUND(BOOST_GCC, >= 40000) && BOOST_WORKAROUND(BOOST_GCC, < 40200)
  450. , result_of::equal_to<
  451. typename StackBegin::car_type::begin_type
  452. , typename StackEnd::car_type::begin_type
  453. >::type::value
  454. #endif
  455. >
  456. {};
  457. template <typename StackBegin, typename StackEnd>
  458. struct make_segmented_range_reduce<StackBegin, StackEnd, 1, 1>
  459. {
  460. typedef
  461. iterator_range<
  462. typename StackBegin::car_type::begin_type
  463. , typename StackEnd::car_type::begin_type
  464. >
  465. range_type;
  466. typedef
  467. single_view<range_type>
  468. segment_type;
  469. typedef
  470. segment_sequence<segment_type>
  471. type;
  472. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  473. static type call(StackBegin stack_begin, StackEnd stack_end)
  474. {
  475. //return segment_sequence(
  476. // single_view(
  477. // iterator_range(begin(car(stack_begin)), begin(car(stack_end)))));
  478. return type(segment_type(range_type(stack_begin.car.first, stack_end.car.first)));
  479. }
  480. };
  481. //auto make_segmented_range(begin, end)
  482. //{
  483. // return make_segmented_range_reduce(reverse(begin.context), reverse(end.context));
  484. //}
  485. template <typename Begin, typename End>
  486. struct make_segmented_range
  487. {
  488. typedef reverse_cons<typename Begin::context_type> reverse_begin_cons;
  489. typedef reverse_cons<typename End::context_type> reverse_end_cons;
  490. typedef
  491. make_segmented_range_reduce<
  492. typename reverse_begin_cons::type
  493. , typename reverse_end_cons::type
  494. >
  495. impl;
  496. typedef typename impl::type type;
  497. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  498. static type call(Begin const& begin, End const& end)
  499. {
  500. return impl::call(
  501. reverse_begin_cons::call(begin.context)
  502. , reverse_end_cons::call(end.context));
  503. }
  504. };
  505. }}}
  506. #endif