libdict 1.0.7
Data Structure C Library
Loading...
Searching...
No Matches
sp_tree.h
Go to the documentation of this file.
1/*
2 * libdict -- splay 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_SP_TREE_H__
29#define LIBDICT_SP_TREE_H__
30
31#include "dict.h"
32
45
47typedef struct sp_tree sp_tree;
48
59size_t sp_tree_free(sp_tree* tree, dict_delete_func delete_func);
60
69 sp_tree_insert(sp_tree* tree, void* key);
72void** sp_tree_search(sp_tree* tree, const void* key);
76void** sp_tree_search_le(sp_tree* tree, const void* key);
80void** sp_tree_search_lt(sp_tree* tree, const void* key);
84void** sp_tree_search_ge(sp_tree* tree, const void* key);
88void** sp_tree_search_gt(sp_tree* tree, const void* key);
94 sp_tree_remove(sp_tree* tree, const void* key);
99size_t sp_tree_clear(sp_tree* tree, dict_delete_func delete_func);
102size_t sp_tree_traverse(sp_tree* tree, dict_visit_func visit, void* user_data);
106bool sp_tree_select(sp_tree* tree, size_t n, const void** key, void** datum);
108size_t sp_tree_count(const sp_tree* tree);
123bool sp_tree_verify(const sp_tree* tree);
124
126typedef struct sp_itor sp_itor;
127
135void sp_itor_free(sp_itor* itor);
136
138bool sp_itor_valid(const sp_itor* itor);
140void sp_itor_invalidate(sp_itor* itor);
144bool sp_itor_next(sp_itor* itor);
148bool sp_itor_prev(sp_itor* itor);
151bool sp_itor_nextn(sp_itor* itor, size_t count);
154bool sp_itor_prevn(sp_itor* itor, size_t count);
157bool sp_itor_first(sp_itor* itor);
160bool sp_itor_last(sp_itor* itor);
164bool sp_itor_search(sp_itor* itor, const void* key);
167bool sp_itor_search_le(sp_itor* itor, const void* key);
170bool sp_itor_search_lt(sp_itor* itor, const void* key);
173bool sp_itor_search_ge(sp_itor* itor, const void* key);
176bool sp_itor_search_gt(sp_itor* itor, const void* key);
180const void* sp_itor_key(const sp_itor* itor);
183void** sp_itor_datum(sp_itor* itor);
188int sp_itor_compare(const sp_itor* i1, const sp_itor* i2);
193bool sp_itor_remove(sp_itor* itor);
194
196
197#endif /* !LIBDICT_SP_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 sp_itor_last(sp_itor *itor)
Position |itor| on the last element in key order and return true, or unposition it and return false i...
Definition sp_tree.c:470
dict * sp_dict_new(dict_compare_func cmp_func)
Like sp_tree_new(), but wrapped in a generic dict for use with the dict_* macros.
Definition sp_tree.c:141
dict_remove_result sp_tree_remove(sp_tree *tree, const void *key)
Remove the element with key |key|.
Definition sp_tree.c:396
void ** sp_itor_datum(sp_itor *itor)
Return the address of the datum |itor| is positioned on, through which it can be read or replaced,...
Definition sp_tree.c:478
bool sp_itor_nextn(sp_itor *itor, size_t count)
Move |itor| |count| elements forward and return whether it is still positioned.
Definition sp_tree.c:467
bool sp_itor_search_ge(sp_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 sp_tree.c:475
bool sp_itor_prev(sp_itor *itor)
Move |itor| to the previous element in key order and return true, or unposition it and return false i...
Definition sp_tree.c:466
dict_insert_result sp_tree_insert(sp_tree *tree, void *key)
Insert |key| unless an equal key is present.
Definition sp_tree.c:260
size_t sp_tree_clear(sp_tree *tree, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL.
Definition sp_tree.c:155
void sp_itor_free(sp_itor *itor)
Free an iterator.
Definition sp_tree.c:462
bool sp_tree_select(sp_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 sp_tree.c:407
size_t sp_tree_min_path_length(const sp_tree *tree)
Return the shallowest depth at which a child link is absent, the root being at depth 1,...
Definition sp_tree.c:409
sp_itor * sp_itor_new(sp_tree *tree)
Create an iterator over |tree| that is not positioned on any element yet.
Definition sp_tree.c:438
bool sp_itor_next(sp_itor *itor)
Move |itor| to the next element in key order and return true, or unposition it and return false if th...
Definition sp_tree.c:465
int sp_itor_compare(const sp_itor *i1, const sp_itor *i2)
Compare the positions of two iterators over the same tree: return a negative value,...
Definition sp_tree.c:481
bool sp_itor_remove(sp_itor *itor)
Remove the element |itor| is positioned on and unposition |itor|; return false if it was not position...
Definition sp_tree.c:487
bool sp_itor_prevn(sp_itor *itor, size_t count)
Move |itor| |count| elements back and return whether it is still positioned.
Definition sp_tree.c:468
size_t sp_tree_max_path_length(const sp_tree *tree)
Return the greatest depth of any node, the root being at depth 1, or 0 for an empty tree: the height ...
Definition sp_tree.c:410
void ** sp_tree_search_lt(sp_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 sp_tree.c:335
dict_itor * sp_dict_itor_new(sp_tree *tree)
Like sp_itor_new(), but wrapped in a generic dict_itor for use with the dict_itor_* macros.
Definition sp_tree.c:449
size_t sp_tree_free(sp_tree *tree, dict_delete_func delete_func)
Remove every element, calling |delete_func|(key, datum) for each unless |delete_func| is NULL,...
Definition sp_tree.c:154
size_t sp_tree_traverse(sp_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 sp_tree.c:406
void sp_itor_invalidate(sp_itor *itor)
Unposition |itor|, so that it is not positioned on any element.
Definition sp_tree.c:464
size_t sp_tree_count(const sp_tree *tree)
Return the number of elements.
Definition sp_tree.c:408
void ** sp_tree_search_gt(sp_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 sp_tree.c:359
void ** sp_tree_search_le(sp_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 sp_tree.c:323
bool sp_itor_valid(const sp_itor *itor)
Return whether |itor| is positioned on an element.
Definition sp_tree.c:463
bool sp_itor_search_le(sp_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 sp_tree.c:473
bool sp_tree_verify(const sp_tree *tree)
Check the internal invariants of the tree and return true if they hold, or print the first failed che...
Definition sp_tree.c:428
bool sp_itor_first(sp_itor *itor)
Position |itor| on the first element in key order and return true, or unposition it and return false ...
Definition sp_tree.c:469
size_t sp_tree_total_path_length(const sp_tree *tree)
Return the sum of the depths of all nodes, the root being at depth 1, or 0 for an empty tree.
Definition sp_tree.c:411
bool sp_itor_search_lt(sp_itor *itor, const void *key)
Position |itor| on the greatest key less than |key| and return true, or unposition it and return fals...
Definition sp_tree.c:474
const void * sp_itor_key(const sp_itor *itor)
Return the key |itor| is positioned on, or NULL if it is not positioned.
Definition sp_tree.c:477
bool sp_itor_search_gt(sp_itor *itor, const void *key)
Position |itor| on the least key greater than |key| and return true, or unposition it and return fals...
Definition sp_tree.c:476
sp_tree * sp_tree_new(dict_compare_func cmp_func)
Create an empty tree that orders keys with |cmp_func| (must not be NULL).
Definition sp_tree.c:126
void ** sp_tree_search_ge(sp_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 sp_tree.c:347
void ** sp_tree_search(sp_tree *tree, const void *key)
Return the address of the datum stored for |key|, or NULL if |key| is not present.
Definition sp_tree.c:301
bool sp_itor_search(sp_itor *itor, const void *key)
Position |itor| on |key| and return true, or unposition it and return false if |key| is not present.
Definition sp_tree.c:472
Definition dict.h:164
dict_compare_func cmp_func
Definition sp_tree.c:59