-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTree.mpp
More file actions
230 lines (199 loc) · 6.76 KB
/
Tree.mpp
File metadata and controls
230 lines (199 loc) · 6.76 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
export module CppUtils.Container.Tree;
import std;
import CppUtils.Type;
import CppUtils.String;
export namespace CppUtils::Container
{
namespace Tree
{
template<class T>
struct Node final
{
using ValueType = T;
ValueType value;
std::vector<Node<T>> nodes = {};
[[nodiscard]] inline constexpr auto find(this auto&& self, const ValueType& key) noexcept -> auto
{
return std::ranges::find_if(self.nodes, [&key](const auto& node) -> bool {
return node.value == key;
});
}
[[nodiscard]] inline constexpr auto exists(const ValueType& key) const noexcept -> bool
{
return find(key) != std::cend(nodes);
}
[[nodiscard]] inline constexpr auto operator[](this auto&& self, const ValueType& key) -> decltype(auto)
{
if (const auto value = self.find(key); value == std::end(self.nodes)) [[unlikely]]
if constexpr (std::is_const_v<std::remove_reference_t<decltype(self)>>)
throw std::out_of_range{"The Node does not contain the requested child."};
else
return self.nodes.emplace_back(key);
else
return *value;
}
[[nodiscard]] inline constexpr auto getNodesWithValue(const ValueType& filterValue) const
{
return nodes | std::views::filter([&filterValue](const auto& node) { return node.value == filterValue; });
}
};
template<class T>
[[nodiscard]] inline auto operator==(const Node<T>& lhs, const Node<T>& rhs) -> bool
{
return lhs.value == rhs.value and lhs.nodes == rhs.nodes;
}
template<class... Types>
requires std::default_initializable<Type::NthType<0, Types...>>
using VariantNode = Node<std::variant<Types...>>;
template<class CharT = char>
struct TokenTree
{
using Node = Tree::Node<Type::Token>;
Node root = Node{Type::hash(std::basic_string<CharT>{'r', 'o', 'o', 't'})};
String::HashTable<CharT> hashTable = String::makeHashTable<CharT>(std::basic_string<CharT>{'r', 'o', 'o', 't'});
};
template<class CharT = char>
struct NodeView final
{
using Node = Node<Type::Token>;
std::reference_wrapper<const Node> node;
std::reference_wrapper<const TokenTree<CharT>> tree;
inline explicit NodeView(const TokenTree<CharT>& tree):
node{std::cref(tree.root)},
tree{std::cref(tree)}
{}
inline explicit NodeView(const Node& node, const TokenTree<CharT>& tree):
node{std::cref(node)},
tree{std::cref(tree)}
{}
[[nodiscard]] inline auto get() const -> const Node&
{
return node.get();
}
[[nodiscard]] inline auto get(Type::Token token) const -> const Node&
{
return get()[token];
}
inline auto to(Type::Token token) -> NodeView&
{
node = std::cref(get(token));
return *this;
}
[[nodiscard]] inline auto getPrintableValue() const -> auto
{
return String::toAscii(String::getNameOrValue(get().value, tree.get().hashTable));
}
};
template<class CharT>
[[nodiscard]] inline auto operator==(const NodeView<CharT>& lhs, const NodeView<CharT>& rhs) -> bool
{
return lhs.get() == rhs.get();
}
template<class CharT>
[[nodiscard]] inline auto operator==(const TokenTree<CharT>& lhs, const NodeView<CharT>& rhs) -> bool
{
return NodeView<CharT>{lhs} == rhs;
}
template<class CharT>
[[nodiscard]] inline auto operator==(const TokenTree<CharT>& lhs, const TokenTree<CharT>& rhs) -> bool
{
return NodeView<CharT>{lhs} == NodeView<CharT>{rhs};
}
template<class CharT>
[[nodiscard]] inline auto operator==(const NodeView<CharT>& lhs, const typename TokenTree<CharT>::Node& rhs) -> bool
{
return lhs.get() == rhs;
}
template<class CharT>
[[nodiscard]] inline auto operator==(const TokenTree<CharT>& lhs, const typename TokenTree<CharT>::Node& rhs) -> bool
{
return NodeView<CharT>{lhs} == rhs;
}
}
template<class CharT = char>
using TokenTree = Tree::TokenTree<CharT>;
template<class CharT = char>
using AST = TokenTree<CharT>;
template<class CharT = char>
using ASTNode = TokenTree<CharT>::Node;
template<class CharT = char>
using ASTNodeView = Tree::NodeView<CharT>;
}
namespace std
{
template<class T, class CharT>
requires CppUtils::Type::Specializes<T, CppUtils::Container::Tree::Node> and CppUtils::Type::Printable<typename T::ValueType>
struct formatter<T, CharT>
{
inline constexpr auto parse(std::format_parse_context& context) -> auto
{
return std::begin(context);
}
template<class FormatContext>
inline auto format(const T& node, FormatContext& context) const -> decltype(context.out())
{
auto&& out = context.out();
formatNode(node, context);
return out;
}
private:
template<class FormatContext>
inline auto formatNode(const T& node, FormatContext& context, const std::basic_string<CharT>& prefix = "") const -> void
{
auto&& out = context.out();
std::format_to(out, "{}\n", node.value);
const auto nbNodes = std::size(node.nodes);
for (auto i = 0uz; i < nbNodes; ++i)
{
const auto isLastNode = (i == nbNodes - 1);
std::format_to(out, "{}{}─ ", prefix, isLastNode ? "└" : "├");
formatNode(node.nodes.at(i), context, prefix + (isLastNode ? " " : "│") + " ");
}
}
};
template<class CharT, class OutputCharT>
struct formatter<CppUtils::Container::ASTNodeView<CharT>, OutputCharT>
{
inline constexpr auto parse(std::format_parse_context& context) -> auto
{
return std::begin(context);
}
template<class FormatContext>
inline auto format(const CppUtils::Container::ASTNodeView<CharT>& nodeView, FormatContext& context) const -> decltype(context.out())
{
auto&& out = context.out();
formatNode(nodeView, context);
return out;
}
private:
template<class FormatContext>
inline auto formatNode(const CppUtils::Container::ASTNodeView<CharT>& nodeView, FormatContext& context, const std::basic_string<OutputCharT>& prefix = "") const -> void
{
const auto& [node, tree] = nodeView;
auto&& out = context.out();
std::format_to(out, "{}\n", nodeView.getPrintableValue());
const auto nbNodes = std::size(node.get().nodes);
for (auto i = 0uz; i < nbNodes; ++i)
{
const auto isLastNode = (i == nbNodes - 1);
std::format_to(out, "{}{}─ ", prefix, isLastNode ? "└" : "├");
auto child = CppUtils::Container::ASTNodeView<CharT>{node.get().nodes.at(i), tree};
formatNode(child, context, prefix + (isLastNode ? " " : "│") + " ");
}
}
};
template<class CharT, class OutputCharT>
struct formatter<CppUtils::Container::TokenTree<CharT>, OutputCharT>
{
inline constexpr auto parse(std::format_parse_context& context) -> auto
{
return std::begin(context);
}
template<class FormatContext>
inline auto format(const CppUtils::Container::TokenTree<CharT>& tree, FormatContext& context) const -> decltype(context.out())
{
auto&& out = context.out();
return std::format_to(out, "{}", typename CppUtils::Container::Tree::NodeView<CharT>{tree});
}
};
}