cleanup comments in public headers
[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  * @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 its 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 FIRM_API set *new_set(set_cmp_fun func, size_t slots);
79
80 /**
81  * Deletes a set and all elements of it.
82  *
83  * @param set  the set to delete
84  */
85 FIRM_API void del_set(set *set);
86
87 /**
88  * Returns the number of elements in a set.
89  *
90  * @param set   the set
91  */
92 FIRM_API size_t set_count(set *set);
93
94 /**
95  * Searches an element in a set.
96  *
97  * @param set   the set to search in
98  * @param key   the element to is searched
99  * @param size  the size of key
100  * @param hash  the hash value of key
101  *
102  * @return
103  *    The address of the found element in the set or NULL if it was not found.
104  */
105 FIRM_API void *set_find(set *set, const void *key, size_t size, unsigned hash);
106
107 /**
108  * Inserts an element into a set.
109  *
110  * @param set   the set to insert in
111  * @param key   a pointer to the element to be inserted.  Element is copied!
112  * @param size  the size of the element that should be inserted
113  * @param hash  the hash-value of the element
114  *
115  * @return a pointer to the inserted element
116  *
117  * @note
118  *    It is not possible to insert one element more than once. If an element
119  *    that should be inserted is already in the set, this functions does
120  *    nothing but returning its pointer.
121  */
122 FIRM_API void *set_insert(set *set, const void *key, size_t size, unsigned hash);
123
124 /**
125  * Inserts an element into a set and returns its set_entry.
126  *
127  * @param set   the set to insert in
128  * @param key   a pointer to the element to be inserted. Element is copied!
129  * @param size  the size of the element that should be inserted
130  * @param hash  the hash-value of the element
131  *
132  * @return a pointer to the set_entry of the inserted element
133  *
134  * @note
135  *    It is not possible to insert an element more than once. If an element
136  *    that should be inserted is already in the set, this functions does
137  *    nothing but returning its set_entry.
138  */
139 FIRM_API set_entry *set_hinsert(set *set, const void *key, size_t size, unsigned hash);
140
141 /**
142  * Inserts an element into a set, zero-terminate it and returns its set_entry.
143  *
144  * @param set   the set to insert in
145  * @param key   a pointer to the element to be inserted.  Element is copied!
146  * @param size  the size of the element that should be inserted
147  * @param hash  the hash-value of the element
148  *
149  * @return a pointer to the set_entry of the inserted element
150  *
151  * @note
152  *    It is not possible to insert on element more than once. If an element
153  *    that should be inserted is already in the set, this functions does
154  *    nothing but returning its set_entry.
155  */
156 FIRM_API set_entry *set_hinsert0(set *set, const void *key, size_t size, unsigned hash);
157
158 /**
159  * Returns the first element of a set.
160  *
161  * @param set  the set to iterate
162  *
163  * @return a pointer to the element or NULL if the set is empty
164  */
165 FIRM_API void *set_first(set *set);
166
167 /**
168  * Returns the next element of a set.
169  *
170  * @param set  the set to iterate
171  *
172  * @return a pointer to the next element or NULL if the
173  *         iteration is finished
174  */
175 FIRM_API void *set_next(set *set);
176
177 /**
178  * Breaks the iteration of a set. Must be called before
179  * the next set_first() call if the iteration was NOT
180  * finished.
181  *
182  * @param set  the set
183  */
184 FIRM_API void set_break(set *set);
185
186 /**
187  * Iterates over an set.
188  *
189  * @param set    the set
190  * @param type   type of iterator variable
191  * @param entry  the iterator
192  */
193 #define foreach_set(set, type, entry) for (entry = (type) set_first(set); entry; entry = (type) set_next(set))
194
195 /* implementation specific */
196 #define new_set(cmp, slots) ((new_set) ((cmp), (slots)))
197 #define set_find(set, key, size, hash) \
198   _set_search ((set), (key), (size), (hash), _set_find)
199 #define set_insert(set, key, size, hash) \
200   _set_search ((set), (key), (size), (hash), _set_insert)
201 #define set_hinsert(set, key, size, hash) \
202   ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert))
203 #define set_hinsert0(set, key, size, hash) \
204   ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert0))
205
206 #define SET_VRFY(set) (void)0
207
208 #ifdef STATS
209 /**
210  * Prints statistics on a set to stdout.
211  *
212  * @param set  the set
213  */
214 void set_stats (set *set);
215 #else
216 # define set_stats(s) ((void)0)
217 #endif
218
219 #ifdef DEBUG
220 /**
221  * Describe a set.
222  *
223  * Writes a description of a set to stdout. The description includes:
224  * - a header telling how many elements (nkey) and segments (nseg) are in use
225  * - for every collision chain the number of element with its hash values
226  *
227  * @param set  the set
228  */
229 FIRM_API void set_describe(set *set);
230 #endif
231
232
233 /* Private */
234
235 typedef enum { _set_find, _set_insert, _set_hinsert, _set_hinsert0 } _set_action;
236
237 FIRM_API void *_set_search(set *, const void *, size_t, unsigned, _set_action);
238
239 #include "../end.h"
240
241 #endif