2 * Based on strlist.c by:
3 * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
5 * Licensed under the GPLv2.
14 int rblist__add_node(struct rblist
*rblist
, const void *new_entry
)
16 struct rb_node
**p
= &rblist
->entries
.rb_root
.rb_node
;
17 struct rb_node
*parent
= NULL
, *new_node
;
25 rc
= rblist
->node_cmp(parent
, new_entry
);
36 new_node
= rblist
->node_new(rblist
, new_entry
);
40 rb_link_node(new_node
, parent
, p
);
41 rb_insert_color_cached(new_node
, &rblist
->entries
, leftmost
);
47 void rblist__remove_node(struct rblist
*rblist
, struct rb_node
*rb_node
)
49 rb_erase_cached(rb_node
, &rblist
->entries
);
51 rblist
->node_delete(rblist
, rb_node
);
54 static struct rb_node
*__rblist__findnew(struct rblist
*rblist
,
58 struct rb_node
**p
= &rblist
->entries
.rb_root
.rb_node
;
59 struct rb_node
*parent
= NULL
, *new_node
= NULL
;
67 rc
= rblist
->node_cmp(parent
, entry
);
79 new_node
= rblist
->node_new(rblist
, entry
);
81 rb_link_node(new_node
, parent
, p
);
82 rb_insert_color_cached(new_node
,
83 &rblist
->entries
, leftmost
);
91 struct rb_node
*rblist__find(struct rblist
*rblist
, const void *entry
)
93 return __rblist__findnew(rblist
, entry
, false);
96 struct rb_node
*rblist__findnew(struct rblist
*rblist
, const void *entry
)
98 return __rblist__findnew(rblist
, entry
, true);
101 void rblist__init(struct rblist
*rblist
)
103 if (rblist
!= NULL
) {
104 rblist
->entries
= RB_ROOT_CACHED
;
105 rblist
->nr_entries
= 0;
111 void rblist__exit(struct rblist
*rblist
)
113 struct rb_node
*pos
, *next
= rb_first_cached(&rblist
->entries
);
118 rblist__remove_node(rblist
, pos
);
122 void rblist__delete(struct rblist
*rblist
)
124 if (rblist
!= NULL
) {
125 rblist__exit(rblist
);
130 struct rb_node
*rblist__entry(const struct rblist
*rblist
, unsigned int idx
)
132 struct rb_node
*node
;
134 for (node
= rb_first_cached(&rblist
->entries
); node
;
135 node
= rb_next(node
)) {