-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode-operations.h
160 lines (123 loc) · 4.75 KB
/
node-operations.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// Created by lars on 28.11.19.
//
#ifndef AGENCY_NODE_OPERATIONS_H
#define AGENCY_NODE_OPERATIONS_H
#include "node.h"
namespace detail {
/*
* Indicates that the value operator does not want a node_null to be created
* if it is applied to a non existent node.
*/
struct value_operator_no_create_node {};
/*
* Indicates that the value operator only wants to be called for nodes of the
* specified type. Otherwise the node should be copied as is.
*/
struct value_operator_type_restricted {}; // use value_operator_only_for instead
template <typename T>
struct value_operator_only_for : value_operator_type_restricted {
using operator_restricted_to = T;
};
template <typename F>
struct value_operator_adapter : private F {
using F::F;
node_ptr operator()(node_ptr const& node) const {
if (auto const& actual_node = fix_node(node); actual_node) {
return visit_node(actual_node);
}
return nullptr;
}
private:
template <typename E = F, std::enable_if_t<std::negation_v<std::is_base_of<value_operator_type_restricted, E>>, int> = 0>
auto visit_node(node_ptr const& node) const {
return make_node_ptr(node->visit(self()));
}
template <typename E = F, std::enable_if_t<std::is_base_of_v<value_operator_type_restricted, E>, int> = 0>
auto visit_node(node_ptr const& node) const {
using T = typename F::operator_restricted_to;
return node->visit(
visitor{[this](T const& v) { return make_node_ptr(self()(v)); },
[node](auto const&) { return node; }});
}
template <typename E = F, std::enable_if_t<std::is_base_of_v<value_operator_no_create_node, E>, int> = 0>
[[nodiscard]] node_ptr const& fix_node(node_ptr const& node) const noexcept {
return node;
}
template <typename E = F, std::enable_if_t<std::negation_v<std::is_base_of<value_operator_no_create_node, E>>, int> = 0>
[[nodiscard]] node_ptr const& fix_node(node_ptr const& node) const noexcept {
return node::node_or_null(node);
}
F& self() { return *static_cast<F*>(this); }
F const& self() const { return *static_cast<F const*>(this); }
};
} // namespace detail
struct increment_value_operator {
double delta = 1.0;
increment_value_operator() noexcept = default;
explicit increment_value_operator(double delta) : delta(delta) {}
node_value<double> operator()(node_value<double> const& v) const noexcept {
return node_value<double>{v.value + delta};
}
template <typename T>
node_value<double> operator()(T const&) const noexcept {
return node_value<double>{delta};
}
};
struct push_value_operator {
node_ptr node;
explicit push_value_operator(node_ptr node) : node(std::move(node)) {}
node_array operator()(node_array const& array) const noexcept {
return array.push(node);
}
template <typename T>
node_array operator()(T const&) const noexcept {
return node_array{node};
}
};
struct prepend_value_operator {
node_ptr node;
explicit prepend_value_operator(node_ptr node) : node(std::move(node)) {}
node_array operator()(node_array const& array) const noexcept {
return array.prepend(node);
}
template <typename T>
node_array operator()(T const&) const noexcept {
return node_array{node};
}
};
struct pop_value_operator : detail::value_operator_no_create_node,
detail::value_operator_only_for<node_array> {
node_value_variant operator()(node_array const& array) const noexcept {
return array.pop();
}
};
struct shift_value_operator : detail::value_operator_no_create_node,
detail::value_operator_only_for<node_array> {
node_array operator()(node_array const& array) const noexcept {
return array.shift();
}
};
struct erase_value_operator : detail::value_operator_no_create_node,
detail::value_operator_only_for<node_array> {
node_ptr node;
explicit erase_value_operator(node_ptr node) : node(std::move(node)) {}
node_array operator()(node_array const& array) const noexcept {
return array.erase(node);
}
};
using prepend_operator = detail::value_operator_adapter<prepend_value_operator>;
using push_operator = detail::value_operator_adapter<push_value_operator>;
using pop_operator = detail::value_operator_adapter<pop_value_operator>;
using shift_operator = detail::value_operator_adapter<shift_value_operator>;
using increment_operator = detail::value_operator_adapter<increment_value_operator>;
using erase_operator = detail::value_operator_adapter<erase_value_operator>;
struct remove_operator {
node_ptr operator()(node_ptr const&) const noexcept { return nullptr; }
};
struct set_operator {
node_ptr node;
explicit set_operator(node_ptr node) : node(std::move(node)) {}
node_ptr const& operator()(node_ptr const&) const { return node; }
};
#endif // AGENCY_NODE_OPERATIONS_H