summaryrefslogtreecommitdiff
path: root/include/patricia.h
blob: 5893a209e440b7a9033fe3f8df7deb3664bf7701 (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

/* Gets the bit-th bit from key */
#define GET_BIT(key, bit) (key[bit / 8] & (1 << (bit & 7)))
/* Checks if the bit-th bit from key1 and key2 differ, returns 1 if they do */
#define GET_BIT_XOR(key1, key2, bit) ((key1[bit / 8] ^ key2[bit / 8]) & (1 << (bit & 7)))

template<typename Data> struct patricia_elem
{
	unsigned int bit;
	patricia_elem<Data> *up, *one, *zero;
	Anope::string key;
	Data data;
};

template<typename Data, typename char_traits = std::std_char_traits>
class patricia_tree
{
	typedef std::basic_string<char, char_traits, std::allocator<char> > String;

	patricia_elem<Data> *root;
	size_t count;

 public:
	patricia_tree()
	{
		this->root = NULL;
		this->count = 0;
	}

	virtual ~patricia_tree()
	{
		while (this->root)
			this->erase(this->root->key);
	}

	inline size_t size() const { return this->count; }
	inline bool empty() const { return this->count == 0; }

	Data find(const Anope::string &ukey)
	{
		Anope::string key;
		for (size_t i = 0, j = ukey.length(); i < j; ++i)
			key.push_back(char_traits::chartolower(ukey[i]));

		size_t keylen = key.length();
		patricia_elem<Data> *prev = NULL, *cur = this->root;
		bool bitval;
		while (cur && (!prev || prev->bit < cur->bit))
		{
			prev = cur;

			if (cur->bit / 8 < keylen)
				bitval = GET_BIT(key, cur->bit);
			else
				bitval = false;

			cur = bitval ? cur->one : cur->zero;
		}

		if (cur && String(cur->key.c_str()).compare(key.c_str()) == 0)
			return cur->data;

		return NULL;
	}

	void insert(const Anope::string &ukey, Data data)
	{
		Anope::string key;
		for (size_t i = 0, j = ukey.length(); i < j; ++i)
			key.push_back(char_traits::chartolower(ukey[i]));

		size_t keylen = key.length();
		patricia_elem<Data> *prev = NULL, *cur = this->root;
		bool bitval;
		while (cur && (!prev || prev->bit < cur->bit))
		{
			prev = cur;

			if (cur->bit / 8 < keylen)
				bitval = GET_BIT(key, cur->bit);
			else
				bitval = false;

			cur = bitval ? cur->one : cur->zero;
		}

		if (cur && String(cur->key.c_str()).compare(key.c_str()) == 0)
			return;

		patricia_elem<Data> *newelem = new patricia_elem<Data>();
		newelem->key = key;
		newelem->data = data;

		if (!cur)
			newelem->bit = prev ? prev->bit + 1 : 0;
		else
			for (newelem->bit = 0; GET_BIT_XOR(key, cur->key, newelem->bit) == 0; ++newelem->bit);

		patricia_elem<Data> *place = prev;

		while (place && newelem->bit < place->bit)
		{
			prev = place;
			place = place->up;
		}

		newelem->up = place;

		if (GET_BIT(key, newelem->bit))
		{
			newelem->one = newelem;
			newelem->zero = place == prev ? cur : prev;
		}
		else
		{
			newelem->zero = newelem;
			newelem->one = place == prev ? cur : prev;
		}

		if (place != prev)
			prev->up = newelem;

		if (place)
		{
			bitval = GET_BIT(key, place->bit);
			if (bitval)
				place->one = newelem;
			else
				place->zero = newelem;
		}
		else
			this->root = newelem;

		++this->count;
	}

	Data erase(const Anope::string &ukey)
	{
		Anope::string key;
		for (size_t i = 0, j = ukey.length(); i < j; ++i)
			key.push_back(char_traits::chartolower(ukey[i]));

		size_t keylen = key.length();
		patricia_elem<Data> *prev = NULL, *cur = this->root;
		bool bitval;
		while (cur && (!prev || prev->bit < cur->bit))
		{
			prev = cur;

			if (cur->bit / 8 < keylen)
				bitval = GET_BIT(key, cur->bit);
			else
				bitval = false;

			cur = bitval ? cur->one : cur->zero;
		}

		if (!cur || String(cur->key.c_str()).compare(key.c_str()))
			return NULL;

		patricia_elem<Data> *other = (bitval ? prev->zero : prev->one);

		if (!prev->up)
			this->root = other;
		else if (prev->up->zero == prev)
			prev->up->zero = other;
		else
			prev->up->one = other;

		if (prev->zero && prev->zero->up == prev)
			prev->zero->up = prev->up;
		if (prev->one && prev->one->up == prev)
			prev->one->up = prev->up;

		if (cur != prev)
		{
			if (!cur->up)
				this->root = prev;
			else if (cur->up->zero == cur)
				cur->up->zero = prev;
			else
				cur->up->one = prev;

			if (cur->zero && cur->zero->up == cur)
				cur->zero->up = prev;
			if (cur->one && cur->one->up == cur)
				cur->one->up = prev;

			prev->one = cur->one;
			prev->zero = cur->zero;
			prev->up = cur->up;
			prev->bit = cur->bit;
		}

		Data data = cur->data;
		delete cur;

		--this->count;

		return data;
	}

	class iterator
	{
		enum IterationState
		{
			ITERATION_AT_CENTER,
			ITERATION_FROM_CENTER
		};

		patricia_elem<Data> *elem;
		IterationState from;

	 public:
		iterator(patricia_tree<Data, char_traits> &tree)
		{
			this->elem = tree.root;
			this->from = ITERATION_AT_CENTER;
		}

		bool next()
		{
			if (this->elem == NULL)
				;
			else if (this->from == ITERATION_AT_CENTER)
			{
				if (this->elem->zero != NULL && this->elem->zero->bit > this->elem->bit)
				{
					this->elem = this->elem->zero;
					return this->next();
				}

				this->from = ITERATION_FROM_CENTER;
				return true;
			}
			else if (this->from == ITERATION_FROM_CENTER)
			{
				if (this->elem->one != NULL && this->elem->one->bit > this->elem->bit)
				{
					this->elem = this->elem->one;
					this->from = ITERATION_AT_CENTER;
					return this->next();
				}

				while (this->elem->up != NULL && this->elem->up->one == this->elem)
					this->elem = this->elem->up;

				if (this->elem->up != NULL)
				{
					this->elem = this->elem->up;
					return true;
				}
			}

			return false;
		}

		inline Data operator*()
		{
			return this->elem->data;
		}
	};
};