blob: 69af446eb586a20cc7ca9a65f16896de83e4e2ed [file] [log] [blame]
Kristian Monsen5ab50182010-05-14 18:53:44 +01001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Elliott Hughes82be86d2017-09-20 17:00:17 -07008 * Copyright (C) 1997 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
Kristian Monsen5ab50182010-05-14 18:53:44 +01009 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Alex Deymo8f1a2142016-06-28 14:49:26 -070012 * are also available at https://curl.haxx.se/docs/copyright.html.
Kristian Monsen5ab50182010-05-14 18:53:44 +010013 *
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 ***************************************************************************/
22
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070023#include "curl_setup.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010024
25#include "splay.h"
26
27/*
28 * This macro compares two node keys i and j and returns:
29 *
30 * negative value: when i is smaller than j
31 * zero : when i is equal to j
32 * positive when : when i is larger than j
33 */
34#define compare(i,j) Curl_splaycomparekeys((i),(j))
35
36/*
37 * Splay using the key i (which may or may not be in the tree.) The starting
38 * root is t.
39 */
Elliott Hughes82be86d2017-09-20 17:00:17 -070040struct Curl_tree *Curl_splay(struct curltime i,
Kristian Monsen5ab50182010-05-14 18:53:44 +010041 struct Curl_tree *t)
42{
43 struct Curl_tree N, *l, *r, *y;
44 long comp;
45
46 if(t == NULL)
47 return t;
48 N.smaller = N.larger = NULL;
49 l = r = &N;
50
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070051 for(;;) {
Kristian Monsen5ab50182010-05-14 18:53:44 +010052 comp = compare(i, t->key);
53 if(comp < 0) {
54 if(t->smaller == NULL)
55 break;
56 if(compare(i, t->smaller->key) < 0) {
57 y = t->smaller; /* rotate smaller */
58 t->smaller = y->larger;
59 y->larger = t;
60 t = y;
61 if(t->smaller == NULL)
62 break;
63 }
64 r->smaller = t; /* link smaller */
65 r = t;
66 t = t->smaller;
67 }
68 else if(comp > 0) {
69 if(t->larger == NULL)
70 break;
71 if(compare(i, t->larger->key) > 0) {
72 y = t->larger; /* rotate larger */
73 t->larger = y->smaller;
74 y->smaller = t;
75 t = y;
76 if(t->larger == NULL)
77 break;
78 }
79 l->larger = t; /* link larger */
80 l = t;
81 t = t->larger;
82 }
83 else
84 break;
85 }
86
87 l->larger = t->smaller; /* assemble */
88 r->smaller = t->larger;
89 t->smaller = N.larger;
90 t->larger = N.smaller;
91
92 return t;
93}
94
95/* Insert key i into the tree t. Return a pointer to the resulting tree or
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070096 * NULL if something went wrong.
97 *
98 * @unittest: 1309
99 */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700100struct Curl_tree *Curl_splayinsert(struct curltime i,
Kristian Monsen5ab50182010-05-14 18:53:44 +0100101 struct Curl_tree *t,
102 struct Curl_tree *node)
103{
Elliott Hughes82be86d2017-09-20 17:00:17 -0700104 static const struct curltime KEY_NOTUSED = {
105 (time_t)-1, (unsigned int)-1
106 }; /* will *NEVER* appear */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100107
108 if(node == NULL)
109 return t;
110
111 if(t != NULL) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700112 t = Curl_splay(i, t);
Alex Deymo486467e2017-12-19 19:04:07 +0100113 if(compare(i, t->key) == 0) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100114 /* There already exists a node in the tree with the very same key. Build
Elliott Hughes82be86d2017-09-20 17:00:17 -0700115 a doubly-linked circular list of nodes. We add the new 'node' struct
116 to the end of this list. */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100117
Elliott Hughes82be86d2017-09-20 17:00:17 -0700118 node->key = KEY_NOTUSED; /* we set the key in the sub node to NOTUSED
119 to quickly identify this node as a subnode */
120 node->samen = t;
121 node->samep = t->samep;
122 t->samep->samen = node;
123 t->samep = node;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100124
Elliott Hughes82be86d2017-09-20 17:00:17 -0700125 return t; /* the root node always stays the same */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100126 }
127 }
128
129 if(t == NULL) {
130 node->smaller = node->larger = NULL;
131 }
132 else if(compare(i, t->key) < 0) {
133 node->smaller = t->smaller;
134 node->larger = t;
135 t->smaller = NULL;
136
137 }
138 else {
139 node->larger = t->larger;
140 node->smaller = t;
141 t->larger = NULL;
142 }
143 node->key = i;
144
Elliott Hughes82be86d2017-09-20 17:00:17 -0700145 /* no identical nodes (yet), we are the only one in the list of nodes */
146 node->samen = node;
147 node->samep = node;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100148 return node;
149}
150
Kristian Monsen5ab50182010-05-14 18:53:44 +0100151/* Finds and deletes the best-fit node from the tree. Return a pointer to the
Elliott Hughes82be86d2017-09-20 17:00:17 -0700152 resulting tree. best-fit means the smallest node if it is not larger than
153 the key */
154struct Curl_tree *Curl_splaygetbest(struct curltime i,
Kristian Monsen5ab50182010-05-14 18:53:44 +0100155 struct Curl_tree *t,
156 struct Curl_tree **removed)
157{
Elliott Hughes82be86d2017-09-20 17:00:17 -0700158 static struct curltime tv_zero = {0, 0};
Kristian Monsen5ab50182010-05-14 18:53:44 +0100159 struct Curl_tree *x;
160
161 if(!t) {
162 *removed = NULL; /* none removed since there was no root */
163 return NULL;
164 }
165
Elliott Hughes82be86d2017-09-20 17:00:17 -0700166 /* find smallest */
167 t = Curl_splay(tv_zero, t);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100168 if(compare(i, t->key) < 0) {
Elliott Hughes82be86d2017-09-20 17:00:17 -0700169 /* even the smallest is too big */
170 *removed = NULL;
171 return t;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100172 }
173
Elliott Hughes82be86d2017-09-20 17:00:17 -0700174 /* FIRST! Check if there is a list with identical keys */
175 x = t->samen;
176 if(x != t) {
177 /* there is, pick one from the list */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100178
Elliott Hughes82be86d2017-09-20 17:00:17 -0700179 /* 'x' is the new root node */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100180
Elliott Hughes82be86d2017-09-20 17:00:17 -0700181 x->key = t->key;
182 x->larger = t->larger;
183 x->smaller = t->smaller;
184 x->samep = t->samep;
185 t->samep->samen = x;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100186
Kristian Monsen5ab50182010-05-14 18:53:44 +0100187 *removed = t;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700188 return x; /* new root */
189 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100190
Elliott Hughes82be86d2017-09-20 17:00:17 -0700191 /* we splayed the tree to the smallest element, there is no smaller */
192 x = t->larger;
193 *removed = t;
194
195 return x;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100196}
197
198
199/* Deletes the very node we point out from the tree if it's there. Stores a
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700200 * pointer to the new resulting tree in 'newroot'.
201 *
202 * Returns zero on success and non-zero on errors! TODO: document error codes.
203 * When returning error, it does not touch the 'newroot' pointer.
204 *
205 * NOTE: when the last node of the tree is removed, there's no tree left so
206 * 'newroot' will be made to point to NULL.
207 *
208 * @unittest: 1309
209 */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100210int Curl_splayremovebyaddr(struct Curl_tree *t,
211 struct Curl_tree *removenode,
212 struct Curl_tree **newroot)
213{
Elliott Hughes82be86d2017-09-20 17:00:17 -0700214 static const struct curltime KEY_NOTUSED = {
215 (time_t)-1, (unsigned int)-1
216 }; /* will *NEVER* appear */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100217 struct Curl_tree *x;
218
219 if(!t || !removenode)
220 return 1;
221
222 if(compare(KEY_NOTUSED, removenode->key) == 0) {
223 /* Key set to NOTUSED means it is a subnode within a 'same' linked list
Elliott Hughes82be86d2017-09-20 17:00:17 -0700224 and thus we can unlink it easily. */
225 if(removenode->samen == removenode)
226 /* A non-subnode should never be set to KEY_NOTUSED */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100227 return 3;
228
Elliott Hughes82be86d2017-09-20 17:00:17 -0700229 removenode->samep->samen = removenode->samen;
230 removenode->samen->samep = removenode->samep;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100231
232 /* Ensures that double-remove gets caught. */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700233 removenode->samen = removenode;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100234
Kristian Monsen5ab50182010-05-14 18:53:44 +0100235 *newroot = t; /* return the same root */
236 return 0;
237 }
238
239 t = Curl_splay(removenode->key, t);
240
241 /* First make sure that we got the same root node as the one we want
242 to remove, as otherwise we might be trying to remove a node that
243 isn't actually in the tree.
244
245 We cannot just compare the keys here as a double remove in quick
246 succession of a node with key != KEY_NOTUSED && same != NULL
247 could return the same key but a different node. */
248 if(t != removenode)
249 return 2;
250
251 /* Check if there is a list with identical sizes, as then we're trying to
252 remove the root node of a list of nodes with identical keys. */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700253 x = t->samen;
254 if(x != t) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100255 /* 'x' is the new root node, we just make it use the root node's
256 smaller/larger links */
257
258 x->key = t->key;
259 x->larger = t->larger;
260 x->smaller = t->smaller;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700261 x->samep = t->samep;
262 t->samep->samen = x;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100263 }
264 else {
265 /* Remove the root node */
266 if(t->smaller == NULL)
267 x = t->larger;
268 else {
269 x = Curl_splay(removenode->key, t->smaller);
270 x->larger = t->larger;
271 }
272 }
273
274 *newroot = x; /* store new root pointer */
275
276 return 0;
277}
278