libdict 1.0.7
Data Structure C Library
Loading...
Searching...
No Matches
hb_tree.h
Go to the documentation of this file.
1/*
2 * libdict -- height-balanced (AVL) 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_HB_TREE_H__
29#define LIBDICT_HB_TREE_H__
30
31#include "dict.h"
32
44
46typedef struct hb_tree hb_tree;
47
58size_t hb_tree_free(hb_tree* tree, dict_delete_func delete_func);
59
67 hb_tree_insert(hb_tree* tree, void* key);
70void** hb_tree_search(hb_tree* tree, const void* key);
73void** hb_tree_search_le(hb_tree* tree, const void* key);
76void** hb_tree_search_lt(hb_tree* tree, const void* key);
79void** hb_tree_search_ge(hb_tree* tree, const void* key);
82void** hb_tree_search_gt(hb_tree* tree, const void* key);
88 hb_tree_remove(hb_tree* tree, const void* key);
93size_t hb_tree_clear(hb_tree* tree, dict_delete_func delete_func);
96size_t hb_tree_traverse(hb_tree* tree, dict_visit_func visit, void* user_data);
100bool hb_tree_select(hb_tree* tree, size_t n, const void** key, void** datum);
102size_t hb_tree_count(const hb_tree* tree);
117bool hb_tree_verify(const hb_tree* tree);
118
121typedef struct hb_itor hb_itor;
122
130void hb_itor_free(hb_itor* itor);
131
133bool hb_itor_valid(const hb_itor* itor);
135void hb_itor_invalidate(hb_itor* itor);
139bool hb_itor_next(hb_itor* itor);
143bool hb_itor_prev(hb_itor* itor);
146bool hb_itor_nextn(hb_itor* itor, size_t count);
149bool hb_itor_prevn(hb_itor* itor, size_t count);
152bool hb_itor_first(hb_itor* itor);
155bool hb_itor_last(hb_itor* itor);
158bool hb_itor_search(hb_itor* itor, const void* key);
161bool hb_itor_search_le(hb_itor* itor, const void* key);
164bool hb_itor_search_lt(hb_itor* itor, const void* key);
167bool hb_itor_search_ge(hb_itor* itor, const void* key);
170bool hb_itor_search_gt(hb_itor* itor, const void* key);
174const void* hb_itor_key(const hb_itor* itor);
177void** hb_itor_datum(hb_itor* itor);
182int hb_itor_compare(const hb_itor* i1, const hb_itor* i2);
187bool hb_itor_remove(hb_itor* itor);
188
190
191#endif /* !LIBDICT_HB_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
bool hb_itor_search(hb_itor *itor, const void *key)
Position |itor| on |key| and return true, or unposition it and return false if |key| is not present.
Definition hb_tree.c:717
dict_itor * hb_dict_itor_new(hb_tree *tree)
Like hb_itor_new(), but wrapped in a generic dict_itor for use with the dict_itor_* macros.
Definition hb_tree.c:674
size_t hb_tree_clear(hb_tree *tree, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL.
Definition hb_tree.c:182
bool hb_itor_search_gt(hb_itor *itor, const void *key)
Position |itor| on the least key greater than |key| and return true, or unposition it and return fals...
Definition hb_tree.c:721
void ** hb_tree_search_lt(hb_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 hb_tree.c:399
void ** hb_tree_search(hb_tree *tree, const void *key)
Return the address of the datum stored for |key|, or NULL if |key| is not present.
Definition hb_tree.c:397
bool hb_tree_select(hb_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 hb_tree.c:521
dict * hb_dict_new(dict_compare_func cmp_func)
Like hb_tree_new(), but wrapped in a generic dict for use with the dict_* macros.
Definition hb_tree.c:161
bool hb_itor_search_ge(hb_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 hb_tree.c:720
void hb_itor_free(hb_itor *itor)
Free an iterator.
Definition hb_tree.c:687
hb_itor * hb_itor_new(hb_tree *tree)
Create an iterator over |tree| that is not positioned on any element yet.
Definition hb_tree.c:663
bool hb_itor_search_lt(hb_itor *itor, const void *key)
Position |itor| on the greatest key less than |key| and return true, or unposition it and return fals...
Definition hb_tree.c:719
bool hb_itor_prev(hb_itor *itor)
Move |itor| to the previous element in key order and return true, or unposition it and return false i...
Definition hb_tree.c:697
void ** hb_tree_search_gt(hb_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 hb_tree.c:401
hb_tree * hb_tree_new(dict_compare_func cmp_func)
Create an empty tree that orders keys with |cmp_func| (must not be NULL).
Definition hb_tree.c:146
void hb_itor_invalidate(hb_itor *itor)
Unposition |itor|, so that it is not positioned on any element.
Definition hb_tree.c:689
size_t hb_tree_count(const hb_tree *tree)
Return the number of elements.
Definition hb_tree.c:548
bool hb_itor_valid(const hb_itor *itor)
Return whether |itor| is positioned on an element.
Definition hb_tree.c:688
size_t hb_tree_min_path_length(const hb_tree *tree)
Return the shallowest depth at which a child link is absent, the root being at depth 1,...
Definition hb_tree.c:549
dict_insert_result hb_tree_insert(hb_tree *tree, void *key)
Insert |key| unless an equal key is present.
Definition hb_tree.c:319
void ** hb_tree_search_ge(hb_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 hb_tree.c:400
bool hb_itor_nextn(hb_itor *itor, size_t count)
Move |itor| |count| elements forward and return whether it is still positioned.
Definition hb_tree.c:703
bool hb_itor_remove(hb_itor *itor)
Remove the element |itor| is positioned on and unposition |itor|; return false if it was not position...
Definition hb_tree.c:727
size_t hb_tree_free(hb_tree *tree, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL,...
Definition hb_tree.c:175
dict_remove_result hb_tree_remove(hb_tree *tree, const void *key)
Remove the element with key |key|.
Definition hb_tree.c:493
size_t hb_tree_max_path_length(const hb_tree *tree)
Return the greatest depth of any node, the root being at depth 1, or 0 for an empty tree: the height ...
Definition hb_tree.c:550
int hb_itor_compare(const hb_itor *i1, const hb_itor *i2)
Compare the positions of two iterators over the same tree: return a negative value,...
Definition hb_tree.c:722
bool hb_itor_last(hb_itor *itor)
Position |itor| on the last element in key order and return true, or unposition it and return false i...
Definition hb_tree.c:716
bool hb_itor_prevn(hb_itor *itor, size_t count)
Move |itor| |count| elements back and return whether it is still positioned.
Definition hb_tree.c:709
void ** hb_itor_datum(hb_itor *itor)
Return the address of the datum |itor| is positioned on, through which it can be read or replaced,...
Definition hb_tree.c:724
bool hb_itor_next(hb_itor *itor)
Move |itor| to the next element in key order and return true, or unposition it and return false if th...
Definition hb_tree.c:691
const void * hb_itor_key(const hb_itor *itor)
Return the key |itor| is positioned on, or NULL if it is not positioned.
Definition hb_tree.c:723
bool hb_itor_search_le(hb_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 hb_tree.c:718
bool hb_tree_verify(const hb_tree *tree)
Check the internal invariants of the tree and return true if they hold, or print the first failed che...
Definition hb_tree.c:650
bool hb_itor_first(hb_itor *itor)
Position |itor| on the first element in key order and return true, or unposition it and return false ...
Definition hb_tree.c:715
size_t hb_tree_traverse(hb_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 hb_tree.c:504
void ** hb_tree_search_le(hb_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 hb_tree.c:398
size_t hb_tree_total_path_length(const hb_tree *tree)
Return the sum of the depths of all nodes, the root being at depth 1, or 0 for an empty tree.
Definition hb_tree.c:551
Definition dict.h:164
dict_compare_func cmp_func
Definition hb_tree.c:72