Added convenience macros for pset creation and introduced default
[libfirm] / ir / adt / pset.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/adt/pset.h
4  * Purpose:     Declarations for pset.
5  * Author:      Markus Armbruster
6  * Modified by:
7  * Created:     1999 by getting from fiasco
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1995, 1996 Markus Armbruster
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifndef _PSET_H
14 #define _PSET_H
15
16 #include <stddef.h>
17
18 #include "hashptr.h"
19 #include "iterator.h"
20
21 /**
22  * The default comparison function for pointers.
23  * @param x A pointer.
24  * @param y A pointer.
25  * @return 0 if @p x and @p y are equal. Some value != 0 otherwise.
26  */
27 int pset_default_ptr_cmp(const void *x, const void *y);
28
29 /*
30  * Define some convenience macros using the predefined hash function.
31  */
32 #define pset_insert_ptr(set,key) pset_insert(set, key, HASH_PTR(key))
33 #define pset_hinsert_ptr(set,key) pset_hinsert(set, key, HASH_PTR(key))
34 #define pset_remove_ptr(set,key) pset_remove(set, key, HASH_PTR(key))
35 #define pset_find_ptr(set,key) pset_find(set, key, HASH_PTR(key))
36 #define pset_new_ptr(slots) new_pset(pset_default_ptr_cmp, slots)
37 #define pset_new_ptr_default() pset_new_ptr(64)
38
39 /**
40  * The abstract type of a pset (Set of pointers).
41  *
42  * This kind of sets stores only pointer to elements, the elements itself
43  * must be stored somewere else.
44  *
45  * @see set
46  */
47 typedef struct pset pset;
48
49 /** The entry of a pset, representing an element pointer in the set and it's meta-information */
50 typedef struct {
51   unsigned hash;
52   void *dptr;
53 } pset_entry;
54
55 /**
56  * The type of a set compare function.
57  *
58  * @param elt   pointer to an element
59  * @param key   pointer to another element
60  *
61  * @return
62  *    0 if the elements are identically, non-zero else
63  */
64 typedef int (*pset_cmp_fun) (const void *elt, const void *key);
65
66 /**
67  * Creates a new pset.
68  *
69  * @param func    The compare function of this pset.
70  * @param slots   Initial number of collision chains.  I.e., #slots
71  *                different keys can be hashed without collisions.
72  *
73  * @returns
74  *    created pset
75  */
76 pset *new_pset (pset_cmp_fun func, int slots);
77
78 /**
79  * Deletes a pset.
80  *
81  * @param pset   the pset
82  *
83  * @note
84  *    This does NOT delete the elements of this pset, just it's pointers!
85  */
86 void del_pset (pset *pset);
87
88 /**
89  * Returns the number of elements in a pset.
90  *
91  * @param pset   the pset
92  */
93 int pset_count (pset *pset);
94
95 /**
96  * Searches an element pointer in a pset.
97  *
98  * @param pset  the pset to search in
99  * @param key   the element to search
100  * @param hash  the hash value of key
101  *
102  * @return
103  *    the pointer of the found element in the pset of NULL if it was not found
104  */
105 void *pset_find (pset *pset, const void *key, unsigned hash);
106
107 /**
108  * Inserts an element pointer into a pset.
109  *
110  * @param pset  the pset to insert in
111  * @param key   a pointer to the element to be inserted
112  * @param hash  the hash-value of the element
113  *
114  * @return a pointer to the inserted element
115  *
116  * @note
117  *    It is not possible to insert on element more than once. If a element
118  *    that should be inserted is already in the set, this functions does
119  *    nothing but returning its already existing set_entry.
120
121  */
122 void *pset_insert (pset *pset, const void *key, unsigned hash);
123
124 /**
125  * Inserts an element pointer into a pset and returns its pset_entry.
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 pset_entry of the inserted element
132  *
133  * @note
134  *    It is not possible to insert on element more than once. If a element
135  *    that should be inserted is already in the pset, this functions does
136  *    nothing but returning its pset_entry.
137  */
138 pset_entry *pset_hinsert (pset *pset, const void *key, unsigned hash);
139
140 /**
141  * Removes an element from a pset.
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
148  *    the pointer to the removed element
149  *
150  * @remark
151  *    The current implementation did not allow to remove non-existing elements.
152  *    Further, it is allowed to remove elements during an iteration
153  *    including the current one.
154  */
155 void *pset_remove (pset *pset, const void *key, unsigned hash);
156
157 /**
158  * Returns the first element of a pset.
159  *
160  * @param pset  the pset to iterate
161  *
162  * @return a pointer to the element or NULL if the set is empty
163  */
164 void *pset_first (pset *pset);
165
166 /**
167  * Returns the next element of a pset.
168  *
169  * @param pset  the pset to iterate
170  *
171  * @return a pointer to the next element or NULL if the
172  *         iteration is finished
173  */
174 void *pset_next (pset *pset);
175
176 /**
177  * Breaks the iteration of a set. Must be called before
178  * the next pset_first() call if the iteration was NOT
179  * finished.
180  *
181  * @param pset  the pset
182  */
183 void pset_break (pset *pset);
184
185 #define new_pset(cmp, slots) (PSET_TRACE (new_pset) ((cmp), (slots)))
186 #define pset_find(pset, key, hash) \
187   _pset_search ((pset), (key), (hash), _pset_find)
188 #define pset_insert(pset, key, hash) \
189   _pset_search ((pset), (key), (hash), _pset_insert)
190 #define pset_hinsert(pset, key, hash) \
191   ((pset_entry *)_pset_search ((pset), (key), (hash), _pset_hinsert))
192
193 #ifdef STATS
194 /**
195  * Prints statistics on a set to stdout.
196  *
197  * @param pset  the pset
198  */
199 void pset_stats (pset *pset);
200 #else
201 # define pset_stats(s) ((void)0)
202 #endif
203
204 #ifdef DEBUG
205 /**
206  * Describe a pset.
207  *
208  * Writes a description of a set to stdout. The description includes:
209  * - a header telling how many elements (nkey) and segments (nseg) are in use
210  * - for every collision chain the number of element with its hash values
211  *
212  * @param pset  the pset
213  */
214 void pset_describe (pset *pset);
215 #endif
216
217 /* @@@ NYI */
218 #define PSET_VRFY(pset) (void)0
219
220
221 /* Private */
222
223 typedef enum { _pset_find, _pset_insert, _pset_hinsert } _pset_action;
224
225 void *_pset_search (pset *, const void *, unsigned, _pset_action);
226
227 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
228 extern const char *pset_tag;
229 # ifdef PSET_ID
230 #   define PSET_TRACE pset_tag = SET_ID,
231 # else
232 #   define PSET_TRACE pset_tag = __FILE__,
233 # endif
234 #else /* !(DEBUG && HAVE_GNU_MALLOC) */
235 #   define PSET_TRACE
236 #endif /* !(DEBUG && HAVE_GNU_MALLOC) */
237
238 #endif