libdict
Data Structure C Library
Loading...
Searching...
No Matches
dict.h
Go to the documentation of this file.
1/*
2 * libdict -- generic interface definitions.
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_DICT_H__
29#define LIBDICT_DICT_H__
30
31#if defined(__cplusplus) || defined(c_plusplus)
32# define BEGIN_DECL extern "C" {
33# define END_DECL }
34#else
35# define BEGIN_DECL
36# define END_DECL
37#endif
38
40
41#include <stddef.h>
42#include <stdint.h>
43#include <stdbool.h>
44
45#define DICT_VERSION_MAJOR 0
46#define DICT_VERSION_MINOR 3
47#define DICT_VERSION_PATCH 0
48extern const char* const kDictVersionString;
49
50/* A pointer to a function that compares two keys. It needs to return a
51 * negative value if k1<k2, a positive value if k1>k2, and zero if the keys are
52 * equal. The comparison should be reflexive (k1>k2 implies k1<k2, etc.),
53 * symmetric (k1=k1) and transitive (k1>k2 and k2>k3 implies k1>k3). */
54typedef int (*dict_compare_func)(const void*, const void*);
55/* A pointer to a function that is called when a key-value pair gets removed
56 * from a dictionary. */
57typedef void (*dict_delete_func)(void*, void*);
58/* A pointer to a function used for iterating over dictionary contents. */
59typedef bool (*dict_visit_func)(const void*, void*, void*);
60/* A pointer to a function that returns the hash value of a key. */
61typedef unsigned (*dict_hash_func)(const void*);
62/* A pointer to a function that returns the priority of a key. */
63typedef unsigned (*dict_prio_func)(const void*);
64
65/* A pointer to a function that libdict will use to allocate memory. */
66extern void* (*dict_malloc_func)(size_t);
67/* A pointer to a function that libdict will use to deallocate memory. */
68extern void (*dict_free_func)(void*);
69
70/* Forward declarations for transparent type dict_itor. */
71typedef struct dict_itor dict_itor;
72
73typedef struct {
74 void** datum_ptr;
77
78typedef struct {
79 void* key;
80 void* datum;
81 bool removed;
83
84typedef dict_itor* (*dict_inew_func)(void* obj);
85typedef size_t (*dict_dfree_func)(void* obj, dict_delete_func delete_func);
87 (*dict_insert_func)(void* obj, void* key);
88typedef void** (*dict_search_func)(void* obj, const void* key);
90 (*dict_remove_func)(void* obj, const void* key);
91typedef size_t (*dict_clear_func)(void* obj, dict_delete_func delete_func);
92typedef size_t (*dict_traverse_func)(void* obj, dict_visit_func visit, void* user_data);
93typedef bool (*dict_select_func)(void *obj, size_t n, const void** key, void** datum);
94typedef size_t (*dict_count_func)(const void* obj);
95typedef bool (*dict_verify_func)(const void* obj);
96
114
115typedef void (*dict_ifree_func)(void* itor);
116typedef bool (*dict_valid_func)(const void* itor);
117typedef void (*dict_invalidate_func)(void* itor);
118typedef bool (*dict_next_func)(void* itor);
119typedef bool (*dict_prev_func)(void* itor);
120typedef bool (*dict_nextn_func)(void* itor, size_t count);
121typedef bool (*dict_prevn_func)(void* itor, size_t count);
122typedef bool (*dict_first_func)(void* itor);
123typedef bool (*dict_last_func)(void* itor);
124typedef void* (*dict_key_func)(void* itor);
125typedef void** (*dict_datum_func)(void* itor);
126typedef bool (*dict_isearch_func)(void* itor, const void* key);
127typedef bool (*dict_iremove_func)(void* itor);
128typedef int (*dict_icompare_func)(void* itor1, void* itor2);
129
150
151typedef struct {
152 void* _object;
154} dict;
155
156#define dict_private(dct) ((dct)->_object)
157#define dict_insert(dct,key) ((dct)->_vtable->insert((dct)->_object, (key)))
158#define dict_search(dct,key) ((dct)->_vtable->search((dct)->_object, (key)))
159#define dict_is_sorted(dct) ((dct)->_vtable->sorted)
160#define dict_search_le(dct,key) ((dct)->_vtable->search_le ? (dct)->_vtable->search_le((dct)->_object, (key)) : NULL)
161#define dict_search_lt(dct,key) ((dct)->_vtable->search_lt ? (dct)->_vtable->search_lt((dct)->_object, (key)) : NULL)
162#define dict_search_ge(dct,key) ((dct)->_vtable->search_ge ? (dct)->_vtable->search_ge((dct)->_object, (key)) : NULL)
163#define dict_search_gt(dct,key) ((dct)->_vtable->search_gt ? (dct)->_vtable->search_gt((dct)->_object, (key)) : NULL)
164#define dict_remove(dct,key) ((dct)->_vtable->remove((dct)->_object, (key)))
165#define dict_clear(dct,func) ((dct)->_vtable->clear((dct)->_object, (func)))
166#define dict_traverse(dct,func,ud) ((dct)->_vtable->traverse((dct)->_object, (func), (ud)))
167#define dict_select(dct,n,key,d) ((dct)->_vtable->select && (dct)->_vtable->select((dct)->_object, (n), (key), (d)))
168#define dict_count(dct) ((dct)->_vtable->count((dct)->_object))
169#define dict_verify(dct) ((dct)->_vtable->verify((dct)->_object))
170#define dict_itor_new(dct) (dct)->_vtable->inew((dct)->_object)
171
172size_t dict_free(dict* dct, dict_delete_func delete_func);
173
174struct dict_itor {
175 void* _itor;
177};
178
179#define dict_itor_private(i) ((i)->_itor)
180#define dict_itor_valid(i) ((i)->_vtable->valid((i)->_itor))
181#define dict_itor_invalidate(i) ((i)->_vtable->invalid((i)->_itor))
182#define dict_itor_next(i) ((i)->_vtable->next((i)->_itor))
183#define dict_itor_prev(i) ((i)->_vtable->prev((i)->_itor))
184#define dict_itor_nextn(i,n) ((i)->_vtable->nextn((i)->_itor, (n)))
185#define dict_itor_prevn(i,n) ((i)->_vtable->prevn((i)->_itor, (n)))
186#define dict_itor_first(i) ((i)->_vtable->first((i)->_itor))
187#define dict_itor_last(i) ((i)->_vtable->last((i)->_itor))
188#define dict_itor_search(i,k) ((i)->_vtable->search((i)->_itor, (k)))
189#define dict_itor_search_le(i,k) ((i)->_vtable->search_le && (i)->_vtable->search_le((i)->_itor, (k)))
190#define dict_itor_search_lt(i,k) ((i)->_vtable->search_lt && (i)->_vtable->search_lt((i)->_itor, (k)))
191#define dict_itor_search_ge(i,k) ((i)->_vtable->search_ge && (i)->_vtable->search_ge((i)->_itor, (k)))
192#define dict_itor_search_gt(i,k) ((i)->_vtable->search_gt && (i)->_vtable->search_gt((i)->_itor, (k)))
193#define dict_itor_key(i) ((i)->_vtable->key((i)->_itor))
194#define dict_itor_datum(i) ((i)->_vtable->datum((i)->_itor))
195#define dict_itor_compare(i1,i2) ((i1)->_vtable->compare((i1)->_itor, (i2)->_itor))
196#define dict_itor_remove(i) ((i)->_vtable->remove && (i)->_vtable->remove((i)->_itor))
197
198void dict_itor_free(dict_itor* itor);
199
200int dict_int_cmp(const void* k1, const void* k2);
201int dict_uint_cmp(const void* k1, const void* k2);
202int dict_long_cmp(const void* k1, const void* k2);
203int dict_ulong_cmp(const void* k1, const void* k2);
204int dict_ptr_cmp(const void* k1, const void* k2);
205int dict_str_cmp(const void* k1, const void* k2);
206unsigned dict_str_hash(const void* str);
207
209
210#include "hashtable.h"
211#include "hashtable2.h"
212#include "hb_tree.h"
213#include "pr_tree.h"
214#include "rb_tree.h"
215#include "skiplist.h"
216#include "sp_tree.h"
217#include "tr_tree.h"
218#include "wb_tree.h"
219
220#endif /* !LIBDICT_DICT_H__ */
dict_remove_result(* dict_remove_func)(void *obj, const void *key)
Definition dict.h:90
#define BEGIN_DECL
Definition dict.h:35
int(* dict_compare_func)(const void *, const void *)
Definition dict.h:54
bool(* dict_select_func)(void *obj, size_t n, const void **key, void **datum)
Definition dict.h:93
int dict_ptr_cmp(const void *k1, const void *k2)
Definition dict.c:73
void(* dict_invalidate_func)(void *itor)
Definition dict.h:117
unsigned dict_str_hash(const void *str)
Definition dict.c:92
size_t(* dict_traverse_func)(void *obj, dict_visit_func visit, void *user_data)
Definition dict.h:92
int dict_ulong_cmp(const void *k1, const void *k2)
Definition dict.c:65
size_t dict_free(dict *dct, dict_delete_func delete_func)
Definition dict.c:103
dict_insert_result(* dict_insert_func)(void *obj, void *key)
Definition dict.h:87
void(* dict_delete_func)(void *, void *)
Definition dict.h:57
int dict_uint_cmp(const void *k1, const void *k2)
Definition dict.c:49
int(* dict_icompare_func)(void *itor1, void *itor2)
Definition dict.h:128
bool(* dict_nextn_func)(void *itor, size_t count)
Definition dict.h:120
bool(* dict_valid_func)(const void *itor)
Definition dict.h:116
size_t(* dict_count_func)(const void *obj)
Definition dict.h:94
bool(* dict_first_func)(void *itor)
Definition dict.h:122
int dict_str_cmp(const void *k1, const void *k2)
Definition dict.c:79
bool(* dict_next_func)(void *itor)
Definition dict.h:118
void *(* dict_key_func)(void *itor)
Definition dict.h:124
unsigned(* dict_hash_func)(const void *)
Definition dict.h:61
size_t(* dict_dfree_func)(void *obj, dict_delete_func delete_func)
Definition dict.h:85
void **(* dict_datum_func)(void *itor)
Definition dict.h:125
bool(* dict_prevn_func)(void *itor, size_t count)
Definition dict.h:121
void **(* dict_search_func)(void *obj, const void *key)
Definition dict.h:88
bool(* dict_isearch_func)(void *itor, const void *key)
Definition dict.h:126
#define END_DECL
Definition dict.h:36
unsigned(* dict_prio_func)(const void *)
Definition dict.h:63
bool(* dict_iremove_func)(void *itor)
Definition dict.h:127
bool(* dict_prev_func)(void *itor)
Definition dict.h:119
void dict_itor_free(dict_itor *itor)
Definition dict.c:113
dict_itor *(* dict_inew_func)(void *obj)
Definition dict.h:84
bool(* dict_last_func)(void *itor)
Definition dict.h:123
int dict_long_cmp(const void *k1, const void *k2)
Definition dict.c:57
bool(* dict_verify_func)(const void *obj)
Definition dict.h:95
bool(* dict_visit_func)(const void *, void *, void *)
Definition dict.h:59
const char *const kDictVersionString
Definition dict.c:33
size_t(* dict_clear_func)(void *obj, dict_delete_func delete_func)
Definition dict.h:91
void(* dict_ifree_func)(void *itor)
Definition dict.h:115
void(* dict_free_func)(void *)
Definition dict.c:38
int dict_int_cmp(const void *k1, const void *k2)
Definition dict.c:41
void ** datum_ptr
Definition dict.h:74
void * _itor
Definition dict.h:175
const itor_vtable * _vtable
Definition dict.h:176
void * datum
Definition dict.h:80
dict_search_func search_le
Definition dict.h:103
dict_select_func select
Definition dict.h:110
dict_dfree_func dfree
Definition dict.h:100
dict_traverse_func traverse
Definition dict.h:109
dict_clear_func clear
Definition dict.h:108
dict_search_func search_gt
Definition dict.h:106
dict_insert_func insert
Definition dict.h:101
dict_verify_func verify
Definition dict.h:112
dict_search_func search
Definition dict.h:102
dict_search_func search_lt
Definition dict.h:104
dict_search_func search_ge
Definition dict.h:105
dict_inew_func inew
Definition dict.h:99
const bool sorted
Definition dict.h:98
dict_count_func count
Definition dict.h:111
dict_remove_func remove
Definition dict.h:107
Definition dict.h:151
void * _object
Definition dict.h:152
const dict_vtable * _vtable
Definition dict.h:153
dict_first_func first
Definition dict.h:138
dict_ifree_func ifree
Definition dict.h:131
dict_next_func next
Definition dict.h:134
dict_key_func key
Definition dict.h:140
dict_nextn_func nextn
Definition dict.h:136
dict_prev_func prev
Definition dict.h:135
dict_valid_func valid
Definition dict.h:132
dict_isearch_func search_lt
Definition dict.h:144
dict_isearch_func search_ge
Definition dict.h:145
dict_isearch_func search
Definition dict.h:142
dict_icompare_func compare
Definition dict.h:148
dict_prevn_func prevn
Definition dict.h:137
dict_isearch_func search_le
Definition dict.h:143
dict_last_func last
Definition dict.h:139
dict_iremove_func remove
Definition dict.h:147
dict_isearch_func search_gt
Definition dict.h:146
dict_invalidate_func invalid
Definition dict.h:133
dict_datum_func datum
Definition dict.h:141