rbtree.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. a talloc based red-black tree
  3. Copyright (C) Ronnie Sahlberg 2007
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <ccan/rbtree/rbtree.h>
  16. static void
  17. tree_destructor_traverse_node(trbt_node_t *node)
  18. {
  19. talloc_set_destructor(node, NULL);
  20. if (node->left) {
  21. tree_destructor_traverse_node(node->left);
  22. }
  23. if (node->right) {
  24. tree_destructor_traverse_node(node->right);
  25. }
  26. talloc_free(node);
  27. }
  28. /*
  29. destroy a tree and remove all its nodes
  30. */
  31. static int tree_destructor(trbt_tree_t *tree)
  32. {
  33. trbt_node_t *node;
  34. if (tree == NULL) {
  35. return 0;
  36. }
  37. node=tree->root;
  38. if (node == NULL) {
  39. return 0;
  40. }
  41. /* traverse the tree and remove the node destructor then delete it.
  42. we don't want to use the existing destructor for the node
  43. since that will remove the nodes one by one from the tree.
  44. since the entire tree will be completely destroyed we don't care
  45. if it is inconsistent or unbalanced while freeing the
  46. individual nodes
  47. */
  48. tree_destructor_traverse_node(node);
  49. return 0;
  50. }
  51. /* create a red black tree */
  52. trbt_tree_t *
  53. trbt_create(TALLOC_CTX *memctx, uint32_t flags)
  54. {
  55. trbt_tree_t *tree;
  56. tree = talloc_zero(memctx, trbt_tree_t);
  57. if (tree == NULL) {
  58. fprintf(stderr, "Failed to allocate memory for rb tree\n");
  59. return NULL;
  60. }
  61. /* If the tree is freed, we must walk over all entries and steal the
  62. node from the stored data pointer and release the node.
  63. Note, when we free the tree we only free the tree and not any of
  64. the data stored in the tree.
  65. */
  66. talloc_set_destructor(tree, tree_destructor);
  67. tree->flags = flags;
  68. return tree;
  69. }
  70. static inline trbt_node_t *
  71. trbt_parent(trbt_node_t *node)
  72. {
  73. return node->parent;
  74. }
  75. static inline trbt_node_t *
  76. trbt_grandparent(trbt_node_t *node)
  77. {
  78. trbt_node_t *parent;
  79. parent=trbt_parent(node);
  80. if(parent){
  81. return parent->parent;
  82. }
  83. return NULL;
  84. }
  85. static inline trbt_node_t *
  86. trbt_uncle(trbt_node_t *node)
  87. {
  88. trbt_node_t *parent, *grandparent;
  89. parent=trbt_parent(node);
  90. if(!parent){
  91. return NULL;
  92. }
  93. grandparent=trbt_parent(parent);
  94. if(!grandparent){
  95. return NULL;
  96. }
  97. if(parent==grandparent->left){
  98. return grandparent->right;
  99. }
  100. return grandparent->left;
  101. }
  102. static inline void trbt_insert_case1(trbt_tree_t *tree, trbt_node_t *node);
  103. static inline void trbt_insert_case2(trbt_tree_t *tree, trbt_node_t *node);
  104. static inline void
  105. trbt_rotate_left(trbt_node_t *node)
  106. {
  107. trbt_tree_t *tree = node->tree;
  108. if(node->parent){
  109. if(node->parent->left==node){
  110. node->parent->left=node->right;
  111. } else {
  112. node->parent->right=node->right;
  113. }
  114. } else {
  115. tree->root=node->right;
  116. }
  117. node->right->parent=node->parent;
  118. node->parent=node->right;
  119. node->right=node->right->left;
  120. if(node->right){
  121. node->right->parent=node;
  122. }
  123. node->parent->left=node;
  124. }
  125. static inline void
  126. trbt_rotate_right(trbt_node_t *node)
  127. {
  128. trbt_tree_t *tree = node->tree;
  129. if(node->parent){
  130. if(node->parent->left==node){
  131. node->parent->left=node->left;
  132. } else {
  133. node->parent->right=node->left;
  134. }
  135. } else {
  136. tree->root=node->left;
  137. }
  138. node->left->parent=node->parent;
  139. node->parent=node->left;
  140. node->left=node->left->right;
  141. if(node->left){
  142. node->left->parent=node;
  143. }
  144. node->parent->right=node;
  145. }
  146. /* NULL nodes are black by definition */
  147. static inline int trbt_get_color(trbt_node_t *node)
  148. {
  149. if (node==NULL) {
  150. return TRBT_BLACK;
  151. }
  152. return node->rb_color;
  153. }
  154. static inline int trbt_get_color_left(trbt_node_t *node)
  155. {
  156. if (node==NULL) {
  157. return TRBT_BLACK;
  158. }
  159. if (node->left==NULL) {
  160. return TRBT_BLACK;
  161. }
  162. return node->left->rb_color;
  163. }
  164. static inline int trbt_get_color_right(trbt_node_t *node)
  165. {
  166. if (node==NULL) {
  167. return TRBT_BLACK;
  168. }
  169. if (node->right==NULL) {
  170. return TRBT_BLACK;
  171. }
  172. return node->right->rb_color;
  173. }
  174. /* setting a NULL node to black is a nop */
  175. static inline void trbt_set_color(trbt_node_t *node, int color)
  176. {
  177. if ( (node==NULL) && (color==TRBT_BLACK) ) {
  178. return;
  179. }
  180. node->rb_color = color;
  181. }
  182. static inline void trbt_set_color_left(trbt_node_t *node, int color)
  183. {
  184. if ( ((node==NULL)||(node->left==NULL)) && (color==TRBT_BLACK) ) {
  185. return;
  186. }
  187. node->left->rb_color = color;
  188. }
  189. static inline void trbt_set_color_right(trbt_node_t *node, int color)
  190. {
  191. if ( ((node==NULL)||(node->right==NULL)) && (color==TRBT_BLACK) ) {
  192. return;
  193. }
  194. node->right->rb_color = color;
  195. }
  196. static inline void
  197. trbt_insert_case5(trbt_node_t *node)
  198. {
  199. trbt_node_t *grandparent;
  200. trbt_node_t *parent;
  201. parent=trbt_parent(node);
  202. grandparent=trbt_parent(parent);
  203. parent->rb_color=TRBT_BLACK;
  204. grandparent->rb_color=TRBT_RED;
  205. if( (node==parent->left) && (parent==grandparent->left) ){
  206. trbt_rotate_right(grandparent);
  207. } else {
  208. trbt_rotate_left(grandparent);
  209. }
  210. }
  211. static inline void
  212. trbt_insert_case4(trbt_node_t *node)
  213. {
  214. trbt_node_t *grandparent;
  215. trbt_node_t *parent;
  216. parent=trbt_parent(node);
  217. grandparent=trbt_parent(parent);
  218. if(!grandparent){
  219. return;
  220. }
  221. if( (node==parent->right) && (parent==grandparent->left) ){
  222. trbt_rotate_left(parent);
  223. node=node->left;
  224. } else if( (node==parent->left) && (parent==grandparent->right) ){
  225. trbt_rotate_right(parent);
  226. node=node->right;
  227. }
  228. trbt_insert_case5(node);
  229. }
  230. static inline void
  231. trbt_insert_case3(trbt_tree_t *tree, trbt_node_t *node)
  232. {
  233. trbt_node_t *grandparent;
  234. trbt_node_t *parent;
  235. trbt_node_t *uncle;
  236. uncle=trbt_uncle(node);
  237. if(uncle && (uncle->rb_color==TRBT_RED)){
  238. parent=trbt_parent(node);
  239. parent->rb_color=TRBT_BLACK;
  240. uncle->rb_color=TRBT_BLACK;
  241. grandparent=trbt_grandparent(node);
  242. grandparent->rb_color=TRBT_RED;
  243. trbt_insert_case1(tree, grandparent);
  244. } else {
  245. trbt_insert_case4(node);
  246. }
  247. }
  248. static inline void
  249. trbt_insert_case2(trbt_tree_t *tree, trbt_node_t *node)
  250. {
  251. trbt_node_t *parent;
  252. parent=trbt_parent(node);
  253. /* parent is always non-NULL here */
  254. if(parent->rb_color==TRBT_BLACK){
  255. return;
  256. }
  257. trbt_insert_case3(tree, node);
  258. }
  259. static inline void
  260. trbt_insert_case1(trbt_tree_t *tree, trbt_node_t *node)
  261. {
  262. trbt_node_t *parent;
  263. parent=trbt_parent(node);
  264. if(!parent){
  265. node->rb_color=TRBT_BLACK;
  266. return;
  267. }
  268. trbt_insert_case2(tree, node);
  269. }
  270. static inline trbt_node_t *
  271. trbt_sibling(trbt_node_t *node)
  272. {
  273. trbt_node_t *parent;
  274. parent=trbt_parent(node);
  275. if(!parent){
  276. return NULL;
  277. }
  278. if (node == parent->left) {
  279. return parent->right;
  280. } else {
  281. return parent->left;
  282. }
  283. }
  284. static inline void
  285. trbt_delete_case6(trbt_node_t *node)
  286. {
  287. trbt_node_t *sibling, *parent;
  288. sibling = trbt_sibling(node);
  289. parent = trbt_parent(node);
  290. trbt_set_color(sibling, parent->rb_color);
  291. trbt_set_color(parent, TRBT_BLACK);
  292. if (node == parent->left) {
  293. trbt_set_color_right(sibling, TRBT_BLACK);
  294. trbt_rotate_left(parent);
  295. } else {
  296. trbt_set_color_left(sibling, TRBT_BLACK);
  297. trbt_rotate_right(parent);
  298. }
  299. }
  300. static inline void
  301. trbt_delete_case5(trbt_node_t *node)
  302. {
  303. trbt_node_t *parent, *sibling;
  304. parent = trbt_parent(node);
  305. sibling = trbt_sibling(node);
  306. if ( (node == parent->left)
  307. &&(trbt_get_color(sibling) == TRBT_BLACK)
  308. &&(trbt_get_color_left(sibling) == TRBT_RED)
  309. &&(trbt_get_color_right(sibling) == TRBT_BLACK) ){
  310. trbt_set_color(sibling, TRBT_RED);
  311. trbt_set_color_left(sibling, TRBT_BLACK);
  312. trbt_rotate_right(sibling);
  313. trbt_delete_case6(node);
  314. return;
  315. }
  316. if ( (node == parent->right)
  317. &&(trbt_get_color(sibling) == TRBT_BLACK)
  318. &&(trbt_get_color_right(sibling) == TRBT_RED)
  319. &&(trbt_get_color_left(sibling) == TRBT_BLACK) ){
  320. trbt_set_color(sibling, TRBT_RED);
  321. trbt_set_color_right(sibling, TRBT_BLACK);
  322. trbt_rotate_left(sibling);
  323. trbt_delete_case6(node);
  324. return;
  325. }
  326. trbt_delete_case6(node);
  327. }
  328. static inline void
  329. trbt_delete_case4(trbt_node_t *node)
  330. {
  331. trbt_node_t *sibling;
  332. sibling = trbt_sibling(node);
  333. if ( (trbt_get_color(node->parent) == TRBT_RED)
  334. &&(trbt_get_color(sibling) == TRBT_BLACK)
  335. &&(trbt_get_color_left(sibling) == TRBT_BLACK)
  336. &&(trbt_get_color_right(sibling) == TRBT_BLACK) ){
  337. trbt_set_color(sibling, TRBT_RED);
  338. trbt_set_color(node->parent, TRBT_BLACK);
  339. } else {
  340. trbt_delete_case5(node);
  341. }
  342. }
  343. static void trbt_delete_case1(trbt_node_t *node);
  344. static inline void
  345. trbt_delete_case3(trbt_node_t *node)
  346. {
  347. trbt_node_t *sibling;
  348. sibling = trbt_sibling(node);
  349. if ( (trbt_get_color(node->parent) == TRBT_BLACK)
  350. &&(trbt_get_color(sibling) == TRBT_BLACK)
  351. &&(trbt_get_color_left(sibling) == TRBT_BLACK)
  352. &&(trbt_get_color_right(sibling) == TRBT_BLACK) ){
  353. trbt_set_color(sibling, TRBT_RED);
  354. trbt_delete_case1(node->parent);
  355. } else {
  356. trbt_delete_case4(node);
  357. }
  358. }
  359. static inline void
  360. trbt_delete_case2(trbt_node_t *node)
  361. {
  362. trbt_node_t *sibling;
  363. sibling = trbt_sibling(node);
  364. if (trbt_get_color(sibling) == TRBT_RED) {
  365. trbt_set_color(node->parent, TRBT_RED);
  366. trbt_set_color(sibling, TRBT_BLACK);
  367. if (node == node->parent->left) {
  368. trbt_rotate_left(node->parent);
  369. } else {
  370. trbt_rotate_right(node->parent);
  371. }
  372. }
  373. trbt_delete_case3(node);
  374. }
  375. static void
  376. trbt_delete_case1(trbt_node_t *node)
  377. {
  378. if (!node->parent) {
  379. return;
  380. } else {
  381. trbt_delete_case2(node);
  382. }
  383. }
  384. static void
  385. delete_node(trbt_node_t *node)
  386. {
  387. trbt_node_t *parent, *child, dc;
  388. trbt_node_t *temp = NULL;
  389. /* This node has two child nodes, then just copy the content
  390. from the next smaller node with this node and delete the
  391. predecessor instead.
  392. The predecessor is guaranteed to have at most one child
  393. node since its right arm must be NULL
  394. (It must be NULL since we are its sucessor and we are above
  395. it in the tree)
  396. */
  397. if (node->left != NULL && node->right != NULL) {
  398. /* This node has two children, just copy the data */
  399. /* find the predecessor */
  400. temp = node->left;
  401. while (temp->right != NULL) {
  402. temp = temp->right;
  403. }
  404. /* swap the predecessor data and key with the node to
  405. be deleted.
  406. */
  407. node->key32 = temp->key32;
  408. node->data = temp->data;
  409. /* now we let node hang off the new data */
  410. talloc_steal(node->data, node);
  411. temp->data = NULL;
  412. temp->key32 = -1;
  413. /* then delete the temp node.
  414. this node is guaranteed to have at least one leaf
  415. child */
  416. delete_node(temp);
  417. goto finished;
  418. }
  419. /* There is at most one child to this node to be deleted */
  420. child = node->left;
  421. if (node->right) {
  422. child = node->right;
  423. }
  424. /* If the node to be deleted did not have any child at all we
  425. create a temporary dummy node for the child and mark it black.
  426. Once the delete of the node is finished, we remove this dummy
  427. node, which is simple to do since it is guaranteed that it will
  428. still not have any children after the delete operation.
  429. This is because we don't represent the leaf-nodes as actual nodes
  430. in this implementation.
  431. */
  432. if (!child) {
  433. child = &dc;
  434. child->tree = node->tree;
  435. child->left=NULL;
  436. child->right=NULL;
  437. child->rb_color=TRBT_BLACK;
  438. child->data=NULL;
  439. }
  440. /* replace node with child */
  441. parent = trbt_parent(node);
  442. if (parent) {
  443. if (parent->left == node) {
  444. parent->left = child;
  445. } else {
  446. parent->right = child;
  447. }
  448. } else {
  449. node->tree->root = child;
  450. }
  451. child->parent = node->parent;
  452. if (node->rb_color == TRBT_BLACK) {
  453. if (trbt_get_color(child) == TRBT_RED) {
  454. child->rb_color = TRBT_BLACK;
  455. } else {
  456. trbt_delete_case1(child);
  457. }
  458. }
  459. /* If we had to create a temporary dummy node to represent a black
  460. leaf child we now has to delete it.
  461. This is simple since this dummy node originally had no children
  462. and we are guaranteed that it will also not have any children
  463. after the node has been deleted and any possible rotations
  464. have occurred.
  465. The only special case is if this was the last node of the tree
  466. in which case we have to reset the root to NULL as well.
  467. Othervise it is enough to just unlink the child from its new
  468. parent.
  469. */
  470. if (child == &dc) {
  471. if (child->parent == NULL) {
  472. node->tree->root = NULL;
  473. } else if (child == child->parent->left) {
  474. child->parent->left = NULL;
  475. } else {
  476. child->parent->right = NULL;
  477. }
  478. }
  479. finished:
  480. /* if we came from a destructor and temp!=NULL this means we
  481. did the node-swap but now the tree still contains the old
  482. node which was freed in the destructor. Not good.
  483. */
  484. if (temp) {
  485. temp->key32 = node->key32;
  486. temp->rb_color = node->rb_color;
  487. temp->data = node->data;
  488. talloc_steal(temp->data, temp);
  489. temp->parent = node->parent;
  490. if (temp->parent) {
  491. if (temp->parent->left == node) {
  492. temp->parent->left = temp;
  493. } else {
  494. temp->parent->right = temp;
  495. }
  496. }
  497. temp->left = node->left;
  498. if (temp->left) {
  499. temp->left->parent = temp;
  500. }
  501. temp->right = node->right;
  502. if (temp->right) {
  503. temp->right->parent = temp;
  504. }
  505. if (temp->tree->root == node) {
  506. temp->tree->root = temp;
  507. }
  508. }
  509. if ( (node->tree->flags & TRBT_AUTOFREE)
  510. && (node->tree->root == NULL) ) {
  511. talloc_free(node->tree);
  512. }
  513. return;
  514. }
  515. /*
  516. destroy a node and remove it from its tree
  517. */
  518. static int node_destructor(trbt_node_t *node)
  519. {
  520. delete_node(node);
  521. return 0;
  522. }
  523. static inline trbt_node_t *
  524. trbt_create_node(trbt_tree_t *tree, trbt_node_t *parent, uint32_t key, void *data)
  525. {
  526. trbt_node_t *node;
  527. node=talloc_zero(tree, trbt_node_t);
  528. if (node == NULL) {
  529. fprintf(stderr, "Failed to allocate memory for rb node\n");
  530. return NULL;
  531. }
  532. node->tree=tree;
  533. node->rb_color=TRBT_BLACK;
  534. node->parent=parent;
  535. node->left=NULL;
  536. node->right=NULL;
  537. node->key32=key;
  538. node->data = data;
  539. /* let this node hang off data so that it is removed when
  540. data is freed
  541. */
  542. talloc_steal(data, node);
  543. talloc_set_destructor(node, node_destructor);
  544. return node;
  545. }
  546. /* insert a new node in the tree.
  547. if there is already a node with a matching key in the tree
  548. we replace it with the new data and return a pointer to the old data
  549. in case the caller wants to take any special action
  550. */
  551. void *
  552. trbt_insert32(trbt_tree_t *tree, uint32_t key, void *data)
  553. {
  554. trbt_node_t *node;
  555. node=tree->root;
  556. /* is this the first node ?*/
  557. if(!node){
  558. node = trbt_create_node(tree, NULL, key, data);
  559. tree->root=node;
  560. return NULL;
  561. }
  562. /* it was not the new root so walk the tree until we find where to
  563. * insert this new leaf.
  564. */
  565. while(1){
  566. /* this node already exists, replace data and return the
  567. old data
  568. */
  569. if(key==node->key32){
  570. void *old_data;
  571. old_data = node->data;
  572. node->data = data;
  573. /* Let the node now be owned by the new data
  574. so the node is freed when the enw data is released
  575. */
  576. talloc_steal(node->data, node);
  577. return old_data;
  578. }
  579. if(key<node->key32) {
  580. if(!node->left){
  581. /* new node to the left */
  582. trbt_node_t *new_node;
  583. new_node = trbt_create_node(tree, node, key, data);
  584. if (!new_node)
  585. return NULL;
  586. node->left=new_node;
  587. node=new_node;
  588. break;
  589. }
  590. node=node->left;
  591. continue;
  592. }
  593. if(key>node->key32) {
  594. if(!node->right){
  595. /* new node to the right */
  596. trbt_node_t *new_node;
  597. new_node = trbt_create_node(tree, node, key, data);
  598. if (!new_node)
  599. return NULL;
  600. node->right=new_node;
  601. node=new_node;
  602. break;
  603. }
  604. node=node->right;
  605. continue;
  606. }
  607. }
  608. /* node will now point to the newly created node */
  609. node->rb_color=TRBT_RED;
  610. trbt_insert_case1(tree, node);
  611. return NULL;
  612. }
  613. void *
  614. trbt_lookup32(trbt_tree_t *tree, uint32_t key)
  615. {
  616. trbt_node_t *node;
  617. node=tree->root;
  618. while(node){
  619. if(key==node->key32){
  620. return node->data;
  621. }
  622. if(key<node->key32){
  623. node=node->left;
  624. continue;
  625. }
  626. if(key>node->key32){
  627. node=node->right;
  628. continue;
  629. }
  630. }
  631. return NULL;
  632. }
  633. /* This deletes a node from the tree.
  634. Note that this does not release the data that the node points to
  635. */
  636. void
  637. trbt_delete32(trbt_tree_t *tree, uint32_t key)
  638. {
  639. trbt_node_t *node;
  640. node=tree->root;
  641. while(node){
  642. if(key==node->key32){
  643. talloc_free(node);
  644. return;
  645. }
  646. if(key<node->key32){
  647. node=node->left;
  648. continue;
  649. }
  650. if(key>node->key32){
  651. node=node->right;
  652. continue;
  653. }
  654. }
  655. }
  656. void
  657. trbt_insert32_callback(trbt_tree_t *tree, uint32_t key, void *(*callback)(void *param, void *data), void *param)
  658. {
  659. trbt_node_t *node;
  660. node=tree->root;
  661. /* is this the first node ?*/
  662. if(!node){
  663. node = trbt_create_node(tree, NULL, key,
  664. callback(param, NULL));
  665. tree->root=node;
  666. return;
  667. }
  668. /* it was not the new root so walk the tree until we find where to
  669. * insert this new leaf.
  670. */
  671. while(1){
  672. /* this node already exists, replace it
  673. */
  674. if(key==node->key32){
  675. node->data = callback(param, node->data);
  676. talloc_steal(node->data, node);
  677. return;
  678. }
  679. if(key<node->key32) {
  680. if(!node->left){
  681. /* new node to the left */
  682. trbt_node_t *new_node;
  683. new_node = trbt_create_node(tree, node, key,
  684. callback(param, NULL));
  685. node->left=new_node;
  686. node=new_node;
  687. break;
  688. }
  689. node=node->left;
  690. continue;
  691. }
  692. if(key>node->key32) {
  693. if(!node->right){
  694. /* new node to the right */
  695. trbt_node_t *new_node;
  696. new_node = trbt_create_node(tree, node, key,
  697. callback(param, NULL));
  698. node->right=new_node;
  699. node=new_node;
  700. break;
  701. }
  702. node=node->right;
  703. continue;
  704. }
  705. }
  706. /* node will now point to the newly created node */
  707. node->rb_color=TRBT_RED;
  708. trbt_insert_case1(tree, node);
  709. return;
  710. }
  711. struct trbt_array_param {
  712. void *(*callback)(void *param, void *data);
  713. void *param;
  714. uint32_t keylen;
  715. uint32_t *key;
  716. trbt_tree_t *tree;
  717. };
  718. static void *array_insert_callback(void *p, void *data)
  719. {
  720. struct trbt_array_param *param = (struct trbt_array_param *)p;
  721. trbt_tree_t *tree = NULL;
  722. /* if keylen has reached 0 we are done and can call the users
  723. callback function with the users parameters
  724. */
  725. if (param->keylen == 0) {
  726. return param->callback(param->param, data);
  727. }
  728. /* keylen is not zero yes so we must create/process more subtrees */
  729. /* if data is NULL this means we did not yet have a subtree here
  730. and we must create one.
  731. */
  732. if (data == NULL) {
  733. /* create a new subtree and hang it off our current tree
  734. set it to autofree so that the tree is freed when
  735. the last node in it has been released.
  736. */
  737. tree = trbt_create(param->tree, TRBT_AUTOFREE);
  738. } else {
  739. /* we already have a subtree for this path */
  740. tree = (trbt_tree_t *)data;
  741. }
  742. trbt_insertarray32_callback(tree, param->keylen, param->key, param->callback, param->param);
  743. /* now return either the old tree we got in *data or the new tree
  744. we created to our caller so he can update his pointer in his
  745. tree to point to our subtree
  746. */
  747. return tree;
  748. }
  749. /* insert into the tree using an array of uint32 as a key */
  750. void
  751. trbt_insertarray32_callback(trbt_tree_t *tree, uint32_t keylen, uint32_t *key, void *(*cb)(void *param, void *data), void *pm)
  752. {
  753. struct trbt_array_param tap;
  754. /* keylen-1 and key[1] since the call to insert32 will consume the
  755. first part of the key.
  756. */
  757. tap.callback= cb;
  758. tap.param = pm;
  759. tap.keylen = keylen-1;
  760. tap.key = &key[1];
  761. tap.tree = tree;
  762. trbt_insert32_callback(tree, key[0], array_insert_callback, &tap);
  763. }
  764. /* lookup the tree using an array of uint32 as a key */
  765. void *
  766. trbt_lookuparray32(trbt_tree_t *tree, uint32_t keylen, uint32_t *key)
  767. {
  768. /* if keylen is 1 we can do a regular lookup and return this to the
  769. user
  770. */
  771. if (keylen == 1) {
  772. return trbt_lookup32(tree, key[0]);
  773. }
  774. /* we need to lookup the next subtree */
  775. tree = trbt_lookup32(tree, key[0]);
  776. if (tree == NULL) {
  777. /* the key does not exist, return NULL */
  778. return NULL;
  779. }
  780. /* now lookup the next part of the key in our new tree */
  781. return trbt_lookuparray32(tree, keylen-1, &key[1]);
  782. }
  783. /* traverse a tree starting at node */
  784. static void
  785. trbt_traversearray32_node(trbt_node_t *node, uint32_t keylen,
  786. void (*callback)(void *param, void *data),
  787. void *param)
  788. {
  789. if (node->left) {
  790. trbt_traversearray32_node(node->left, keylen, callback, param);
  791. }
  792. /* this is the smallest node in this subtree
  793. if keylen is 0 this means we can just call the callback
  794. otherwise we must pull the next subtree and traverse that one as well
  795. */
  796. if (keylen == 0) {
  797. callback(param, node->data);
  798. } else {
  799. trbt_traversearray32(node->data, keylen, callback, param);
  800. }
  801. if (node->right) {
  802. trbt_traversearray32_node(node->right, keylen, callback, param);
  803. }
  804. }
  805. /* traverse the tree using an array of uint32 as a key */
  806. void
  807. trbt_traversearray32(trbt_tree_t *tree, uint32_t keylen,
  808. void (*callback)(void *param, void *data),
  809. void *param)
  810. {
  811. trbt_node_t *node;
  812. if (tree == NULL) {
  813. return;
  814. }
  815. node=tree->root;
  816. if (node == NULL) {
  817. return;
  818. }
  819. trbt_traversearray32_node(node, keylen-1, callback, param);
  820. }
  821. /* this function will return the first node in a tree where
  822. the key is an array of uint32_t
  823. */
  824. void *
  825. trbt_findfirstarray32(trbt_tree_t *tree, uint32_t keylen)
  826. {
  827. trbt_node_t *node;
  828. if (keylen < 1) {
  829. return NULL;
  830. }
  831. if (tree == NULL) {
  832. return NULL;
  833. }
  834. node=tree->root;
  835. if (node == NULL) {
  836. return NULL;
  837. }
  838. while (node->left) {
  839. node = node->left;
  840. }
  841. /* we found our node so return the data */
  842. if (keylen == 1) {
  843. return node->data;
  844. }
  845. /* we are still traversing subtrees so find the first node in the
  846. next level of trees
  847. */
  848. return trbt_findfirstarray32(node->data, keylen-1);
  849. }