libdict 1.0.7
Data Structure C Library
Loading...
Searching...
No Matches
rb_tree.h
Go to the documentation of this file.
1/*
2 * libdict -- red-black tree interface.
3 *
4 * Copyright (c) 2001-2014, Farooq Mela
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef LIBDICT_RB_TREE_H__
29#define LIBDICT_RB_TREE_H__
30
31#include "dict.h"
32
43
45typedef struct rb_tree rb_tree;
46
57size_t rb_tree_free(rb_tree* tree, dict_delete_func delete_func);
58
66 rb_tree_insert(rb_tree* tree, void* key);
69void** rb_tree_search(rb_tree* tree, const void* key);
72void** rb_tree_search_le(rb_tree* tree, const void* key);
75void** rb_tree_search_lt(rb_tree* tree, const void* key);
78void** rb_tree_search_ge(rb_tree* tree, const void* key);
81void** rb_tree_search_gt(rb_tree* tree, const void* key);
87 rb_tree_remove(rb_tree* tree, const void* key);
92size_t rb_tree_clear(rb_tree* tree, dict_delete_func delete_func);
95size_t rb_tree_traverse(rb_tree* tree, dict_visit_func visit, void* user_data);
99bool rb_tree_select(rb_tree* tree, size_t n, const void** key, void** datum);
101size_t rb_tree_count(const rb_tree* tree);
116bool rb_tree_verify(const rb_tree* tree);
117
119typedef struct rb_itor rb_itor;
120
128void rb_itor_free(rb_itor* itor);
129
131bool rb_itor_valid(const rb_itor* itor);
133void rb_itor_invalidate(rb_itor* itor);
137bool rb_itor_next(rb_itor* itor);
141bool rb_itor_prev(rb_itor* itor);
144bool rb_itor_nextn(rb_itor* itor, size_t count);
147bool rb_itor_prevn(rb_itor* itor, size_t count);
150bool rb_itor_first(rb_itor* itor);
153bool rb_itor_last(rb_itor* itor);
156bool rb_itor_search(rb_itor* itor, const void* key);
159bool rb_itor_search_le(rb_itor* itor, const void* key);
162bool rb_itor_search_lt(rb_itor* itor, const void* key);
165bool rb_itor_search_ge(rb_itor* itor, const void* key);
168bool rb_itor_search_gt(rb_itor* itor, const void* key);
172const void* rb_itor_key(const rb_itor* itor);
175void** rb_itor_datum(rb_itor* itor);
180int rb_itor_compare(const rb_itor* i1, const rb_itor* i2);
185bool rb_itor_remove(rb_itor* itor);
186
188
189#endif /* !LIBDICT_RB_TREE_H__ */
#define BEGIN_DECL
Definition dict.h:42
int(* dict_compare_func)(const void *, const void *)
A pointer to a function that compares two keys.
Definition dict.h:59
void(* dict_delete_func)(void *, void *)
A pointer to a function that is called when a key-value pair gets removed from a dictionary.
Definition dict.h:62
#define END_DECL
Definition dict.h:43
bool(* dict_visit_func)(const void *, void *, void *)
A pointer to a function used for iterating over dictionary contents.
Definition dict.h:64
void ** rb_tree_search_lt(rb_tree *tree, const void *key)
Return the address of the datum of the greatest key less than |key|, or NULL if there is none.
Definition rb_tree.c:224
size_t rb_tree_min_path_length(const rb_tree *tree)
Return the shallowest depth at which a child link is absent, the root being at depth 1,...
Definition rb_tree.c:433
bool rb_itor_search_lt(rb_itor *itor, const void *key)
Position |itor| on the greatest key less than |key| and return true, or unposition it and return fals...
Definition rb_tree.c:699
bool rb_itor_search_ge(rb_itor *itor, const void *key)
Position |itor| on the least key greater than or equal to |key| and return true, or unposition it and...
Definition rb_tree.c:700
const void * rb_itor_key(const rb_itor *itor)
Return the key |itor| is positioned on, or NULL if it is not positioned.
Definition rb_tree.c:702
rb_tree * rb_tree_new(dict_compare_func cmp_func)
Create an empty tree that orders keys with |cmp_func| (must not be NULL).
Definition rb_tree.c:163
bool rb_itor_next(rb_itor *itor)
Move |itor| to the next element in key order and return true, or unposition it and return false if th...
Definition rb_tree.c:660
bool rb_itor_valid(const rb_itor *itor)
Return whether |itor| is positioned on an element.
Definition rb_tree.c:656
void ** rb_tree_search_ge(rb_tree *tree, const void *key)
Return the address of the datum of the least key greater than or equal to |key|, or NULL if there is ...
Definition rb_tree.c:225
dict_itor * rb_dict_itor_new(rb_tree *tree)
Like rb_itor_new(), but wrapped in a generic dict_itor for use with the dict_itor_* macros.
Definition rb_tree.c:642
size_t rb_tree_traverse(rb_tree *tree, dict_visit_func visit, void *user_data)
Call |visit|(key, datum, |user_data|) for each element in key order, stopping early if |visit| return...
Definition rb_tree.c:438
size_t rb_tree_total_path_length(const rb_tree *tree)
Return the sum of the depths of all nodes, the root being at depth 1, or 0 for an empty tree.
Definition rb_tree.c:435
void ** rb_tree_search(rb_tree *tree, const void *key)
Return the address of the datum stored for |key|, or NULL if |key| is not present.
Definition rb_tree.c:222
bool rb_itor_search_le(rb_itor *itor, const void *key)
Position |itor| on the greatest key less than or equal to |key| and return true, or unposition it and...
Definition rb_tree.c:698
void rb_itor_invalidate(rb_itor *itor)
Unposition |itor|, so that it is not positioned on any element.
Definition rb_tree.c:657
size_t rb_tree_clear(rb_tree *tree, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL.
Definition rb_tree.c:199
size_t rb_tree_max_path_length(const rb_tree *tree)
Return the greatest depth of any node, the root being at depth 1, or 0 for an empty tree: the height ...
Definition rb_tree.c:434
rb_itor * rb_itor_new(rb_tree *tree)
Create an iterator over |tree| that is not positioned on any element yet.
Definition rb_tree.c:631
bool rb_itor_prevn(rb_itor *itor, size_t count)
Move |itor| |count| elements back and return whether it is still positioned.
Definition rb_tree.c:686
bool rb_itor_search(rb_itor *itor, const void *key)
Position |itor| on |key| and return true, or unposition it and return false if |key| is not present.
Definition rb_tree.c:697
bool rb_itor_prev(rb_itor *itor)
Move |itor| to the previous element in key order and return true, or unposition it and return false i...
Definition rb_tree.c:668
bool rb_itor_remove(rb_itor *itor)
Remove the element |itor| is positioned on and unposition |itor|; return false if it was not position...
Definition rb_tree.c:707
void ** rb_itor_datum(rb_itor *itor)
Return the address of the datum |itor| is positioned on, through which it can be read or replaced,...
Definition rb_tree.c:703
bool rb_itor_first(rb_itor *itor)
Position |itor| on the first element in key order and return true, or unposition it and return false ...
Definition rb_tree.c:695
size_t rb_tree_free(rb_tree *tree, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL,...
Definition rb_tree.c:192
bool rb_itor_nextn(rb_itor *itor, size_t count)
Move |itor| |count| elements forward and return whether it is still positioned.
Definition rb_tree.c:676
void rb_itor_free(rb_itor *itor)
Free an iterator.
Definition rb_tree.c:655
bool rb_itor_last(rb_itor *itor)
Position |itor| on the last element in key order and return true, or unposition it and return false i...
Definition rb_tree.c:696
dict_insert_result rb_tree_insert(rb_tree *tree, void *key)
Insert |key| unless an equal key is present.
Definition rb_tree.c:229
bool rb_tree_verify(const rb_tree *tree)
Check the internal invariants of the tree and return true if they hold, or print the first failed che...
Definition rb_tree.c:609
void ** rb_tree_search_le(rb_tree *tree, const void *key)
Return the address of the datum of the greatest key less than or equal to |key|, or NULL if there is ...
Definition rb_tree.c:223
size_t rb_tree_count(const rb_tree *tree)
Return the number of elements.
Definition rb_tree.c:432
dict * rb_dict_new(dict_compare_func cmp_func)
Like rb_tree_new(), but wrapped in a generic dict for use with the dict_* macros.
Definition rb_tree.c:178
dict_remove_result rb_tree_remove(rb_tree *tree, const void *key)
Remove the element with key |key|.
Definition rb_tree.c:346
bool rb_tree_select(rb_tree *tree, size_t n, const void **key, void **datum)
Store the key and datum of the |n|th element in key order (0-based) through |key| and |datum| and ret...
Definition rb_tree.c:453
bool rb_itor_search_gt(rb_itor *itor, const void *key)
Position |itor| on the least key greater than |key| and return true, or unposition it and return fals...
Definition rb_tree.c:701
void ** rb_tree_search_gt(rb_tree *tree, const void *key)
Return the address of the datum of the least key greater than |key|, or NULL if there is none.
Definition rb_tree.c:226
int rb_itor_compare(const rb_itor *i1, const rb_itor *i2)
Compare the positions of two iterators over the same tree: return a negative value,...
Definition rb_tree.c:704
Definition dict.h:164
dict_compare_func cmp_func
Definition rb_tree.c:77