libdict 1.0.7
Data Structure C Library
Loading...
Searching...
No Matches
tree_common.h
Go to the documentation of this file.
1/*
2 * libdict - common definitions for binary search trees.
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_TREE_COMMON_H__
29#define LIBDICT_TREE_COMMON_H__
30
31#include "dict.h"
32
33#define TREE_NODE_FIELDS(node_type) \
34 void* key; \
35 void* datum; \
36 node_type* parent; \
37 node_type* llink; \
38 node_type* rlink
39
43
44#define TREE_FIELDS(node_type) \
45 node_type* root; \
46 size_t count; \
47 dict_compare_func cmp_func; \
48 size_t rotation_count
49
53
54#define TREE_ITERATOR_FIELDS(tree_type, node_type) \
55 tree_type* tree; \
56 node_type* node
57
58/* Rotate |node| left.
59 * |node| and |node->rlink| must not be NULL. */
60void tree_node_rot_left(void *tree, void *node);
61/* Rotate |node| right.
62 * |node| and |node->llink| must not be NULL. */
63void tree_node_rot_right(void *tree, void *node);
64/* Return the predecessor of |node|, or NULL if |node| has no predecessor.
65 * |node| must not be NULL. */
66void* tree_node_prev(void *node);
67/* Return the successor of |node|, or NULL if |node| has no successor.
68 * |node| must not be NULL. */
69void* tree_node_next(void *node);
70/* Return the leftmost descendant of |node|, or |node| itself if it has no left
71 * child. Returns NULL if |node| is NULL. */
72void* tree_node_min(void *node);
73/* Return the rightmost descendant of |node|, or |node| itself if it has no
74 * right child. Returns NULL if |node| is NULL. */
75void* tree_node_max(void *node);
76/* Return the address of the data for the given key, or NULL if not found. */
77void** tree_search(void *tree, const void *key);
78/* Return the node that has the key, or NULL if not found. */
79void* tree_search_node(void *tree, const void *key);
80/* Return the data/node associated with the greatest key less than or
81 * equal to the specified key, or NULL if not found. */
82void** tree_search_le(void *tree, const void *key);
83void* tree_search_le_node(void *tree, const void *key);
84/* Return the data/node associated with the greatest key less than the
85 * specified key, or NULL if not found. */
86void** tree_search_lt(void *tree, const void *key);
87void* tree_search_lt_node(void *tree, const void *key);
88/* Return the data/node associated with the least key greater than or
89 * equal to the specified key, or NULL if not found. */
90void** tree_search_ge(void *tree, const void *key);
91void* tree_search_ge_node(void *tree, const void *key);
92/* Return the data/node associated with the least key greater than the
93 * specified key, or NULL if not found. */
94void** tree_search_gt(void *tree, const void *key);
95void* tree_search_gt_node(void *tree, const void *key);
96/* Traverses the tree in order, calling |visit| with each key and value pair,
97 * stopping if |visit| returns false. Returns the number of times |visit| was
98 * called. */
99size_t tree_traverse(void *tree, dict_visit_func visit, void* user_data);
100/* Put the key and datum of the |n|th element of |tree| into |key| and |datum|
101 * and return true, or, if n is greater than or equal to the number of elements,
102 * store NULL through each and return false. Either |key| or |datum| may be
103 * NULL, in which case that value is not stored. */
104bool tree_select(void *tree, size_t n, const void **key, void **datum);
105/* Verify the invariants shared by all trees (root/parent links, global key
106 * ordering, and count). Iterative, so it is safe on a degenerate tree. Must
107 * not be used by trees that tag their parent pointer (hb_tree, rb_tree). */
108bool tree_verify_common(const void *tree);
109/* Return a count of the elements in |tree|. */
110size_t tree_count(const void *tree);
111/* Remove all elements from |tree|. */
112size_t tree_clear(void *tree, dict_delete_func delete_func);
113/* Remove all elements from |tree| and free its memory. */
114size_t tree_free(void *tree, dict_delete_func delete_func);
115/* Returns the shallowest depth at which a child link is absent (the root being
116 * at depth 1), or 0 for an empty tree. */
117size_t tree_min_path_length(const void *tree);
118/* Returns the greatest depth of any node (the root being at depth 1), or 0 for
119 * an empty tree. */
120size_t tree_max_path_length(const void *tree);
121/* Returns the sum of the depths of all nodes, or 0 for an empty tree. */
122size_t tree_total_path_length(const void *tree);
123
124bool tree_iterator_valid(const void *iterator);
125void tree_iterator_invalidate(void *iterator);
126void tree_iterator_free(void *iterator);
127bool tree_iterator_next(void *iterator);
128bool tree_iterator_prev(void *iterator);
129bool tree_iterator_nextn(void *iterator, size_t count);
130bool tree_iterator_prevn(void *iterator, size_t count);
131bool tree_iterator_first(void *iterator);
132bool tree_iterator_last(void *iterator);
133bool tree_iterator_search(void *iterator, const void *key);
134bool tree_iterator_search_le(void *iterator, const void *key);
135bool tree_iterator_search_lt(void *iterator, const void *key);
136bool tree_iterator_search_ge(void *iterator, const void *key);
137bool tree_iterator_search_gt(void *iterator, const void *key);
138int tree_iterator_compare(const void* iterator1, const void* iterator2);
139const void* tree_iterator_key(const void *iterator);
140void** tree_iterator_datum(void *iterator);
141/* tree_iterator_key() and tree_iterator_compare() with the exact
142 * dict_key_func and dict_icompare_func slot types, for use in vtables. */
143void* tree_iterator_key_vt(void *iterator);
144int tree_iterator_compare_vt(void* iterator1, void* iterator2);
145
146#endif /* !LIBDICT_TREE_COMMON_H__ */
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
bool(* dict_visit_func)(const void *, void *, void *)
A pointer to a function used for iterating over dictionary contents.
Definition dict.h:64
bool tree_iterator_valid(const void *iterator)
int tree_iterator_compare_vt(void *iterator1, void *iterator2)
void ** tree_iterator_datum(void *iterator)
void ** tree_search_lt(void *tree, const void *key)
void tree_iterator_free(void *iterator)
void tree_node_rot_right(void *tree, void *node)
Definition tree_common.c:62
void * tree_search_node(void *tree, const void *key)
size_t tree_free(void *tree, dict_delete_func delete_func)
size_t tree_min_path_length(const void *tree)
bool tree_iterator_next(void *iterator)
void * tree_node_max(void *node)
bool tree_iterator_nextn(void *iterator, size_t count)
bool tree_iterator_prev(void *iterator)
size_t tree_traverse(void *tree, dict_visit_func visit, void *user_data)
void * tree_search_gt_node(void *tree, const void *key)
bool tree_verify_common(const void *tree)
size_t tree_max_path_length(const void *tree)
bool tree_iterator_first(void *iterator)
void ** tree_search(void *tree, const void *key)
void * tree_search_ge_node(void *tree, const void *key)
void ** tree_search_le(void *tree, const void *key)
bool tree_iterator_search_ge(void *iterator, const void *key)
size_t tree_clear(void *tree, dict_delete_func delete_func)
void ** tree_search_gt(void *tree, const void *key)
bool tree_iterator_prevn(void *iterator, size_t count)
#define TREE_NODE_FIELDS(node_type)
Definition tree_common.h:33
struct tree_node_base tree_node_base
void * tree_node_prev(void *node)
Definition tree_common.c:78
void tree_node_rot_left(void *tree, void *node)
Definition tree_common.c:46
void * tree_node_next(void *node)
Definition tree_common.c:92
const void * tree_iterator_key(const void *iterator)
#define TREE_FIELDS(node_type)
Definition tree_common.h:44
bool tree_iterator_search_gt(void *iterator, const void *key)
bool tree_iterator_last(void *iterator)
int tree_iterator_compare(const void *iterator1, const void *iterator2)
bool tree_select(void *tree, size_t n, const void **key, void **datum)
void * tree_search_lt_node(void *tree, const void *key)
bool tree_iterator_search_le(void *iterator, const void *key)
struct tree_base tree_base
bool tree_iterator_search_lt(void *iterator, const void *key)
void * tree_search_le_node(void *tree, const void *key)
void * tree_iterator_key_vt(void *iterator)
void tree_iterator_invalidate(void *iterator)
size_t tree_count(const void *tree)
size_t tree_total_path_length(const void *tree)
void * tree_node_min(void *node)
bool tree_iterator_search(void *iterator, const void *key)
void ** tree_search_ge(void *tree, const void *key)