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