summaryrefslogtreecommitdiff
path: root/common/dict.h
blob: 080f6b8b7dac3a60eb70be723bf4607d20702899 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Copyright (c) 2004 Stefan Walter
 * Copyright (c) 2011 Collabora Ltd.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above
 *       copyright notice, this list of conditions and the
 *       following disclaimer.
 *     * Redistributions in binary form must reproduce the
 *       above copyright notice, this list of conditions and
 *       the following disclaimer in the documentation and/or
 *       other materials provided with the distribution.
 *     * The names of contributors to this software may not be
 *       used to endorse or promote products derived from this
 *       software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * Author: Stef Waler <stefw@collabora.co.uk>
 */

#ifndef P11_DICT_H_
#define P11_DICT_H_

#include "compat.h"

/*
 * ARGUMENT DOCUMENTATION
 *
 * dict: The dict
 * key: Pointer to the key value
 * val: Pointer to the value
 * iter: A dict iterator
 */


/* ----------------------------------------------------------------------------------
 * TYPES
 */

/* Abstract type for dicts. */
typedef struct _p11_dict p11_dict;

/* Type for scanning hash tables.  */
typedef struct _p11_dictiter {
	p11_dict *dict;
	struct _p11_dictbucket *next;
	unsigned int index;
} p11_dictiter;

typedef unsigned int (*p11_dict_hasher)        (const void *data);

typedef bool         (*p11_dict_equals)        (const void *one,
                                                const void *two);

#ifndef P11_DESTROYER_DEFINED
#define P11_DESTROYER_DEFINED

typedef void         (*p11_destroyer)          (void *data);

#endif

/* -----------------------------------------------------------------------------
 * MAIN
 */

/*
 * p11_dict_create : Create a hash table
 * - returns an allocated hashtable
 */
p11_dict *          p11_dict_new               (p11_dict_hasher hasher,
                                                p11_dict_equals equals,
                                                p11_destroyer key_destroyer,
                                                p11_destroyer value_destroyer);

/*
 * p11_dict_free : Free a hash table
 */
void                p11_dict_free              (p11_dict *dict);

/*
 *  p11_dict_size: Number of values in hash table
 * - returns the number of entries in hash table
 */
unsigned int        p11_dict_size              (p11_dict *dict);

/*
 *  p11_dict_get: Retrieves a value from the hash table
 * - returns the value of the entry
 */
void*               p11_dict_get               (p11_dict *dict,
                                                const void *key);

/*
 *  p11_dict_set: Set a value in the hash table
 * - returns true if the entry was added properly
 */
bool                p11_dict_set               (p11_dict *dict,
                                                void *key,
                                                void *value);

/*
 *  p11_dict_remove: Remove a value from the hash table
 * - returns true if the entry was found
 */
bool                p11_dict_remove            (p11_dict *dict,
                                                const void *key);

/*
 *  p11_dict_steal: Remove a value from the hash table without calling
 * destroy funcs
 * - returns true if the entry was found
 */
bool                p11_dict_steal             (p11_dict *dict,
                                                const void *key,
                                                void **stolen_key,
                                                void **stolen_value);

/*
 *  p11_dict_iterate: Start enumerating through the hash table
 * - returns a hash iterator
 */
void                p11_dict_iterate           (p11_dict *dict,
                                                p11_dictiter *iter);

/*
 *  p11_dict_next: Enumerate through hash table
 * - sets key and value to key and/or value
 * - returns whether there was another entry
 * - p11_dict_remove or p11_dict_steal is safe to use on
 *   the current key.
 */
bool                p11_dict_next              (p11_dictiter *iter,
                                                void **key,
                                                void **value);

/*
 *  p11_dict_clear: Clear all values from has htable.
 */
void                p11_dict_clear             (p11_dict *dict);

/* -----------------------------------------------------------------------------
 * KEY FUNCTIONS
 */

unsigned int        p11_dict_str_hash          (const void *string);

bool                p11_dict_str_equal         (const void *string_one,
                                                const void *string_two);

unsigned int        p11_dict_ulongptr_hash     (const void *to_ulong);

bool                p11_dict_ulongptr_equal    (const void *ulong_one,
                                                const void *ulong_two);

unsigned int        p11_dict_intptr_hash       (const void *to_int);

bool                p11_dict_intptr_equal      (const void *int_one,
                                                const void *int_two);

unsigned int        p11_dict_direct_hash       (const void *ptr);

bool                p11_dict_direct_equal      (const void *ptr_one,
                                                const void *ptr_two);

#endif  /* __P11_DICT_H__ */