535c1441e0ca8bf8270e0308f034adde8311d951
[libfirm] / ir / ir / irnodeset.h
1 /*
2  * Copyright (C) 1995-2008 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  * @note      Actually the bits to make the behaviour deterministic are not
27  *            implemented yet...
28  */
29 #ifndef _FIRM_IRNODESET_H_
30 #define _FIRM_IRNODESET_H_
31
32 #include "firm_types.h"
33 #include "xmalloc.h"
34
35 /*
36  * sebastian experimental:
37  * use ordered arrays as node sets.
38  * the guys here have made good experiences with that.
39  * Internally we use normal Firm arrays and binary
40  * search for locating the elements. Using arrays should
41  * give the sets a small footprint.
42  */
43 #undef  IR_NODESET_USE_ORDERED_SETS
44
45 #define HashSet          ir_nodeset_t
46 #define HashSetIterator  ir_nodeset_iterator_t
47 #define ValueType        ir_node*
48 #define DO_REHASH
49
50 #include "hashset.h"
51
52 #undef DO_REHASH
53 #undef ValueType
54 #undef HashSetIterator
55 #undef HashSet
56
57 typedef struct ir_nodeset_t          ir_nodeset_t;
58 typedef struct ir_nodeset_iterator_t ir_nodeset_iterator_t;
59
60 /**
61  * Initializes a nodeset with default size.
62  *
63  * @param nodeset      Pointer to allocated space for the nodeset
64  */
65 void ir_nodeset_init(ir_nodeset_t *nodeset);
66
67 /**
68  * Initializes a nodeset
69  *
70  * @param nodeset             Pointer to allocated space for the nodeset
71  * @param expected_elements   Number of elements expected in the nodeset (roughly)
72  */
73 void ir_nodeset_init_size(ir_nodeset_t *nodeset, size_t expected_elements);
74
75 /**
76  * Destroys a nodeset and frees the memory allocated for hashtable. The memory of
77  * the nodeset itself is not freed.
78  *
79  * @param nodeset   Pointer to the nodeset
80  */
81 void ir_nodeset_destroy(ir_nodeset_t *nodeset);
82
83 /**
84  * Allocates memory for a nodeset and initializes the set.
85  *
86  * @param expected_elements   Number of elements expected in the nodeset (roughly)
87  * @return The initialized nodeset
88  */
89 static inline ir_nodeset_t *ir_nodeset_new(size_t expected_elements) {
90         ir_nodeset_t *res = XMALLOC(ir_nodeset_t);
91         ir_nodeset_init_size(res, expected_elements);
92         return res;
93 }
94
95 /**
96  * Destroys a nodeset and frees the memory of the nodeset itself.
97  */
98 static inline void ir_nodeset_del(ir_nodeset_t *nodeset) {
99         ir_nodeset_destroy(nodeset);
100         xfree(nodeset);
101 }
102
103 /**
104  * Inserts a node into a nodeset.
105  *
106  * @param nodeset   Pointer to the nodeset
107  * @param node      node to insert into the nodeset
108  * @returns         1 if the element has been inserted,
109  *                  0 if it was already there
110  */
111 int ir_nodeset_insert(ir_nodeset_t *nodeset, ir_node *node);
112
113
114 /**
115  * Removes a node from a nodeset. Does nothing if the nodeset doesn't contain
116  * the node.
117  *
118  * @param nodeset  Pointer to the nodeset
119  * @param node     Node to remove from the nodeset
120  */
121 void ir_nodeset_remove(ir_nodeset_t *nodeset, const ir_node *node);
122
123 /**
124  * Tests whether a nodeset contains a specific node
125  *
126  * @param nodeset   Pointer to the nodeset
127  * @param node      The pointer to find
128  * @returns         1 if nodeset contains the node, 0 else
129  */
130 int ir_nodeset_contains(const ir_nodeset_t *nodeset, const ir_node *node);
131
132 /**
133  * Returns the number of pointers contained in the nodeset
134  *
135  * @param nodeset   Pointer to the nodeset
136  * @returns       Number of pointers contained in the nodeset
137  */
138 size_t ir_nodeset_size(const ir_nodeset_t *nodeset);
139
140 /**
141  * Initializes a nodeset iterator. Sets the iterator before the first element in
142  * the nodeset.
143  *
144  * @param iterator   Pointer to already allocated iterator memory
145  * @param nodeset       Pointer to the nodeset
146  */
147 void ir_nodeset_iterator_init(ir_nodeset_iterator_t *iterator,
148                               const ir_nodeset_t *nodeset);
149
150 /**
151  * Advances the iterator and returns the current element or NULL if all elements
152  * in the nodeset have been processed.
153  * @attention It is not allowed to use nodeset_insert or nodeset_remove while
154  *            iterating over a nodeset.
155  *
156  * @param iterator  Pointer to the nodeset iterator.
157  * @returns         Next element in the nodeset or NULL
158  */
159 ir_node *ir_nodeset_iterator_next(ir_nodeset_iterator_t *iterator);
160
161 /**
162  * Removes the element the iterator currently points to
163  *
164  * @param nodeset   Pointer to the nodeset
165  * @param iterator  Pointer to the nodeset iterator.
166  */
167 void ir_nodeset_remove_iterator(ir_nodeset_t *nodeset,
168                                 const ir_nodeset_iterator_t *iterator);
169
170 #define foreach_ir_nodeset(nodeset, irn, iter) \
171         for(ir_nodeset_iterator_init(&iter, nodeset), \
172         irn = ir_nodeset_iterator_next(&iter);    \
173                 irn != NULL; irn = ir_nodeset_iterator_next(&iter))
174
175 #endif