2
0

splay.c 10 KB

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