]>
git.ipfire.org Git - thirdparty/squid.git/blob - include/splay.h
2 * $Id: splay.h,v 1.16 2003/02/12 06:10:50 robertc Exp $
9 /* legacy C bindings - can be removed when mempool is C++ */
10 typedef struct _splay_node
{
12 struct _splay_node
*left
;
13 struct _splay_node
*right
;
16 typedef int SPLAYCMP(const void **a
, const void **b
);
17 typedef void SPLAYWALKEE(void **nodedata
, void *state
);
19 SQUIDCEXTERN
int splayLastResult
;
21 /* MUST match C++ prototypes */
22 SQUIDCEXTERN splayNode
*splay_insert(void *, splayNode
*, SPLAYCMP
*);
23 SQUIDCEXTERN splayNode
*splay_splay(const void **, splayNode
*, SPLAYCMP
*);
24 SQUIDCEXTERN splayNode
*splay_delete(const void *, splayNode
*, SPLAYCMP
*);
25 SQUIDCEXTERN
void splay_walk(splayNode
*, SPLAYWALKEE
*, void *);
33 typedef int SPLAYCMP(Value
const &a
, Value
const &b
);
34 typedef void SPLAYFREE(Value
&);
35 typedef void SPLAYWALKEE(Value
const & nodedata
, void *state
);
36 static void DefaultFree (Value
&aValue
) {aValue
->deleteSelf();}
38 mutable SplayNode
<V
> *left
;
39 mutable SplayNode
<V
> *right
;
40 void destroy(SPLAYFREE
*);
41 void walk(SPLAYWALKEE
*, void *callerState
);
42 SplayNode
<V
> * remove(const Value data
, SPLAYCMP
* compare
);
43 SplayNode
<V
> * insert(Value data
, SPLAYCMP
* compare
);
44 SplayNode
<V
> * splay(const Value
&data
, SPLAYCMP
* compare
) const;
47 typedef SplayNode
<void *> splayNode
;
53 typedef int SPLAYCMP(Value
const &a
, Value
const &b
);
55 mutable SplayNode
<V
> * head
;
56 Value
const *find (Value
const &, SPLAYCMP
*compare
) const;
60 SQUIDCEXTERN
int splayLastResult
;
62 SQUIDCEXTERN splayNode
*splay_insert(void *, splayNode
*, splayNode::SPLAYCMP
*);
63 SQUIDCEXTERN splayNode
*splay_delete(const void *, splayNode
*, splayNode::SPLAYCMP
*);
64 SQUIDCEXTERN splayNode
*splay_splay(const void **, splayNode
*, splayNode::SPLAYCMP
*);
65 SQUIDCEXTERN
void splay_destroy(splayNode
*, splayNode::SPLAYFREE
*);
66 SQUIDCEXTERN
void splay_walk(splayNode
*, splayNode::SPLAYWALKEE
*, void *callerState
);
71 SplayNode
<V
>::walk(SPLAYWALKEE
* walkee
, void *state
)
76 left
->walk(walkee
, state
);
79 right
->walk(walkee
, state
);
84 SplayNode
<V
>::destroy(SPLAYFREE
* free_func
)
89 left
->destroy(free_func
);
91 right
->destroy(free_func
);
98 SplayNode
<V
>::remove(Value
const data
, SPLAYCMP
* compare
)
102 SplayNode
<V
> *result
= splay(data
, compare
);
103 if (splayLastResult
== 0) { /* found it */
104 SplayNode
<V
> *newTop
;
105 if (result
->left
== NULL
) {
106 newTop
= result
->right
;
108 newTop
= result
->left
->splay(data
, compare
);
110 newTop
->right
= result
->right
;
111 result
->right
= NULL
;
116 return result
; /* It wasn't there */
121 SplayNode
<V
>::insert(Value data
, SPLAYCMP
* compare
)
123 /* create node to insert */
124 SplayNode
<V
> *newNode
= new SplayNode
<V
>;
125 newNode
->data
= data
;
127 newNode
->left
= newNode
->right
= NULL
;
131 SplayNode
<V
> *newTop
= splay(data
, compare
);
132 if (splayLastResult
< 0) {
133 newNode
->left
= newTop
->left
;
134 newNode
->right
= newTop
;
137 } else if (splayLastResult
> 0) {
138 newNode
->right
= newTop
->right
;
139 newNode
->left
= newTop
;
140 newTop
->right
= NULL
;
143 /* duplicate entry */
151 SplayNode
<V
>::splay(Value
const &data
, SPLAYCMP
* compare
) const
159 N
.left
= N
.right
= NULL
;
162 SplayNode
<V
> *top
= const_cast<SplayNode
<V
> *>(this);
164 splayLastResult
= compare(data
, top
->data
);
165 if (splayLastResult
< 0) {
166 if (top
->left
== NULL
)
168 if ((splayLastResult
= compare(data
, top
->left
->data
)) < 0) {
169 y
= top
->left
; /* rotate right */
170 top
->left
= y
->right
;
173 if (top
->left
== NULL
)
176 r
->left
= top
; /* link right */
179 } else if (splayLastResult
> 0) {
180 if (top
->right
== NULL
)
182 if ((splayLastResult
= compare(data
, top
->right
->data
)) > 0) {
183 y
= top
->right
; /* rotate left */
184 top
->right
= y
->left
;
187 if (top
->right
== NULL
)
190 l
->right
= top
; /* link left */
197 l
->right
= top
->left
; /* assemble */
198 r
->left
= top
->right
;
205 typename Splay
<V
>::Value
const *
206 Splay
<V
>::find (Value
const &value
, SPLAYCMP
*compare
) const
208 head
= head
->splay(value
, compare
);
209 if (splayLastResult
!= 0)
216 #endif /* SQUID_SPLAY_H */