splay.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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 "curlx/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) curlx_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. static const struct curltime SPLAY_SUBNODE = {
  89. ~0, -1
  90. };
  91. /* Insert key i into the tree t. Return a pointer to the resulting tree or
  92. * NULL if something went wrong.
  93. *
  94. * @unittest: 1309
  95. */
  96. struct Curl_tree *Curl_splayinsert(struct curltime i,
  97. struct Curl_tree *t,
  98. struct Curl_tree *node)
  99. {
  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 = SPLAY_SUBNODE; /* identify this node as a subnode */
  109. node->samen = t;
  110. node->samep = t->samep;
  111. t->samep->samen = node;
  112. t->samep = node;
  113. return t; /* the root node always stays the same */
  114. }
  115. }
  116. if(!t) {
  117. node->smaller = node->larger = NULL;
  118. }
  119. else if(compare(i, t->key) < 0) {
  120. node->smaller = t->smaller;
  121. node->larger = t;
  122. t->smaller = NULL;
  123. }
  124. else {
  125. node->larger = t->larger;
  126. node->smaller = t;
  127. t->larger = NULL;
  128. }
  129. node->key = i;
  130. /* no identical nodes (yet), we are the only one in the list of nodes */
  131. node->samen = node;
  132. node->samep = node;
  133. return node;
  134. }
  135. /* Finds and deletes the best-fit node from the tree. Return a pointer to the
  136. resulting tree. best-fit means the smallest node if it is not larger than
  137. the key */
  138. struct Curl_tree *Curl_splaygetbest(struct curltime i,
  139. struct Curl_tree *t,
  140. struct Curl_tree **removed)
  141. {
  142. static const struct curltime tv_zero = {0, 0};
  143. struct Curl_tree *x;
  144. if(!t) {
  145. *removed = NULL; /* none removed since there was no root */
  146. return NULL;
  147. }
  148. /* find smallest */
  149. t = Curl_splay(tv_zero, t);
  150. DEBUGASSERT(t);
  151. if(compare(i, t->key) < 0) {
  152. /* even the smallest is too big */
  153. *removed = NULL;
  154. return t;
  155. }
  156. /* FIRST! Check if there is a list with identical keys */
  157. x = t->samen;
  158. if(x != t) {
  159. /* there is, pick one from the list */
  160. /* 'x' is the new root node */
  161. x->key = t->key;
  162. x->larger = t->larger;
  163. x->smaller = t->smaller;
  164. x->samep = t->samep;
  165. t->samep->samen = x;
  166. *removed = t;
  167. return x; /* new root */
  168. }
  169. /* we splayed the tree to the smallest element, there is no smaller */
  170. x = t->larger;
  171. *removed = t;
  172. return x;
  173. }
  174. /* Deletes the node we point out from the tree if it is there. Stores a
  175. * pointer to the new resulting tree in 'newroot'.
  176. *
  177. * Returns zero on success and non-zero on errors!
  178. * When returning error, it does not touch the 'newroot' pointer.
  179. *
  180. * NOTE: when the last node of the tree is removed, there is no tree left so
  181. * 'newroot' will be made to point to NULL.
  182. *
  183. * @unittest: 1309
  184. */
  185. int Curl_splayremove(struct Curl_tree *t,
  186. struct Curl_tree *removenode,
  187. struct Curl_tree **newroot)
  188. {
  189. struct Curl_tree *x;
  190. if(!t)
  191. return 1;
  192. DEBUGASSERT(removenode);
  193. if(compare(SPLAY_SUBNODE, removenode->key) == 0) {
  194. /* It is a subnode within a 'same' linked list and thus we can unlink it
  195. easily. */
  196. DEBUGASSERT(removenode->samen != removenode);
  197. if(removenode->samen == removenode)
  198. /* A non-subnode should never be set to SPLAY_SUBNODE */
  199. return 3;
  200. removenode->samep->samen = removenode->samen;
  201. removenode->samen->samep = removenode->samep;
  202. /* Ensures that double-remove gets caught. */
  203. removenode->samen = removenode;
  204. *newroot = t; /* return the same root */
  205. return 0;
  206. }
  207. t = Curl_splay(removenode->key, t);
  208. DEBUGASSERT(t);
  209. /* First make sure that we got the same root node as the one we want
  210. to remove, as otherwise we might be trying to remove a node that
  211. is not actually in the tree.
  212. We cannot just compare the keys here as a double remove in quick
  213. succession of a node with key != SPLAY_SUBNODE && same != NULL
  214. could return the same key but a different node. */
  215. DEBUGASSERT(t == removenode);
  216. if(t != removenode)
  217. return 2;
  218. /* Check if there is a list with identical sizes, as then we are trying to
  219. remove the root node of a list of nodes with identical keys. */
  220. x = t->samen;
  221. if(x != t) {
  222. /* 'x' is the new root node, we just make it use the root node's
  223. smaller/larger links */
  224. x->key = t->key;
  225. x->larger = t->larger;
  226. x->smaller = t->smaller;
  227. x->samep = t->samep;
  228. t->samep->samen = x;
  229. }
  230. else {
  231. /* Remove the root node */
  232. if(!t->smaller)
  233. x = t->larger;
  234. else {
  235. x = Curl_splay(removenode->key, t->smaller);
  236. DEBUGASSERT(x);
  237. x->larger = t->larger;
  238. }
  239. }
  240. *newroot = x; /* store new root pointer */
  241. return 0;
  242. }
  243. /* set and get the custom payload for this tree node */
  244. void Curl_splayset(struct Curl_tree *node, void *payload)
  245. {
  246. DEBUGASSERT(node);
  247. node->ptr = payload;
  248. }
  249. void *Curl_splayget(struct Curl_tree *node)
  250. {
  251. DEBUGASSERT(node);
  252. return node->ptr;
  253. }