Line | Count | Source |
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 | | #include "curl_setup.h" |
25 | | |
26 | | #include "splay.h" |
27 | | |
28 | | /* |
29 | | * This macro compares two node keys i and j and returns: |
30 | | * |
31 | | * negative value: when i is smaller than j |
32 | | * zero : when i is equal to j |
33 | | * positive when : when i is larger than j |
34 | | */ |
35 | 69.5M | #define splay_compare(i, j) curlx_ptimediff_us(i, j) |
36 | | |
37 | | /* |
38 | | * Splay using the key i (which may or may not be in the tree.) The starting |
39 | | * root is t. |
40 | | */ |
41 | | struct Curl_tree *Curl_splay(const struct curltime *pkey, |
42 | | struct Curl_tree *t) |
43 | 34.5M | { |
44 | 34.5M | struct Curl_tree N, *l, *r, *y; |
45 | | |
46 | 34.5M | if(!t) |
47 | 0 | return NULL; |
48 | 34.5M | N.smaller = N.larger = NULL; |
49 | 34.5M | l = r = &N; |
50 | | |
51 | 34.6M | for(;;) { |
52 | 34.6M | timediff_t comp = splay_compare(pkey, &t->key); |
53 | 34.6M | if(comp < 0) { |
54 | 33.7M | if(!t->smaller) |
55 | 33.7M | break; |
56 | 5.88k | if(splay_compare(pkey, &t->smaller->key) < 0) { |
57 | 5.88k | y = t->smaller; /* rotate smaller */ |
58 | 5.88k | t->smaller = y->larger; |
59 | 5.88k | y->larger = t; |
60 | 5.88k | t = y; |
61 | 5.88k | if(!t->smaller) |
62 | 1.23k | break; |
63 | 5.88k | } |
64 | 4.65k | r->smaller = t; /* link smaller */ |
65 | 4.65k | r = t; |
66 | 4.65k | t = t->smaller; |
67 | 4.65k | } |
68 | 878k | else if(comp > 0) { |
69 | 30.9k | if(!t->larger) |
70 | 22.0k | break; |
71 | 8.95k | if(splay_compare(pkey, &t->larger->key) > 0) { |
72 | 4.22k | y = t->larger; /* rotate larger */ |
73 | 4.22k | t->larger = y->smaller; |
74 | 4.22k | y->smaller = t; |
75 | 4.22k | t = y; |
76 | 4.22k | if(!t->larger) |
77 | 2.40k | break; |
78 | 4.22k | } |
79 | 6.55k | l->larger = t; /* link larger */ |
80 | 6.55k | l = t; |
81 | 6.55k | t = t->larger; |
82 | 6.55k | } |
83 | 847k | else |
84 | 847k | break; |
85 | 34.6M | } |
86 | | |
87 | 34.5M | l->larger = t->smaller; /* assemble */ |
88 | 34.5M | r->smaller = t->larger; |
89 | 34.5M | t->smaller = N.larger; |
90 | 34.5M | t->larger = N.smaller; |
91 | | |
92 | 34.5M | return t; |
93 | 34.5M | } |
94 | | |
95 | | static const struct curltime SPLAY_SUBNODE = { |
96 | | ~0, -1 |
97 | | }; |
98 | | |
99 | | /* Insert key i into the tree t. Return a pointer to the resulting tree or |
100 | | * NULL if something went wrong. |
101 | | * |
102 | | * @unittest: 1309 |
103 | | */ |
104 | | struct Curl_tree *Curl_splayinsert(const struct curltime *pkey, |
105 | | struct Curl_tree *t, |
106 | | struct Curl_tree *node) |
107 | 849k | { |
108 | 849k | DEBUGASSERT(node); |
109 | | |
110 | 849k | if(t) { |
111 | 376k | t = Curl_splay(pkey, t); |
112 | 376k | DEBUGASSERT(t); |
113 | 376k | if(splay_compare(pkey, &t->key) == 0) { |
114 | | /* There already exists a node in the tree with the same key. Build a |
115 | | doubly-linked circular list of nodes. We add the new 'node' struct to |
116 | | the end of this list. */ |
117 | |
|
118 | 0 | node->key = SPLAY_SUBNODE; /* identify this node as a subnode */ |
119 | 0 | node->samen = t; |
120 | 0 | node->samep = t->samep; |
121 | 0 | t->samep->samen = node; |
122 | 0 | t->samep = node; |
123 | |
|
124 | 0 | return t; /* the root node always stays the same */ |
125 | 0 | } |
126 | 376k | } |
127 | | |
128 | 849k | if(!t) { |
129 | 473k | node->smaller = node->larger = NULL; |
130 | 473k | } |
131 | 376k | else if(splay_compare(pkey, &t->key) < 0) { |
132 | 363k | node->smaller = t->smaller; |
133 | 363k | node->larger = t; |
134 | 363k | t->smaller = NULL; |
135 | 363k | } |
136 | 12.0k | else { |
137 | 12.0k | node->larger = t->larger; |
138 | 12.0k | node->smaller = t; |
139 | 12.0k | t->larger = NULL; |
140 | 12.0k | } |
141 | 849k | node->key = *pkey; |
142 | | |
143 | | /* no identical nodes (yet), we are the only one in the list of nodes */ |
144 | 849k | node->samen = node; |
145 | 849k | node->samep = node; |
146 | 849k | return node; |
147 | 849k | } |
148 | | |
149 | | /* Finds and deletes the best-fit node from the tree. Return a pointer to the |
150 | | resulting tree. best-fit means the smallest node if it is not larger than |
151 | | the key */ |
152 | | struct Curl_tree *Curl_splaygetbest(const struct curltime *pkey, |
153 | | struct Curl_tree *t, |
154 | | struct Curl_tree **removed) |
155 | 33.3M | { |
156 | 33.3M | static const struct curltime tv_zero = { 0, 0 }; |
157 | 33.3M | struct Curl_tree *x; |
158 | | |
159 | 33.3M | if(!t) { |
160 | 0 | *removed = NULL; /* none removed since there was no root */ |
161 | 0 | return NULL; |
162 | 0 | } |
163 | | |
164 | | /* find smallest */ |
165 | 33.3M | t = Curl_splay(&tv_zero, t); |
166 | 33.3M | DEBUGASSERT(t); |
167 | 33.3M | if(splay_compare(pkey, &t->key) < 0) { |
168 | | /* even the smallest is too big */ |
169 | 33.3M | *removed = NULL; |
170 | 33.3M | return t; |
171 | 33.3M | } |
172 | | |
173 | | /* FIRST! Check if there is a list with identical keys */ |
174 | 2.28k | x = t->samen; |
175 | 2.28k | if(x != t) { |
176 | | /* there is, pick one from the list */ |
177 | | |
178 | | /* 'x' is the new root node */ |
179 | |
|
180 | 0 | x->key = t->key; |
181 | 0 | x->larger = t->larger; |
182 | 0 | x->smaller = t->smaller; |
183 | 0 | x->samep = t->samep; |
184 | 0 | t->samep->samen = x; |
185 | |
|
186 | 0 | *removed = t; |
187 | 0 | return x; /* new root */ |
188 | 0 | } |
189 | | |
190 | | /* we splayed the tree to the smallest element, there is no smaller */ |
191 | 2.28k | x = t->larger; |
192 | 2.28k | *removed = t; |
193 | | |
194 | 2.28k | return x; |
195 | 2.28k | } |
196 | | |
197 | | /* Deletes the node we point out from the tree if it is there. Stores a |
198 | | * pointer to the new resulting tree in 'newroot'. |
199 | | * |
200 | | * Returns zero on success and non-zero on errors! |
201 | | * When returning error, it does not touch the 'newroot' pointer. |
202 | | * |
203 | | * NOTE: when the last node of the tree is removed, there is no tree left so |
204 | | * 'newroot' will be made to point to NULL. |
205 | | * |
206 | | * @unittest: 1309 |
207 | | */ |
208 | | int Curl_splayremove(struct Curl_tree *t, |
209 | | struct Curl_tree *removenode, |
210 | | struct Curl_tree **newroot) |
211 | 847k | { |
212 | 847k | struct Curl_tree *x; |
213 | | |
214 | 847k | if(!t) |
215 | 0 | return 1; |
216 | | |
217 | 847k | DEBUGASSERT(removenode); |
218 | | |
219 | 847k | if(splay_compare(&SPLAY_SUBNODE, &removenode->key) == 0) { |
220 | | /* It is a subnode within a 'same' linked list and thus we can unlink it |
221 | | easily. */ |
222 | 0 | DEBUGASSERT(removenode->samen != removenode); |
223 | 0 | if(removenode->samen == removenode) |
224 | | /* A non-subnode should never be set to SPLAY_SUBNODE */ |
225 | 0 | return 3; |
226 | | |
227 | 0 | removenode->samep->samen = removenode->samen; |
228 | 0 | removenode->samen->samep = removenode->samep; |
229 | | |
230 | | /* Ensures that double-remove gets caught. */ |
231 | 0 | removenode->samen = removenode; |
232 | |
|
233 | 0 | *newroot = t; /* return the same root */ |
234 | 0 | return 0; |
235 | 0 | } |
236 | | |
237 | 847k | t = Curl_splay(&removenode->key, t); |
238 | 847k | DEBUGASSERT(t); |
239 | | |
240 | | /* First make sure that we got the same root node as the one we want |
241 | | to remove, as otherwise we might be trying to remove a node that |
242 | | is not actually in the tree. |
243 | | |
244 | | We cannot just compare the keys here as a double remove in quick |
245 | | succession of a node with key != SPLAY_SUBNODE && same != NULL |
246 | | could return the same key but a different node. */ |
247 | 847k | DEBUGASSERT(t == removenode); |
248 | 847k | if(t != removenode) |
249 | 0 | return 2; |
250 | | |
251 | | /* Check if there is a list with identical sizes, as then we are trying to |
252 | | remove the root node of a list of nodes with identical keys. */ |
253 | 847k | x = t->samen; |
254 | 847k | if(x != t) { |
255 | | /* 'x' is the new root node, we just make it use the root node's |
256 | | smaller/larger links */ |
257 | |
|
258 | 0 | x->key = t->key; |
259 | 0 | x->larger = t->larger; |
260 | 0 | x->smaller = t->smaller; |
261 | 0 | x->samep = t->samep; |
262 | 0 | t->samep->samen = x; |
263 | 0 | } |
264 | 847k | else { |
265 | | /* Remove the root node */ |
266 | 847k | if(!t->smaller) |
267 | 835k | x = t->larger; |
268 | 12.3k | else { |
269 | 12.3k | x = Curl_splay(&removenode->key, t->smaller); |
270 | 12.3k | DEBUGASSERT(x); |
271 | 12.3k | x->larger = t->larger; |
272 | 12.3k | } |
273 | 847k | } |
274 | | |
275 | 847k | *newroot = x; /* store new root pointer */ |
276 | | |
277 | 847k | return 0; |
278 | 847k | } |
279 | | |
280 | | /* set and get the custom payload for this tree node */ |
281 | | void Curl_splayset(struct Curl_tree *node, void *payload) |
282 | 847k | { |
283 | 847k | DEBUGASSERT(node); |
284 | 847k | node->ptr = payload; |
285 | 847k | } |
286 | | |
287 | | void *Curl_splayget(struct Curl_tree *node) |
288 | 2.28k | { |
289 | 2.28k | DEBUGASSERT(node); |
290 | 2.28k | return node->ptr; |
291 | 2.28k | } |