view.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. @file
  3. Defines experimental views.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_EXPERIMENTAL_VIEW_HPP
  9. #define BOOST_HANA_EXPERIMENTAL_VIEW_HPP
  10. #include <boost/hana/and.hpp>
  11. #include <boost/hana/at.hpp>
  12. #include <boost/hana/bool.hpp>
  13. #include <boost/hana/detail/decay.hpp>
  14. #include <boost/hana/fold_left.hpp>
  15. #include <boost/hana/functional/compose.hpp>
  16. #include <boost/hana/functional/on.hpp>
  17. #include <boost/hana/fwd/ap.hpp>
  18. #include <boost/hana/fwd/concat.hpp>
  19. #include <boost/hana/fwd/drop_front.hpp>
  20. #include <boost/hana/fwd/empty.hpp>
  21. #include <boost/hana/fwd/equal.hpp>
  22. #include <boost/hana/fwd/flatten.hpp>
  23. #include <boost/hana/fwd/is_empty.hpp>
  24. #include <boost/hana/fwd/less.hpp>
  25. #include <boost/hana/fwd/lift.hpp>
  26. #include <boost/hana/fwd/transform.hpp>
  27. #include <boost/hana/integral_constant.hpp>
  28. #include <boost/hana/length.hpp>
  29. #include <boost/hana/lexicographical_compare.hpp>
  30. #include <boost/hana/range.hpp>
  31. #include <boost/hana/tuple.hpp>
  32. #include <boost/hana/unpack.hpp>
  33. #include <cstddef>
  34. #include <type_traits>
  35. #include <utility>
  36. // Pros of views
  37. // - No temporary container created between algorithms
  38. // - Lazy, so only the minimum is required
  39. //
  40. // Cons of views
  41. // - Reference semantics mean possibility for dangling references
  42. // - Lose the ability to move from temporary containers
  43. // - When fetching the members of a view multiple times, no caching is done.
  44. // So for example, `t = transform(xs, f); at_c<0>(t); at_c<0>(t)` will
  45. // compute `f(at_c<0>(xs))` twice.
  46. // - push_back creates a joint_view and a single_view. The single_view holds
  47. // the value as a member. When doing multiple push_backs, we end up with a
  48. // joint_view<xxx, joint_view<single_view<T>, joint_view<single_view<T>, ....>>>
  49. // which contains a reference to `xxx` and all the `T`s by value. Such a
  50. // "view" is not cheap to copy, which is inconsistent with the usual
  51. // expectations about views.
  52. BOOST_HANA_NAMESPACE_BEGIN
  53. namespace experimental {
  54. struct view_tag;
  55. namespace detail {
  56. template <typename Sequence>
  57. struct is_view {
  58. static constexpr bool value = false;
  59. };
  60. template <typename Sequence>
  61. using view_storage = typename std::conditional<
  62. detail::is_view<Sequence>::value, Sequence, Sequence&
  63. >::type;
  64. }
  65. //////////////////////////////////////////////////////////////////////////
  66. // sliced_view
  67. //////////////////////////////////////////////////////////////////////////
  68. template <typename Sequence, std::size_t ...indices>
  69. struct sliced_view_t {
  70. detail::view_storage<Sequence> sequence_;
  71. using hana_tag = view_tag;
  72. };
  73. template <typename Sequence, typename Indices>
  74. constexpr auto sliced(Sequence& sequence, Indices const& indices) {
  75. return hana::unpack(indices, [&](auto ...i) {
  76. return sliced_view_t<Sequence, decltype(i)::value...>{sequence};
  77. });
  78. }
  79. namespace detail {
  80. template <typename Sequence, std::size_t ...i>
  81. struct is_view<sliced_view_t<Sequence, i...>> {
  82. static constexpr bool value = true;
  83. };
  84. }
  85. //////////////////////////////////////////////////////////////////////////
  86. // transformed_view
  87. //////////////////////////////////////////////////////////////////////////
  88. template <typename Sequence, typename F>
  89. struct transformed_view_t {
  90. detail::view_storage<Sequence> sequence_;
  91. F f_;
  92. using hana_tag = view_tag;
  93. };
  94. template <typename Sequence, typename F>
  95. constexpr transformed_view_t<Sequence, typename hana::detail::decay<F>::type>
  96. transformed(Sequence& sequence, F&& f) {
  97. return {sequence, static_cast<F&&>(f)};
  98. }
  99. namespace detail {
  100. template <typename Sequence, typename F>
  101. struct is_view<transformed_view_t<Sequence, F>> {
  102. static constexpr bool value = true;
  103. };
  104. }
  105. //////////////////////////////////////////////////////////////////////////
  106. // filtered_view
  107. //////////////////////////////////////////////////////////////////////////
  108. #if 0
  109. template <typename Sequence, typename Pred>
  110. using filtered_view_t = sliced_view_t<Sequence, detail::filtered_indices<...>>;
  111. template <typename Sequence, typename Pred>
  112. constexpr filtered_view_t<Sequence, Pred> filtered(Sequence& sequence, Pred&& pred) {
  113. return {sequence};
  114. }
  115. #endif
  116. //////////////////////////////////////////////////////////////////////////
  117. // joined_view
  118. //////////////////////////////////////////////////////////////////////////
  119. template <typename Sequence1, typename Sequence2>
  120. struct joined_view_t {
  121. detail::view_storage<Sequence1> sequence1_;
  122. detail::view_storage<Sequence2> sequence2_;
  123. using hana_tag = view_tag;
  124. };
  125. struct make_joined_view_t {
  126. template <typename Sequence1, typename Sequence2>
  127. constexpr joined_view_t<Sequence1, Sequence2> operator()(Sequence1& s1, Sequence2& s2) const {
  128. return {s1, s2};
  129. }
  130. };
  131. constexpr make_joined_view_t joined{};
  132. namespace detail {
  133. template <typename Sequence1, typename Sequence2>
  134. struct is_view<joined_view_t<Sequence1, Sequence2>> {
  135. static constexpr bool value = true;
  136. };
  137. }
  138. //////////////////////////////////////////////////////////////////////////
  139. // single_view
  140. //////////////////////////////////////////////////////////////////////////
  141. template <typename T>
  142. struct single_view_t {
  143. T value_;
  144. using hana_tag = view_tag;
  145. };
  146. template <typename T>
  147. constexpr single_view_t<typename hana::detail::decay<T>::type> single_view(T&& t) {
  148. return {static_cast<T&&>(t)};
  149. }
  150. namespace detail {
  151. template <typename T>
  152. struct is_view<single_view_t<T>> {
  153. static constexpr bool value = true;
  154. };
  155. }
  156. //////////////////////////////////////////////////////////////////////////
  157. // empty_view
  158. //////////////////////////////////////////////////////////////////////////
  159. struct empty_view_t {
  160. using hana_tag = view_tag;
  161. };
  162. constexpr empty_view_t empty_view() {
  163. return {};
  164. }
  165. namespace detail {
  166. template <>
  167. struct is_view<empty_view_t> {
  168. static constexpr bool value = true;
  169. };
  170. }
  171. } // end namespace experimental
  172. //////////////////////////////////////////////////////////////////////////
  173. // Foldable
  174. //////////////////////////////////////////////////////////////////////////
  175. template <>
  176. struct unpack_impl<experimental::view_tag> {
  177. // sliced_view
  178. template <typename Sequence, std::size_t ...i, typename F>
  179. static constexpr decltype(auto)
  180. apply(experimental::sliced_view_t<Sequence, i...> view, F&& f) {
  181. (void)view; // Remove spurious unused variable warning with GCC
  182. return static_cast<F&&>(f)(hana::at_c<i>(view.sequence_)...);
  183. }
  184. // transformed_view
  185. template <typename Sequence, typename F, typename G>
  186. static constexpr decltype(auto)
  187. apply(experimental::transformed_view_t<Sequence, F> view, G&& g) {
  188. return hana::unpack(view.sequence_, hana::on(static_cast<G&&>(g), view.f_));
  189. }
  190. // joined_view
  191. template <typename View, typename F, std::size_t ...i1, std::size_t ...i2>
  192. static constexpr decltype(auto)
  193. unpack_joined(View view, F&& f, std::index_sequence<i1...>,
  194. std::index_sequence<i2...>)
  195. {
  196. (void)view; // Remove spurious unused variable warning with GCC
  197. return static_cast<F&&>(f)(hana::at_c<i1>(view.sequence1_)...,
  198. hana::at_c<i2>(view.sequence2_)...);
  199. }
  200. template <typename S1, typename S2, typename F>
  201. static constexpr decltype(auto)
  202. apply(experimental::joined_view_t<S1, S2> view, F&& f) {
  203. constexpr auto N1 = decltype(hana::length(view.sequence1_))::value;
  204. constexpr auto N2 = decltype(hana::length(view.sequence2_))::value;
  205. return unpack_joined(view, static_cast<F&&>(f),
  206. std::make_index_sequence<N1>{},
  207. std::make_index_sequence<N2>{});
  208. }
  209. // single_view
  210. template <typename T, typename F>
  211. static constexpr decltype(auto) apply(experimental::single_view_t<T> view, F&& f) {
  212. return static_cast<F&&>(f)(view.value_);
  213. }
  214. // empty_view
  215. template <typename F>
  216. static constexpr decltype(auto) apply(experimental::empty_view_t, F&& f) {
  217. return static_cast<F&&>(f)();
  218. }
  219. };
  220. //////////////////////////////////////////////////////////////////////////
  221. // Iterable
  222. //////////////////////////////////////////////////////////////////////////
  223. template <>
  224. struct at_impl<experimental::view_tag> {
  225. // sliced_view
  226. template <typename Sequence, std::size_t ...i, typename N>
  227. static constexpr decltype(auto)
  228. apply(experimental::sliced_view_t<Sequence, i...> view, N const&) {
  229. constexpr std::size_t indices[] = {i...};
  230. constexpr std::size_t n = indices[N::value];
  231. return hana::at_c<n>(view.sequence_);
  232. }
  233. // transformed_view
  234. template <typename Sequence, typename F, typename N>
  235. static constexpr decltype(auto)
  236. apply(experimental::transformed_view_t<Sequence, F> view, N const& n) {
  237. return view.f_(hana::at(view.sequence_, n));
  238. }
  239. // joined_view
  240. template <std::size_t Left, typename View, typename N>
  241. static constexpr decltype(auto) at_joined_view(View view, N const&, hana::true_) {
  242. return hana::at_c<N::value>(view.sequence1_);
  243. }
  244. template <std::size_t Left, typename View, typename N>
  245. static constexpr decltype(auto) at_joined_view(View view, N const&, hana::false_) {
  246. return hana::at_c<N::value - Left>(view.sequence2_);
  247. }
  248. template <typename S1, typename S2, typename N>
  249. static constexpr decltype(auto)
  250. apply(experimental::joined_view_t<S1, S2> view, N const& n) {
  251. constexpr auto Left = decltype(hana::length(view.sequence1_))::value;
  252. return at_joined_view<Left>(view, n, hana::bool_c<(N::value < Left)>);
  253. }
  254. // single_view
  255. template <typename T, typename N>
  256. static constexpr decltype(auto) apply(experimental::single_view_t<T> view, N const&) {
  257. static_assert(N::value == 0,
  258. "trying to fetch an out-of-bounds element in a hana::single_view");
  259. return view.value_;
  260. }
  261. // empty_view
  262. template <typename N>
  263. static constexpr decltype(auto) apply(experimental::empty_view_t, N const&) = delete;
  264. };
  265. template <>
  266. struct length_impl<experimental::view_tag> {
  267. // sliced_view
  268. template <typename Sequence, std::size_t ...i>
  269. static constexpr auto
  270. apply(experimental::sliced_view_t<Sequence, i...>) {
  271. return hana::size_c<sizeof...(i)>;
  272. }
  273. // transformed_view
  274. template <typename Sequence, typename F>
  275. static constexpr auto apply(experimental::transformed_view_t<Sequence, F> view) {
  276. return hana::length(view.sequence_);
  277. }
  278. // joined_view
  279. template <typename S1, typename S2>
  280. static constexpr auto apply(experimental::joined_view_t<S1, S2> view) {
  281. return hana::size_c<
  282. decltype(hana::length(view.sequence1_))::value +
  283. decltype(hana::length(view.sequence2_))::value
  284. >;
  285. }
  286. // single_view
  287. template <typename T>
  288. static constexpr auto apply(experimental::single_view_t<T>) {
  289. return hana::size_c<1>;
  290. }
  291. // empty_view
  292. static constexpr auto apply(experimental::empty_view_t) {
  293. return hana::size_c<0>;
  294. }
  295. };
  296. template <>
  297. struct is_empty_impl<experimental::view_tag> {
  298. // sliced_view
  299. template <typename Sequence, std::size_t ...i>
  300. static constexpr auto
  301. apply(experimental::sliced_view_t<Sequence, i...>) {
  302. return hana::bool_c<sizeof...(i) == 0>;
  303. }
  304. // transformed_view
  305. template <typename Sequence, typename F>
  306. static constexpr auto apply(experimental::transformed_view_t<Sequence, F> view) {
  307. return hana::is_empty(view.sequence_);
  308. }
  309. // joined_view
  310. template <typename S1, typename S2>
  311. static constexpr auto apply(experimental::joined_view_t<S1, S2> view) {
  312. return hana::and_(hana::is_empty(view.sequence1_),
  313. hana::is_empty(view.sequence2_));
  314. }
  315. // single_view
  316. template <typename T>
  317. static constexpr auto apply(experimental::single_view_t<T>) {
  318. return hana::false_c;
  319. }
  320. // empty_view
  321. static constexpr auto apply(experimental::empty_view_t) {
  322. return hana::true_c;
  323. }
  324. };
  325. template <>
  326. struct drop_front_impl<experimental::view_tag> {
  327. template <typename View, typename N>
  328. static constexpr auto apply(View view, N const&) {
  329. constexpr auto n = N::value;
  330. constexpr auto Length = decltype(hana::length(view))::value;
  331. return experimental::sliced(view, hana::range_c<std::size_t, n, Length>);
  332. }
  333. };
  334. //////////////////////////////////////////////////////////////////////////
  335. // Functor
  336. //////////////////////////////////////////////////////////////////////////
  337. template <>
  338. struct transform_impl<experimental::view_tag> {
  339. template <typename Sequence, typename F, typename G>
  340. static constexpr auto
  341. apply(experimental::transformed_view_t<Sequence, F> view, G&& g) {
  342. return experimental::transformed(view.sequence_,
  343. hana::compose(static_cast<G&&>(g), view.f_));
  344. }
  345. template <typename View, typename F>
  346. static constexpr auto apply(View view, F&& f) {
  347. return experimental::transformed(view, static_cast<F&&>(f));
  348. }
  349. };
  350. //////////////////////////////////////////////////////////////////////////
  351. // Applicative
  352. //////////////////////////////////////////////////////////////////////////
  353. template <>
  354. struct lift_impl<experimental::view_tag> {
  355. template <typename T>
  356. static constexpr auto apply(T&& t) {
  357. return experimental::single_view(static_cast<T&&>(t));
  358. }
  359. };
  360. template <>
  361. struct ap_impl<experimental::view_tag> {
  362. template <typename F, typename X>
  363. static constexpr auto apply(F&& f, X&& x) {
  364. // TODO: Implement cleverly; we most likely need a cartesian_product
  365. // view or something like that.
  366. return hana::ap(hana::to_tuple(f), hana::to_tuple(x));
  367. }
  368. };
  369. //////////////////////////////////////////////////////////////////////////
  370. // Monad
  371. //////////////////////////////////////////////////////////////////////////
  372. template <>
  373. struct flatten_impl<experimental::view_tag> {
  374. template <typename View>
  375. static constexpr auto apply(View view) {
  376. // TODO: Implement a flattened_view instead
  377. return hana::fold_left(view, experimental::empty_view(),
  378. experimental::joined);
  379. }
  380. };
  381. //////////////////////////////////////////////////////////////////////////
  382. // MonadPlus
  383. //////////////////////////////////////////////////////////////////////////
  384. template <>
  385. struct concat_impl<experimental::view_tag> {
  386. template <typename View1, typename View2>
  387. static constexpr auto apply(View1 view1, View2 view2) {
  388. return experimental::joined(view1, view2);
  389. }
  390. };
  391. template <>
  392. struct empty_impl<experimental::view_tag> {
  393. static constexpr auto apply() {
  394. return experimental::empty_view();
  395. }
  396. };
  397. //////////////////////////////////////////////////////////////////////////
  398. // Comparable
  399. //////////////////////////////////////////////////////////////////////////
  400. template <>
  401. struct equal_impl<experimental::view_tag, experimental::view_tag> {
  402. template <typename View1, typename View2>
  403. static constexpr auto apply(View1 v1, View2 v2) {
  404. // TODO: Use a lexicographical comparison algorithm.
  405. return hana::equal(hana::to_tuple(v1), hana::to_tuple(v2));
  406. }
  407. };
  408. template <typename S>
  409. struct equal_impl<experimental::view_tag, S, hana::when<hana::Sequence<S>::value>> {
  410. template <typename View1, typename Seq>
  411. static constexpr auto apply(View1 v1, Seq const& s) {
  412. // TODO: Use a lexicographical comparison algorithm.
  413. return hana::equal(hana::to_tuple(v1), hana::to_tuple(s));
  414. }
  415. };
  416. template <typename S>
  417. struct equal_impl<S, experimental::view_tag, hana::when<hana::Sequence<S>::value>> {
  418. template <typename Seq, typename View2>
  419. static constexpr auto apply(Seq const& s, View2 v2) {
  420. // TODO: Use a lexicographical comparison algorithm.
  421. return hana::equal(hana::to_tuple(s), hana::to_tuple(v2));
  422. }
  423. };
  424. //////////////////////////////////////////////////////////////////////////
  425. // Orderable
  426. //////////////////////////////////////////////////////////////////////////
  427. template <>
  428. struct less_impl<experimental::view_tag, experimental::view_tag> {
  429. template <typename View1, typename View2>
  430. static constexpr auto apply(View1 v1, View2 v2) {
  431. return hana::lexicographical_compare(v1, v2);
  432. }
  433. };
  434. template <typename S>
  435. struct less_impl<experimental::view_tag, S, hana::when<hana::Sequence<S>::value>> {
  436. template <typename View1, typename Seq>
  437. static constexpr auto apply(View1 v1, Seq const& s) {
  438. return hana::lexicographical_compare(v1, s);
  439. }
  440. };
  441. template <typename S>
  442. struct less_impl<S, experimental::view_tag, hana::when<hana::Sequence<S>::value>> {
  443. template <typename Seq, typename View2>
  444. static constexpr auto apply(Seq const& s, View2 v2) {
  445. return hana::lexicographical_compare(s, v2);
  446. }
  447. };
  448. BOOST_HANA_NAMESPACE_END
  449. #endif // !BOOST_HANA_EXPERIMENTAL_VIEW_HPP