1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1997 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
28 * This macro compares two node keys i and j and returns:
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
34 #define compare(i,j) Curl_splaycomparekeys((i),(j))
37 * Splay using the key i (which may or may not be in the tree.) The starting
40 struct Curl_tree *Curl_splay(struct timeval i,
43 struct Curl_tree N, *l, *r, *y;
48 N.smaller = N.larger = NULL;
52 comp = compare(i, t->key);
54 if(t->smaller == NULL)
56 if(compare(i, t->smaller->key) < 0) {
57 y = t->smaller; /* rotate smaller */
58 t->smaller = y->larger;
61 if(t->smaller == NULL)
64 r->smaller = t; /* link smaller */
71 if(compare(i, t->larger->key) > 0) {
72 y = t->larger; /* rotate larger */
73 t->larger = y->smaller;
79 l->larger = t; /* link larger */
87 l->larger = t->smaller; /* assemble */
88 r->smaller = t->larger;
89 t->smaller = N.larger;
90 t->larger = N.smaller;
95 /* Insert key i into the tree t. Return a pointer to the resulting tree or
96 * NULL if something went wrong.
100 struct Curl_tree *Curl_splayinsert(struct timeval i,
102 struct Curl_tree *node)
104 static const struct timeval KEY_NOTUSED = {-1, -1}; /* will *NEVER* appear */
110 t = Curl_splay(i, t);
111 if(compare(i, t->key)==0) {
112 /* There already exists a node in the tree with the very same key. Build
113 a linked list of nodes. We make the new 'node' struct the new master
114 node and make the previous node the first one in the 'same' list. */
118 node->smaller = t->smaller;
119 node->larger = t->larger;
121 t->smaller = node; /* in the sub node for this same key, we use the
122 smaller pointer to point back to the master
125 t->key = KEY_NOTUSED; /* and we set the key in the sub node to NOTUSED
126 to quickly identify this node as a subnode */
128 return node; /* new root node */
133 node->smaller = node->larger = NULL;
135 else if(compare(i, t->key) < 0) {
136 node->smaller = t->smaller;
142 node->larger = t->larger;
148 node->same = NULL; /* no identical node (yet) */
152 /* Finds and deletes the best-fit node from the tree. Return a pointer to the
153 resulting tree. best-fit means the node with the given or lower key */
154 struct Curl_tree *Curl_splaygetbest(struct timeval i,
156 struct Curl_tree **removed)
161 *removed = NULL; /* none removed since there was no root */
165 t = Curl_splay(i, t);
166 if(compare(i, t->key) < 0) {
167 /* too big node, try the smaller chain */
169 t=Curl_splay(t->smaller->key, t);
177 if(compare(i, t->key) >= 0) { /* found it */
178 /* FIRST! Check if there is a list with identical keys */
181 /* there is, pick one from the list */
183 /* 'x' is the new root node */
186 x->larger = t->larger;
187 x->smaller = t->smaller;
190 return x; /* new root */
193 if(t->smaller == NULL) {
197 x = Curl_splay(i, t->smaller);
198 x->larger = t->larger;
205 *removed = NULL; /* no match */
206 return t; /* It wasn't there */
211 /* Deletes the very node we point out from the tree if it's there. Stores a
212 * pointer to the new resulting tree in 'newroot'.
214 * Returns zero on success and non-zero on errors! TODO: document error codes.
215 * When returning error, it does not touch the 'newroot' pointer.
217 * NOTE: when the last node of the tree is removed, there's no tree left so
218 * 'newroot' will be made to point to NULL.
222 int Curl_splayremovebyaddr(struct Curl_tree *t,
223 struct Curl_tree *removenode,
224 struct Curl_tree **newroot)
226 static const struct timeval KEY_NOTUSED = {-1, -1}; /* will *NEVER* appear */
229 if(!t || !removenode)
232 if(compare(KEY_NOTUSED, removenode->key) == 0) {
233 /* Key set to NOTUSED means it is a subnode within a 'same' linked list
234 and thus we can unlink it easily. The 'smaller' link of a subnode
235 links to the parent node. */
236 if(removenode->smaller == NULL)
239 removenode->smaller->same = removenode->same;
241 removenode->same->smaller = removenode->smaller;
243 /* Ensures that double-remove gets caught. */
244 removenode->smaller = NULL;
246 /* voila, we're done! */
247 *newroot = t; /* return the same root */
251 t = Curl_splay(removenode->key, t);
253 /* First make sure that we got the same root node as the one we want
254 to remove, as otherwise we might be trying to remove a node that
255 isn't actually in the tree.
257 We cannot just compare the keys here as a double remove in quick
258 succession of a node with key != KEY_NOTUSED && same != NULL
259 could return the same key but a different node. */
263 /* Check if there is a list with identical sizes, as then we're trying to
264 remove the root node of a list of nodes with identical keys. */
267 /* 'x' is the new root node, we just make it use the root node's
268 smaller/larger links */
271 x->larger = t->larger;
272 x->smaller = t->smaller;
275 /* Remove the root node */
276 if(t->smaller == NULL)
279 x = Curl_splay(removenode->key, t->smaller);
280 x->larger = t->larger;
284 *newroot = x; /* store new root pointer */