rblist.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Based on strlist.c by:
  3. * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
  4. *
  5. * Licensed under the GPLv2.
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "rblist.h"
  11. int rblist__add_node(struct rblist *rblist, const void *new_entry)
  12. {
  13. struct rb_node **p = &rblist->entries.rb_node;
  14. struct rb_node *parent = NULL, *new_node;
  15. while (*p != NULL) {
  16. int rc;
  17. parent = *p;
  18. rc = rblist->node_cmp(parent, new_entry);
  19. if (rc > 0)
  20. p = &(*p)->rb_left;
  21. else if (rc < 0)
  22. p = &(*p)->rb_right;
  23. else
  24. return -EEXIST;
  25. }
  26. new_node = rblist->node_new(rblist, new_entry);
  27. if (new_node == NULL)
  28. return -ENOMEM;
  29. rb_link_node(new_node, parent, p);
  30. rb_insert_color(new_node, &rblist->entries);
  31. ++rblist->nr_entries;
  32. return 0;
  33. }
  34. void rblist__remove_node(struct rblist *rblist, struct rb_node *rb_node)
  35. {
  36. rb_erase(rb_node, &rblist->entries);
  37. --rblist->nr_entries;
  38. rblist->node_delete(rblist, rb_node);
  39. }
  40. static struct rb_node *__rblist__findnew(struct rblist *rblist,
  41. const void *entry,
  42. bool create)
  43. {
  44. struct rb_node **p = &rblist->entries.rb_node;
  45. struct rb_node *parent = NULL, *new_node = NULL;
  46. while (*p != NULL) {
  47. int rc;
  48. parent = *p;
  49. rc = rblist->node_cmp(parent, entry);
  50. if (rc > 0)
  51. p = &(*p)->rb_left;
  52. else if (rc < 0)
  53. p = &(*p)->rb_right;
  54. else
  55. return parent;
  56. }
  57. if (create) {
  58. new_node = rblist->node_new(rblist, entry);
  59. if (new_node) {
  60. rb_link_node(new_node, parent, p);
  61. rb_insert_color(new_node, &rblist->entries);
  62. ++rblist->nr_entries;
  63. }
  64. }
  65. return new_node;
  66. }
  67. struct rb_node *rblist__find(struct rblist *rblist, const void *entry)
  68. {
  69. return __rblist__findnew(rblist, entry, false);
  70. }
  71. struct rb_node *rblist__findnew(struct rblist *rblist, const void *entry)
  72. {
  73. return __rblist__findnew(rblist, entry, true);
  74. }
  75. void rblist__init(struct rblist *rblist)
  76. {
  77. if (rblist != NULL) {
  78. rblist->entries = RB_ROOT;
  79. rblist->nr_entries = 0;
  80. }
  81. return;
  82. }
  83. void rblist__delete(struct rblist *rblist)
  84. {
  85. if (rblist != NULL) {
  86. struct rb_node *pos, *next = rb_first(&rblist->entries);
  87. while (next) {
  88. pos = next;
  89. next = rb_next(pos);
  90. rblist__remove_node(rblist, pos);
  91. }
  92. free(rblist);
  93. }
  94. }
  95. struct rb_node *rblist__entry(const struct rblist *rblist, unsigned int idx)
  96. {
  97. struct rb_node *node;
  98. for (node = rb_first(&rblist->entries); node; node = rb_next(node)) {
  99. if (!idx--)
  100. return node;
  101. }
  102. return NULL;
  103. }