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