splay.c 11 KB

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