libdict 1.0.7
Data Structure C Library
Loading...
Searching...
No Matches
wb_tree.h
Go to the documentation of this file.
1/*
2 * libdict -- weight balanced 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_WB_TREE_H__
29#define LIBDICT_WB_TREE_H__
30
31#include "dict.h"
32
43
45typedef struct wb_tree wb_tree;
46
57size_t wb_tree_free(wb_tree* tree, dict_delete_func delete_func);
58
66 wb_tree_insert(wb_tree* tree, void* key);
69void** wb_tree_search(wb_tree* tree, const void* key);
72void** wb_tree_search_le(wb_tree* tree, const void* key);
75void** wb_tree_search_lt(wb_tree* tree, const void* key);
78void** wb_tree_search_ge(wb_tree* tree, const void* key);
81void** wb_tree_search_gt(wb_tree* tree, const void* key);
82
88 wb_tree_remove(wb_tree* tree, const void* key);
93size_t wb_tree_clear(wb_tree* tree, dict_delete_func delete_func);
96size_t wb_tree_traverse(wb_tree* tree, dict_visit_func visit, void* user_data);
101bool wb_tree_select(wb_tree* tree, size_t n, const void** key, void** datum);
103size_t wb_tree_count(const wb_tree* tree);
118bool wb_tree_verify(const wb_tree* tree);
119
122typedef struct wb_itor wb_itor;
123
131void wb_itor_free(wb_itor* itor);
132
134bool wb_itor_valid(const wb_itor* itor);
136void wb_itor_invalidate(wb_itor* itor);
140bool wb_itor_next(wb_itor* itor);
144bool wb_itor_prev(wb_itor* itor);
147bool wb_itor_nextn(wb_itor* itor, size_t count);
150bool wb_itor_prevn(wb_itor* itor, size_t count);
153bool wb_itor_first(wb_itor* itor);
156bool wb_itor_last(wb_itor* itor);
159bool wb_itor_search(wb_itor* itor, const void* key);
162bool wb_itor_search_le(wb_itor* itor, const void* key);
165bool wb_itor_search_lt(wb_itor* itor, const void* key);
168bool wb_itor_search_ge(wb_itor* itor, const void* key);
171bool wb_itor_search_gt(wb_itor* itor, const void* key);
175const void* wb_itor_key(const wb_itor* itor);
178void** wb_itor_datum(wb_itor* itor);
183int wb_itor_compare(const wb_itor* i1, const wb_itor* i2);
188bool wb_itor_remove(wb_itor* itor);
189
191
192#endif /* !LIBDICT_WB_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
Definition dict.h:164
dict_compare_func cmp_func
Definition wb_tree.c:76
bool wb_itor_first(wb_itor *itor)
Position |itor| on the first element in key order and return true, or unposition it and return false ...
Definition wb_tree.c:471
size_t wb_tree_clear(wb_tree *tree, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL.
Definition wb_tree.c:337
void wb_itor_free(wb_itor *itor)
Free an iterator.
Definition wb_tree.c:464
void ** wb_tree_search_ge(wb_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 wb_tree.c:168
void ** wb_tree_search_le(wb_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 wb_tree.c:166
size_t wb_tree_count(const wb_tree *tree)
Return the number of elements.
Definition wb_tree.c:369
int wb_itor_compare(const wb_itor *i1, const wb_itor *i2)
Compare the positions of two iterators over the same tree: return a negative value,...
Definition wb_tree.c:480
bool wb_itor_last(wb_itor *itor)
Position |itor| on the last element in key order and return true, or unposition it and return false i...
Definition wb_tree.c:472
bool wb_itor_search_lt(wb_itor *itor, const void *key)
Position |itor| on the greatest key less than |key| and return true, or unposition it and return fals...
Definition wb_tree.c:475
const void * wb_itor_key(const wb_itor *itor)
Return the key |itor| is positioned on, or NULL if it is not positioned.
Definition wb_tree.c:478
bool wb_itor_remove(wb_itor *itor)
Remove the element |itor| is positioned on and unposition |itor|; return false if it was not position...
Definition wb_tree.c:483
bool wb_itor_search_gt(wb_itor *itor, const void *key)
Position |itor| on the least key greater than |key| and return true, or unposition it and return fals...
Definition wb_tree.c:477
void ** wb_tree_search_lt(wb_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 wb_tree.c:167
size_t wb_tree_traverse(wb_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 wb_tree.c:338
wb_tree * wb_tree_new(dict_compare_func cmp_func)
Create an empty tree that orders keys with |cmp_func| (must not be NULL).
Definition wb_tree.c:137
dict_insert_result wb_tree_insert(wb_tree *tree, void *key)
Insert |key| unless an equal key is present.
Definition wb_tree.c:248
dict * wb_dict_new(dict_compare_func cmp_func)
Like wb_tree_new(), but wrapped in a generic dict for use with the dict_* macros.
Definition wb_tree.c:152
bool wb_itor_prevn(wb_itor *itor, size_t count)
Move |itor| |count| elements back and return whether it is still positioned.
Definition wb_tree.c:470
dict_itor * wb_dict_itor_new(wb_tree *tree)
Like wb_itor_new(), but wrapped in a generic dict_itor for use with the dict_itor_* macros.
Definition wb_tree.c:451
bool wb_itor_search(wb_itor *itor, const void *key)
Position |itor| on |key| and return true, or unposition it and return false if |key| is not present.
Definition wb_tree.c:473
bool wb_tree_verify(const wb_tree *tree)
Check the internal invariants of the tree and return true if they hold, or print the first failed che...
Definition wb_tree.c:425
void ** wb_itor_datum(wb_itor *itor)
Return the address of the datum |itor| is positioned on, through which it can be read or replaced,...
Definition wb_tree.c:479
bool wb_itor_search_ge(wb_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 wb_tree.c:476
bool wb_itor_nextn(wb_itor *itor, size_t count)
Move |itor| |count| elements forward and return whether it is still positioned.
Definition wb_tree.c:469
bool wb_itor_search_le(wb_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 wb_tree.c:474
void wb_itor_invalidate(wb_itor *itor)
Unposition |itor|, so that it is not positioned on any element.
Definition wb_tree.c:466
size_t wb_tree_free(wb_tree *tree, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL,...
Definition wb_tree.c:336
bool wb_itor_valid(const wb_itor *itor)
Return whether |itor| is positioned on an element.
Definition wb_tree.c:465
bool wb_itor_prev(wb_itor *itor)
Move |itor| to the previous element in key order and return true, or unposition it and return false i...
Definition wb_tree.c:468
bool wb_tree_select(wb_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 wb_tree.c:341
size_t wb_tree_min_path_length(const wb_tree *tree)
Return the shallowest depth at which a child link is absent, the root being at depth 1,...
Definition wb_tree.c:370
dict_remove_result wb_tree_remove(wb_tree *tree, const void *key)
Remove the element with key |key|.
Definition wb_tree.c:326
bool wb_itor_next(wb_itor *itor)
Move |itor| to the next element in key order and return true, or unposition it and return false if th...
Definition wb_tree.c:467
void ** wb_tree_search(wb_tree *tree, const void *key)
Return the address of the datum stored for |key|, or NULL if |key| is not present.
Definition wb_tree.c:165
void ** wb_tree_search_gt(wb_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 wb_tree.c:169
wb_itor * wb_itor_new(wb_tree *tree)
Create an iterator over |tree| that is not positioned on any element yet.
Definition wb_tree.c:440
size_t wb_tree_max_path_length(const wb_tree *tree)
Return the greatest depth of any node, the root being at depth 1, or 0 for an empty tree: the height ...
Definition wb_tree.c:371
size_t wb_tree_total_path_length(const wb_tree *tree)
Return the sum of the depths of all nodes, the root being at depth 1, or 0 for an empty tree.
Definition wb_tree.c:372