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