update copyright message
[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  * @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 #include "hashset.h"
54
55 #undef DO_REHASH
56 #undef ValueType
57 #undef HashSetIterator
58 #undef HashSet
59
60 typedef struct ir_nodeset_t          ir_nodeset_t;
61 typedef struct ir_nodeset_iterator_t ir_nodeset_iterator_t;
62
63 /**
64  * Initializes a nodeset with default size.
65  *
66  * @param nodeset      Pointer to allocated space for the nodeset
67  */
68 void ir_nodeset_init(ir_nodeset_t *nodeset);
69
70 /**
71  * Initializes a nodeset
72  *
73  * @param nodeset             Pointer to allocated space for the nodeset
74  * @param expected_elements   Number of elements expected in the nodeset (roughly)
75  */
76 void ir_nodeset_init_size(ir_nodeset_t *nodeset, size_t expected_elements);
77
78 /**
79  * Destroys a nodeset and frees the memory allocated for hashtable. The memory of
80  * the nodeset itself is not freed.
81  *
82  * @param nodeset   Pointer to the nodeset
83  */
84 void ir_nodeset_destroy(ir_nodeset_t *nodeset);
85
86 /**
87  * Allocates memory for a nodeset and initializes the set.
88  *
89  * @param expected_elements   Number of elements expected in the nodeset (roughly)
90  * @return The initialized nodeset
91  */
92 static INLINE ir_nodeset_t *ir_nodeset_new(size_t expected_elements) {
93         ir_nodeset_t *res = xmalloc(sizeof(*res));
94         ir_nodeset_init_size(res, expected_elements);
95         return res;
96 }
97
98 /**
99  * Destroys a nodeset and frees the memory of the nodeset itself.
100  */
101 static INLINE void ir_nodeset_del(ir_nodeset_t *nodeset) {
102         ir_nodeset_destroy(nodeset);
103         xfree(nodeset);
104 }
105
106 /**
107  * Inserts a node into a nodeset.
108  *
109  * @param nodeset   Pointer to the nodeset
110  * @param node      node to insert into the nodeset
111  * @returns         1 if the element has been inserted,
112  *                  0 if it was already there
113  */
114 int ir_nodeset_insert(ir_nodeset_t *nodeset, ir_node *node);
115
116
117 /**
118  * Removes a node from a nodeset. Does nothing if the nodeset doesn't contain
119  * the node.
120  *
121  * @param nodeset  Pointer to the nodeset
122  * @param node     Node to remove from the nodeset
123  */
124 void ir_nodeset_remove(ir_nodeset_t *nodeset, const ir_node *node);
125
126 /**
127  * Tests whether a nodeset contains a specific node
128  *
129  * @param nodeset   Pointer to the nodeset
130  * @param node      The pointer to find
131  * @returns         1 if nodeset contains the node, 0 else
132  */
133 int ir_nodeset_contains(const ir_nodeset_t *nodeset, const ir_node *node);
134
135 /**
136  * Returns the number of pointers contained in the nodeset
137  *
138  * @param nodeset   Pointer to the nodeset
139  * @returns       Number of pointers contained in the nodeset
140  */
141 size_t ir_nodeset_size(const ir_nodeset_t *nodeset);
142
143 /**
144  * Initializes a nodeset iterator. Sets the iterator before the first element in
145  * the nodeset.
146  *
147  * @param iterator   Pointer to already allocated iterator memory
148  * @param nodeset       Pointer to the nodeset
149  */
150 void ir_nodeset_iterator_init(ir_nodeset_iterator_t *iterator,
151                               const ir_nodeset_t *nodeset);
152
153 /**
154  * Advances the iterator and returns the current element or NULL if all elements
155  * in the nodeset have been processed.
156  * @attention It is not allowed to use nodeset_insert or nodeset_remove while
157  *            iterating over a nodeset.
158  *
159  * @param iterator  Pointer to the nodeset iterator.
160  * @returns         Next element in the nodeset or NULL
161  */
162 ir_node *ir_nodeset_iterator_next(ir_nodeset_iterator_t *iterator);
163
164 /**
165  * Removes the element the iterator currently points to
166  *
167  * @param nodeset   Pointer to the nodeset
168  * @param iterator  Pointer to the nodeset iterator.
169  */
170 void ir_nodeset_remove_iterator(ir_nodeset_t *nodeset,
171                                 const ir_nodeset_iterator_t *iterator);
172
173 #define foreach_ir_nodeset(nodeset, irn, iter) \
174         for(ir_nodeset_iterator_init(&iter, nodeset), \
175         irn = ir_nodeset_iterator_next(&iter);    \
176                 irn != NULL; irn = ir_nodeset_iterator_next(&iter))
177
178 #endif