1 
2 /*
3  * libEtPan! -- a mail stuff library
4  *
5  * chash - Implements generic hash tables.
6  *
7  * Copyright (c) 1999-2000, Ga�l Roualland <gael.roualland@iname.com>
8  * interface changes - 2002 - DINH Viet Hoa
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
22  *
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
33  * SUCH DAMAGE.
34  */
35 
36 /*
37  * $Id$
38  */
39 
40 #ifndef CHASH_H
41 #define CHASH_H
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 typedef struct {
48   void * data;
49   unsigned int len;
50 } chashdatum;
51 
52 struct chash {
53   unsigned int size;
54   unsigned int count;
55   int copyvalue;
56   int copykey;
57   struct chashcell ** cells;
58 };
59 
60 typedef struct chash chash;
61 
62 struct chashcell {
63   unsigned int func;
64   chashdatum key;
65   chashdatum value;
66   struct chashcell * next;
67 };
68 
69 typedef struct chashcell chashiter;
70 
71 #define CHASH_COPYNONE    0
72 #define CHASH_COPYKEY     1
73 #define CHASH_COPYVALUE   2
74 #define CHASH_COPYALL     (CHASH_COPYKEY | CHASH_COPYVALUE)
75 
76 #define CHASH_DEFAULTSIZE 13
77 
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.
84  */
85 chash * chash_new(unsigned int size, int flags);
86 
87 /* Frees a hash */
88 void chash_free(chash * hash);
89 
90 /* Removes all elements from a hash */
91 void chash_clear(chash * hash);
92 
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,
99 	      chashdatum * key,
100 	      chashdatum * value,
101 	      chashdatum * oldvalue);
102 
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);
107 
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,
112 		 chashdatum * key,
113 		 chashdatum * oldvalue);
114 
115 /* Resizes the hash table to the passed size. */
116 int chash_resize(chash * hash, unsigned int size);
117 
118 /* Returns an iterator to the first non-empty entry of the hash table */
119 chashiter * chash_begin(chash * hash);
120 
121 /* Returns the next non-empty entry of the hash table */
122 chashiter * chash_next(chash * hash, chashiter * iter);
123 
124 /* Some of the following routines can be implemented as macros to
125    be faster. If you don't want it, define NO_MACROS */
126 #ifdef NO_MACROS
127 /* Returns the size of the hash table */
128 unsigned int          chash_size(chash * hash);
129 
130 /* Returns the number of entries in the hash table */
131 unsigned int          chash_count(chash * hash);
132 
133 /* Returns the key part of the entry pointed by the iterator */
134 void chash_key(chashiter * iter, chashdatum * result);
135 
136 /* Returns the value part of the entry pointed by the iterator */
137 void chash_value(chashiter * iter, chashdatum * result);
138 
139 #else
chash_size(chash * hash)140 static inline unsigned int chash_size(chash * hash)
141 {
142   return hash->size;
143 }
144 
chash_count(chash * hash)145 static inline unsigned int chash_count(chash * hash)
146 {
147   return hash->count;
148 }
149 
chash_key(chashiter * iter,chashdatum * result)150 static inline void chash_key(chashiter * iter, chashdatum * result)
151 {
152   * result = iter->key;
153 }
154 
chash_value(chashiter * iter,chashdatum * result)155 static inline void chash_value(chashiter * iter, chashdatum * result)
156 {
157   * result = iter->value;
158 }
159 
160 #endif
161 
162 #ifdef __cplusplus
163 }
164 #endif
165 
166 #endif
167