]>
git.ipfire.org Git - thirdparty/squid.git/blob - include/splay.h
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
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.
12 #if defined(__cplusplus)
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
;}
29 SplayNode
<V
> (Value
const &);
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;
39 SplayNode
<V
> * remove(const Value data
, SPLAYCMP
* compare
);
41 SplayNode
<V
> * insert(Value data
, SPLAYCMP
* compare
);
43 template <class FindValue
> SplayNode
<V
> * splay(const FindValue
&data
, int( * compare
)(FindValue
const &a
, Value
const &b
)) const;
45 /// recursively visit left nodes, this node, and then right nodes
46 template <class Visitor
> void visit(Visitor
&v
) const;
49 typedef SplayNode
<void *> splayNode
;
52 class SplayConstIterator
;
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) {}
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
);
73 void remove(Value
const &, SPLAYCMP
*compare
);
75 void destroy(SPLAYFREE
*);
77 SplayNode
<V
> const * start() const;
79 SplayNode
<V
> const * finish() const;
83 const_iterator
begin() const;
85 const_iterator
end() const;
87 /// recursively visit all nodes, in left-to-right order
88 template <class Visitor
> void visit(Visitor
&v
) const;
93 SQUIDCEXTERN
int splayLastResult
;
95 SQUIDCEXTERN splayNode
*splay_insert(void *, splayNode
*, splayNode::SPLAYCMP
*);
97 SQUIDCEXTERN splayNode
*splay_delete(const void *, splayNode
*, splayNode::SPLAYCMP
*);
99 SQUIDCEXTERN splayNode
*splay_splay(const void **, splayNode
*, splayNode::SPLAYCMP
*);
101 SQUIDCEXTERN
void splay_destroy(splayNode
*, splayNode::SPLAYFREE
*);
103 SQUIDCEXTERN
void splay_walk(splayNode
*, splayNode::SPLAYWALKEE
*, void *callerState
);
107 SplayNode
<V
>::SplayNode (Value
const &someData
) : data(someData
), left(NULL
), right (NULL
) {}
111 SplayNode
<V
>::walk(SPLAYWALKEE
* walkee
, void *state
)
117 left
->walk(walkee
, state
);
122 right
->walk(walkee
, state
);
127 SplayNode
<V
>::start() const
130 return left
->start();
137 SplayNode
<V
>::finish() const
140 return right
->finish();
147 SplayNode
<V
>::destroy(SPLAYFREE
* free_func
)
153 left
->destroy(free_func
);
156 right
->destroy(free_func
);
165 SplayNode
<V
>::remove(Value
const dataToRemove
, SPLAYCMP
* compare
)
170 SplayNode
<V
> *result
= splay(dataToRemove
, compare
);
172 if (splayLastResult
== 0) { /* found it */
173 SplayNode
<V
> *newTop
;
175 if (result
->left
== NULL
) {
176 newTop
= result
->right
;
178 newTop
= result
->left
->splay(dataToRemove
, compare
);
180 newTop
->right
= result
->right
;
181 result
->right
= NULL
;
188 return result
; /* It wasn't there */
193 SplayNode
<V
>::insert(Value dataToInsert
, SPLAYCMP
* compare
)
195 /* create node to insert */
196 SplayNode
<V
> *newNode
= new SplayNode
<V
>(dataToInsert
);
199 splayLastResult
= -1;
200 newNode
->left
= newNode
->right
= NULL
;
204 SplayNode
<V
> *newTop
= splay(dataToInsert
, compare
);
206 if (splayLastResult
< 0) {
207 newNode
->left
= newTop
->left
;
208 newNode
->right
= newTop
;
211 } else if (splayLastResult
> 0) {
212 newNode
->right
= newTop
->right
;
213 newNode
->left
= newTop
;
214 newTop
->right
= NULL
;
217 /* duplicate entry */
224 template<class FindValue
>
226 SplayNode
<V
>::splay(FindValue
const &dataToFind
, int( * compare
)(FindValue
const &a
, Value
const &b
)) const
229 /* can't have compared successfully :} */
230 splayLastResult
= -1;
234 Value temp
= Value();
235 SplayNode
<V
> N(temp
);
239 N
.left
= N
.right
= NULL
;
242 SplayNode
<V
> *top
= const_cast<SplayNode
<V
> *>(this);
245 splayLastResult
= compare(dataToFind
, top
->data
);
247 if (splayLastResult
< 0) {
248 if (top
->left
== NULL
)
251 if ((splayLastResult
= compare(dataToFind
, top
->left
->data
)) < 0) {
252 y
= top
->left
; /* rotate right */
253 top
->left
= y
->right
;
257 if (top
->left
== NULL
)
261 r
->left
= top
; /* link right */
264 } else if (splayLastResult
> 0) {
265 if (top
->right
== NULL
)
268 if ((splayLastResult
= compare(dataToFind
, top
->right
->data
)) > 0) {
269 y
= top
->right
; /* rotate left */
270 top
->right
= y
->left
;
274 if (top
->right
== NULL
)
278 l
->right
= top
; /* link left */
286 l
->right
= top
->left
; /* assemble */
287 r
->left
= top
->right
;
294 template <class Visitor
>
296 SplayNode
<V
>::visit(Visitor
&visitor
) const
299 left
->visit(visitor
);
302 right
->visit(visitor
);
306 template <class Visitor
>
308 Splay
<V
>::visit(Visitor
&visitor
) const
311 head
->visit(visitor
);
315 template <class FindValue
>
316 typename Splay
<V
>::Value
const *
317 Splay
<V
>::find (FindValue
const &value
, int( * compare
)(FindValue
const &a
, Value
const &b
)) const
319 head
= head
->splay(value
, compare
);
321 if (splayLastResult
!= 0)
329 Splay
<V
>::insert(Value
const &value
, SPLAYCMP
*compare
)
331 assert (!find (value
, compare
));
332 head
= head
->insert(value
, compare
);
338 Splay
<V
>::remove(Value
const &value
, SPLAYCMP
*compare
)
340 assert (find (value
, compare
));
342 head
= head
->remove(value
, compare
);
349 Splay
<V
>:: start() const
352 return head
->start();
359 Splay
<V
>:: finish() const
362 return head
->finish();
369 Splay
<V
>:: destroy(SPLAYFREE
*free_func
)
372 head
->destroy(free_func
);
381 Splay
<V
>::size() const
387 const SplayConstIterator
<V
>
388 Splay
<V
>::begin() const
390 return const_iterator(head
);
394 const SplayConstIterator
<V
>
395 Splay
<V
>::end() const
397 return const_iterator(NULL
);
400 // XXX: This does not seem to iterate the whole thing in some cases.
402 class SplayConstIterator
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;
415 void addLeftPath(SplayNode
<V
> *aNode
);
416 void init(SplayNode
<V
> *);
417 std::stack
<SplayNode
<V
> *> toVisit
;
421 SplayConstIterator
<V
>::SplayConstIterator (SplayNode
<V
> *aNode
)
428 SplayConstIterator
<V
>::operator == (SplayConstIterator
const &right
) const
430 if (toVisit
.empty() && right
.toVisit
.empty())
432 if (!toVisit
.empty() && !right
.toVisit
.empty())
433 return toVisit
.top() == right
.toVisit
.top();
434 // only one of the two is empty
439 SplayConstIterator
<V
> &
440 SplayConstIterator
<V
>::operator ++ ()
447 SplayConstIterator
<V
>
448 SplayConstIterator
<V
>::operator ++ (int dummy
)
450 SplayConstIterator
<V
> result
= *this;
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
461 * then add the node back.
465 SplayConstIterator
<V
>::advance()
476 SplayNode
<V
> *currentNode
= toVisit
.top();
479 addLeftPath(currentNode
->right
);
481 toVisit
.push(currentNode
);
486 SplayConstIterator
<V
>::addLeftPath(SplayNode
<V
> *aNode
)
494 } while (aNode
!= NULL
);
499 SplayConstIterator
<V
>::init(SplayNode
<V
> *head
)
506 SplayConstIterator
<V
>::operator * () const
508 /* can't dereference when past the end */
510 if (toVisit
.size() == 0)
511 fatal ("Attempt to dereference SplayConstIterator past-the-end\n");
513 return toVisit
.top()->data
;
516 #endif /* cplusplus */
518 #endif /* SQUID_SPLAY_H */