2
0

splay.c 7.8 KB

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