libdict 1.0.7
Data Structure C Library
Loading...
Searching...
No Matches
pr_tree.h
Go to the documentation of this file.
1/*
2 * libdict -- internal path reduction 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_PR_TREE_H__
29#define LIBDICT_PR_TREE_H__
30
31#include "dict.h"
32
43
45typedef struct pr_tree pr_tree;
46
57size_t pr_tree_free(pr_tree* tree, dict_delete_func delete_func);
58
66 pr_tree_insert(pr_tree* tree, void* key);
69void** pr_tree_search(pr_tree* tree, const void* key);
72void** pr_tree_search_le(pr_tree* tree, const void* key);
75void** pr_tree_search_lt(pr_tree* tree, const void* key);
78void** pr_tree_search_ge(pr_tree* tree, const void* key);
81void** pr_tree_search_gt(pr_tree* tree, const void* key);
87 pr_tree_remove(pr_tree* tree, const void* key);
92size_t pr_tree_clear(pr_tree* tree, dict_delete_func delete_func);
95size_t pr_tree_traverse(pr_tree* tree, dict_visit_func visit, void* user_data);
100bool pr_tree_select(pr_tree* tree, size_t n, const void** key, void** datum);
102size_t pr_tree_count(const pr_tree* tree);
117bool pr_tree_verify(const pr_tree* tree);
118
121typedef struct pr_itor pr_itor;
122
130void pr_itor_free(pr_itor* itor);
131
133bool pr_itor_valid(const pr_itor* itor);
135void pr_itor_invalidate(pr_itor* itor);
139bool pr_itor_next(pr_itor* itor);
143bool pr_itor_prev(pr_itor* itor);
146bool pr_itor_nextn(pr_itor* itor, size_t count);
149bool pr_itor_prevn(pr_itor* itor, size_t count);
152bool pr_itor_first(pr_itor* itor);
155bool pr_itor_last(pr_itor* itor);
158bool pr_itor_search(pr_itor* itor, const void* key);
161bool pr_itor_search_le(pr_itor* itor, const void* key);
164bool pr_itor_search_lt(pr_itor* itor, const void* key);
167bool pr_itor_search_ge(pr_itor* itor, const void* key);
170bool pr_itor_search_gt(pr_itor* itor, const void* key);
174const void* pr_itor_key(const pr_itor* itor);
177void** pr_itor_datum(pr_itor* itor);
182int pr_itor_compare(const pr_itor* i1, const pr_itor* i2);
187bool pr_itor_remove(pr_itor* itor);
188
190
191#endif /* !LIBDICT_PR_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
pr_tree * pr_tree_new(dict_compare_func cmp_func)
Create an empty tree that orders keys with |cmp_func| (must not be NULL).
Definition pr_tree.c:110
size_t pr_tree_max_path_length(const pr_tree *tree)
Return the greatest depth of any node, the root being at depth 1, or 0 for an empty tree: the height ...
Definition pr_tree.c:389
dict * pr_dict_new(dict_compare_func cmp_func)
Like pr_tree_new(), but wrapped in a generic dict for use with the dict_* macros.
Definition pr_tree.c:125
void ** pr_tree_search_gt(pr_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 pr_tree.c:143
void ** pr_tree_search_lt(pr_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 pr_tree.c:141
const void * pr_itor_key(const pr_itor *itor)
Return the key |itor| is positioned on, or NULL if it is not positioned.
Definition pr_tree.c:549
bool pr_itor_next(pr_itor *itor)
Move |itor| to the next element in key order and return true, or unposition it and return false if th...
Definition pr_tree.c:538
dict_itor * pr_dict_itor_new(pr_tree *tree)
Like pr_itor_new(), but wrapped in a generic dict_itor for use with the dict_itor_* macros.
Definition pr_tree.c:522
dict_insert_result pr_tree_insert(pr_tree *tree, void *key)
Insert |key| unless an equal key is present.
Definition pr_tree.c:265
size_t pr_tree_traverse(pr_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 pr_tree.c:354
bool pr_itor_prevn(pr_itor *itor, size_t count)
Move |itor| |count| elements back and return whether it is still positioned.
Definition pr_tree.c:541
bool pr_itor_nextn(pr_itor *itor, size_t count)
Move |itor| |count| elements forward and return whether it is still positioned.
Definition pr_tree.c:540
dict_remove_result pr_tree_remove(pr_tree *tree, const void *key)
Remove the element with key |key|.
Definition pr_tree.c:341
void pr_itor_free(pr_itor *itor)
Free an iterator.
Definition pr_tree.c:535
bool pr_itor_valid(const pr_itor *itor)
Return whether |itor| is positioned on an element.
Definition pr_tree.c:536
bool pr_itor_remove(pr_itor *itor)
Remove the element |itor| is positioned on and unposition |itor|; return false if it was not position...
Definition pr_tree.c:554
void ** pr_tree_search_ge(pr_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 pr_tree.c:142
int pr_itor_compare(const pr_itor *i1, const pr_itor *i2)
Compare the positions of two iterators over the same tree: return a negative value,...
Definition pr_tree.c:551
bool pr_itor_prev(pr_itor *itor)
Move |itor| to the previous element in key order and return true, or unposition it and return false i...
Definition pr_tree.c:539
bool pr_tree_select(pr_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 pr_tree.c:360
size_t pr_tree_min_path_length(const pr_tree *tree)
Return the shallowest depth at which a child link is absent, the root being at depth 1,...
Definition pr_tree.c:390
bool pr_itor_last(pr_itor *itor)
Position |itor| on the last element in key order and return true, or unposition it and return false i...
Definition pr_tree.c:543
size_t pr_tree_clear(pr_tree *tree, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL.
Definition pr_tree.c:351
bool pr_itor_search_le(pr_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 pr_tree.c:545
bool pr_itor_search_ge(pr_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 pr_tree.c:547
size_t pr_tree_free(pr_tree *tree, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL,...
Definition pr_tree.c:138
bool pr_itor_search(pr_itor *itor, const void *key)
Position |itor| on |key| and return true, or unposition it and return false if |key| is not present.
Definition pr_tree.c:544
bool pr_itor_first(pr_itor *itor)
Position |itor| on the first element in key order and return true, or unposition it and return false ...
Definition pr_tree.c:542
void ** pr_tree_search_le(pr_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 pr_tree.c:140
void ** pr_itor_datum(pr_itor *itor)
Return the address of the datum |itor| is positioned on, through which it can be read or replaced,...
Definition pr_tree.c:550
size_t pr_tree_total_path_length(const pr_tree *tree)
Return the sum of the depths of all nodes, the root being at depth 1, or 0 for an empty tree.
Definition pr_tree.c:391
size_t pr_tree_count(const pr_tree *tree)
Return the number of elements.
Definition pr_tree.c:388
bool pr_tree_verify(const pr_tree *tree)
Check the internal invariants of the tree and return true if they hold, or print the first failed che...
Definition pr_tree.c:496
pr_itor * pr_itor_new(pr_tree *tree)
Create an iterator over |tree| that is not positioned on any element yet.
Definition pr_tree.c:511
void pr_itor_invalidate(pr_itor *itor)
Unposition |itor|, so that it is not positioned on any element.
Definition pr_tree.c:537
void ** pr_tree_search(pr_tree *tree, const void *key)
Return the address of the datum stored for |key|, or NULL if |key| is not present.
Definition pr_tree.c:139
bool pr_itor_search_gt(pr_itor *itor, const void *key)
Position |itor| on the least key greater than |key| and return true, or unposition it and return fals...
Definition pr_tree.c:548
bool pr_itor_search_lt(pr_itor *itor, const void *key)
Position |itor| on the greatest key less than |key| and return true, or unposition it and return fals...
Definition pr_tree.c:546
Definition dict.h:164
dict_compare_func cmp_func
Definition pr_tree.c:46