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