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