-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper-immut.h
197 lines (157 loc) · 5.01 KB
/
helper-immut.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifndef AGENCY_HELPER_H
#define AGENCY_HELPER_H
#include <optional>
#include "velocypack/Buffer.h"
#include "velocypack/Builder.h"
#include "velocypack/Iterator.h"
#include "velocypack/Parser.h"
#include "velocypack/Slice.h"
using namespace arangodb::velocypack;
using VPackBufferPtr = std::shared_ptr<Buffer<uint8_t>>;
static inline Buffer<uint8_t> vpackFromJsonString(char const* c) {
Options options;
options.checkAttributeUniqueness = true;
Parser parser(&options);
parser.parse(c);
std::shared_ptr<Builder> builder = parser.steal();
std::shared_ptr<Buffer<uint8_t>> buffer = builder->steal();
Buffer<uint8_t> b = std::move(*buffer);
return b;
}
static inline Buffer<uint8_t> operator"" _vpack(const char* json, size_t) {
return vpackFromJsonString(json);
}
template <class... Ts>
struct visitor : Ts... {
using Ts::operator()...;
};
template <class... Ts>
visitor(Ts...)->visitor<Ts...>;
template <typename T>
struct immut_list {
template <typename S>
struct element {
using pointer = std::shared_ptr<element<S>>;
S value;
std::shared_ptr<element<S>> next;
explicit element(S v) : value(std::move(v)), next(nullptr) {}
};
using element_pointer = typename element<T>::pointer;
element_pointer head;
[[nodiscard]] bool empty() const noexcept { return head == nullptr; }
immut_list(std::initializer_list<T> l) : head(nullptr) {
typename element<T>::pointer last = nullptr;
for (auto const& v : l) {
auto next = std::make_shared<element<T>>(v);
if (head == nullptr) {
head = next;
} else {
last->next = next;
}
last = next;
}
}
immut_list() : head(nullptr) {}
explicit immut_list(typename element<T>::pointer head)
: head(std::move(head)) {}
};
template<typename T>
struct immutable_list {
immutable_list() noexcept = default;
immutable_list(immutable_list const&) = default;
immutable_list(immutable_list&&) noexcept = default;
immutable_list& operator=(immutable_list const&) = default;
immutable_list& operator=(immutable_list&&) noexcept = default;
immutable_list(std::initializer_list<T> list) noexcept {
container_pointer current;
for (auto &element : list) {
container_pointer next = std::make_shared<element_container>(nullptr, element);
if (_head == nullptr) {
_head = next;
} else {
current->next = next;
}
current = next;
}
};
immutable_list<T> push_front(T t) const noexcept {
return immutable_list(std::make_shared<element_container>(_head, std::move(t)));
}
template<typename... As>
immutable_list<T> emplace_front(As&&... a) const noexcept {
return immutable_list(std::make_shared<element_container>(_head, std::forward<As>(a)...));
}
explicit operator bool() const noexcept { return _head.operator bool(); }
[[nodiscard]] bool empty() const noexcept { return _head; }
immutable_list<T> tail() const noexcept { return immutable_list(_head ? _head->next : nullptr); }
T const& head() const noexcept { return *_head; }
bool operator==(immutable_list<T> const& other) const noexcept {
if (_head == nullptr || other._head == nullptr) {
return _head == other._head;
}
if (_head->value == other._head->value) {
return tail() == other.tail();
}
return false;
}
bool has_prefix(immutable_list<T> const& prefix) const noexcept {
if (prefix._head == nullptr) {
return true;
}
if (_head == nullptr) {
return false;
}
if (_head->value == prefix._head->value) {
return tail().has_prefix(prefix.tail());
}
return false;
}
bool operator<(immutable_list<T> const& other) const noexcept {
if (other._head == nullptr) {
return false;
}
if (_head == nullptr) {
return true;
}
if (_head->value < other._head->value) {
return tail() < other.tail();
}
return false;
}
private:
struct element_container;
using container_pointer = std::shared_ptr<element_container>;
struct element_container {
container_pointer next;
T value;
template<typename... As>
explicit element_container(container_pointer next, As&&... a) : next(std::move(next)), value(std::forward<As>(a)...) {}
explicit element_container(container_pointer next, T value) : next(std::move(next)), value(std::move(value)) {}
};
explicit immutable_list(container_pointer head) noexcept : _head(std::move(head)) {}
container_pointer _head;
};
namespace std {
template <typename T>
struct tuple_size<immut_list<T>> {
constexpr static auto value = 2;
};
template <typename T>
struct tuple_element<0, immut_list<T>> {
using type = T;
};
template <typename T>
struct tuple_element<1, immut_list<T>> {
using type = immut_list<T>;
};
template <std::size_t I, typename T>
auto get(immut_list<T> const& l) -> typename std::tuple_element<I, immut_list<T>>::type {
static_assert(0 <= I && I <= 1);
if constexpr (I == 0) {
return l.head->value;
} else if constexpr (I == 1) {
return immut_list<T>{l.head->next};
}
}
} // namespace std
#endif