remove $Id$, it doesn't work with git anyway
[libfirm] / include / libfirm / adt / pset.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      optimized version of set for sets containing only pointers
23  *             (deprecated)
24  * @author     Markus Armbruster
25  * @note       This code has been deprecated. Use pset_new or cpset for new
26  *             code.
27  */
28 #ifndef FIRM_ADT_PSET_H
29 #define FIRM_ADT_PSET_H
30
31 #include <stddef.h>
32
33 #include "hashptr.h"
34 #include "iterator.h"
35
36 #include "../begin.h"
37
38 /**
39  * The default comparison function for pointers.
40  * @param x A pointer.
41  * @param y A pointer.
42  * @return 0 if @p x and @p y are equal. Some value != 0 otherwise.
43  */
44 FIRM_API int pset_default_ptr_cmp(const void *x, const void *y);
45
46 /**
47  * The abstract type of a pset (Set of pointers).
48  *
49  * This kind of sets stores only pointer to elements, the elements itself
50  * must be stored somewhere else.
51  *
52  * @see set
53  */
54 typedef struct pset pset;
55
56 /*
57  * Define some convenience macros using the predefined hash function.
58  */
59 #define pset_insert_ptr(set,key)  pset_insert(set, key, HASH_PTR(key))
60 #define pset_hinsert_ptr(set,key) pset_hinsert(set, key, HASH_PTR(key))
61 #define pset_remove_ptr(set,key)  pset_remove(set, key, HASH_PTR(key))
62 #define pset_find_ptr(set,key)    pset_find(set, key, HASH_PTR(key))
63 #define pset_new_ptr(slots)       new_pset(pset_default_ptr_cmp, slots)
64 #define pset_new_ptr_default()    pset_new_ptr(64)
65
66 /** The entry of a pset, representing an element pointer in the set and its meta-information */
67 typedef struct {
68   unsigned hash;
69   void *dptr;
70 } pset_entry;
71
72 /**
73  * The type of a set compare function.
74  *
75  * @param elt   pointer to an element
76  * @param key   pointer to another element
77  *
78  * @return
79  *    0 if the elements are identically, non-zero else
80  */
81 typedef int (*pset_cmp_fun) (const void *elt, const void *key);
82
83 /**
84  * Creates a new pset.
85  *
86  * @param func    The compare function of this pset.
87  * @param slots   Initial number of collision chains.  I.e., \#slots
88  *                different keys can be hashed without collisions.
89  *
90  * @returns
91  *    created pset
92  */
93 FIRM_API pset *new_pset(pset_cmp_fun func, size_t slots);
94
95 /**
96  * Deletes a pset.
97  *
98  * @param pset   the pset
99  *
100  * @note
101  *    This does NOT delete the elements of this pset, just its pointers!
102  */
103 FIRM_API void del_pset(pset *pset);
104
105 /**
106  * Returns the number of elements in a pset.
107  *
108  * @param pset   the pset
109  */
110 FIRM_API size_t pset_count(pset *pset);
111
112 /**
113  * Searches an element pointer in a pset.
114  *
115  * @param pset  the pset to search in
116  * @param key   the element to search
117  * @param hash  the hash value of key
118  *
119  * @return
120  *    the pointer of the found element in the pset or NULL if it was not found
121  */
122 FIRM_API void *pset_find(pset *pset, const void *key, unsigned hash);
123
124 /**
125  * Inserts an element pointer into a pset.
126  *
127  * @param pset  the pset to insert in
128  * @param key   a pointer to the element to be inserted
129  * @param hash  the hash-value of the element
130  *
131  * @return a pointer to the inserted element
132  *
133  * @note
134  *    It is not possible to insert an element more than once. If an element
135  *    that should be inserted is already in the set, this functions does
136  *    nothing but returning its already existing set_entry.
137
138  */
139 FIRM_API void *pset_insert(pset *pset, const void *key, unsigned hash);
140
141 /**
142  * Inserts an element pointer into a pset and returns its pset_entry.
143  *
144  * @param pset  the pset to insert in
145  * @param key   a pointer to the element to be inserted
146  * @param hash  the hash-value of the element
147  *
148  * @return a pointer to the pset_entry of the inserted element
149  *
150  * @note
151  *    It is not possible to insert an element more than once. If an element
152  *    that should be inserted is already in the pset, this functions does
153  *    nothing but returning its pset_entry.
154  */
155 FIRM_API pset_entry *pset_hinsert(pset *pset, const void *key, unsigned hash);
156
157 /**
158  * Removes an element from a pset.
159  *
160  * @param pset  the pset to delete in
161  * @param key   a pointer to the element to be deleted
162  * @param hash  the hash-value of the element
163  *
164  * @return
165  *    the pointer to the removed element
166  *
167  * @remark
168  *    The current implementation did not allow to remove non-existing elements.
169  *    @@@ so, does it do now?
170  *    Further, it is allowed to remove elements during an iteration
171  *    including the current one.
172  */
173 FIRM_API void *pset_remove(pset *pset, const void *key, unsigned hash);
174
175 /**
176  * Returns the first element of a pset.
177  *
178  * @param pset  the pset to iterate
179  *
180  * @return a pointer to the element or NULL if the set is empty
181  */
182 FIRM_API void *pset_first(pset *pset);
183
184 /**
185  * Returns the next element of a pset.
186  *
187  * @param pset  the pset to iterate
188  *
189  * @return a pointer to the next element or NULL if the
190  *         iteration is finished
191  */
192 FIRM_API void *pset_next(pset *pset);
193
194 /**
195  * Breaks the iteration of a set. Must be called before
196  * the next pset_first() call if the iteration was NOT
197  * finished.
198  *
199  * @param pset  the pset
200  */
201 FIRM_API void pset_break(pset *pset);
202
203 /**
204  * Iterates oven an pset.
205  *
206  * @param pset   the pset
207  * @param type   type of iterator variable
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) ((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 #include "../end.h"
264
265 #endif