]>
git.ipfire.org Git - thirdparty/squid.git/blob - include/splay.h
19 typedef int SPLAYCMP(Value
const &a
, Value
const &b
);
20 typedef void SPLAYFREE(Value
&);
21 typedef void SPLAYWALKEE(Value
const & nodedata
, void *state
);
22 static void DefaultFree (Value
&aValue
) {delete aValue
;}
24 SplayNode
<V
> (Value
const &);
26 mutable SplayNode
<V
> *left
;
27 mutable SplayNode
<V
> *right
;
28 void destroy(SPLAYFREE
*);
29 void walk(SPLAYWALKEE
*, void *callerState
);
30 bool empty() const { return this == NULL
; }
31 SplayNode
<V
> const * start() const;
32 SplayNode
<V
> const * finish() const;
34 SplayNode
<V
> * remove(const Value data
, SPLAYCMP
* compare
);
36 SplayNode
<V
> * insert(Value data
, SPLAYCMP
* compare
);
38 template <class FindValue
> SplayNode
<V
> * splay(const FindValue
&data
, int( * compare
)(FindValue
const &a
, Value
const &b
)) const;
41 typedef SplayNode
<void *> splayNode
;
44 class SplayConstIterator
;
55 typedef int SPLAYCMP(Value
const &a
, Value
const &b
);
56 typedef void SPLAYFREE(Value
&);
57 typedef SplayIterator
<V
> iterator
;
58 typedef const SplayConstIterator
<V
> const_iterator
;
59 Splay():head(NULL
), elements (0) {}
61 mutable SplayNode
<V
> * head
;
62 template <class FindValue
> Value
const *find (FindValue
const &, int( * compare
)(FindValue
const &a
, Value
const &b
)) const;
63 void insert(Value
const &, SPLAYCMP
*compare
);
65 void remove(Value
const &, SPLAYCMP
*compare
);
67 void destroy(SPLAYFREE
*);
69 SplayNode
<V
> const * start() const;
71 SplayNode
<V
> const * finish() const;
75 const_iterator
begin() const;
77 const_iterator
end() const;
82 SQUIDCEXTERN
int splayLastResult
;
84 SQUIDCEXTERN splayNode
*splay_insert(void *, splayNode
*, splayNode::SPLAYCMP
*);
86 SQUIDCEXTERN splayNode
*splay_delete(const void *, splayNode
*, splayNode::SPLAYCMP
*);
88 SQUIDCEXTERN splayNode
*splay_splay(const void **, splayNode
*, splayNode::SPLAYCMP
*);
90 SQUIDCEXTERN
void splay_destroy(splayNode
*, splayNode::SPLAYFREE
*);
92 SQUIDCEXTERN
void splay_walk(splayNode
*, splayNode::SPLAYWALKEE
*, void *callerState
);
96 SplayNode
<V
>::SplayNode (Value
const &someData
) : data(someData
), left(NULL
), right (NULL
) {}
100 SplayNode
<V
>::walk(SPLAYWALKEE
* walkee
, void *state
)
106 left
->walk(walkee
, state
);
111 right
->walk(walkee
, state
);
116 SplayNode
<V
>::start() const
119 return left
->start();
126 SplayNode
<V
>::finish() const
129 return right
->finish();
136 SplayNode
<V
>::destroy(SPLAYFREE
* free_func
)
142 left
->destroy(free_func
);
145 right
->destroy(free_func
);
154 SplayNode
<V
>::remove(Value
const dataToRemove
, SPLAYCMP
* compare
)
159 SplayNode
<V
> *result
= splay(dataToRemove
, compare
);
161 if (splayLastResult
== 0) { /* found it */
162 SplayNode
<V
> *newTop
;
164 if (result
->left
== NULL
) {
165 newTop
= result
->right
;
167 newTop
= result
->left
->splay(dataToRemove
, compare
);
169 newTop
->right
= result
->right
;
170 result
->right
= NULL
;
177 return result
; /* It wasn't there */
182 SplayNode
<V
>::insert(Value dataToInsert
, SPLAYCMP
* compare
)
184 /* create node to insert */
185 SplayNode
<V
> *newNode
= new SplayNode
<V
>(dataToInsert
);
188 splayLastResult
= -1;
189 newNode
->left
= newNode
->right
= NULL
;
193 SplayNode
<V
> *newTop
= splay(dataToInsert
, compare
);
195 if (splayLastResult
< 0) {
196 newNode
->left
= newTop
->left
;
197 newNode
->right
= newTop
;
200 } else if (splayLastResult
> 0) {
201 newNode
->right
= newTop
->right
;
202 newNode
->left
= newTop
;
203 newTop
->right
= NULL
;
206 /* duplicate entry */
213 template<class FindValue
>
215 SplayNode
<V
>::splay(FindValue
const &dataToFind
, int( * compare
)(FindValue
const &a
, Value
const &b
)) const
218 /* can't have compared successfully :} */
219 splayLastResult
= -1;
223 Value temp
= Value();
224 SplayNode
<V
> N(temp
);
228 N
.left
= N
.right
= NULL
;
231 SplayNode
<V
> *top
= const_cast<SplayNode
<V
> *>(this);
234 splayLastResult
= compare(dataToFind
, top
->data
);
236 if (splayLastResult
< 0) {
237 if (top
->left
== NULL
)
240 if ((splayLastResult
= compare(dataToFind
, top
->left
->data
)) < 0) {
241 y
= top
->left
; /* rotate right */
242 top
->left
= y
->right
;
246 if (top
->left
== NULL
)
250 r
->left
= top
; /* link right */
253 } else if (splayLastResult
> 0) {
254 if (top
->right
== NULL
)
257 if ((splayLastResult
= compare(dataToFind
, top
->right
->data
)) > 0) {
258 y
= top
->right
; /* rotate left */
259 top
->right
= y
->left
;
263 if (top
->right
== NULL
)
267 l
->right
= top
; /* link left */
275 l
->right
= top
->left
; /* assemble */
276 r
->left
= top
->right
;
283 template <class FindValue
>
284 typename Splay
<V
>::Value
const *
285 Splay
<V
>::find (FindValue
const &value
, int( * compare
)(FindValue
const &a
, Value
const &b
)) const
287 head
= head
->splay(value
, compare
);
289 if (splayLastResult
!= 0)
297 Splay
<V
>::insert(Value
const &value
, SPLAYCMP
*compare
)
299 assert (!find (value
, compare
));
300 head
= head
->insert(value
, compare
);
306 Splay
<V
>::remove(Value
const &value
, SPLAYCMP
*compare
)
308 assert (find (value
, compare
));
310 head
= head
->remove(value
, compare
);
317 Splay
<V
>:: start() const
320 return head
->start();
327 Splay
<V
>:: finish() const
330 return head
->finish();
337 Splay
<V
>:: destroy(SPLAYFREE
*free_func
)
340 head
->destroy(free_func
);
349 Splay
<V
>::size() const
355 const SplayConstIterator
<V
>
356 Splay
<V
>::begin() const
358 return const_iterator(head
);
362 const SplayConstIterator
<V
>
363 Splay
<V
>::end() const
365 return const_iterator(NULL
);
369 class SplayConstIterator
373 typedef const V value_type
;
374 SplayConstIterator (SplayNode
<V
> *aNode
);
375 bool operator == (SplayConstIterator
const &right
) const;
376 SplayConstIterator
operator ++ (int dummy
);
377 SplayConstIterator
&operator ++ ();
378 V
const & operator * () const;
382 void addLeftPath(SplayNode
<V
> *aNode
);
383 void init(SplayNode
<V
> *);
384 Stack
<SplayNode
<V
> *> toVisit
;
388 SplayConstIterator
<V
>::SplayConstIterator (SplayNode
<V
> *aNode
)
395 SplayConstIterator
<V
>::operator == (SplayConstIterator
const &right
) const
397 return toVisit
.top() == right
.toVisit
.top();
401 SplayConstIterator
<V
> &
402 SplayConstIterator
<V
>::operator ++ ()
409 SplayConstIterator
<V
>
410 SplayConstIterator
<V
>::operator ++ (int dummy
)
412 SplayConstIterator
<V
> result
= *this;
417 /* advance is simple enough:
418 * if the stack is empty, we're done.
419 * otherwise, pop the last visited node
420 * then, pop the next node to visit
421 * if that has a right child, add it and it's
423 * then add the node back.
427 SplayConstIterator
<V
>::advance()
429 if (toVisit
.size() == 0)
434 if (toVisit
.size() == 0)
437 SplayNode
<V
> *currentNode
= toVisit
.pop();
439 addLeftPath(currentNode
->right
);
441 toVisit
.push_back(currentNode
);
446 SplayConstIterator
<V
>::addLeftPath(SplayNode
<V
> *aNode
)
452 toVisit
.push_back(aNode
);
454 } while (aNode
!= NULL
);
459 SplayConstIterator
<V
>::init(SplayNode
<V
> *head
)
466 SplayConstIterator
<V
>::operator * () const
468 /* can't dereference when past the end */
470 if (toVisit
.size() == 0)
471 fatal ("Attempt to dereference SplayConstIterator past-the-end\n");
473 return toVisit
.top()->data
;
476 #endif /* cplusplus */
478 #endif /* SQUID_SPLAY_H */