]>
Commit | Line | Data |
---|---|---|
5c193dec AJ |
1 | /* |
2 | * Copyright (C) 1996-2014 The Squid Software Foundation and contributors | |
3 | * | |
4 | * Squid software is distributed under GPLv2+ license and includes | |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
7 | */ | |
8 | ||
b5638623 | 9 | #ifndef SQUID_SPLAY_H |
10 | #define SQUID_SPLAY_H | |
3c01c392 | 11 | |
cfb88efb | 12 | #include "fatal.h" |
cfb88efb | 13 | #include <stack> |
29b17d63 | 14 | |
fb3eae94 | 15 | // private class of Splay. Do not use directly |
b67e2c8c | 16 | template <class V> |
42a503bd | 17 | class SplayNode |
18 | { | |
42a503bd | 19 | public: |
b67e2c8c | 20 | typedef V Value; |
29b17d63 | 21 | typedef int SPLAYCMP(Value const &a, Value const &b); |
22 | typedef void SPLAYFREE(Value &); | |
23 | typedef void SPLAYWALKEE(Value const & nodedata, void *state); | |
00d77d6b | 24 | static void DefaultFree (Value &aValue) {delete aValue;} |
42a503bd | 25 | |
2bcafc95 | 26 | SplayNode<V> (Value const &); |
29b17d63 | 27 | Value data; |
28 | mutable SplayNode<V> *left; | |
29 | mutable SplayNode<V> *right; | |
fb3eae94 | 30 | void destroy(SPLAYFREE * = DefaultFree); |
29b17d63 | 31 | void walk(SPLAYWALKEE *, void *callerState); |
4c50505b | 32 | SplayNode<V> const * start() const; |
ccd33084 | 33 | SplayNode<V> const * finish() const; |
42a503bd | 34 | |
6ca34f6f | 35 | SplayNode<V> * remove(const Value data, SPLAYCMP * compare); |
42a503bd | 36 | |
29b17d63 | 37 | SplayNode<V> * insert(Value data, SPLAYCMP * compare); |
42a503bd | 38 | |
fb3eae94 FC |
39 | /// look in the splay for data for where compare(data,candidate) == true. |
40 | /// return NULL if not found, a pointer to the sought data if found. | |
b001e822 | 41 | template <class FindValue> SplayNode<V> * splay(const FindValue &data, int( * compare)(FindValue const &a, Value const &b)) const; |
97754f5a AR |
42 | |
43 | /// recursively visit left nodes, this node, and then right nodes | |
44 | template <class Visitor> void visit(Visitor &v) const; | |
b67e2c8c | 45 | }; |
46 | ||
29b17d63 | 47 | typedef SplayNode<void *> splayNode; |
48 | ||
49 | template <class V> | |
b8bad68c | 50 | class SplayConstIterator; |
51 | ||
52 | template <class V> | |
ccd33084 | 53 | class SplayIterator; |
54 | ||
55 | template <class V> | |
42a503bd | 56 | class Splay |
57 | { | |
42a503bd | 58 | public: |
29b17d63 | 59 | typedef V Value; |
60 | typedef int SPLAYCMP(Value const &a, Value const &b); | |
42a503bd | 61 | typedef void SPLAYFREE(Value &); |
b8bad68c | 62 | typedef SplayIterator<V> iterator; |
63 | typedef const SplayConstIterator<V> const_iterator; | |
c5dd4956 | 64 | Splay():head(NULL), elements (0) {} |
42a503bd | 65 | |
b001e822 | 66 | template <class FindValue> Value const *find (FindValue const &, int( * compare)(FindValue const &a, Value const &b)) const; |
fb3eae94 | 67 | |
0353e724 | 68 | void insert(Value const &, SPLAYCMP *compare); |
42a503bd | 69 | |
6ca34f6f | 70 | void remove(Value const &, SPLAYCMP *compare); |
42a503bd | 71 | |
fb3eae94 | 72 | void destroy(SPLAYFREE * = SplayNode<V>::DefaultFree); |
42a503bd | 73 | |
4c50505b | 74 | SplayNode<V> const * start() const; |
42a503bd | 75 | |
ccd33084 | 76 | SplayNode<V> const * finish() const; |
42a503bd | 77 | |
78 | size_t size() const; | |
79 | ||
4a0199ee | 80 | bool empty() const { return size() == 0; } |
fb3eae94 | 81 | |
b8bad68c | 82 | const_iterator begin() const; |
83 | ||
84 | const_iterator end() const; | |
85 | ||
97754f5a AR |
86 | /// recursively visit all nodes, in left-to-right order |
87 | template <class Visitor> void visit(Visitor &v) const; | |
88 | ||
0353e724 | 89 | size_t elements; |
f5dc4237 FC |
90 | private: |
91 | mutable SplayNode<V> * head; | |
29b17d63 | 92 | }; |
b67e2c8c | 93 | |
b67e2c8c | 94 | SQUIDCEXTERN int splayLastResult; |
95 | ||
29b17d63 | 96 | template<class V> |
2bcafc95 | 97 | SplayNode<V>::SplayNode (Value const &someData) : data(someData), left(NULL), right (NULL) {} |
98 | ||
87c692d7 | 99 | template<class V> |
29b17d63 | 100 | void |
101 | SplayNode<V>::walk(SPLAYWALKEE * walkee, void *state) | |
102 | { | |
29b17d63 | 103 | if (left) |
42a503bd | 104 | left->walk(walkee, state); |
105 | ||
29b17d63 | 106 | walkee(data, state); |
42a503bd | 107 | |
29b17d63 | 108 | if (right) |
42a503bd | 109 | right->walk(walkee, state); |
29b17d63 | 110 | } |
111 | ||
4c50505b | 112 | template<class V> |
113 | SplayNode<V> const * | |
114 | SplayNode<V>::start() const | |
115 | { | |
e45bad43 | 116 | if (left) |
42a503bd | 117 | return left->start(); |
118 | ||
119 | return this; | |
120 | } | |
121 | ||
122 | template<class V> | |
123 | SplayNode<V> const * | |
ccd33084 | 124 | SplayNode<V>::finish() const |
42a503bd | 125 | { |
e45bad43 | 126 | if (right) |
ccd33084 | 127 | return right->finish(); |
42a503bd | 128 | |
4c50505b | 129 | return this; |
130 | } | |
131 | ||
29b17d63 | 132 | template<class V> |
133 | void | |
134 | SplayNode<V>::destroy(SPLAYFREE * free_func) | |
135 | { | |
29b17d63 | 136 | if (left) |
42a503bd | 137 | left->destroy(free_func); |
138 | ||
29b17d63 | 139 | if (right) |
42a503bd | 140 | right->destroy(free_func); |
141 | ||
29b17d63 | 142 | free_func(data); |
42a503bd | 143 | |
29b17d63 | 144 | delete this; |
145 | } | |
146 | ||
147 | template<class V> | |
148 | SplayNode<V> * | |
6ca34f6f | 149 | SplayNode<V>::remove(Value const dataToRemove, SPLAYCMP * compare) |
29b17d63 | 150 | { |
626e1f97 | 151 | SplayNode<V> *result = splay(dataToRemove, compare); |
42a503bd | 152 | |
f53969cc | 153 | if (splayLastResult == 0) { /* found it */ |
42a503bd | 154 | SplayNode<V> *newTop; |
155 | ||
156 | if (result->left == NULL) { | |
157 | newTop = result->right; | |
158 | } else { | |
159 | newTop = result->left->splay(dataToRemove, compare); | |
160 | /* temporary */ | |
161 | newTop->right = result->right; | |
162 | result->right = NULL; | |
163 | } | |
164 | ||
165 | delete result; | |
166 | return newTop; | |
29b17d63 | 167 | } |
42a503bd | 168 | |
f53969cc | 169 | return result; /* It wasn't there */ |
29b17d63 | 170 | } |
b67e2c8c | 171 | |
29b17d63 | 172 | template<class V> |
173 | SplayNode<V> * | |
626e1f97 | 174 | SplayNode<V>::insert(Value dataToInsert, SPLAYCMP * compare) |
29b17d63 | 175 | { |
176 | /* create node to insert */ | |
2bcafc95 | 177 | SplayNode<V> *newNode = new SplayNode<V>(dataToInsert); |
626e1f97 | 178 | SplayNode<V> *newTop = splay(dataToInsert, compare); |
42a503bd | 179 | |
29b17d63 | 180 | if (splayLastResult < 0) { |
42a503bd | 181 | newNode->left = newTop->left; |
182 | newNode->right = newTop; | |
183 | newTop->left = NULL; | |
184 | return newNode; | |
29b17d63 | 185 | } else if (splayLastResult > 0) { |
42a503bd | 186 | newNode->right = newTop->right; |
187 | newNode->left = newTop; | |
188 | newTop->right = NULL; | |
189 | return newNode; | |
29b17d63 | 190 | } else { |
42a503bd | 191 | /* duplicate entry */ |
192 | delete newNode; | |
193 | return newTop; | |
29b17d63 | 194 | } |
195 | } | |
196 | ||
197 | template<class V> | |
b001e822 | 198 | template<class FindValue> |
29b17d63 | 199 | SplayNode<V> * |
b001e822 | 200 | SplayNode<V>::splay(FindValue const &dataToFind, int( * compare)(FindValue const &a, Value const &b)) const |
29b17d63 | 201 | { |
b001e822 | 202 | Value temp = Value(); |
203 | SplayNode<V> N(temp); | |
29b17d63 | 204 | SplayNode<V> *l; |
205 | SplayNode<V> *r; | |
206 | SplayNode<V> *y; | |
207 | N.left = N.right = NULL; | |
208 | l = r = &N; | |
209 | ||
210 | SplayNode<V> *top = const_cast<SplayNode<V> *>(this); | |
42a503bd | 211 | |
29b17d63 | 212 | for (;;) { |
42a503bd | 213 | splayLastResult = compare(dataToFind, top->data); |
214 | ||
215 | if (splayLastResult < 0) { | |
216 | if (top->left == NULL) | |
217 | break; | |
218 | ||
219 | if ((splayLastResult = compare(dataToFind, top->left->data)) < 0) { | |
f53969cc | 220 | y = top->left; /* rotate right */ |
42a503bd | 221 | top->left = y->right; |
222 | y->right = top; | |
223 | top = y; | |
224 | ||
225 | if (top->left == NULL) | |
226 | break; | |
227 | } | |
228 | ||
f53969cc | 229 | r->left = top; /* link right */ |
42a503bd | 230 | r = top; |
231 | top = top->left; | |
232 | } else if (splayLastResult > 0) { | |
233 | if (top->right == NULL) | |
234 | break; | |
235 | ||
236 | if ((splayLastResult = compare(dataToFind, top->right->data)) > 0) { | |
f53969cc | 237 | y = top->right; /* rotate left */ |
42a503bd | 238 | top->right = y->left; |
239 | y->left = top; | |
240 | top = y; | |
241 | ||
242 | if (top->right == NULL) | |
243 | break; | |
244 | } | |
245 | ||
f53969cc | 246 | l->right = top; /* link left */ |
42a503bd | 247 | l = top; |
248 | top = top->right; | |
249 | } else { | |
250 | break; | |
251 | } | |
29b17d63 | 252 | } |
42a503bd | 253 | |
f53969cc | 254 | l->right = top->left; /* assemble */ |
29b17d63 | 255 | r->left = top->right; |
256 | top->left = N.right; | |
257 | top->right = N.left; | |
258 | return top; | |
259 | } | |
260 | ||
97754f5a AR |
261 | template <class V> |
262 | template <class Visitor> | |
263 | void | |
2da4bfe6 A |
264 | SplayNode<V>::visit(Visitor &visitor) const |
265 | { | |
97754f5a AR |
266 | if (left) |
267 | left->visit(visitor); | |
268 | visitor(data); | |
269 | if (right) | |
270 | right->visit(visitor); | |
271 | } | |
272 | ||
273 | template <class V> | |
274 | template <class Visitor> | |
275 | void | |
2da4bfe6 A |
276 | Splay<V>::visit(Visitor &visitor) const |
277 | { | |
97754f5a AR |
278 | if (head) |
279 | head->visit(visitor); | |
280 | } | |
281 | ||
29b17d63 | 282 | template <class V> |
b001e822 | 283 | template <class FindValue> |
29b17d63 | 284 | typename Splay<V>::Value const * |
b001e822 | 285 | Splay<V>::find (FindValue const &value, int( * compare)(FindValue const &a, Value const &b)) const |
29b17d63 | 286 | { |
e45bad43 FC |
287 | if (head == NULL) |
288 | return NULL; | |
289 | ||
29b17d63 | 290 | head = head->splay(value, compare); |
42a503bd | 291 | |
29b17d63 | 292 | if (splayLastResult != 0) |
42a503bd | 293 | return NULL; |
294 | ||
29b17d63 | 295 | return &head->data; |
296 | } | |
b67e2c8c | 297 | |
0353e724 | 298 | template <class V> |
299 | void | |
300 | Splay<V>::insert(Value const &value, SPLAYCMP *compare) | |
301 | { | |
302 | assert (!find (value, compare)); | |
006b562c FC |
303 | if (head == NULL) |
304 | head = new SplayNode<V>(value); | |
305 | else | |
306 | head = head->insert(value, compare); | |
0353e724 | 307 | ++elements; |
308 | } | |
309 | ||
310 | template <class V> | |
311 | void | |
6ca34f6f | 312 | Splay<V>::remove(Value const &value, SPLAYCMP *compare) |
0353e724 | 313 | { |
314 | assert (find (value, compare)); | |
42a503bd | 315 | |
6ca34f6f | 316 | head = head->remove(value, compare); |
42a503bd | 317 | |
0353e724 | 318 | --elements; |
319 | } | |
320 | ||
4c50505b | 321 | template <class V> |
42a503bd | 322 | SplayNode<V> const * |
323 | Splay<V>:: start() const | |
324 | { | |
325 | if (head) | |
326 | return head->start(); | |
327 | ||
328 | return NULL; | |
329 | } | |
330 | ||
331 | template <class V> | |
332 | SplayNode<V> const * | |
ccd33084 | 333 | Splay<V>:: finish() const |
42a503bd | 334 | { |
4c50505b | 335 | if (head) |
ccd33084 | 336 | return head->finish(); |
42a503bd | 337 | |
4c50505b | 338 | return NULL; |
339 | } | |
340 | ||
42a503bd | 341 | template <class V> |
342 | void | |
343 | Splay<V>:: destroy(SPLAYFREE *free_func) | |
344 | { | |
345 | if (head) | |
346 | head->destroy(free_func); | |
347 | ||
348 | head = NULL; | |
349 | ||
350 | elements = 0; | |
351 | } | |
352 | ||
353 | template <class V> | |
354 | size_t | |
355 | Splay<V>::size() const | |
356 | { | |
357 | return elements; | |
358 | } | |
359 | ||
b8bad68c | 360 | template <class V> |
411c6ea3 | 361 | const SplayConstIterator<V> |
b8bad68c | 362 | Splay<V>::begin() const |
363 | { | |
364 | return const_iterator(head); | |
365 | } | |
366 | ||
367 | template <class V> | |
411c6ea3 | 368 | const SplayConstIterator<V> |
b8bad68c | 369 | Splay<V>::end() const |
370 | { | |
371 | return const_iterator(NULL); | |
372 | } | |
373 | ||
97754f5a | 374 | // XXX: This does not seem to iterate the whole thing in some cases. |
b8bad68c | 375 | template <class V> |
b8bad68c | 376 | class SplayConstIterator |
377 | { | |
378 | ||
379 | public: | |
380 | typedef const V value_type; | |
381 | SplayConstIterator (SplayNode<V> *aNode); | |
382 | bool operator == (SplayConstIterator const &right) const; | |
383 | SplayConstIterator operator ++ (int dummy); | |
384 | SplayConstIterator &operator ++ (); | |
385 | V const & operator * () const; | |
386 | ||
387 | private: | |
388 | void advance(); | |
389 | void addLeftPath(SplayNode<V> *aNode); | |
390 | void init(SplayNode<V> *); | |
cfb88efb | 391 | std::stack<SplayNode<V> *> toVisit; |
b8bad68c | 392 | }; |
393 | ||
394 | template <class V> | |
395 | SplayConstIterator<V>::SplayConstIterator (SplayNode<V> *aNode) | |
396 | { | |
397 | init(aNode); | |
398 | } | |
399 | ||
400 | template <class V> | |
401 | bool | |
402 | SplayConstIterator<V>::operator == (SplayConstIterator const &right) const | |
403 | { | |
cfb88efb AR |
404 | if (toVisit.empty() && right.toVisit.empty()) |
405 | return true; | |
406 | if (!toVisit.empty() && !right.toVisit.empty()) | |
407 | return toVisit.top() == right.toVisit.top(); | |
408 | // only one of the two is empty | |
409 | return false; | |
b8bad68c | 410 | } |
411 | ||
412 | template <class V> | |
413 | SplayConstIterator<V> & | |
414 | SplayConstIterator<V>::operator ++ () | |
415 | { | |
416 | advance(); | |
417 | return *this; | |
418 | } | |
419 | ||
420 | template <class V> | |
421 | SplayConstIterator<V> | |
422 | SplayConstIterator<V>::operator ++ (int dummy) | |
423 | { | |
424 | SplayConstIterator<V> result = *this; | |
425 | advance(); | |
426 | return result; | |
427 | } | |
428 | ||
429 | /* advance is simple enough: | |
430 | * if the stack is empty, we're done. | |
431 | * otherwise, pop the last visited node | |
432 | * then, pop the next node to visit | |
433 | * if that has a right child, add it and it's | |
434 | * left-to-end path. | |
435 | * then add the node back. | |
436 | */ | |
437 | template <class V> | |
438 | void | |
439 | SplayConstIterator<V>::advance() | |
440 | { | |
cfb88efb | 441 | if (toVisit.empty()) |
b8bad68c | 442 | return; |
443 | ||
444 | toVisit.pop(); | |
445 | ||
cfb88efb | 446 | if (toVisit.empty()) |
b8bad68c | 447 | return; |
448 | ||
cfb88efb AR |
449 | // not empty |
450 | SplayNode<V> *currentNode = toVisit.top(); | |
451 | toVisit.pop(); | |
b8bad68c | 452 | |
453 | addLeftPath(currentNode->right); | |
454 | ||
cfb88efb | 455 | toVisit.push(currentNode); |
b8bad68c | 456 | } |
457 | ||
458 | template <class V> | |
459 | void | |
460 | SplayConstIterator<V>::addLeftPath(SplayNode<V> *aNode) | |
461 | { | |
462 | if (aNode == NULL) | |
463 | return; | |
464 | ||
465 | do { | |
cfb88efb | 466 | toVisit.push(aNode); |
b8bad68c | 467 | aNode = aNode->left; |
468 | } while (aNode != NULL); | |
469 | } | |
470 | ||
471 | template <class V> | |
472 | void | |
473 | SplayConstIterator<V>::init(SplayNode<V> *head) | |
474 | { | |
475 | addLeftPath(head); | |
476 | } | |
477 | ||
478 | template <class V> | |
479 | V const & | |
480 | SplayConstIterator<V>::operator * () const | |
481 | { | |
482 | /* can't dereference when past the end */ | |
483 | ||
484 | if (toVisit.size() == 0) | |
485 | fatal ("Attempt to dereference SplayConstIterator past-the-end\n"); | |
486 | ||
487 | return toVisit.top()->data; | |
488 | } | |
489 | ||
b5638623 | 490 | #endif /* SQUID_SPLAY_H */ |
f53969cc | 491 |