splay.c 7.5 KB

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