/src/PROJ/curl/lib/splay.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #include "curlx/timeval.h" |
28 | | #include "splay.h" |
29 | | |
30 | | /* |
31 | | * This macro compares two node keys i and j and returns: |
32 | | * |
33 | | * negative value: when i is smaller than j |
34 | | * zero : when i is equal to j |
35 | | * positive when : when i is larger than j |
36 | | */ |
37 | 0 | #define compare(i,j) curlx_timediff_us(i,j) |
38 | | |
39 | | /* |
40 | | * Splay using the key i (which may or may not be in the tree.) The starting |
41 | | * root is t. |
42 | | */ |
43 | | struct Curl_tree *Curl_splay(struct curltime i, |
44 | | struct Curl_tree *t) |
45 | 0 | { |
46 | 0 | struct Curl_tree N, *l, *r, *y; |
47 | |
|
48 | 0 | if(!t) |
49 | 0 | return NULL; |
50 | 0 | N.smaller = N.larger = NULL; |
51 | 0 | l = r = &N; |
52 | |
|
53 | 0 | for(;;) { |
54 | 0 | timediff_t comp = compare(i, t->key); |
55 | 0 | if(comp < 0) { |
56 | 0 | if(!t->smaller) |
57 | 0 | break; |
58 | 0 | if(compare(i, t->smaller->key) < 0) { |
59 | 0 | y = t->smaller; /* rotate smaller */ |
60 | 0 | t->smaller = y->larger; |
61 | 0 | y->larger = t; |
62 | 0 | t = y; |
63 | 0 | if(!t->smaller) |
64 | 0 | break; |
65 | 0 | } |
66 | 0 | r->smaller = t; /* link smaller */ |
67 | 0 | r = t; |
68 | 0 | t = t->smaller; |
69 | 0 | } |
70 | 0 | else if(comp > 0) { |
71 | 0 | if(!t->larger) |
72 | 0 | break; |
73 | 0 | if(compare(i, t->larger->key) > 0) { |
74 | 0 | y = t->larger; /* rotate larger */ |
75 | 0 | t->larger = y->smaller; |
76 | 0 | y->smaller = t; |
77 | 0 | t = y; |
78 | 0 | if(!t->larger) |
79 | 0 | break; |
80 | 0 | } |
81 | 0 | l->larger = t; /* link larger */ |
82 | 0 | l = t; |
83 | 0 | t = t->larger; |
84 | 0 | } |
85 | 0 | else |
86 | 0 | break; |
87 | 0 | } |
88 | |
|
89 | 0 | l->larger = t->smaller; /* assemble */ |
90 | 0 | r->smaller = t->larger; |
91 | 0 | t->smaller = N.larger; |
92 | 0 | t->larger = N.smaller; |
93 | |
|
94 | 0 | return t; |
95 | 0 | } |
96 | | |
97 | | /* Insert key i into the tree t. Return a pointer to the resulting tree or |
98 | | * NULL if something went wrong. |
99 | | * |
100 | | * @unittest: 1309 |
101 | | */ |
102 | | struct Curl_tree *Curl_splayinsert(struct curltime i, |
103 | | struct Curl_tree *t, |
104 | | struct Curl_tree *node) |
105 | 0 | { |
106 | 0 | static const struct curltime KEY_NOTUSED = { |
107 | 0 | ~0, -1 |
108 | 0 | }; /* will *NEVER* appear */ |
109 | |
|
110 | 0 | DEBUGASSERT(node); |
111 | |
|
112 | 0 | if(t) { |
113 | 0 | t = Curl_splay(i, t); |
114 | 0 | DEBUGASSERT(t); |
115 | 0 | if(compare(i, t->key) == 0) { |
116 | | /* There already exists a node in the tree with the same key. Build a |
117 | | doubly-linked circular list of nodes. We add the new 'node' struct to |
118 | | the end of this list. */ |
119 | |
|
120 | 0 | node->key = KEY_NOTUSED; /* we set the key in the sub node to NOTUSED |
121 | | to quickly identify this node as a subnode */ |
122 | 0 | node->samen = t; |
123 | 0 | node->samep = t->samep; |
124 | 0 | t->samep->samen = node; |
125 | 0 | t->samep = node; |
126 | |
|
127 | 0 | return t; /* the root node always stays the same */ |
128 | 0 | } |
129 | 0 | } |
130 | | |
131 | 0 | if(!t) { |
132 | 0 | node->smaller = node->larger = NULL; |
133 | 0 | } |
134 | 0 | else if(compare(i, t->key) < 0) { |
135 | 0 | node->smaller = t->smaller; |
136 | 0 | node->larger = t; |
137 | 0 | t->smaller = NULL; |
138 | |
|
139 | 0 | } |
140 | 0 | else { |
141 | 0 | node->larger = t->larger; |
142 | 0 | node->smaller = t; |
143 | 0 | t->larger = NULL; |
144 | 0 | } |
145 | 0 | node->key = i; |
146 | | |
147 | | /* no identical nodes (yet), we are the only one in the list of nodes */ |
148 | 0 | node->samen = node; |
149 | 0 | node->samep = node; |
150 | 0 | return node; |
151 | 0 | } |
152 | | |
153 | | /* Finds and deletes the best-fit node from the tree. Return a pointer to the |
154 | | resulting tree. best-fit means the smallest node if it is not larger than |
155 | | the key */ |
156 | | struct Curl_tree *Curl_splaygetbest(struct curltime i, |
157 | | struct Curl_tree *t, |
158 | | struct Curl_tree **removed) |
159 | 0 | { |
160 | 0 | static const struct curltime tv_zero = {0, 0}; |
161 | 0 | struct Curl_tree *x; |
162 | |
|
163 | 0 | if(!t) { |
164 | 0 | *removed = NULL; /* none removed since there was no root */ |
165 | 0 | return NULL; |
166 | 0 | } |
167 | | |
168 | | /* find smallest */ |
169 | 0 | t = Curl_splay(tv_zero, t); |
170 | 0 | DEBUGASSERT(t); |
171 | 0 | if(compare(i, t->key) < 0) { |
172 | | /* even the smallest is too big */ |
173 | 0 | *removed = NULL; |
174 | 0 | return t; |
175 | 0 | } |
176 | | |
177 | | /* FIRST! Check if there is a list with identical keys */ |
178 | 0 | x = t->samen; |
179 | 0 | if(x != t) { |
180 | | /* there is, pick one from the list */ |
181 | | |
182 | | /* 'x' is the new root node */ |
183 | |
|
184 | 0 | x->key = t->key; |
185 | 0 | x->larger = t->larger; |
186 | 0 | x->smaller = t->smaller; |
187 | 0 | x->samep = t->samep; |
188 | 0 | t->samep->samen = x; |
189 | |
|
190 | 0 | *removed = t; |
191 | 0 | return x; /* new root */ |
192 | 0 | } |
193 | | |
194 | | /* we splayed the tree to the smallest element, there is no smaller */ |
195 | 0 | x = t->larger; |
196 | 0 | *removed = t; |
197 | |
|
198 | 0 | return x; |
199 | 0 | } |
200 | | |
201 | | |
202 | | /* Deletes the node we point out from the tree if it is there. Stores a |
203 | | * pointer to the new resulting tree in 'newroot'. |
204 | | * |
205 | | * Returns zero on success and non-zero on errors! |
206 | | * When returning error, it does not touch the 'newroot' pointer. |
207 | | * |
208 | | * NOTE: when the last node of the tree is removed, there is no tree left so |
209 | | * 'newroot' will be made to point to NULL. |
210 | | * |
211 | | * @unittest: 1309 |
212 | | */ |
213 | | int Curl_splayremove(struct Curl_tree *t, |
214 | | struct Curl_tree *removenode, |
215 | | struct Curl_tree **newroot) |
216 | 0 | { |
217 | 0 | static const struct curltime KEY_NOTUSED = { |
218 | 0 | ~0, -1 |
219 | 0 | }; /* will *NEVER* appear */ |
220 | 0 | struct Curl_tree *x; |
221 | |
|
222 | 0 | if(!t) |
223 | 0 | return 1; |
224 | | |
225 | 0 | DEBUGASSERT(removenode); |
226 | |
|
227 | 0 | if(compare(KEY_NOTUSED, removenode->key) == 0) { |
228 | | /* Key set to NOTUSED means it is a subnode within a 'same' linked list |
229 | | and thus we can unlink it easily. */ |
230 | 0 | if(removenode->samen == removenode) |
231 | | /* A non-subnode should never be set to KEY_NOTUSED */ |
232 | 0 | return 3; |
233 | | |
234 | 0 | removenode->samep->samen = removenode->samen; |
235 | 0 | removenode->samen->samep = removenode->samep; |
236 | | |
237 | | /* Ensures that double-remove gets caught. */ |
238 | 0 | removenode->samen = removenode; |
239 | |
|
240 | 0 | *newroot = t; /* return the same root */ |
241 | 0 | return 0; |
242 | 0 | } |
243 | | |
244 | 0 | t = Curl_splay(removenode->key, t); |
245 | 0 | DEBUGASSERT(t); |
246 | | |
247 | | /* First make sure that we got the same root node as the one we want |
248 | | to remove, as otherwise we might be trying to remove a node that |
249 | | is not actually in the tree. |
250 | | |
251 | | We cannot just compare the keys here as a double remove in quick |
252 | | succession of a node with key != KEY_NOTUSED && same != NULL |
253 | | could return the same key but a different node. */ |
254 | 0 | if(t != removenode) |
255 | 0 | return 2; |
256 | | |
257 | | /* Check if there is a list with identical sizes, as then we are trying to |
258 | | remove the root node of a list of nodes with identical keys. */ |
259 | 0 | x = t->samen; |
260 | 0 | if(x != t) { |
261 | | /* 'x' is the new root node, we just make it use the root node's |
262 | | smaller/larger links */ |
263 | |
|
264 | 0 | x->key = t->key; |
265 | 0 | x->larger = t->larger; |
266 | 0 | x->smaller = t->smaller; |
267 | 0 | x->samep = t->samep; |
268 | 0 | t->samep->samen = x; |
269 | 0 | } |
270 | 0 | else { |
271 | | /* Remove the root node */ |
272 | 0 | if(!t->smaller) |
273 | 0 | x = t->larger; |
274 | 0 | else { |
275 | 0 | x = Curl_splay(removenode->key, t->smaller); |
276 | 0 | DEBUGASSERT(x); |
277 | 0 | x->larger = t->larger; |
278 | 0 | } |
279 | 0 | } |
280 | |
|
281 | 0 | *newroot = x; /* store new root pointer */ |
282 | |
|
283 | 0 | return 0; |
284 | 0 | } |
285 | | |
286 | | /* set and get the custom payload for this tree node */ |
287 | | void Curl_splayset(struct Curl_tree *node, void *payload) |
288 | 0 | { |
289 | 0 | DEBUGASSERT(node); |
290 | 0 | node->ptr = payload; |
291 | 0 | } |
292 | | |
293 | | void *Curl_splayget(struct Curl_tree *node) |
294 | 0 | { |
295 | 0 | DEBUGASSERT(node); |
296 | 0 | return node->ptr; |
297 | 0 | } |