splay.c 7.6 KB

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