f1ae2668cdaf168cb41629780bc3ca7dfbd11aa4
[libfirm] / include / libfirm / adt / set.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       hashset: datastructure containing objects accessible by their key
23  * @author      Markus Armbruster
24  * @version     $Id$
25  */
26 #ifndef FIRM_ADT_SET_H
27 #define FIRM_ADT_SET_H
28
29 #include <stddef.h>
30
31 #include "../begin.h"
32
33 /**
34  * The abstract type of a set.
35  *
36  * This sets stores copies of its elements, so there is no need
37  * to store the elements after they were added to a set.
38  *
39  * @see pset
40  */
41 typedef struct set set;
42
43 /** The entry of a set, representing an element in the set and it's meta-information */
44 typedef struct set_entry {
45   unsigned hash;    /**< the hash value of the element */
46   size_t size;      /**< the size of the element */
47   int dptr[1];      /**< the element itself, data copied in must not need more
48                           alignment than this */
49 } set_entry;
50
51 /**
52  * The type of a set compare function.
53  *
54  * @param elt   pointer to an element
55  * @param key   pointer to another element
56  * @param size  size of the elements
57  *
58  * @return
59  *    0 if the elements are identically, non-zero else
60  *
61  * @note
62  *    Although it is possible to define different meanings of equality
63  *    of two elements of a set, they can be only equal if their sizes are
64  *    are equal. This is checked before the compare function is called.
65  */
66 typedef int (*set_cmp_fun) (const void *elt, const void *key, size_t size);
67
68 /**
69  * Creates a new set.
70  *
71  * @param func    The compare function of this set.
72  * @param slots   Initial number of collision chains.  I.e., \#slots
73  *                different keys can be hashed without collisions.
74  *
75  * @returns
76  *    created set
77  */
78 set *new_set (set_cmp_fun func, int slots);
79
80 /**
81  * Deletes a set and all elements of it.
82  */
83 void del_set (set *set);
84
85 /**
86  * Returns the number of elements in a set.
87  *
88  * @param set   the set
89  */
90 int set_count (set *set);
91
92 /**
93  * Searches an element in a set.
94  *
95  * @param set   the set to search in
96  * @param key   the element to is searched
97  * @param size  the size of key
98  * @param hash  the hash value of key
99  *
100  * @return
101  *    The address of the found element in the set or NULL if it was not found.
102  */
103 void *set_find (set *set, const void *key, size_t size, unsigned hash);
104
105 /**
106  * Inserts an element into a set.
107  *
108  * @param set   the set to insert in
109  * @param key   a pointer to the element to be inserted.  Element is copied!
110  * @param size  the size of the element that should be inserted
111  * @param hash  the hash-value of the element
112  *
113  * @return a pointer to the inserted element
114  *
115  * @note
116  *    It is not possible to insert one element more than once. If an element
117  *    that should be inserted is already in the set, this functions does
118  *    nothing but returning its pointer.
119  */
120 void *set_insert (set *set, const void *key, size_t size, unsigned hash);
121
122 /**
123  * Inserts an element into a set and returns its set_entry.
124  *
125  * @param set   the set to insert in
126  * @param key   a pointer to the element to be inserted. Element is copied!
127  * @param size  the size of the element that should be inserted
128  * @param hash  the hash-value of the element
129  *
130  * @return a pointer to the set_entry of the inserted element
131  *
132  * @note
133  *    It is not possible to insert an element more than once. If an element
134  *    that should be inserted is already in the set, this functions does
135  *    nothing but returning its set_entry.
136  */
137 set_entry *set_hinsert (set *set, const void *key, size_t size, unsigned hash);
138
139 /**
140  * Inserts an element into a set, zero-terminate it and returns its set_entry.
141  *
142  * @param set   the set to insert in
143  * @param key   a pointer to the element to be inserted.  Element is copied!
144  * @param size  the size of the element that should be inserted
145  * @param hash  the hash-value of the element
146  *
147  * @return a pointer to the set_entry of the inserted element
148  *
149  * @note
150  *    It is not possible to insert on element more than once. If an element
151  *    that should be inserted is already in the set, this functions does
152  *    nothing but returning its set_entry.
153  */
154 set_entry *set_hinsert0 (set *set, const void *key, size_t size, unsigned hash);
155
156 /**
157  * Returns the first element of a set.
158  *
159  * @param set  the set to iterate
160  *
161  * @return a pointer to the element or NULL if the set is empty
162  */
163 void *set_first (set *set);
164
165 /**
166  * Returns the next element of a set.
167  *
168  * @param set  the set to iterate
169  *
170  * @return a pointer to the next element or NULL if the
171  *         iteration is finished
172  */
173 void *set_next (set *set);
174
175 /**
176  * Breaks the iteration of a set. Must be called before
177  * the next set_first() call if the iteration was NOT
178  * finished.
179  *
180  * @param set  the set
181  */
182 void set_break (set *set);
183
184 /**
185  * Iterates over an set.
186  *
187  * @param set    the set
188  * @param entry  the iterator
189  */
190 #define foreach_set(set, entry) for (entry = set_first(set); entry; entry = set_next(set))
191
192 /* implementation specific */
193 #define new_set(cmp, slots) (SET_TRACE (new_set) ((cmp), (slots)))
194 #define set_find(set, key, size, hash) \
195   _set_search ((set), (key), (size), (hash), _set_find)
196 #define set_insert(set, key, size, hash) \
197   _set_search ((set), (key), (size), (hash), _set_insert)
198 #define set_hinsert(set, key, size, hash) \
199   ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert))
200 #define set_hinsert0(set, key, size, hash) \
201   ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert0))
202
203 #define SET_VRFY(set) (void)0
204
205 #ifdef STATS
206 /**
207  * Prints statistics on a set to stdout.
208  *
209  * @param set  the set
210  */
211 void set_stats (set *set);
212 #else
213 # define set_stats(s) ((void)0)
214 #endif
215
216 #ifdef DEBUG
217 /**
218  * Describe a set.
219  *
220  * Writes a description of a set to stdout. The description includes:
221  * - a header telling how many elements (nkey) and segments (nseg) are in use
222  * - for every collision chain the number of element with its hash values
223  *
224  * @param set  the set
225  */
226 void set_describe (set *set);
227 #endif
228
229
230 /* Private */
231
232 typedef enum { _set_find, _set_insert, _set_hinsert, _set_hinsert0 } _set_action;
233
234 void *_set_search (set *, const void *, size_t, unsigned, _set_action);
235
236 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
237 extern const char *set_tag;
238 # ifdef SET_ID
239 #   define SET_TRACE set_tag = SET_ID,
240 # else
241 #   define SET_TRACE set_tag = __FILE__,
242 # endif
243 #else /* !(DEBUG && HAVE_GNU_MALLOC) */
244 #   define SET_TRACE
245 #endif /* !(DEBUG && HAVE_GNU_MALLOC) */
246
247 #include "../end.h"
248
249 #endif