Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 8 | * Copyright (C) 1997 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 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 |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 12 | * are also available at https://curl.haxx.se/docs/copyright.html. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 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 | ***************************************************************************/ |
| 22 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 23 | #include "curl_setup.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 24 | |
| 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 Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 40 | struct Curl_tree *Curl_splay(struct curltime i, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 41 | 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 SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 51 | for(;;) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 52 | 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 SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 96 | * NULL if something went wrong. |
| 97 | * |
| 98 | * @unittest: 1309 |
| 99 | */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 100 | struct Curl_tree *Curl_splayinsert(struct curltime i, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 101 | struct Curl_tree *t, |
| 102 | struct Curl_tree *node) |
| 103 | { |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 104 | static const struct curltime KEY_NOTUSED = { |
| 105 | (time_t)-1, (unsigned int)-1 |
| 106 | }; /* will *NEVER* appear */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 107 | |
| 108 | if(node == NULL) |
| 109 | return t; |
| 110 | |
| 111 | if(t != NULL) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 112 | t = Curl_splay(i, t); |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 113 | if(compare(i, t->key) == 0) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 114 | /* There already exists a node in the tree with the very same key. Build |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 115 | a doubly-linked circular list of nodes. We add the new 'node' struct |
| 116 | to the end of this list. */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 117 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 118 | 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 Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 124 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 125 | return t; /* the root node always stays the same */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 126 | } |
| 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 Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 145 | /* no identical nodes (yet), we are the only one in the list of nodes */ |
| 146 | node->samen = node; |
| 147 | node->samep = node; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 148 | return node; |
| 149 | } |
| 150 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 151 | /* Finds and deletes the best-fit node from the tree. Return a pointer to the |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 152 | resulting tree. best-fit means the smallest node if it is not larger than |
| 153 | the key */ |
| 154 | struct Curl_tree *Curl_splaygetbest(struct curltime i, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 155 | struct Curl_tree *t, |
| 156 | struct Curl_tree **removed) |
| 157 | { |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 158 | static struct curltime tv_zero = {0, 0}; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 159 | struct Curl_tree *x; |
| 160 | |
| 161 | if(!t) { |
| 162 | *removed = NULL; /* none removed since there was no root */ |
| 163 | return NULL; |
| 164 | } |
| 165 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 166 | /* find smallest */ |
| 167 | t = Curl_splay(tv_zero, t); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 168 | if(compare(i, t->key) < 0) { |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 169 | /* even the smallest is too big */ |
| 170 | *removed = NULL; |
| 171 | return t; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 174 | /* 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 Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 178 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 179 | /* 'x' is the new root node */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 180 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 181 | 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 Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 186 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 187 | *removed = t; |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 188 | return x; /* new root */ |
| 189 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 190 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 191 | /* we splayed the tree to the smallest element, there is no smaller */ |
| 192 | x = t->larger; |
| 193 | *removed = t; |
| 194 | |
| 195 | return x; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
| 199 | /* Deletes the very node we point out from the tree if it's there. Stores a |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 200 | * 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 Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 210 | int Curl_splayremovebyaddr(struct Curl_tree *t, |
| 211 | struct Curl_tree *removenode, |
| 212 | struct Curl_tree **newroot) |
| 213 | { |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 214 | static const struct curltime KEY_NOTUSED = { |
| 215 | (time_t)-1, (unsigned int)-1 |
| 216 | }; /* will *NEVER* appear */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 217 | 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 Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 224 | and thus we can unlink it easily. */ |
| 225 | if(removenode->samen == removenode) |
| 226 | /* A non-subnode should never be set to KEY_NOTUSED */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 227 | return 3; |
| 228 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 229 | removenode->samep->samen = removenode->samen; |
| 230 | removenode->samen->samep = removenode->samep; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 231 | |
| 232 | /* Ensures that double-remove gets caught. */ |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 233 | removenode->samen = removenode; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 234 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 235 | *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 Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 253 | x = t->samen; |
| 254 | if(x != t) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 255 | /* '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 Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 261 | x->samep = t->samep; |
| 262 | t->samep->samen = x; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 263 | } |
| 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 | |