-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautotree.tcc
204 lines (184 loc) · 5.01 KB
/
autotree.tcc
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
#ifndef _AUTOTREE_TCC_
#define _AUTOTREE_TCC_
namespace AutoTree
{
//////////////////
// Node methods //
//////////////////
template <typename Key,
typename Tp,
typename Parent,
typename Compare,
typename Equ>
Node<Key,Tp,Parent,Compare,Equ>::Node (
Node<Key,Tp,Parent,Compare,Equ>&& other)
: mVal(std::move(other.mVal)), mParent(nullptr),
mChildren(std::move(other.mChildren))
{
}
template <typename Key,
typename Tp,
typename Parent,
typename Compare,
typename Equ>
Node<Key,Tp,Parent,Compare,Equ>& Node<Key,Tp,Parent,Compare,Equ>::operator= (
Node<Key,Tp,Parent,Compare,Equ>&& other)
{
if (&other != this)
{
mVal = std::move(other.mVal);
mParent = nullptr;
mChildren = std::move(other.mChildren);
}
return *this;
}
template <typename Key,
typename Tp,
typename Parent,
typename Compare,
typename Equ>
Node<Key,Tp,Parent,Compare,Equ>::Node(const Tp& val)
: mVal(val), mParent(nullptr), mChildren()
{
}
template <typename Key,
typename Tp,
typename Parent,
typename Compare,
typename Equ>
void Node<Key,Tp,Parent,Compare,Equ>::insert (
std::list<Key>& klist, const Tp& val)
{
// Precondition: a direct child-key is at end of klist.
if (klist.empty()) // end of recursion
{
mVal = val;
}
else // continue recursion
{
Key child_key = klist.back();
klist.pop_back();
// Insert new member into map of children, or point to existing child
Node<Key,Tp,Parent,Compare,Equ> candidate (Tp{});
std::pair<Key, Node<Key,Tp,Parent,Compare,Equ> > pr;
pr.first = child_key;
pr.second = std::move(candidate);
auto iter = mChildren.insert (std::move(pr)).first; // map iterator
// Iterator may point to candidate, or to existing child in map.
iter->second.mParent = this;
iter->second.insert (klist, val);
}
}
//////////////////
// Tree methods //
//////////////////
template <typename Key,
typename Tp,
typename Parent,
typename Compare,
typename Equ>
Tree<Key,Tp,Parent,Compare,Equ>::Tree (
Tree<Key,Tp,Parent,Compare,Equ>&& other)
: Node<Key,Tp,Parent,Compare,Equ> (std::move(other)),
mBase (std::move (other.mBase))
{
}
template <typename Key,
typename Tp,
typename Parent,
typename Compare,
typename Equ>
Tree<Key,Tp,Parent,Compare,Equ>& Tree<Key,Tp,Parent,Compare,Equ>::operator= (
Tree<Key,Tp,Parent,Compare,Equ>&& other)
{
if (&other != this)
{
mBase = std::move(other.mBase);
Node<Key,Tp,Parent,Compare,Equ>::operator= (std::move(other));
}
return *this;
}
template <typename Key,
typename Tp,
typename Parent,
typename Compare,
typename Equ>
Tree<Key,Tp,Parent,Compare,Equ>::Tree(const Key& base)
: Node<Key,Tp,Parent,Compare,Equ>(), mBase(base)
{
}
template <typename Key,
typename Tp,
typename Parent,
typename Compare,
typename Equ>
void Tree<Key,Tp,Parent,Compare,Equ>::insert (const Key& key, const Tp& val)
{
// Construct list with new key at the beginning and
// a direct child of self-key at the end...
std::list<Key> klist;
if (!expand (mBase, key, klist)) return;
// ...that if-test handles the case where the input key does not belong in
// this tree.
// Begin recursion
Node<Key,Tp,Parent,Compare,Equ>::insert (klist, val);
}
template <typename Key,
typename Tp,
typename Parent,
typename Compare,
typename Equ>
bool
Tree<Key,Tp,Parent,Compare,Equ>::expand (
const Key& base_key, const Key& new_key, std::list<Key>& klist)
{
// Construct list with new key at the beginning and
// a direct child of self-key at the end.
//
// A return value of true means success.
Key keyCopy (new_key);
Parent par;
Equ eq;
while (!eq (keyCopy, Key{}))
{
klist.push_back (keyCopy);
keyCopy = par(keyCopy);
if (eq (keyCopy, base_key)) break;
}
return !(eq (keyCopy, Key{}));
}
////////////////////////////
// Tree::iterator methods //
////////////////////////////
template <typename Key,
typename Tp,
typename Parent,
typename Compare,
typename Equ>
Tree<Key,Tp,Parent,Compare,Equ>::iterator::iterator()
: mMapIterStack()
{
}
template <typename Key,
typename Tp,
typename Parent,
typename Compare,
typename Equ>
Tree<Key,Tp,Parent,Compare,Equ>::iterator::iterator(iterator&& other)
: mMapIterStack(std::move(other.mMapIterStack))
{
}
template <typename Key,
typename Tp,
typename Parent,
typename Compare,
typename Equ>
typename Tree<Key,Tp,Parent,Compare,Equ>::iterator&
Tree<Key,Tp,Parent,Compare,Equ>::iterator::operator= (
Tree<Key,Tp,Parent,Compare,Equ>::iterator&& other)
{
if (&other != *this) mMapIterStack = std::move(other.mMapIterStack);
return *this;
}
} // namespace AutoTree
#endif // _AUTOTREE_TCC_