3 * libEtPan! -- a mail stuff library
5 * chash - Implements generic hash tables.
7 * Copyright (c) 1999-2000, Gaƫl Roualland <gael.roualland@iname.com>
8 * interface changes - 2002 - DINH Viet Hoa
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the libEtPan! project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 int size, count, copyvalue, copykey;
50 struct chashcell ** cells;
53 typedef struct chash chash;
59 struct chashcell * next;
62 typedef struct chashcell chashiter;
64 #define CHASH_COPYNONE 0
65 #define CHASH_COPYKEY 1
66 #define CHASH_COPYVALUE 2
67 #define CHASH_COPYALL (CHASH_COPYKEY | CHASH_COPYVALUE)
69 #define CHASH_DEFAULTSIZE 13
71 /* Allocates a new (empty) hash using this initial size and the given flags,
72 specifying which data should be copied in the hash.
73 CHASH_COPYNONE : Keys/Values are not copied.
74 CHASH_COPYKEY : Keys are dupped and freed as needed in the hash.
75 CHASH_COPYVALUE : Values are dupped and freed as needed in the hash.
76 CHASH_COPYALL : Both keys and values are dupped in the hash.
78 chash * chash_new(int size, int flags);
81 void chash_free(chash * hash);
83 /* Removes all elements from a hash */
84 void chash_clear(chash * hash);
86 /* Adds an entry in the hash table.
87 Length can be 0 if key/value are strings.
88 If an entry already exists for this key, it is replaced, and its value
89 is returned. Otherwise, the data pointer will be NULL and the length
90 field be set to TRUE or FALSe to indicate success or failure. */
91 int chash_set(chash * hash,
94 chashdatum * oldvalue);
96 /* Retrieves the data associated to the key if it is found in the hash table.
97 The data pointer and the length will be NULL if not found*/
98 int chash_get(chash * hash,
99 chashdatum * key, chashdatum * result);
101 /* Removes the entry associated to this key if it is found in the hash table,
102 and returns its contents if not dupped (otherwise, pointer will be NULL
103 and len TRUE). If entry is not found both pointer and len will be NULL. */
104 int chash_delete(chash * hash,
106 chashdatum * oldvalue);
108 /* Resizes the hash table to the passed size. */
109 int chash_resize(chash * hash, int size);
111 /* Returns an iterator to the first non-empty entry of the hash table */
112 chashiter * chash_begin(chash * hash);
114 /* Returns the next non-empty entry of the hash table */
115 chashiter * chash_next(chash * hash, chashiter * iter);
117 /* Some of the following routines can be implemented as macros to
118 be faster. If you don't want it, define NO_MACROS */
120 /* Returns the size of the hash table */
121 int chash_size(chash * hash);
123 /* Returns the number of entries in the hash table */
124 int chash_count(chash * hash);
126 /* Returns the key part of the entry pointed by the iterator */
127 void chash_key(chashiter * iter, chashdatum * result);
129 /* Returns the value part of the entry pointed by the iterator */
130 void chash_value(chashiter * iter, chashdatum * result);
133 static inline int chash_size(chash * hash)
138 static inline int chash_count(chash * hash)
143 static inline void chash_key(chashiter * iter, chashdatum * result)
145 * result = iter->key;
148 static inline void chash_value(chashiter * iter, chashdatum * result)
150 * result = iter->value;