_info 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <stdio.h>
  2. #include <string.h>
  3. /**
  4. * rbtree - talloc-aware Red Black Tree
  5. *
  6. * This is an implementation of a red-black tree based on talloc.
  7. * Talloc objects that are stored in the tree have nice properties
  8. * such as when the object is talloc_free()d, the object is also
  9. * automatically removed from the tree. This is done by making the
  10. * nodes of the tree child objects of the talloc object stored in the
  11. * tree, so that destructors are called to automatically remove the
  12. * node from the tree.
  13. *
  14. * The object stored in the tree does NOT become a child object of the
  15. * tree itself, so the same object can be stored under several keys at
  16. * the same time, and even in several different trees at the same
  17. * time.
  18. *
  19. * The example below is a trivial example program that shows how to
  20. * use trees that are keyed by a uint32_t. The rb_tree code also contains
  21. * support for managing trees that are keyed by an array of uint32. It
  22. * is trivial to expand this to "key as string". Just pad the string with
  23. * 0 to be a multiple of uint32_t and then chop it up as an array of
  24. * uint32_t.
  25. *
  26. * This code originates from ctdb, where talloc based trees keyed are
  27. * used in several places.
  28. *
  29. * License: GPL (3 or any later version)
  30. * Author: Ronnie Sahlberg <ronniesahlberg@gmail.com>
  31. *
  32. * Example:
  33. * #include <stdio.h>
  34. * #include <ccan/talloc/talloc.h>
  35. * #include <ccan/rbtree/rbtree.h>
  36. *
  37. * static void printtree(trbt_node_t *node, int levels)
  38. * {
  39. * int i;
  40. * if(node==NULL)return;
  41. * printtree(node->left, levels+1);
  42. * for(i=0;i<levels;i++)printf(" ");
  43. * printf("key:%d COLOR:%s\n",
  44. * node->key32, node->rb_color==TRBT_BLACK?"BLACK":"RED");
  45. * printtree(node->right, levels+1);
  46. * }
  47. *
  48. * static void print_tree(trbt_tree_t *tree)
  49. * {
  50. * if(tree->root==NULL){
  51. * printf("tree is empty\n");
  52. * return;
  53. * }
  54. * printf("---\n");
  55. * printtree(tree->root->left, 1);
  56. * printf("key:%d COLOR:%s\n", tree->root->key32,
  57. * tree->root->rb_color==TRBT_BLACK?"BLACK":"RED");
  58. * printtree(tree->root->right, 1);
  59. * printf("===\n");
  60. * }
  61. *
  62. * int main(int argc, char *argv[])
  63. * {
  64. * TALLOC_CTX *mem_ctx;
  65. * TALLOC_CTX *val;
  66. * int i;
  67. *
  68. * trbt_tree_t *tree;
  69. *
  70. * printf("Example of tree keyed by UINT32\n");
  71. * mem_ctx = talloc_new(NULL);
  72. *
  73. * // create a tree and store some talloc objects there
  74. * tree=trbt_create(mem_ctx, 0);
  75. * for (i=0; i<10; i++) {
  76. * val = talloc_asprintf(mem_ctx,
  77. * "Value string for key %d", i);
  78. * trbt_insert32(tree, i, val);
  79. * }
  80. * // show what the tree looks like
  81. * print_tree(tree);
  82. *
  83. * printf("Lookup item with key 7\n");
  84. * val = trbt_lookup32(tree, 7);
  85. * printf("Item with key:7 has value:%s\n", (char *)val);
  86. * printf("Talloc_free this item\n");
  87. * talloc_free(val);
  88. * printf("Item is automagically removed from the tree\n");
  89. * print_tree(tree);
  90. *
  91. * talloc_free(mem_ctx);
  92. * return 0;
  93. * }
  94. */
  95. int main(int argc, char *argv[])
  96. {
  97. if (argc != 2)
  98. return 1;
  99. if (strcmp(argv[1], "depends") == 0) {
  100. printf("ccan/talloc\n");
  101. return 0;
  102. }
  103. return 1;
  104. }