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