summaryrefslogtreecommitdiffstats
path: root/api/logic/SeparatorPrefixTree.h
blob: fd149af0a6e4fa49880af2ca167673d94b716760 (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#pragma once
#include <QString>
#include <QMap>
#include <QStringList>

template <char Tseparator>
class SeparatorPrefixTree
{
public:
	SeparatorPrefixTree(QStringList paths)
	{
		insert(paths);
	}

	SeparatorPrefixTree(bool contained = false)
	{
		m_contained = contained;
	}

	void insert(QStringList paths)
	{
		for(auto &path: paths)
		{
			insert(path);
		}
	}

	/// insert an exact path into the tree
	SeparatorPrefixTree & insert(QString path)
	{
		auto sepIndex = path.indexOf(Tseparator);
		if(sepIndex == -1)
		{
			children[path] = SeparatorPrefixTree(true);
			return children[path];
		}
		else
		{
			auto prefix = path.left(sepIndex);
			if(!children.contains(prefix))
			{
				children[prefix] = SeparatorPrefixTree(false);
			}
			return children[prefix].insert(path.mid(sepIndex + 1));
		}
	}

	/// is the path fully contained in the tree?
	bool contains(QString path) const
	{
		auto node = find(path);
		return node != nullptr;
	}

	/// does the tree cover a path? That means the prefix of the path is contained in the tree
	bool covers(QString path) const
	{
		// if we found some valid node, it's good enough. the tree covers the path
		if(m_contained)
		{
			return true;
		}
		auto sepIndex = path.indexOf(Tseparator);
		if(sepIndex == -1)
		{
			auto found = children.find(path);
			if(found == children.end())
			{
				return false;
			}
			return (*found).covers(QString());
		}
		else
		{
			auto prefix = path.left(sepIndex);
			auto found = children.find(prefix);
			if(found == children.end())
			{
				return false;
			}
			return (*found).covers(path.mid(sepIndex + 1));
		}
	}

	/// return the contained path that covers the path specified
	QString cover(QString path) const
	{
		// if we found some valid node, it's good enough. the tree covers the path
		if(m_contained)
		{
			return QString("");
		}
		auto sepIndex = path.indexOf(Tseparator);
		if(sepIndex == -1)
		{
			auto found = children.find(path);
			if(found == children.end())
			{
				return QString();
			}
			auto nested = (*found).cover(QString());
			if(nested.isNull())
			{
				return nested;
			}
			if(nested.isEmpty())
				return path;
			return path + Tseparator + nested;
		}
		else
		{
			auto prefix = path.left(sepIndex);
			auto found = children.find(prefix);
			if(found == children.end())
			{
				return QString();
			}
			auto nested = (*found).cover(path.mid(sepIndex + 1));
			if(nested.isNull())
			{
				return nested;
			}
			if(nested.isEmpty())
				return prefix;
			return prefix + Tseparator + nested;
		}
	}

	/// Does the path-specified node exist in the tree? It does not have to be contained.
	bool exists(QString path) const
	{
		auto sepIndex = path.indexOf(Tseparator);
		if(sepIndex == -1)
		{
			auto found = children.find(path);
			if(found == children.end())
			{
				return false;
			}
			return true;
		}
		else
		{
			auto prefix = path.left(sepIndex);
			auto found = children.find(prefix);
			if(found == children.end())
			{
				return false;
			}
			return (*found).exists(path.mid(sepIndex + 1));
		}
	}

	/// find a node in the tree by name
	const SeparatorPrefixTree * find(QString path) const
	{
		auto sepIndex = path.indexOf(Tseparator);
		if(sepIndex == -1)
		{
			auto found = children.find(path);
			if(found == children.end())
			{
				return nullptr;
			}
			return &(*found);
		}
		else
		{
			auto prefix = path.left(sepIndex);
			auto found = children.find(prefix);
			if(found == children.end())
			{
				return nullptr;
			}
			return (*found).find(path.mid(sepIndex + 1));
		}
	}

	/// is this a leaf node?
	bool leaf() const
	{
		return children.isEmpty();
	}

	/// is this node actually contained in the tree, or is it purely structural?
	bool contained() const
	{
		return m_contained;
	}

	/// Remove a path from the tree
	bool remove(QString path)
	{
		return removeInternal(path) != Failed;
	}

	/// Clear all children of this node tree node
	void clear()
	{
		children.clear();
	}

	QStringList toStringList() const
	{
		QStringList collected;
		// collecting these is more expensive.
		auto iter = children.begin();
		while(iter != children.end())
		{
			QStringList list = iter.value().toStringList();
			for(int i = 0; i < list.size(); i++)
			{
				list[i] = iter.key() + Tseparator + list[i];
			}
			collected.append(list);
			if((*iter).m_contained)
			{
				collected.append(iter.key());
			}
			iter++;
		}
		return collected;
	}
private:
	enum Removal
	{
		Failed,
		Succeeded,
		HasChildren
	};
	Removal removeInternal(QString path = QString())
	{
		if(path.isEmpty())
		{
			if(!m_contained)
			{
				// remove all children - we are removing a prefix
				clear();
				return Succeeded;
			}
			m_contained = false;
			if(children.size())
			{
				return HasChildren;
			}
			return Succeeded;
		}
		Removal remStatus = Failed;
		QString childToRemove;
		auto sepIndex = path.indexOf(Tseparator);
		if(sepIndex == -1)
		{
			childToRemove = path;
			auto found = children.find(childToRemove);
			if(found == children.end())
			{
				return Failed;
			}
			remStatus = (*found).removeInternal();
		}
		else
		{
			childToRemove = path.left(sepIndex);
			auto found = children.find(childToRemove);
			if(found == children.end())
			{
				return Failed;
			}
			remStatus = (*found).removeInternal(path.mid(sepIndex + 1));
		}
		switch (remStatus)
		{
			case Failed:
			case HasChildren:
			{
				return remStatus;
			}
			case Succeeded:
			{
				children.remove(childToRemove);
				if(m_contained)
				{
					return HasChildren;
				}
				if(children.size())
				{
					return HasChildren;
				}
				return Succeeded;
			}
		}
		return Failed;
	}

private:
	QMap<QString,SeparatorPrefixTree<Tseparator>> children;
	bool m_contained = false;
};