]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/hana/example/misc/tree.cpp
1 // Copyright Louis Dionne 2013-2017
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
5 #include <boost/hana/and.hpp>
6 #include <boost/hana/ap.hpp>
7 #include <boost/hana/assert.hpp>
8 #include <boost/hana/concat.hpp>
9 #include <boost/hana/equal.hpp>
10 #include <boost/hana/flatten.hpp>
11 #include <boost/hana/fold_left.hpp>
12 #include <boost/hana/lift.hpp>
13 #include <boost/hana/sum.hpp>
14 #include <boost/hana/transform.hpp>
15 #include <boost/hana/tuple.hpp>
16 namespace hana
= boost::hana
;
21 template <typename X
, typename Subforest
>
26 using hana_tag
= tree_tag
;
29 constexpr auto make_forest
= hana::make_tuple
;
31 template <typename X
, typename Subforest
>
32 constexpr auto make_node(X x
, Subforest subforest
) {
33 return node
<X
, Subforest
>{x
, subforest
};
36 namespace boost
{ namespace hana
{
37 //////////////////////////////////////////////////////////////////////////
39 //////////////////////////////////////////////////////////////////////////
41 struct equal_impl
<tree_tag
, tree_tag
> {
42 template <typename Node1
, typename Node2
>
43 static constexpr auto apply(Node1 node1
, Node2 node2
) {
45 hana::equal(node1
.value
, node2
.value
),
46 hana::equal(node1
.subforest
, node2
.subforest
)
51 //////////////////////////////////////////////////////////////////////////
53 //////////////////////////////////////////////////////////////////////////
55 struct transform_impl
<tree_tag
> {
56 template <typename Node
, typename F
>
57 static constexpr auto apply(Node node
, F f
) {
60 hana::transform(node
.subforest
, [=](auto subtree
) {
61 return hana::transform(subtree
, f
);
67 //////////////////////////////////////////////////////////////////////////
69 //////////////////////////////////////////////////////////////////////////
71 struct lift_impl
<tree_tag
> {
73 static constexpr auto apply(X x
)
74 { return make_node(x
, make_forest()); }
78 struct ap_impl
<tree_tag
> {
79 template <typename F
, typename X
>
80 static constexpr auto apply(F f
, X x
) {
84 hana::transform(x
.subforest
, [=](auto subtree
) {
85 return hana::transform(subtree
, f
.value
);
87 hana::transform(f
.subforest
, [=](auto subtree
) {
88 return hana::ap(subtree
, x
);
95 //////////////////////////////////////////////////////////////////////////
97 //////////////////////////////////////////////////////////////////////////
99 struct flatten_impl
<tree_tag
> {
100 template <typename Node
>
101 static constexpr auto apply(Node node
) {
105 node
.value
.subforest
,
106 hana::transform(node
.subforest
, hana::flatten
)
112 //////////////////////////////////////////////////////////////////////////
114 //////////////////////////////////////////////////////////////////////////
116 struct fold_left_impl
<tree_tag
> {
117 template <typename Node
, typename State
, typename F
>
118 static constexpr auto apply(Node node
, State state
, F f
) {
119 return hana::fold_left(node
.subforest
, f(state
, node
.value
),
120 [=](auto state
, auto subtree
) {
121 return hana::fold_left(subtree
, state
, f
);
128 constexpr auto tree
= make_node(1, make_forest(
129 make_node(2, make_forest()),
130 make_node(3, make_forest()),
131 make_node(4, make_forest())
134 BOOST_HANA_CONSTEXPR_CHECK(hana::sum
<>(tree
) == 10);
136 BOOST_HANA_CONSTEXPR_CHECK(hana::equal(
137 hana::transform(tree
, [](int i
) { return i
+ 1; }),
138 make_node(2, make_forest(
139 make_node(3, make_forest()),
140 make_node(4, make_forest()),
141 make_node(5, make_forest())