]>
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 false;}
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
)
114 left
->walk(walkee
, state
);
119 right
->walk(walkee
, state
);
124 SplayNode
<V
>::start() const
127 return left
->start();
134 SplayNode
<V
>::finish() const
137 return right
->finish();
144 SplayNode
<V
>::destroy(SPLAYFREE
* free_func
)
147 left
->destroy(free_func
);
150 right
->destroy(free_func
);
159 SplayNode
<V
>::remove(Value
const dataToRemove
, SPLAYCMP
* compare
)
161 SplayNode
<V
> *result
= splay(dataToRemove
, compare
);
163 if (splayLastResult
== 0) { /* found it */
164 SplayNode
<V
> *newTop
;
166 if (result
->left
== NULL
) {
167 newTop
= result
->right
;
169 newTop
= result
->left
->splay(dataToRemove
, compare
);
171 newTop
->right
= result
->right
;
172 result
->right
= NULL
;
179 return result
; /* It wasn't there */
184 SplayNode
<V
>::insert(Value dataToInsert
, SPLAYCMP
* compare
)
186 /* create node to insert */
187 SplayNode
<V
> *newNode
= new SplayNode
<V
>(dataToInsert
);
189 SplayNode
<V
> *newTop
= splay(dataToInsert
, compare
);
191 if (splayLastResult
< 0) {
192 newNode
->left
= newTop
->left
;
193 newNode
->right
= newTop
;
196 } else if (splayLastResult
> 0) {
197 newNode
->right
= newTop
->right
;
198 newNode
->left
= newTop
;
199 newTop
->right
= NULL
;
202 /* duplicate entry */
209 template<class FindValue
>
211 SplayNode
<V
>::splay(FindValue
const &dataToFind
, int( * compare
)(FindValue
const &a
, Value
const &b
)) const
213 Value temp
= Value();
214 SplayNode
<V
> N(temp
);
218 N
.left
= N
.right
= NULL
;
221 SplayNode
<V
> *top
= const_cast<SplayNode
<V
> *>(this);
224 splayLastResult
= compare(dataToFind
, top
->data
);
226 if (splayLastResult
< 0) {
227 if (top
->left
== NULL
)
230 if ((splayLastResult
= compare(dataToFind
, top
->left
->data
)) < 0) {
231 y
= top
->left
; /* rotate right */
232 top
->left
= y
->right
;
236 if (top
->left
== NULL
)
240 r
->left
= top
; /* link right */
243 } else if (splayLastResult
> 0) {
244 if (top
->right
== NULL
)
247 if ((splayLastResult
= compare(dataToFind
, top
->right
->data
)) > 0) {
248 y
= top
->right
; /* rotate left */
249 top
->right
= y
->left
;
253 if (top
->right
== NULL
)
257 l
->right
= top
; /* link left */
265 l
->right
= top
->left
; /* assemble */
266 r
->left
= top
->right
;
273 template <class Visitor
>
275 SplayNode
<V
>::visit(Visitor
&visitor
) const
278 left
->visit(visitor
);
281 right
->visit(visitor
);
285 template <class Visitor
>
287 Splay
<V
>::visit(Visitor
&visitor
) const
290 head
->visit(visitor
);
294 template <class FindValue
>
295 typename Splay
<V
>::Value
const *
296 Splay
<V
>::find (FindValue
const &value
, int( * compare
)(FindValue
const &a
, Value
const &b
)) const
298 head
= head
->splay(value
, compare
);
300 if (splayLastResult
!= 0)
308 Splay
<V
>::insert(Value
const &value
, SPLAYCMP
*compare
)
310 assert (!find (value
, compare
));
311 head
= head
->insert(value
, compare
);
317 Splay
<V
>::remove(Value
const &value
, SPLAYCMP
*compare
)
319 assert (find (value
, compare
));
321 head
= head
->remove(value
, compare
);
328 Splay
<V
>:: start() const
331 return head
->start();
338 Splay
<V
>:: finish() const
341 return head
->finish();
348 Splay
<V
>:: destroy(SPLAYFREE
*free_func
)
351 head
->destroy(free_func
);
360 Splay
<V
>::size() const
366 const SplayConstIterator
<V
>
367 Splay
<V
>::begin() const
369 return const_iterator(head
);
373 const SplayConstIterator
<V
>
374 Splay
<V
>::end() const
376 return const_iterator(NULL
);
379 // XXX: This does not seem to iterate the whole thing in some cases.
381 class SplayConstIterator
385 typedef const V value_type
;
386 SplayConstIterator (SplayNode
<V
> *aNode
);
387 bool operator == (SplayConstIterator
const &right
) const;
388 SplayConstIterator
operator ++ (int dummy
);
389 SplayConstIterator
&operator ++ ();
390 V
const & operator * () const;
394 void addLeftPath(SplayNode
<V
> *aNode
);
395 void init(SplayNode
<V
> *);
396 std::stack
<SplayNode
<V
> *> toVisit
;
400 SplayConstIterator
<V
>::SplayConstIterator (SplayNode
<V
> *aNode
)
407 SplayConstIterator
<V
>::operator == (SplayConstIterator
const &right
) const
409 if (toVisit
.empty() && right
.toVisit
.empty())
411 if (!toVisit
.empty() && !right
.toVisit
.empty())
412 return toVisit
.top() == right
.toVisit
.top();
413 // only one of the two is empty
418 SplayConstIterator
<V
> &
419 SplayConstIterator
<V
>::operator ++ ()
426 SplayConstIterator
<V
>
427 SplayConstIterator
<V
>::operator ++ (int dummy
)
429 SplayConstIterator
<V
> result
= *this;
434 /* advance is simple enough:
435 * if the stack is empty, we're done.
436 * otherwise, pop the last visited node
437 * then, pop the next node to visit
438 * if that has a right child, add it and it's
440 * then add the node back.
444 SplayConstIterator
<V
>::advance()
455 SplayNode
<V
> *currentNode
= toVisit
.top();
458 addLeftPath(currentNode
->right
);
460 toVisit
.push(currentNode
);
465 SplayConstIterator
<V
>::addLeftPath(SplayNode
<V
> *aNode
)
473 } while (aNode
!= NULL
);
478 SplayConstIterator
<V
>::init(SplayNode
<V
> *head
)
485 SplayConstIterator
<V
>::operator * () const
487 /* can't dereference when past the end */
489 if (toVisit
.size() == 0)
490 fatal ("Attempt to dereference SplayConstIterator past-the-end\n");
492 return toVisit
.top()->data
;
495 #endif /* cplusplus */
497 #endif /* SQUID_SPLAY_H */