Checks now the Load_mode
[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 /**
19  * The abstract type of a pset (Set of pointers).
20  *
21  * This kind of sets stores only pointer to elements, the elements itself
22  * must be stored somewere else.
23  *
24  * @see set
25  */
26 typedef struct pset pset;
27
28 /** The entry of a pset, representing an element pointer in the set and it's meta-information */
29 typedef struct {
30   unsigned hash;
31   void *dptr;
32 } pset_entry;
33
34 /**
35  * The type of a set compare function.
36  *
37  * @param elt   pointer to an element
38  * @param key   pointer to another element
39  *
40  * @return
41  *    0 if the elements are identically, non-zero else
42  */
43 typedef int (*pset_cmp_fun) (const void *elt, const void *key);
44
45 /**
46  * Creates a new pset.
47  *
48  * @param func    the compare function of this pset
49  * @param slots   number of initial slots
50  *
51  * @returns
52  *    created pset
53  */
54 pset *new_pset (pset_cmp_fun func, int slots);
55
56 /**
57  * Deletes a pset.
58  *
59  * @note
60  *    This does NOT delete the elements of this pset, just it's pointers!
61  */
62 void del_pset (pset *pset);
63
64 /**
65  * Returns the number of elements in a pset.
66  *
67  * @param pset   the pset
68  */
69 int pset_count (pset *pset);
70
71 /**
72  * Searches an element pointer in a pset.
73  *
74  * @param pset  the pset to search in
75  * @param key   the element to search
76  * @param hash  the hash value of key
77  *
78  * @return
79  *    the pointer of the found element in the pset of NULL if it was not found
80  */
81 void *pset_find (pset *pset, const void *key, unsigned hash);
82
83 /**
84  * Inserts an element pointer into a pset.
85  *
86  * @param pset  the pset to insert in
87  * @param key   a pointer to the element to be inserted
88  * @param hash  the hash-value of the element
89  *
90  * @return a pointer to the inserted element
91  *
92  * @note
93  *    It is not possible to insert on element more than once. If a element
94  *    that should be inserted is already in the set, this functions does
95  *    nothing but returning its already existing set_entry.
96
97  */
98 void *pset_insert (pset *pset, const void *key, unsigned hash);
99
100 /**
101  * Inserts an element pointer into a pset and returns its pset_entry.
102  *
103  * @param pset  the pset to insert in
104  * @param key   a pointer to the element to be inserted
105  * @param hash  the hash-value of the element
106  *
107  * @return a pointer to the pset_entry of the inserted element
108  *
109  * @note
110  *    It is not possible to insert on element more than once. If a element
111  *    that should be inserted is already in the pset, this functions does
112  *    nothing but returning its pset_entry.
113  */
114 pset_entry *pset_hinsert (pset *pset, const void *key, unsigned hash);
115
116 /**
117  * Removes an element from a pset.
118  *
119  * @param pset  the pset to insert in
120  * @param key   a pointer to the element to be inserted
121  * @param hash  the hash-value of the element
122  *
123  * @return
124  *    the pointer to the removed element
125  *
126  * @remark
127  *    The current implementation did not allow to remove non-existing elements.
128  *    Further, it is allowed to remove elements during an iteration
129  *    including the current one.
130  */
131 void *pset_remove (pset *pset, const void *key, unsigned hash);
132
133 /**
134  * Returns the first element of a pset.
135  *
136  * @param pset  the pset to iterate
137  *
138  * @return a pointer to the element or NULL if the set is empty
139  */
140 void *pset_first (pset *pset);
141
142 /**
143  * Returns the next element of a pset.
144  *
145  * @param pset  the pset to iterate
146  *
147  * @return a pointer to the next element or NULL if the
148  *         iteration is finished
149  */
150 void *pset_next (pset *pset);
151
152 /**
153  * Breaks the iteration of a set. Must be called before
154  * the next pset_first() call if the iteration was NOT
155  * finished.
156  *
157  * @param pset  the pset
158  */
159 void pset_break (pset *pset);
160
161 #define new_pset(cmp, slots) (PSET_TRACE (new_pset) ((cmp), (slots)))
162 #define pset_find(pset, key, hash) \
163   _pset_search ((pset), (key), (hash), _pset_find)
164 #define pset_insert(pset, key, hash) \
165   _pset_search ((pset), (key), (hash), _pset_insert)
166 #define pset_hinsert(pset, key, hash) \
167   ((pset_entry *)_pset_search ((pset), (key), (hash), _pset_hinsert))
168
169 #ifdef STATS
170 /**
171  * Prints statistics on a set to stdout.
172  *
173  * @param pset  the pset
174  */
175 void pset_stats (pset *pset);
176 #else
177 # define pset_stats(s) ((void)0)
178 #endif
179
180 #ifdef DEBUG
181 /**
182  * Describe a set by printing all elements
183  * to stdout.
184  */
185 void pset_describe (pset *);
186 #endif
187
188 /* @@@ NYI */
189 #define PSET_VRFY(pset) (void)0
190
191
192 /* Private */
193
194 typedef enum { _pset_find, _pset_insert, _pset_hinsert } _pset_action;
195
196 void *_pset_search (pset *, const void *, unsigned, _pset_action);
197
198 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
199 extern const char *pset_tag;
200 # ifdef PSET_ID
201 #   define PSET_TRACE pset_tag = SET_ID,
202 # else
203 #   define PSET_TRACE pset_tag = __FILE__,
204 # endif
205 #else /* !(DEBUG && HAVE_GNU_MALLOC) */
206 #   define PSET_TRACE
207 #endif /* !(DEBUG && HAVE_GNU_MALLOC) */
208
209 #endif