fix for icc compilation
[libfirm] / ir / ir / irnodeset.h
1 /*
2  * Copyright (C) 1995-2007 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  * @author    Matthias Braun
23  * @date      30.03.2007
24  * @brief     A nodeset. This should be prefered over a simple pset, because it
25  *            tries to guarantee deterministic behavior. (and is faster)
26  * @version   $Id$
27  * @note      Actually the bits to make the behaviour deterministic are not
28  *            implemented yet...
29  */
30 #ifndef _FIRM_IRNODESET_H_
31 #define _FIRM_IRNODESET_H_
32
33 #include "firm_config.h"
34
35 #include "firm_types.h"
36 #include "xmalloc.h"
37
38 /*
39  * sebastian experimental:
40  * use ordered arrays as node sets.
41  * the guys here have made good experiences with that.
42  * Internally we use normal Firm arrays and binary
43  * search for locating the elements. Using arrays should
44  * give the sets a small footprint.
45  */
46 #undef  IR_NODESET_USE_ORDERED_SETS
47
48 #define HashSet          ir_nodeset_t
49 #define HashSetIterator  ir_nodeset_iterator_t
50 #define ValueType        ir_node*
51 #define DO_REHASH
52
53 #ifdef IR_NODESET_USE_ORDERED_SETS
54 #include "arrayset.h"
55 #else
56 #include "hashset.h"
57 #endif
58
59 #undef DO_REHASH
60 #undef ValueType
61 #undef HashSetIterator
62 #undef HashSet
63
64 typedef struct ir_nodeset_t          ir_nodeset_t;
65 typedef struct ir_nodeset_iterator_t ir_nodeset_iterator_t;
66
67 /**
68  * Initializes a nodeset with default size.
69  *
70  * @param nodeset      Pointer to allocated space for the nodeset
71  */
72 void ir_nodeset_init(ir_nodeset_t *nodeset);
73
74 /**
75  * Initializes a nodeset
76  *
77  * @param nodeset             Pointer to allocated space for the nodeset
78  * @param expected_elements   Number of elements expected in the nodeset (roughly)
79  */
80 void ir_nodeset_init_size(ir_nodeset_t *nodeset, size_t expected_elements);
81
82 /**
83  * Destroys a nodeset and frees the memory allocated for hashtable. The memory of
84  * the nodeset itself is not freed.
85  *
86  * @param nodeset   Pointer to the nodeset
87  */
88 void ir_nodeset_destroy(ir_nodeset_t *nodeset);
89
90 /**
91  * Allocates memory for a nodeset and initializes the set.
92  *
93  * @param expected_elements   Number of elements expected in the nodeset (roughly)
94  * @return The initialized nodeset
95  */
96 static INLINE ir_nodeset_t *ir_nodeset_new(size_t expected_elements) {
97         ir_nodeset_t *res = xmalloc(sizeof(*res));
98         ir_nodeset_init_size(res, expected_elements);
99         return res;
100 }
101
102 /**
103  * Destroys a nodeset and frees the memory of the nodeset itself.
104  */
105 static INLINE void ir_nodeset_del(ir_nodeset_t *nodeset) {
106         ir_nodeset_destroy(nodeset);
107         xfree(nodeset);
108 }
109
110 /**
111  * Inserts a node into a nodeset.
112  *
113  * @param nodeset   Pointer to the nodeset
114  * @param node      node to insert into the nodeset
115  * @returns         1 if the element has been inserted,
116  *                  0 if it was already there
117  */
118 int ir_nodeset_insert(ir_nodeset_t *nodeset, ir_node *node);
119
120
121 /**
122  * Removes a node from a nodeset. Does nothing if the nodeset doesn't contain
123  * the node.
124  *
125  * @param nodeset  Pointer to the nodeset
126  * @param node     Node to remove from the nodeset
127  */
128 void ir_nodeset_remove(ir_nodeset_t *nodeset, const ir_node *node);
129
130 /**
131  * Tests whether a nodeset contains a specific node
132  *
133  * @param nodeset   Pointer to the nodeset
134  * @param node      The pointer to find
135  * @returns         1 if nodeset contains the node, 0 else
136  */
137 int ir_nodeset_contains(const ir_nodeset_t *nodeset, const ir_node *node);
138
139 /**
140  * Returns the number of pointers contained in the nodeset
141  *
142  * @param nodeset   Pointer to the nodeset
143  * @returns       Number of pointers contained in the nodeset
144  */
145 size_t ir_nodeset_size(const ir_nodeset_t *nodeset);
146
147 /**
148  * Initializes a nodeset iterator. Sets the iterator before the first element in
149  * the nodeset.
150  *
151  * @param iterator   Pointer to already allocated iterator memory
152  * @param nodeset       Pointer to the nodeset
153  */
154 void ir_nodeset_iterator_init(ir_nodeset_iterator_t *iterator,
155                               const ir_nodeset_t *nodeset);
156
157 /**
158  * Advances the iterator and returns the current element or NULL if all elements
159  * in the nodeset have been processed.
160  * @attention It is not allowed to use nodeset_insert or nodeset_remove while
161  *            iterating over a nodeset.
162  *
163  * @param iterator  Pointer to the nodeset iterator.
164  * @returns         Next element in the nodeset or NULL
165  */
166 ir_node *ir_nodeset_iterator_next(ir_nodeset_iterator_t *iterator);
167
168 /**
169  * Removes the element the iterator currently points to
170  *
171  * @param nodeset   Pointer to the nodeset
172  * @param iterator  Pointer to the nodeset iterator.
173  */
174 void ir_nodeset_remove_iterator(ir_nodeset_t *nodeset,
175                                 const ir_nodeset_iterator_t *iterator);
176
177 #define foreach_ir_nodeset(nodeset, irn, iter) \
178         for(ir_nodeset_iterator_init(&iter, nodeset), \
179         irn = ir_nodeset_iterator_next(&iter);    \
180                 irn != NULL; irn = ir_nodeset_iterator_next(&iter))
181
182
183 #ifdef IR_NODESET_USE_ORDERED_SETS
184
185 /**
186  * Insert an element quickly into from the set.
187  * This method may destroy internal invariats of the set (think of sorted arrays).
188  * All calls to other routines but
189  * - iteration
190  * - get the number of elements in the set
191  * will not work until ir_nodeset_fixup() was called.
192  * @param nodeset The nodeset.
193  * @param node    The node to insert.
194  */
195 void ir_nodeset_insert_quick(ir_nodeset_t *nodeset, ir_node *node);
196
197 /**
198  * Remove an element quickly from the set.
199  * This method may destroy internal invariats of the set (think of sorted arrays).
200  * All calls to other routines but
201  * - iteration
202  * - get the number of elements in the set
203  * will not work until ir_nodeset_fixup() was called.
204  * @param nodeset The nodeset.
205  * @param node    The node to delete.
206  */
207 void ir_nodeset_remove_quick(ir_nodeset_t *nodeset, const ir_node *node);
208
209 /**
210  * Fixes up internal state of the set.
211  * Is needed when one of the _quick functions was called.
212  * @param nodeset The nodeset.
213  */
214 void ir_nodeset_fixup(ir_nodeset_t *nodeset);
215
216 #else
217
218 #define ir_nodeset_remove_quick ir_nodeset_remove
219 #define ir_nodeset_insert_quick ir_nodeset_insert
220 #define ir_nodeset_fixup(set)
221
222 #endif /* IR_NODESET_USE_ORDERED_SETS */
223
224 #endif