]> git.ipfire.org Git - thirdparty/squid.git/blame - include/splay.h
SourceFormat Enforcement
[thirdparty/squid.git] / include / splay.h
CommitLineData
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 16template <class V>
42a503bd 17class SplayNode
18{
42a503bd 19public:
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 47typedef SplayNode<void *> splayNode;
48
49template <class V>
b8bad68c 50class SplayConstIterator;
51
52template <class V>
ccd33084 53class SplayIterator;
54
55template <class V>
42a503bd 56class Splay
57{
42a503bd 58public:
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
f5dc4237
FC
89private:
90 mutable SplayNode<V> * head;
e9505c9b 91 size_t elements;
29b17d63 92};
b67e2c8c 93
b67e2c8c 94SQUIDCEXTERN int splayLastResult;
95
29b17d63 96template<class V>
2bcafc95 97SplayNode<V>::SplayNode (Value const &someData) : data(someData), left(NULL), right (NULL) {}
98
87c692d7 99template<class V>
29b17d63 100void
101SplayNode<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 112template<class V>
113SplayNode<V> const *
114SplayNode<V>::start() const
115{
e45bad43 116 if (left)
42a503bd 117 return left->start();
118
119 return this;
120}
121
122template<class V>
123SplayNode<V> const *
ccd33084 124SplayNode<V>::finish() const
42a503bd 125{
e45bad43 126 if (right)
ccd33084 127 return right->finish();
42a503bd 128
4c50505b 129 return this;
130}
131
29b17d63 132template<class V>
133void
134SplayNode<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
147template<class V>
148SplayNode<V> *
6ca34f6f 149SplayNode<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 172template<class V>
173SplayNode<V> *
626e1f97 174SplayNode<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
197template<class V>
b001e822 198template<class FindValue>
29b17d63 199SplayNode<V> *
b001e822 200SplayNode<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
261template <class V>
262template <class Visitor>
263void
2da4bfe6
A
264SplayNode<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
273template <class V>
274template <class Visitor>
275void
2da4bfe6
A
276Splay<V>::visit(Visitor &visitor) const
277{
97754f5a
AR
278 if (head)
279 head->visit(visitor);
280}
281
29b17d63 282template <class V>
b001e822 283template <class FindValue>
29b17d63 284typename Splay<V>::Value const *
b001e822 285Splay<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 298template <class V>
299void
300Splay<V>::insert(Value const &value, SPLAYCMP *compare)
301{
5bc2be30 302 assert (find (value, compare) == NULL);
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
310template <class V>
311void
6ca34f6f 312Splay<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 321template <class V>
42a503bd 322SplayNode<V> const *
323Splay<V>:: start() const
324{
325 if (head)
326 return head->start();
327
328 return NULL;
329}
330
331template <class V>
332SplayNode<V> const *
ccd33084 333Splay<V>:: finish() const
42a503bd 334{
4c50505b 335 if (head)
ccd33084 336 return head->finish();
42a503bd 337
4c50505b 338 return NULL;
339}
340
42a503bd 341template <class V>
342void
343Splay<V>:: destroy(SPLAYFREE *free_func)
344{
345 if (head)
346 head->destroy(free_func);
347
348 head = NULL;
349
350 elements = 0;
351}
352
353template <class V>
354size_t
355Splay<V>::size() const
356{
357 return elements;
358}
359
b8bad68c 360template <class V>
411c6ea3 361const SplayConstIterator<V>
b8bad68c 362Splay<V>::begin() const
363{
364 return const_iterator(head);
365}
366
367template <class V>
411c6ea3 368const SplayConstIterator<V>
b8bad68c 369Splay<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 375template <class V>
b8bad68c 376class SplayConstIterator
377{
378
379public:
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
387private:
388 void advance();
389 void addLeftPath(SplayNode<V> *aNode);
390 void init(SplayNode<V> *);
cfb88efb 391 std::stack<SplayNode<V> *> toVisit;
b8bad68c 392};
393
394template <class V>
395SplayConstIterator<V>::SplayConstIterator (SplayNode<V> *aNode)
396{
397 init(aNode);
398}
399
400template <class V>
401bool
402SplayConstIterator<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
412template <class V>
413SplayConstIterator<V> &
414SplayConstIterator<V>::operator ++ ()
415{
416 advance();
417 return *this;
418}
419
420template <class V>
421SplayConstIterator<V>
422SplayConstIterator<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*/
437template <class V>
438void
439SplayConstIterator<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
458template <class V>
459void
460SplayConstIterator<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
471template <class V>
472void
473SplayConstIterator<V>::init(SplayNode<V> *head)
474{
475 addLeftPath(head);
476}
477
478template <class V>
479V const &
480SplayConstIterator<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