remove STATS code alternative
[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
35 #include "../begin.h"
36
37 /**
38  * The default comparison function for pointers.
39  * @param x A pointer.
40  * @param y A pointer.
41  * @return 0 if @p x and @p y are equal. Some value != 0 otherwise.
42  */
43 FIRM_API int pset_default_ptr_cmp(const void *x, const void *y);
44
45 /**
46  * The abstract type of a pset (Set of pointers).
47  *
48  * This kind of sets stores only pointer to elements, the elements itself
49  * must be stored somewhere else.
50  *
51  * @see set
52  */
53 typedef struct pset pset;
54
55 /** Inserts into pointer set with default hash function. */
56 #define pset_insert_ptr(set,key)  pset_insert(set, key, hash_ptr(key))
57 /** Inserts into pointer set with default hash function and return entry */
58 #define pset_hinsert_ptr(set,key) pset_hinsert(set, key, hash_ptr(key))
59 /** Removes pointer from pointer set with default hash function */
60 #define pset_remove_ptr(set,key)  pset_remove(set, key, hash_ptr(key))
61 /** Finds pointer in pointer set with default hash function */
62 #define pset_find_ptr(set,key)    pset_find(set, key, hash_ptr(key))
63 /** Creates new pointer set with default compare function */
64 #define pset_new_ptr(slots)       new_pset(pset_default_ptr_cmp, slots)
65 /** Creates new pointer set with default compare function and default size */
66 #define pset_new_ptr_default()    pset_new_ptr(64)
67
68 /** The entry of a pset, representing an element pointer in the set and its meta-information */
69 typedef struct {
70   unsigned hash;
71   void *dptr;
72 } pset_entry;
73
74 /**
75  * The type of a set compare function.
76  *
77  * @param elt   pointer to an element
78  * @param key   pointer to another element
79  *
80  * @return
81  *    0 if the elements are identically, non-zero else
82  */
83 typedef int (*pset_cmp_fun) (const void *elt, const void *key);
84
85 /**
86  * Creates a new pset.
87  *
88  * @param func    The compare function of this pset.
89  * @param slots   Initial number of collision chains.  I.e., \#slots
90  *                different keys can be hashed without collisions.
91  *
92  * @returns
93  *    created pset
94  */
95 FIRM_API pset *new_pset(pset_cmp_fun func, size_t slots);
96
97 /**
98  * Deletes a pset.
99  *
100  * @param pset   the pset
101  *
102  * @note
103  *    This does NOT delete the elements of this pset, just its pointers!
104  */
105 FIRM_API void del_pset(pset *pset);
106
107 /**
108  * Returns the number of elements in a pset.
109  *
110  * @param pset   the pset
111  */
112 FIRM_API size_t pset_count(pset *pset);
113
114 /**
115  * Searches an element pointer in a pset.
116  *
117  * @param pset  the pset to search in
118  * @param key   the element to search
119  * @param hash  the hash value of key
120  *
121  * @return
122  *    the pointer of the found element in the pset or NULL if it was not found
123  */
124 FIRM_API void *pset_find(pset *pset, const void *key, unsigned hash);
125
126 /**
127  * Inserts an element pointer into a pset.
128  *
129  * @param pset  the pset to insert in
130  * @param key   a pointer to the element to be inserted
131  * @param hash  the hash-value of the element
132  *
133  * @return a pointer to the inserted element
134  *
135  * @note
136  *    It is not possible to insert an element more than once. If an element
137  *    that should be inserted is already in the set, this functions does
138  *    nothing but returning its already existing set_entry.
139
140  */
141 FIRM_API void *pset_insert(pset *pset, const void *key, unsigned hash);
142
143 /**
144  * Inserts an element pointer into a pset and returns its pset_entry.
145  *
146  * @param pset  the pset to insert in
147  * @param key   a pointer to the element to be inserted
148  * @param hash  the hash-value of the element
149  *
150  * @return a pointer to the pset_entry of the inserted element
151  *
152  * @note
153  *    It is not possible to insert an element more than once. If an element
154  *    that should be inserted is already in the pset, this functions does
155  *    nothing but returning its pset_entry.
156  */
157 FIRM_API pset_entry *pset_hinsert(pset *pset, const void *key, unsigned hash);
158
159 /**
160  * Removes an element from a pset.
161  *
162  * @param pset  the pset to delete in
163  * @param key   a pointer to the element to be deleted
164  * @param hash  the hash-value of the element
165  *
166  * @return
167  *    the pointer to the removed element
168  *
169  * @remark
170  *    The current implementation did not allow to remove non-existing elements.
171  *    @@@ so, does it do now?
172  *    Further, it is allowed to remove elements during an iteration
173  *    including the current one.
174  */
175 FIRM_API void *pset_remove(pset *pset, const void *key, unsigned hash);
176
177 /**
178  * Returns the first element of a pset.
179  *
180  * @param pset  the pset to iterate
181  *
182  * @return a pointer to the element or NULL if the set is empty
183  */
184 FIRM_API void *pset_first(pset *pset);
185
186 /**
187  * Returns the next element of a pset.
188  *
189  * @param pset  the pset to iterate
190  *
191  * @return a pointer to the next element or NULL if the
192  *         iteration is finished
193  */
194 FIRM_API void *pset_next(pset *pset);
195
196 /**
197  * Breaks the iteration of a set. Must be called before
198  * the next pset_first() call if the iteration was NOT
199  * finished.
200  *
201  * @param pset  the pset
202  */
203 FIRM_API void pset_break(pset *pset);
204
205 /**
206  * Iterates oven an pset.
207  *
208  * @param pset   the pset
209  * @param type   type of iterator variable
210  * @param entry  the iterator
211  */
212 #define foreach_pset(pset, type, entry) for (entry = (type)pset_first(pset); entry; entry = (type)pset_next(pset))
213
214 /**
215  * Inserts all elements of the pointer set src into
216  * the set target (union).
217  *
218  * @param target  the target set, will contain the union
219  * @param src     a set, will not be changed
220  */
221 FIRM_API void pset_insert_pset_ptr(pset *target, pset *src);
222
223 #define new_pset(cmp, slots) ((new_pset) ((cmp), (slots)))
224 #define pset_find(pset, key, hash) \
225   _pset_search ((pset), (key), (hash), _pset_find)
226 #define pset_insert(pset, key, hash) \
227   _pset_search ((pset), (key), (hash), _pset_insert)
228 #define pset_hinsert(pset, key, hash) \
229   ((pset_entry *)_pset_search ((pset), (key), (hash), _pset_hinsert))
230
231 /** @privatesection */
232
233 typedef enum { _pset_find, _pset_insert, _pset_hinsert } _pset_action;
234
235 FIRM_API void *_pset_search(pset *, const void *, unsigned, _pset_action);
236
237 #include "../end.h"
238
239 #endif