]>
git.ipfire.org Git - thirdparty/squid.git/blob - include/splay.h
4 #if defined(__cplusplus)
16 typedef int SPLAYCMP(Value
const &a
, Value
const &b
);
17 typedef void SPLAYFREE(Value
&);
18 typedef void SPLAYWALKEE(Value
const & nodedata
, void *state
);
19 static void DefaultFree (Value
&aValue
) {delete aValue
;}
21 SplayNode
<V
> (Value
const &);
23 mutable SplayNode
<V
> *left
;
24 mutable SplayNode
<V
> *right
;
25 void destroy(SPLAYFREE
*);
26 void walk(SPLAYWALKEE
*, void *callerState
);
27 bool empty() const { return this == NULL
; }
28 SplayNode
<V
> const * start() const;
29 SplayNode
<V
> const * finish() const;
31 SplayNode
<V
> * remove(const Value data
, SPLAYCMP
* compare
);
33 SplayNode
<V
> * insert(Value data
, SPLAYCMP
* compare
);
35 template <class FindValue
> SplayNode
<V
> * splay(const FindValue
&data
, int( * compare
)(FindValue
const &a
, Value
const &b
)) const;
37 /// recursively visit left nodes, this node, and then right nodes
38 template <class Visitor
> void visit(Visitor
&v
) 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;
79 /// recursively visit all nodes, in left-to-right order
80 template <class Visitor
> void visit(Visitor
&v
) const;
85 SQUIDCEXTERN
int splayLastResult
;
87 SQUIDCEXTERN splayNode
*splay_insert(void *, splayNode
*, splayNode::SPLAYCMP
*);
89 SQUIDCEXTERN splayNode
*splay_delete(const void *, splayNode
*, splayNode::SPLAYCMP
*);
91 SQUIDCEXTERN splayNode
*splay_splay(const void **, splayNode
*, splayNode::SPLAYCMP
*);
93 SQUIDCEXTERN
void splay_destroy(splayNode
*, splayNode::SPLAYFREE
*);
95 SQUIDCEXTERN
void splay_walk(splayNode
*, splayNode::SPLAYWALKEE
*, void *callerState
);
99 SplayNode
<V
>::SplayNode (Value
const &someData
) : data(someData
), left(NULL
), right (NULL
) {}
103 SplayNode
<V
>::walk(SPLAYWALKEE
* walkee
, void *state
)
109 left
->walk(walkee
, state
);
114 right
->walk(walkee
, state
);
119 SplayNode
<V
>::start() const
122 return left
->start();
129 SplayNode
<V
>::finish() const
132 return right
->finish();
139 SplayNode
<V
>::destroy(SPLAYFREE
* free_func
)
145 left
->destroy(free_func
);
148 right
->destroy(free_func
);
157 SplayNode
<V
>::remove(Value
const dataToRemove
, SPLAYCMP
* compare
)
162 SplayNode
<V
> *result
= splay(dataToRemove
, compare
);
164 if (splayLastResult
== 0) { /* found it */
165 SplayNode
<V
> *newTop
;
167 if (result
->left
== NULL
) {
168 newTop
= result
->right
;
170 newTop
= result
->left
->splay(dataToRemove
, compare
);
172 newTop
->right
= result
->right
;
173 result
->right
= NULL
;
180 return result
; /* It wasn't there */
185 SplayNode
<V
>::insert(Value dataToInsert
, SPLAYCMP
* compare
)
187 /* create node to insert */
188 SplayNode
<V
> *newNode
= new SplayNode
<V
>(dataToInsert
);
191 splayLastResult
= -1;
192 newNode
->left
= newNode
->right
= NULL
;
196 SplayNode
<V
> *newTop
= splay(dataToInsert
, compare
);
198 if (splayLastResult
< 0) {
199 newNode
->left
= newTop
->left
;
200 newNode
->right
= newTop
;
203 } else if (splayLastResult
> 0) {
204 newNode
->right
= newTop
->right
;
205 newNode
->left
= newTop
;
206 newTop
->right
= NULL
;
209 /* duplicate entry */
216 template<class FindValue
>
218 SplayNode
<V
>::splay(FindValue
const &dataToFind
, int( * compare
)(FindValue
const &a
, Value
const &b
)) const
221 /* can't have compared successfully :} */
222 splayLastResult
= -1;
226 Value temp
= Value();
227 SplayNode
<V
> N(temp
);
231 N
.left
= N
.right
= NULL
;
234 SplayNode
<V
> *top
= const_cast<SplayNode
<V
> *>(this);
237 splayLastResult
= compare(dataToFind
, top
->data
);
239 if (splayLastResult
< 0) {
240 if (top
->left
== NULL
)
243 if ((splayLastResult
= compare(dataToFind
, top
->left
->data
)) < 0) {
244 y
= top
->left
; /* rotate right */
245 top
->left
= y
->right
;
249 if (top
->left
== NULL
)
253 r
->left
= top
; /* link right */
256 } else if (splayLastResult
> 0) {
257 if (top
->right
== NULL
)
260 if ((splayLastResult
= compare(dataToFind
, top
->right
->data
)) > 0) {
261 y
= top
->right
; /* rotate left */
262 top
->right
= y
->left
;
266 if (top
->right
== NULL
)
270 l
->right
= top
; /* link left */
278 l
->right
= top
->left
; /* assemble */
279 r
->left
= top
->right
;
286 template <class Visitor
>
288 SplayNode
<V
>::visit(Visitor
&visitor
) const {
290 left
->visit(visitor
);
293 right
->visit(visitor
);
297 template <class Visitor
>
299 Splay
<V
>::visit(Visitor
&visitor
) const {
301 head
->visit(visitor
);
305 template <class FindValue
>
306 typename Splay
<V
>::Value
const *
307 Splay
<V
>::find (FindValue
const &value
, int( * compare
)(FindValue
const &a
, Value
const &b
)) const
309 head
= head
->splay(value
, compare
);
311 if (splayLastResult
!= 0)
319 Splay
<V
>::insert(Value
const &value
, SPLAYCMP
*compare
)
321 assert (!find (value
, compare
));
322 head
= head
->insert(value
, compare
);
328 Splay
<V
>::remove(Value
const &value
, SPLAYCMP
*compare
)
330 assert (find (value
, compare
));
332 head
= head
->remove(value
, compare
);
339 Splay
<V
>:: start() const
342 return head
->start();
349 Splay
<V
>:: finish() const
352 return head
->finish();
359 Splay
<V
>:: destroy(SPLAYFREE
*free_func
)
362 head
->destroy(free_func
);
371 Splay
<V
>::size() const
377 const SplayConstIterator
<V
>
378 Splay
<V
>::begin() const
380 return const_iterator(head
);
384 const SplayConstIterator
<V
>
385 Splay
<V
>::end() const
387 return const_iterator(NULL
);
390 // XXX: This does not seem to iterate the whole thing in some cases.
392 class SplayConstIterator
396 typedef const V value_type
;
397 SplayConstIterator (SplayNode
<V
> *aNode
);
398 bool operator == (SplayConstIterator
const &right
) const;
399 SplayConstIterator
operator ++ (int dummy
);
400 SplayConstIterator
&operator ++ ();
401 V
const & operator * () const;
405 void addLeftPath(SplayNode
<V
> *aNode
);
406 void init(SplayNode
<V
> *);
407 std::stack
<SplayNode
<V
> *> toVisit
;
411 SplayConstIterator
<V
>::SplayConstIterator (SplayNode
<V
> *aNode
)
418 SplayConstIterator
<V
>::operator == (SplayConstIterator
const &right
) const
420 if (toVisit
.empty() && right
.toVisit
.empty())
422 if (!toVisit
.empty() && !right
.toVisit
.empty())
423 return toVisit
.top() == right
.toVisit
.top();
424 // only one of the two is empty
429 SplayConstIterator
<V
> &
430 SplayConstIterator
<V
>::operator ++ ()
437 SplayConstIterator
<V
>
438 SplayConstIterator
<V
>::operator ++ (int dummy
)
440 SplayConstIterator
<V
> result
= *this;
445 /* advance is simple enough:
446 * if the stack is empty, we're done.
447 * otherwise, pop the last visited node
448 * then, pop the next node to visit
449 * if that has a right child, add it and it's
451 * then add the node back.
455 SplayConstIterator
<V
>::advance()
466 SplayNode
<V
> *currentNode
= toVisit
.top();
469 addLeftPath(currentNode
->right
);
471 toVisit
.push(currentNode
);
476 SplayConstIterator
<V
>::addLeftPath(SplayNode
<V
> *aNode
)
484 } while (aNode
!= NULL
);
489 SplayConstIterator
<V
>::init(SplayNode
<V
> *head
)
496 SplayConstIterator
<V
>::operator * () const
498 /* can't dereference when past the end */
500 if (toVisit
.size() == 0)
501 fatal ("Attempt to dereference SplayConstIterator past-the-end\n");
503 return toVisit
.top()->data
;
506 #endif /* cplusplus */
508 #endif /* SQUID_SPLAY_H */