0d748616ce43ee9a26d1aa2395fd2c5e387c5968
[libfirm] / include / libfirm / adt / pset.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      optimized version of set for sets containing only pointers
23  *             (deprecated)
24  * @author     Markus Armbruster
25  * @version    $Id$
26  * @note       This code has been deprecated. Use pset_new or cpset for new
27  *             code.
28  */
29 #ifndef FIRM_ADT_PSET_H
30 #define FIRM_ADT_PSET_H
31
32 #include <stddef.h>
33
34 #include "hashptr.h"
35 #include "iterator.h"
36
37 #include "../begin.h"
38
39 /**
40  * The default comparison function for pointers.
41  * @param x A pointer.
42  * @param y A pointer.
43  * @return 0 if @p x and @p y are equal. Some value != 0 otherwise.
44  */
45 FIRM_API int pset_default_ptr_cmp(const void *x, const void *y);
46
47 /**
48  * The abstract type of a pset (Set of pointers).
49  *
50  * This kind of sets stores only pointer to elements, the elements itself
51  * must be stored somewhere else.
52  *
53  * @see set
54  */
55 typedef struct pset pset;
56
57 /*
58  * Define some convenience macros using the predefined hash function.
59  */
60 #define pset_insert_ptr(set,key)  pset_insert(set, key, HASH_PTR(key))
61 #define pset_hinsert_ptr(set,key) pset_hinsert(set, key, HASH_PTR(key))
62 #define pset_remove_ptr(set,key)  pset_remove(set, key, HASH_PTR(key))
63 #define pset_find_ptr(set,key)    pset_find(set, key, HASH_PTR(key))
64 #define pset_new_ptr(slots)       new_pset(pset_default_ptr_cmp, slots)
65 #define pset_new_ptr_default()    pset_new_ptr(64)
66
67 /** The entry of a pset, representing an element pointer in the set and it's meta-information */
68 typedef struct {
69   unsigned hash;
70   void *dptr;
71 } pset_entry;
72
73 /**
74  * The type of a set compare function.
75  *
76  * @param elt   pointer to an element
77  * @param key   pointer to another element
78  *
79  * @return
80  *    0 if the elements are identically, non-zero else
81  */
82 typedef int (*pset_cmp_fun) (const void *elt, const void *key);
83
84 /**
85  * Creates a new pset.
86  *
87  * @param func    The compare function of this pset.
88  * @param slots   Initial number of collision chains.  I.e., \#slots
89  *                different keys can be hashed without collisions.
90  *
91  * @returns
92  *    created pset
93  */
94 FIRM_API pset *new_pset(pset_cmp_fun func, int slots);
95
96 /**
97  * Deletes a pset.
98  *
99  * @param pset   the pset
100  *
101  * @note
102  *    This does NOT delete the elements of this pset, just it's pointers!
103  */
104 FIRM_API void del_pset(pset *pset);
105
106 /**
107  * Returns the number of elements in a pset.
108  *
109  * @param pset   the pset
110  */
111 FIRM_API int pset_count(pset *pset);
112
113 /**
114  * Searches an element pointer in a pset.
115  *
116  * @param pset  the pset to search in
117  * @param key   the element to search
118  * @param hash  the hash value of key
119  *
120  * @return
121  *    the pointer of the found element in the pset or NULL if it was not found
122  */
123 FIRM_API void *pset_find(pset *pset, const void *key, unsigned hash);
124
125 /**
126  * Inserts an element pointer into a pset.
127  *
128  * @param pset  the pset to insert in
129  * @param key   a pointer to the element to be inserted
130  * @param hash  the hash-value of the element
131  *
132  * @return a pointer to 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 already existing set_entry.
138
139  */
140 FIRM_API void *pset_insert(pset *pset, const void *key, unsigned hash);
141
142 /**
143  * Inserts an element pointer into a pset and returns its pset_entry.
144  *
145  * @param pset  the pset to insert in
146  * @param key   a pointer to the element to be inserted
147  * @param hash  the hash-value of the element
148  *
149  * @return a pointer to the pset_entry of the inserted element
150  *
151  * @note
152  *    It is not possible to insert an element more than once. If an element
153  *    that should be inserted is already in the pset, this functions does
154  *    nothing but returning its pset_entry.
155  */
156 FIRM_API pset_entry *pset_hinsert(pset *pset, const void *key, unsigned hash);
157
158 /**
159  * Removes an element from a pset.
160  *
161  * @param pset  the pset to delete in
162  * @param key   a pointer to the element to be deleted
163  * @param hash  the hash-value of the element
164  *
165  * @return
166  *    the pointer to the removed element
167  *
168  * @remark
169  *    The current implementation did not allow to remove non-existing elements.
170  *    @@@ so, does it do now?
171  *    Further, it is allowed to remove elements during an iteration
172  *    including the current one.
173  */
174 FIRM_API void *pset_remove(pset *pset, const void *key, unsigned hash);
175
176 /**
177  * Returns the first element of a pset.
178  *
179  * @param pset  the pset to iterate
180  *
181  * @return a pointer to the element or NULL if the set is empty
182  */
183 FIRM_API void *pset_first(pset *pset);
184
185 /**
186  * Returns the next element of a pset.
187  *
188  * @param pset  the pset to iterate
189  *
190  * @return a pointer to the next element or NULL if the
191  *         iteration is finished
192  */
193 FIRM_API void *pset_next(pset *pset);
194
195 /**
196  * Breaks the iteration of a set. Must be called before
197  * the next pset_first() call if the iteration was NOT
198  * finished.
199  *
200  * @param pset  the pset
201  */
202 FIRM_API void pset_break(pset *pset);
203
204 /**
205  * Iterates oven an pset.
206  *
207  * @param pset   the pset
208  * @param entry  the iterator
209  */
210 #define foreach_pset(pset, type, entry) for (entry = (type)pset_first(pset); entry; entry = (type)pset_next(pset))
211
212 /**
213  * Inserts all elements of the pointer set src into
214  * the set target (union).
215  *
216  * @param target  the target set, will contain the union
217  * @param src     a set, will not be changed
218  */
219 FIRM_API void pset_insert_pset_ptr(pset *target, pset *src);
220
221 #define new_pset(cmp, slots) (PSET_TRACE (new_pset) ((cmp), (slots)))
222 #define pset_find(pset, key, hash) \
223   _pset_search ((pset), (key), (hash), _pset_find)
224 #define pset_insert(pset, key, hash) \
225   _pset_search ((pset), (key), (hash), _pset_insert)
226 #define pset_hinsert(pset, key, hash) \
227   ((pset_entry *)_pset_search ((pset), (key), (hash), _pset_hinsert))
228
229 #ifdef STATS
230 /**
231  * Prints statistics on a set to stdout.
232  *
233  * @param pset  the pset
234  */
235 void pset_stats (pset *pset);
236 #else
237 # define pset_stats(s) ((void)0)
238 #endif
239
240 #ifdef DEBUG
241 /**
242  * Describe a pset.
243  *
244  * Writes a description of a set to stdout. The description includes:
245  * - a header telling how many elements (nkey) and segments (nseg) are in use
246  * - for every collision chain the number of element with its hash values
247  *
248  * @param pset  the pset
249  */
250 void pset_describe (pset *pset);
251 #endif
252
253 /* @@@ NYI */
254 #define PSET_VRFY(pset) (void)0
255
256
257 /* Private */
258
259 typedef enum { _pset_find, _pset_insert, _pset_hinsert } _pset_action;
260
261 FIRM_API void *_pset_search(pset *, const void *, unsigned, _pset_action);
262
263 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
264 extern const char *pset_tag;
265 # ifdef PSET_ID
266 #   define PSET_TRACE pset_tag = SET_ID,
267 # else
268 #   define PSET_TRACE pset_tag = __FILE__,
269 # endif
270 #else /* !(DEBUG && HAVE_GNU_MALLOC) */
271 #   define PSET_TRACE
272 #endif /* !(DEBUG && HAVE_GNU_MALLOC) */
273
274 #include "../end.h"
275
276 #endif