splay.c 7.5 KB

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