summaryrefslogtreecommitdiffabout
path: root/libetpan/src/data-types/chash.h
Unidiff
Diffstat (limited to 'libetpan/src/data-types/chash.h') (more/less context) (ignore whitespace changes)
-rw-r--r--libetpan/src/data-types/chash.h165
1 files changed, 165 insertions, 0 deletions
diff --git a/libetpan/src/data-types/chash.h b/libetpan/src/data-types/chash.h
new file mode 100644
index 0000000..a9a61e5
--- a/dev/null
+++ b/libetpan/src/data-types/chash.h
@@ -0,0 +1,165 @@
1/*
2 * libEtPan! -- a mail stuff library
3 *
4 * chash - Implements generic hash tables.
5 *
6 * Copyright (c) 1999-2005, Gaël Roualland <gael.roualland@iname.com>
7 * interface changes - 2005 - DINH Viet Hoa
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the libEtPan! project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35/*
36 * $Id$
37 */
38
39#ifndef CHASH_H
40#define CHASH_H
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46typedef struct {
47 void * data;
48 unsigned int len;
49} chashdatum;
50
51struct chash {
52 unsigned int size;
53 unsigned int count;
54 int copyvalue;
55 int copykey;
56 struct chashcell ** cells;
57};
58
59typedef struct chash chash;
60
61struct chashcell {
62 unsigned int func;
63 chashdatum key;
64 chashdatum value;
65 struct chashcell * next;
66};
67
68typedef struct chashcell chashiter;
69
70#define CHASH_COPYNONE 0
71#define CHASH_COPYKEY 1
72#define CHASH_COPYVALUE 2
73#define CHASH_COPYALL (CHASH_COPYKEY | CHASH_COPYVALUE)
74
75#define CHASH_DEFAULTSIZE 13
76
77/* Allocates a new (empty) hash using this initial size and the given flags,
78 specifying which data should be copied in the hash.
79 CHASH_COPYNONE : Keys/Values are not copied.
80 CHASH_COPYKEY : Keys are dupped and freed as needed in the hash.
81 CHASH_COPYVALUE : Values are dupped and freed as needed in the hash.
82 CHASH_COPYALL : Both keys and values are dupped in the hash.
83 */
84chash * chash_new(unsigned int size, int flags);
85
86/* Frees a hash */
87void chash_free(chash * hash);
88
89/* Removes all elements from a hash */
90void chash_clear(chash * hash);
91
92/* Adds an entry in the hash table.
93 Length can be 0 if key/value are strings.
94 If an entry already exists for this key, it is replaced, and its value
95 is returned. Otherwise, the data pointer will be NULL and the length
96 field be set to TRUE or FALSe to indicate success or failure. */
97int chash_set(chash * hash,
98 chashdatum * key,
99 chashdatum * value,
100 chashdatum * oldvalue);
101
102/* Retrieves the data associated to the key if it is found in the hash table.
103 The data pointer and the length will be NULL if not found*/
104int chash_get(chash * hash,
105 chashdatum * key, chashdatum * result);
106
107/* Removes the entry associated to this key if it is found in the hash table,
108 and returns its contents if not dupped (otherwise, pointer will be NULL
109 and len TRUE). If entry is not found both pointer and len will be NULL. */
110int chash_delete(chash * hash,
111 chashdatum * key,
112 chashdatum * oldvalue);
113
114/* Resizes the hash table to the passed size. */
115int chash_resize(chash * hash, unsigned int size);
116
117/* Returns an iterator to the first non-empty entry of the hash table */
118chashiter * chash_begin(chash * hash);
119
120/* Returns the next non-empty entry of the hash table */
121chashiter * chash_next(chash * hash, chashiter * iter);
122
123/* Some of the following routines can be implemented as macros to
124 be faster. If you don't want it, define NO_MACROS */
125#ifdef NO_MACROS
126/* Returns the size of the hash table */
127unsigned int chash_size(chash * hash);
128
129/* Returns the number of entries in the hash table */
130unsigned int chash_count(chash * hash);
131
132/* Returns the key part of the entry pointed by the iterator */
133void chash_key(chashiter * iter, chashdatum * result);
134
135/* Returns the value part of the entry pointed by the iterator */
136void chash_value(chashiter * iter, chashdatum * result);
137
138#else
139static inline unsigned int chash_size(chash * hash)
140{
141 return hash->size;
142}
143
144static inline unsigned int chash_count(chash * hash)
145{
146 return hash->count;
147}
148
149static inline void chash_key(chashiter * iter, chashdatum * result)
150{
151 * result = iter->key;
152}
153
154static inline void chash_value(chashiter * iter, chashdatum * result)
155{
156 * result = iter->value;
157}
158
159#endif
160
161#ifdef __cplusplus
162}
163#endif
164
165#endif