]> git.ipfire.org Git - thirdparty/squid.git/blame - include/splay.h
SSL_OP_NO_TICKET SSL option to http[s]_port
[thirdparty/squid.git] / include / splay.h
CommitLineData
5c193dec 1/*
bde978a6 2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
5c193dec
AJ
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{
8456681e
AJ
302 if (find(value, compare) != NULL) // ignore duplicates
303 return;
304
006b562c
FC
305 if (head == NULL)
306 head = new SplayNode<V>(value);
307 else
308 head = head->insert(value, compare);
0353e724 309 ++elements;
310}
311
312template <class V>
313void
6ca34f6f 314Splay<V>::remove(Value const &value, SPLAYCMP *compare)
0353e724 315{
316 assert (find (value, compare));
42a503bd 317
6ca34f6f 318 head = head->remove(value, compare);
42a503bd 319
0353e724 320 --elements;
321}
322
4c50505b 323template <class V>
42a503bd 324SplayNode<V> const *
325Splay<V>:: start() const
326{
327 if (head)
328 return head->start();
329
330 return NULL;
331}
332
333template <class V>
334SplayNode<V> const *
ccd33084 335Splay<V>:: finish() const
42a503bd 336{
4c50505b 337 if (head)
ccd33084 338 return head->finish();
42a503bd 339
4c50505b 340 return NULL;
341}
342
42a503bd 343template <class V>
344void
345Splay<V>:: destroy(SPLAYFREE *free_func)
346{
347 if (head)
348 head->destroy(free_func);
349
350 head = NULL;
351
352 elements = 0;
353}
354
355template <class V>
356size_t
357Splay<V>::size() const
358{
359 return elements;
360}
361
b8bad68c 362template <class V>
411c6ea3 363const SplayConstIterator<V>
b8bad68c 364Splay<V>::begin() const
365{
366 return const_iterator(head);
367}
368
369template <class V>
411c6ea3 370const SplayConstIterator<V>
b8bad68c 371Splay<V>::end() const
372{
373 return const_iterator(NULL);
374}
375
97754f5a 376// XXX: This does not seem to iterate the whole thing in some cases.
b8bad68c 377template <class V>
b8bad68c 378class SplayConstIterator
379{
380
381public:
382 typedef const V value_type;
383 SplayConstIterator (SplayNode<V> *aNode);
384 bool operator == (SplayConstIterator const &right) const;
385 SplayConstIterator operator ++ (int dummy);
386 SplayConstIterator &operator ++ ();
387 V const & operator * () const;
388
389private:
390 void advance();
391 void addLeftPath(SplayNode<V> *aNode);
392 void init(SplayNode<V> *);
cfb88efb 393 std::stack<SplayNode<V> *> toVisit;
b8bad68c 394};
395
396template <class V>
397SplayConstIterator<V>::SplayConstIterator (SplayNode<V> *aNode)
398{
399 init(aNode);
400}
401
402template <class V>
403bool
404SplayConstIterator<V>::operator == (SplayConstIterator const &right) const
405{
cfb88efb
AR
406 if (toVisit.empty() && right.toVisit.empty())
407 return true;
408 if (!toVisit.empty() && !right.toVisit.empty())
409 return toVisit.top() == right.toVisit.top();
410 // only one of the two is empty
411 return false;
b8bad68c 412}
413
414template <class V>
415SplayConstIterator<V> &
416SplayConstIterator<V>::operator ++ ()
417{
418 advance();
419 return *this;
420}
421
422template <class V>
423SplayConstIterator<V>
424SplayConstIterator<V>::operator ++ (int dummy)
425{
426 SplayConstIterator<V> result = *this;
427 advance();
428 return result;
429}
430
431/* advance is simple enough:
432* if the stack is empty, we're done.
433* otherwise, pop the last visited node
434* then, pop the next node to visit
435* if that has a right child, add it and it's
436* left-to-end path.
437* then add the node back.
438*/
439template <class V>
440void
441SplayConstIterator<V>::advance()
442{
cfb88efb 443 if (toVisit.empty())
b8bad68c 444 return;
445
446 toVisit.pop();
447
cfb88efb 448 if (toVisit.empty())
b8bad68c 449 return;
450
cfb88efb
AR
451 // not empty
452 SplayNode<V> *currentNode = toVisit.top();
453 toVisit.pop();
b8bad68c 454
455 addLeftPath(currentNode->right);
456
cfb88efb 457 toVisit.push(currentNode);
b8bad68c 458}
459
460template <class V>
461void
462SplayConstIterator<V>::addLeftPath(SplayNode<V> *aNode)
463{
464 if (aNode == NULL)
465 return;
466
467 do {
cfb88efb 468 toVisit.push(aNode);
b8bad68c 469 aNode = aNode->left;
470 } while (aNode != NULL);
471}
472
473template <class V>
474void
475SplayConstIterator<V>::init(SplayNode<V> *head)
476{
477 addLeftPath(head);
478}
479
480template <class V>
481V const &
482SplayConstIterator<V>::operator * () const
483{
484 /* can't dereference when past the end */
485
486 if (toVisit.size() == 0)
487 fatal ("Attempt to dereference SplayConstIterator past-the-end\n");
488
489 return toVisit.top()->data;
490}
491
b5638623 492#endif /* SQUID_SPLAY_H */
f53969cc 493