]>
git.ipfire.org Git - thirdparty/squid.git/blob - include/splay.h
4 #if defined(__cplusplus)
14 typedef int SPLAYCMP(Value
const &a
, Value
const &b
);
15 typedef void SPLAYFREE(Value
&);
16 typedef void SPLAYWALKEE(Value
const & nodedata
, void *state
);
17 static void DefaultFree (Value
&aValue
) {delete aValue
;}
19 SplayNode
<V
> (Value
const &);
21 mutable SplayNode
<V
> *left
;
22 mutable SplayNode
<V
> *right
;
23 void destroy(SPLAYFREE
*);
24 void walk(SPLAYWALKEE
*, void *callerState
);
25 bool empty() const { return this == NULL
; }
26 SplayNode
<V
> const * start() const;
27 SplayNode
<V
> const * finish() const;
29 SplayNode
<V
> * remove(const Value data
, SPLAYCMP
* compare
);
31 SplayNode
<V
> * insert(Value data
, SPLAYCMP
* compare
);
33 template <class FindValue
> SplayNode
<V
> * splay(const FindValue
&data
, int( * compare
)(FindValue
const &a
, Value
const &b
)) const;
36 typedef SplayNode
<void *> splayNode
;
39 class SplayConstIterator
;
50 typedef int SPLAYCMP(Value
const &a
, Value
const &b
);
51 typedef void SPLAYFREE(Value
&);
52 typedef SplayIterator
<V
> iterator
;
53 typedef const SplayConstIterator
<V
> const_iterator
;
54 Splay():head(NULL
), elements (0) {}
56 mutable SplayNode
<V
> * head
;
57 template <class FindValue
> Value
const *find (FindValue
const &, int( * compare
)(FindValue
const &a
, Value
const &b
)) const;
58 void insert(Value
const &, SPLAYCMP
*compare
);
60 void remove(Value
const &, SPLAYCMP
*compare
);
62 void destroy(SPLAYFREE
*);
64 SplayNode
<V
> const * start() const;
66 SplayNode
<V
> const * finish() const;
70 const_iterator
begin() const;
72 const_iterator
end() const;
77 SQUIDCEXTERN
int splayLastResult
;
79 SQUIDCEXTERN splayNode
*splay_insert(void *, splayNode
*, splayNode::SPLAYCMP
*);
81 SQUIDCEXTERN splayNode
*splay_delete(const void *, splayNode
*, splayNode::SPLAYCMP
*);
83 SQUIDCEXTERN splayNode
*splay_splay(const void **, splayNode
*, splayNode::SPLAYCMP
*);
85 SQUIDCEXTERN
void splay_destroy(splayNode
*, splayNode::SPLAYFREE
*);
87 SQUIDCEXTERN
void splay_walk(splayNode
*, splayNode::SPLAYWALKEE
*, void *callerState
);
91 SplayNode
<V
>::SplayNode (Value
const &someData
) : data(someData
), left(NULL
), right (NULL
) {}
95 SplayNode
<V
>::walk(SPLAYWALKEE
* walkee
, void *state
)
101 left
->walk(walkee
, state
);
106 right
->walk(walkee
, state
);
111 SplayNode
<V
>::start() const
114 return left
->start();
121 SplayNode
<V
>::finish() const
124 return right
->finish();
131 SplayNode
<V
>::destroy(SPLAYFREE
* free_func
)
137 left
->destroy(free_func
);
140 right
->destroy(free_func
);
149 SplayNode
<V
>::remove(Value
const dataToRemove
, SPLAYCMP
* compare
)
154 SplayNode
<V
> *result
= splay(dataToRemove
, compare
);
156 if (splayLastResult
== 0) { /* found it */
157 SplayNode
<V
> *newTop
;
159 if (result
->left
== NULL
) {
160 newTop
= result
->right
;
162 newTop
= result
->left
->splay(dataToRemove
, compare
);
164 newTop
->right
= result
->right
;
165 result
->right
= NULL
;
172 return result
; /* It wasn't there */
177 SplayNode
<V
>::insert(Value dataToInsert
, SPLAYCMP
* compare
)
179 /* create node to insert */
180 SplayNode
<V
> *newNode
= new SplayNode
<V
>(dataToInsert
);
183 splayLastResult
= -1;
184 newNode
->left
= newNode
->right
= NULL
;
188 SplayNode
<V
> *newTop
= splay(dataToInsert
, compare
);
190 if (splayLastResult
< 0) {
191 newNode
->left
= newTop
->left
;
192 newNode
->right
= newTop
;
195 } else if (splayLastResult
> 0) {
196 newNode
->right
= newTop
->right
;
197 newNode
->left
= newTop
;
198 newTop
->right
= NULL
;
201 /* duplicate entry */
208 template<class FindValue
>
210 SplayNode
<V
>::splay(FindValue
const &dataToFind
, int( * compare
)(FindValue
const &a
, Value
const &b
)) const
213 /* can't have compared successfully :} */
214 splayLastResult
= -1;
218 Value temp
= Value();
219 SplayNode
<V
> N(temp
);
223 N
.left
= N
.right
= NULL
;
226 SplayNode
<V
> *top
= const_cast<SplayNode
<V
> *>(this);
229 splayLastResult
= compare(dataToFind
, top
->data
);
231 if (splayLastResult
< 0) {
232 if (top
->left
== NULL
)
235 if ((splayLastResult
= compare(dataToFind
, top
->left
->data
)) < 0) {
236 y
= top
->left
; /* rotate right */
237 top
->left
= y
->right
;
241 if (top
->left
== NULL
)
245 r
->left
= top
; /* link right */
248 } else if (splayLastResult
> 0) {
249 if (top
->right
== NULL
)
252 if ((splayLastResult
= compare(dataToFind
, top
->right
->data
)) > 0) {
253 y
= top
->right
; /* rotate left */
254 top
->right
= y
->left
;
258 if (top
->right
== NULL
)
262 l
->right
= top
; /* link left */
270 l
->right
= top
->left
; /* assemble */
271 r
->left
= top
->right
;
278 template <class FindValue
>
279 typename Splay
<V
>::Value
const *
280 Splay
<V
>::find (FindValue
const &value
, int( * compare
)(FindValue
const &a
, Value
const &b
)) const
282 head
= head
->splay(value
, compare
);
284 if (splayLastResult
!= 0)
292 Splay
<V
>::insert(Value
const &value
, SPLAYCMP
*compare
)
294 assert (!find (value
, compare
));
295 head
= head
->insert(value
, compare
);
301 Splay
<V
>::remove(Value
const &value
, SPLAYCMP
*compare
)
303 assert (find (value
, compare
));
305 head
= head
->remove(value
, compare
);
312 Splay
<V
>:: start() const
315 return head
->start();
322 Splay
<V
>:: finish() const
325 return head
->finish();
332 Splay
<V
>:: destroy(SPLAYFREE
*free_func
)
335 head
->destroy(free_func
);
344 Splay
<V
>::size() const
350 const SplayConstIterator
<V
>
351 Splay
<V
>::begin() const
353 return const_iterator(head
);
357 const SplayConstIterator
<V
>
358 Splay
<V
>::end() const
360 return const_iterator(NULL
);
364 class SplayConstIterator
368 typedef const V value_type
;
369 SplayConstIterator (SplayNode
<V
> *aNode
);
370 bool operator == (SplayConstIterator
const &right
) const;
371 SplayConstIterator
operator ++ (int dummy
);
372 SplayConstIterator
&operator ++ ();
373 V
const & operator * () const;
377 void addLeftPath(SplayNode
<V
> *aNode
);
378 void init(SplayNode
<V
> *);
379 Stack
<SplayNode
<V
> *> toVisit
;
383 SplayConstIterator
<V
>::SplayConstIterator (SplayNode
<V
> *aNode
)
390 SplayConstIterator
<V
>::operator == (SplayConstIterator
const &right
) const
392 return toVisit
.top() == right
.toVisit
.top();
396 SplayConstIterator
<V
> &
397 SplayConstIterator
<V
>::operator ++ ()
404 SplayConstIterator
<V
>
405 SplayConstIterator
<V
>::operator ++ (int dummy
)
407 SplayConstIterator
<V
> result
= *this;
412 /* advance is simple enough:
413 * if the stack is empty, we're done.
414 * otherwise, pop the last visited node
415 * then, pop the next node to visit
416 * if that has a right child, add it and it's
418 * then add the node back.
422 SplayConstIterator
<V
>::advance()
424 if (toVisit
.size() == 0)
429 if (toVisit
.size() == 0)
432 SplayNode
<V
> *currentNode
= toVisit
.pop();
434 addLeftPath(currentNode
->right
);
436 toVisit
.push_back(currentNode
);
441 SplayConstIterator
<V
>::addLeftPath(SplayNode
<V
> *aNode
)
447 toVisit
.push_back(aNode
);
449 } while (aNode
!= NULL
);
454 SplayConstIterator
<V
>::init(SplayNode
<V
> *head
)
461 SplayConstIterator
<V
>::operator * () const
463 /* can't dereference when past the end */
465 if (toVisit
.size() == 0)
466 fatal ("Attempt to dereference SplayConstIterator past-the-end\n");
468 return toVisit
.top()->data
;
471 #endif /* cplusplus */
473 #endif /* SQUID_SPLAY_H */