-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.hpp
128 lines (111 loc) · 2.03 KB
/
node.hpp
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
#ifndef NODE_H
#define NODE_H
template <typename T, int N>
struct PODNode
{
T data;
PODNode* links[N];
T& get() {
return data;
}
const T& get() const {
return data;
}
void set(const T& _data) {
data = _data;
}
void set(T&& _data) {
data = _data;
}
PODNode* link(int idx) {
return links[idx];
}
const PODNode* link(int idx) const {
return links[idx];
}
void set_link(int idx, PODNode* _link) {
links[idx] = _link;
}
bool operator<(const PODNode& other) const
{
return data < other.data;
}
bool operator>(const PODNode& other) const
{
return data > other.data;
}
bool operator==(const PODNode& other) const
{
return data == other.data;
}
bool operator<=(const PODNode& other) const
{
return data <= other.data;
}
bool operator>=(const PODNode& other) const
{
return data >= other.data;
}
bool operator!=(const PODNode& other) const
{
return data != other.data;
}
};
template <typename T>
void DestroyPODNodes(T* tree)
{
if (!tree) return;
for (T* node : tree->links)
DestroyPODNodes(node);
delete tree;
}
template <typename T>
class PODNodeGuard
{
public:
explicit PODNodeGuard(T* node_ = nullptr)
: node{ node_ } {}
explicit PODNodeGuard(PODNodeGuard&& other)
: node{ other.node }
{
other.node = nullptr;
}
~PODNodeGuard() {
DestroyPODNodes(node);
}
PODNodeGuard& operator=(const PODNodeGuard&) = delete;
PODNodeGuard& operator=(PODNodeGuard&& other) noexcept {
DestroyPODNodes(node);
node = other.node;
other.node = nullptr;
return *this;
}
PODNodeGuard& operator=(T* node_) {
DestroyPODNodes(node);
node = node_;
return *this;
}
T* get() {
return node;
}
const T* get() const {
return node;
}
T* operator->() {
return node;
}
const T* operator->() const {
return node;
}
T* release() {
T* temp = node;
node = nullptr;
return temp;
}
operator bool() {
return node;
}
private:
T* node;
};
#endif // NODE_H