splay.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1997 - 2009, 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 http://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 "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. struct Curl_tree *Curl_splayinsert(struct timeval i,
  89. struct Curl_tree *t,
  90. struct Curl_tree *node)
  91. {
  92. static struct timeval KEY_NOTUSED = {-1,-1}; /* key that will *NEVER* appear */
  93. if(node == NULL)
  94. return t;
  95. if(t != NULL) {
  96. t = Curl_splay(i,t);
  97. if(compare(i, t->key)==0) {
  98. /* There already exists a node in the tree with the very same key. Build
  99. a linked list of nodes. We make the new 'node' struct the new master
  100. node and make the previous node the first one in the 'same' list. */
  101. node->same = t;
  102. node->key = i;
  103. node->smaller = t->smaller;
  104. node->larger = t->larger;
  105. t->smaller = node; /* in the sub node for this same key, we use the
  106. smaller pointer to point back to the master
  107. node */
  108. t->key = KEY_NOTUSED; /* and we set the key in the sub node to NOTUSED
  109. to quickly identify this node as a subnode */
  110. return node; /* new root node */
  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. node->same = NULL; /* no identical node (yet) */
  128. return node;
  129. }
  130. #if 0
  131. /* Deletes 'i' from the tree if it's there (with an exact match). Returns a
  132. pointer to the resulting tree.
  133. Function not used in libcurl.
  134. */
  135. struct Curl_tree *Curl_splayremove(struct timeval i,
  136. struct Curl_tree *t,
  137. struct Curl_tree **removed)
  138. {
  139. struct Curl_tree *x;
  140. *removed = NULL; /* default to no removed */
  141. if(t==NULL)
  142. return NULL;
  143. t = Curl_splay(i,t);
  144. if(compare(i, t->key) == 0) { /* found it */
  145. /* FIRST! Check if there is a list with identical sizes */
  146. if((x = t->same) != NULL) {
  147. /* there is, pick one from the list */
  148. /* 'x' is the new root node */
  149. x->key = t->key;
  150. x->larger = t->larger;
  151. x->smaller = t->smaller;
  152. *removed = t;
  153. return x; /* new root */
  154. }
  155. if(t->smaller == NULL) {
  156. x = t->larger;
  157. }
  158. else {
  159. x = Curl_splay(i, t->smaller);
  160. x->larger = t->larger;
  161. }
  162. *removed = t;
  163. return x;
  164. }
  165. else
  166. return t; /* It wasn't there */
  167. }
  168. #endif
  169. /* Finds and deletes the best-fit node from the tree. Return a pointer to the
  170. resulting tree. best-fit means the node with the given or lower key */
  171. struct Curl_tree *Curl_splaygetbest(struct timeval i,
  172. struct Curl_tree *t,
  173. struct Curl_tree **removed)
  174. {
  175. struct Curl_tree *x;
  176. if(!t) {
  177. *removed = NULL; /* none removed since there was no root */
  178. return NULL;
  179. }
  180. t = Curl_splay(i,t);
  181. if(compare(i, t->key) < 0) {
  182. /* too big node, try the smaller chain */
  183. if(t->smaller)
  184. t=Curl_splay(t->smaller->key, t);
  185. else {
  186. /* fail */
  187. *removed = NULL;
  188. return t;
  189. }
  190. }
  191. if(compare(i, t->key) >= 0) { /* found it */
  192. /* FIRST! Check if there is a list with identical keys */
  193. x = t->same;
  194. if(x) {
  195. /* there is, pick one from the list */
  196. /* 'x' is the new root node */
  197. x->key = t->key;
  198. x->larger = t->larger;
  199. x->smaller = t->smaller;
  200. *removed = t;
  201. return x; /* new root */
  202. }
  203. if(t->smaller == NULL) {
  204. x = t->larger;
  205. }
  206. else {
  207. x = Curl_splay(i, t->smaller);
  208. x->larger = t->larger;
  209. }
  210. *removed = t;
  211. return x;
  212. }
  213. else {
  214. *removed = NULL; /* no match */
  215. return t; /* It wasn't there */
  216. }
  217. }
  218. /* Deletes the very node we point out from the tree if it's there. Stores a
  219. pointer to the new resulting tree in 'newroot'.
  220. Returns zero on success and non-zero on errors! TODO: document error codes.
  221. When returning error, it does not touch the 'newroot' pointer.
  222. NOTE: when the last node of the tree is removed, there's no tree left so
  223. 'newroot' will be made to point to NULL.
  224. */
  225. int Curl_splayremovebyaddr(struct Curl_tree *t,
  226. struct Curl_tree *removenode,
  227. struct Curl_tree **newroot)
  228. {
  229. static struct timeval KEY_NOTUSED = {-1,-1}; /* key that will *NEVER* appear */
  230. struct Curl_tree *x;
  231. if(!t || !removenode)
  232. return 1;
  233. if(compare(KEY_NOTUSED, removenode->key) == 0) {
  234. /* Key set to NOTUSED means it is a subnode within a 'same' linked list
  235. and thus we can unlink it easily. The 'smaller' link of a subnode
  236. links to the parent node. */
  237. if(removenode->smaller == NULL)
  238. return 3;
  239. removenode->smaller->same = removenode->same;
  240. if(removenode->same)
  241. removenode->same->smaller = removenode->smaller;
  242. /* Ensures that double-remove gets caught. */
  243. removenode->smaller = NULL;
  244. /* voila, we're done! */
  245. *newroot = t; /* return the same root */
  246. return 0;
  247. }
  248. t = Curl_splay(removenode->key, t);
  249. /* First make sure that we got the same root node as the one we want
  250. to remove, as otherwise we might be trying to remove a node that
  251. isn't actually in the tree.
  252. We cannot just compare the keys here as a double remove in quick
  253. succession of a node with key != KEY_NOTUSED && same != NULL
  254. could return the same key but a different node. */
  255. if(t != removenode)
  256. return 2;
  257. /* Check if there is a list with identical sizes, as then we're trying to
  258. remove the root node of a list of nodes with identical keys. */
  259. x = t->same;
  260. if(x) {
  261. /* 'x' is the new root node, we just make it use the root node's
  262. smaller/larger links */
  263. x->key = t->key;
  264. x->larger = t->larger;
  265. x->smaller = t->smaller;
  266. }
  267. else {
  268. /* Remove the root node */
  269. if(t->smaller == NULL)
  270. x = t->larger;
  271. else {
  272. x = Curl_splay(removenode->key, t->smaller);
  273. x->larger = t->larger;
  274. }
  275. }
  276. *newroot = x; /* store new root pointer */
  277. return 0;
  278. }
  279. #ifdef DEBUGBUILD
  280. void Curl_splayprint(struct Curl_tree * t, int d, char output)
  281. {
  282. struct Curl_tree *node;
  283. int i;
  284. int count;
  285. if(t == NULL)
  286. return;
  287. Curl_splayprint(t->larger, d+1, output);
  288. for (i=0; i<d; i++)
  289. if(output)
  290. fprintf(stderr, " ");
  291. if(output) {
  292. #ifdef TEST_SPLAY
  293. fprintf(stderr, "%ld[%d]", (long)t->key.tv_usec, i);
  294. #else
  295. fprintf(stderr, "%ld.%ld[%d]", (long)t->key.tv_sec, (long)t->key.tv_usec, i);
  296. #endif
  297. }
  298. for(count=0, node = t->same; node; node = node->same, count++)
  299. ;
  300. if(output) {
  301. if(count)
  302. fprintf(stderr, " [%d more]\n", count);
  303. else
  304. fprintf(stderr, "\n");
  305. }
  306. Curl_splayprint(t->smaller, d+1, output);
  307. }
  308. #endif
  309. #ifdef TEST_SPLAY
  310. /*#define TEST2 */
  311. #define MAX 50
  312. #define TEST2
  313. /* A sample use of these functions. Start with the empty tree, insert some
  314. stuff into it, and then delete it */
  315. int main(int argc, argv_item_t argv[])
  316. {
  317. struct Curl_tree *root, *t;
  318. void *ptrs[MAX];
  319. int adds=0;
  320. int rc;
  321. static const long sizes[]={
  322. 50, 60, 50, 100, 60, 200, 120, 300, 400, 200, 256, 122, 60, 120, 200, 300,
  323. 220, 80, 90, 50, 100, 60, 200, 120, 300, 400, 200, 256, 122, 60, 120, 200,
  324. 300, 220, 80, 90, 50, 100, 60, 200, 120, 300, 400, 200, 256, 122, 60, 120,
  325. 200, 300, 220, 80, 90};
  326. int i;
  327. root = NULL; /* the empty tree */
  328. for (i = 0; i < MAX; i++) {
  329. struct timeval key;
  330. ptrs[i] = t = malloc(sizeof(struct Curl_tree));
  331. if(!t) {
  332. puts("out of memory!");
  333. return 0;
  334. }
  335. key.tv_sec = 0;
  336. #ifdef TEST2
  337. key.tv_usec = sizes[i];
  338. #elif defined(TEST1)
  339. key.tv_usec = (541*i)%1023;
  340. #elif defined(TEST3)
  341. key.tv_usec = 100;
  342. #endif
  343. t->payload = (void *)key.tv_usec; /* for simplicity */
  344. root = Curl_splayinsert(key, root, t);
  345. }
  346. #if 0
  347. puts("Result:");
  348. Curl_splayprint(root, 0, 1);
  349. #endif
  350. #if 1
  351. for (i = 0; i < MAX; i++) {
  352. int rem = (i+7)%MAX;
  353. struct Curl_tree *r;
  354. printf("Tree look:\n");
  355. Curl_splayprint(root, 0, 1);
  356. printf("remove pointer %d, payload %ld\n", rem,
  357. (long)((struct Curl_tree *)ptrs[rem])->payload);
  358. rc = Curl_splayremovebyaddr(root, (struct Curl_tree *)ptrs[rem], &root);
  359. if(rc)
  360. /* failed! */
  361. printf("remove %d failed!\n", rem);
  362. }
  363. #endif
  364. return 0;
  365. }
  366. #endif /* TEST_SPLAY */