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