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
57 struct chashcell
** cells
;
60 typedef struct chash chash
;
66 struct chashcell
* next
;
69 typedef struct chashcell chashiter
;
71 #define CHASH_COPYNONE 0
72 #define CHASH_COPYKEY 1
73 #define CHASH_COPYVALUE 2
74 #define CHASH_COPYALL (CHASH_COPYKEY | CHASH_COPYVALUE)
76 #define CHASH_DEFAULTSIZE 13
78 /* Allocates a new (empty) hash using this initial size and the given flags,
79 specifying which data should be copied in the hash.
80 CHASH_COPYNONE : Keys/Values are not copied.
81 CHASH_COPYKEY : Keys are dupped and freed as needed in the hash.
82 CHASH_COPYVALUE : Values are dupped and freed as needed in the hash.
83 CHASH_COPYALL : Both keys and values are dupped in the hash.
85 chash
* chash_new(unsigned int size
, int flags
);
88 void chash_free(chash
* hash
);
90 /* Removes all elements from a hash */
91 void chash_clear(chash
* hash
);
93 /* Adds an entry in the hash table.
94 Length can be 0 if key/value are strings.
95 If an entry already exists for this key, it is replaced, and its value
96 is returned. Otherwise, the data pointer will be NULL and the length
97 field be set to TRUE or FALSe to indicate success or failure. */
98 int chash_set(chash
* hash
,
101 chashdatum
* oldvalue
);
103 /* Retrieves the data associated to the key if it is found in the hash table.
104 The data pointer and the length will be NULL if not found*/
105 int chash_get(chash
* hash
,
106 chashdatum
* key
, chashdatum
* result
);
108 /* Removes the entry associated to this key if it is found in the hash table,
109 and returns its contents if not dupped (otherwise, pointer will be NULL
110 and len TRUE). If entry is not found both pointer and len will be NULL. */
111 int chash_delete(chash
* hash
,
113 chashdatum
* oldvalue
);
115 /* Resizes the hash table to the passed size. */
116 int chash_resize(chash
* hash
, unsigned int size
);
118 /* Returns an iterator to the first non-empty entry of the hash table */
119 chashiter
* chash_begin(chash
* hash
);
121 /* Returns the next non-empty entry of the hash table */
122 chashiter
* chash_next(chash
* hash
, chashiter
* iter
);
124 /* Some of the following routines can be implemented as macros to
125 be faster. If you don't want it, define NO_MACROS */
127 /* Returns the size of the hash table */
128 unsigned int chash_size(chash
* hash
);
130 /* Returns the number of entries in the hash table */
131 unsigned int chash_count(chash
* hash
);
133 /* Returns the key part of the entry pointed by the iterator */
134 void chash_key(chashiter
* iter
, chashdatum
* result
);
136 /* Returns the value part of the entry pointed by the iterator */
137 void chash_value(chashiter
* iter
, chashdatum
* result
);
140 static inline unsigned int chash_size(chash
* hash
)
145 static inline unsigned int chash_count(chash
* hash
)
150 static inline void chash_key(chashiter
* iter
, chashdatum
* result
)
152 * result
= iter
->key
;
155 static inline void chash_value(chashiter
* iter
, chashdatum
* result
)
157 * result
= iter
->value
;