* src/carray.h
[claws.git] / src / chash.h
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 #ifndef CHASH_H
37 #define CHASH_H
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 typedef struct {
44   char * data;
45   int len;
46 } chashdatum;
47
48 struct chash {
49   int size, count, copyvalue, copykey;
50   struct chashcell ** cells; 
51 };
52
53 typedef struct chash chash;
54
55 struct chashcell {
56   unsigned int func;
57   chashdatum key;
58   chashdatum value;
59   struct chashcell * next;
60 };
61
62 typedef struct chashcell chashiter;
63
64 #define CHASH_COPYNONE    0
65 #define CHASH_COPYKEY     1
66 #define CHASH_COPYVALUE   2
67 #define CHASH_COPYALL     (CHASH_COPYKEY | CHASH_COPYVALUE)
68
69 #define CHASH_DEFAULTSIZE 13
70   
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.
77  */
78 chash * chash_new(int size, int flags);
79
80 /* Frees a hash */
81 void chash_free(chash * hash);
82
83 /* Removes all elements from a hash */
84 void chash_clear(chash * hash);
85
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,
92               chashdatum * key,
93               chashdatum * value,
94               chashdatum * oldvalue);
95
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);
100
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,
105                  chashdatum * key,
106                  chashdatum * oldvalue);
107
108 /* Resizes the hash table to the passed size. */
109 int chash_resize(chash * hash, int size);
110
111 /* Returns an iterator to the first non-empty entry of the hash table */
112 chashiter * chash_begin(chash * hash);
113
114 /* Returns the next non-empty entry of the hash table */
115 chashiter * chash_next(chash * hash, chashiter * iter);
116
117 /* Some of the following routines can be implemented as macros to
118    be faster. If you don't want it, define NO_MACROS */
119 #ifdef NO_MACROS
120 /* Returns the size of the hash table */
121 int          chash_size(chash * hash);
122
123 /* Returns the number of entries in the hash table */
124 int          chash_count(chash * hash);
125
126 /* Returns the key part of the entry pointed by the iterator */
127 void chash_key(chashiter * iter, chashdatum * result);
128
129 /* Returns the value part of the entry pointed by the iterator */
130 void chash_value(chashiter * iter, chashdatum * result);
131
132 #else
133 static inline int chash_size(chash * hash)
134 {
135   return hash->size;
136 }
137
138 static inline int chash_count(chash * hash)
139 {
140   return hash->count;
141 }
142
143 static inline void chash_key(chashiter * iter, chashdatum * result)
144 {
145   * result = iter->key;
146 }
147
148 static inline void chash_value(chashiter * iter, chashdatum * result)
149 {
150   * result = iter->value;
151 }
152
153 #endif
154
155 #ifdef __cplusplus
156 }
157 #endif
158
159 #endif