Let foreach_set() declare the iterator variable.
[libfirm] / include / libfirm / adt / set.h
1 /*
2  * Copyright (C) 1995-2011 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  */
25 #ifndef FIRM_ADT_SET_H
26 #define FIRM_ADT_SET_H
27
28 #include <stddef.h>
29
30 #include "../begin.h"
31
32 /**
33  * @ingroup adt
34  * @defgroup set Generic Hashset
35  * Generic Hashset
36  * @note This code has been deprecated. Use hashset for new code.
37  * @{
38  */
39
40 /**
41  * The abstract type of a set.
42  *
43  * This sets stores copies of its elements, so there is no need
44  * to store the elements after they were added to a set.
45  *
46  * @see pset
47  */
48 typedef struct set set;
49
50 /** The entry of a set, representing an element in the set and its meta-information */
51 typedef struct set_entry {
52         unsigned hash;  /**< the hash value of the element */
53         size_t size;    /**< the size of the element */
54         int dptr[1];    /**< the element itself, data copied in must not need more
55                              alignment than this */
56 } set_entry;
57
58 /**
59  * The type of a set compare function.
60  *
61  * @param elt   pointer to an element
62  * @param key   pointer to another element
63  * @param size  size of the elements
64  *
65  * @return
66  *    0 if the elements are identically, non-zero else
67  *
68  * @note
69  *    Although it is possible to define different meanings of equality
70  *    of two elements of a set, they can be only equal if their sizes are
71  *    are equal. This is checked before the compare function is called.
72  */
73 typedef int (*set_cmp_fun) (const void *elt, const void *key, size_t size);
74
75 /**
76  * Creates a new set.
77  *
78  * @param func    The compare function of this set.
79  * @param slots   Initial number of collision chains.  I.e., \#slots
80  *                different keys can be hashed without collisions.
81  *
82  * @returns
83  *    created set
84  */
85 FIRM_API set *new_set(set_cmp_fun func, size_t slots);
86
87 /**
88  * Deletes a set and all elements of it.
89  *
90  * @param set  the set to delete
91  */
92 FIRM_API void del_set(set *set);
93
94 /**
95  * Returns the number of elements in a set.
96  *
97  * @param set   the set
98  */
99 FIRM_API size_t set_count(set *set);
100
101 /**
102  * Searches an element in a set.
103  *
104  * @param set   the set to search in
105  * @param key   the element to is searched
106  * @param size  the size of key
107  * @param hash  the hash value of key
108  *
109  * @return
110  *    The address of the found element in the set or NULL if it was not found.
111  */
112 FIRM_API void *set_find(set *set, const void *key, size_t size, unsigned hash);
113
114 /**
115  * Inserts an element into a set.
116  *
117  * @param set   the set to insert in
118  * @param key   a pointer to the element to be inserted.  Element is copied!
119  * @param size  the size of the element that should be inserted
120  * @param hash  the hash-value of the element
121  *
122  * @return a pointer to the inserted element
123  *
124  * @note
125  *    It is not possible to insert one element more than once. If an element
126  *    that should be inserted is already in the set, this functions does
127  *    nothing but returning its pointer.
128  */
129 FIRM_API void *set_insert(set *set, const void *key, size_t size, unsigned hash);
130
131 /**
132  * Inserts an element into a set and returns its set_entry.
133  *
134  * @param set   the set to insert in
135  * @param key   a pointer to the element to be inserted. Element is copied!
136  * @param size  the size of the element that should be inserted
137  * @param hash  the hash-value of the element
138  *
139  * @return a pointer to the set_entry of the inserted element
140  *
141  * @note
142  *    It is not possible to insert an element more than once. If an element
143  *    that should be inserted is already in the set, this functions does
144  *    nothing but returning its set_entry.
145  */
146 FIRM_API set_entry *set_hinsert(set *set, const void *key, size_t size, unsigned hash);
147
148 /**
149  * Inserts an element into a set, zero-terminate it and returns its set_entry.
150  *
151  * @param set   the set to insert in
152  * @param key   a pointer to the element to be inserted.  Element is copied!
153  * @param size  the size of the element that should be inserted
154  * @param hash  the hash-value of the element
155  *
156  * @return a pointer to the set_entry of the inserted element
157  *
158  * @note
159  *    It is not possible to insert on element more than once. If an element
160  *    that should be inserted is already in the set, this functions does
161  *    nothing but returning its set_entry.
162  */
163 FIRM_API set_entry *set_hinsert0(set *set, const void *key, size_t size, unsigned hash);
164
165 /**
166  * Returns the first element of a set.
167  *
168  * @param set  the set to iterate
169  *
170  * @return a pointer to the element or NULL if the set is empty
171  */
172 FIRM_API void *set_first(set *set);
173
174 #define set_first(type, set) ((type*)set_first((set)))
175
176 /**
177  * Returns the next element of a set.
178  *
179  * @param set  the set to iterate
180  *
181  * @return a pointer to the next element or NULL if the
182  *         iteration is finished
183  */
184 FIRM_API void *set_next(set *set);
185
186 #define set_next(type, set) ((type*)set_next((set)))
187
188 /**
189  * Breaks the iteration of a set. Must be called before
190  * the next set_first() call if the iteration was NOT
191  * finished.
192  *
193  * @param set  the set
194  */
195 FIRM_API void set_break(set *set);
196
197 /**
198  * Iterates over an set.
199  *
200  * @param set    the set
201  * @param type   type of iterator variable
202  * @param entry  the iterator
203  */
204 #define foreach_set(set, type, entry) for (type *entry = set_first(type, set); entry; entry = set_next(type, set))
205
206 /** @cond PRIVATE */
207
208 /* implementation specific */
209 #define new_set(cmp, slots) ((new_set) ((cmp), (slots)))
210 #define set_find(type, set, key, size, hash) \
211   ((type*)_set_search((set), 1 ? (key) : (type*)0 /* type check */, (size), (hash), _set_find))
212 #define set_insert(type, set, key, size, hash) \
213   ((type*)_set_search((set), 1 ? (key) : (type*)0 /* type check */, (size), (hash), _set_insert))
214 #define set_hinsert(set, key, size, hash) \
215   ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert))
216 #define set_hinsert0(set, key, size, hash) \
217   ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert0))
218
219 typedef enum { _set_find, _set_insert, _set_hinsert, _set_hinsert0 } _set_action;
220
221 FIRM_API void *_set_search(set *, const void *, size_t, unsigned, _set_action);
222
223 /** @endcond */
224
225 /** @} */
226
227 #include "../end.h"
228
229 #endif