2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @author Matthias Braun
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
29 #ifndef _FIRM_IRNODESET_H_
30 #define _FIRM_IRNODESET_H_
33 #include "firm_types.h"
36 #define HashSet ir_nodeset_t
37 #define HashSetIterator ir_nodeset_iterator_t
38 #define ValueType ir_node*
45 #undef HashSetIterator
48 typedef struct ir_nodeset_t ir_nodeset_t;
49 typedef struct ir_nodeset_iterator_t ir_nodeset_iterator_t;
52 * Initializes a nodeset with default size.
54 * @param nodeset Pointer to allocated space for the nodeset
56 void ir_nodeset_init(ir_nodeset_t *nodeset);
59 * Initializes a nodeset
61 * @param nodeset Pointer to allocated space for the nodeset
62 * @param expected_elements Number of elements expected in the nodeset (roughly)
64 void ir_nodeset_init_size(ir_nodeset_t *nodeset, size_t expected_elements);
67 * Destroys a nodeset and frees the memory allocated for hashtable. The memory of
68 * the nodeset itself is not freed.
70 * @param nodeset Pointer to the nodeset
72 void ir_nodeset_destroy(ir_nodeset_t *nodeset);
75 * Allocates memory for a nodeset and initializes the set.
77 * @param expected_elements Number of elements expected in the nodeset (roughly)
78 * @return The initialized nodeset
80 static inline ir_nodeset_t *ir_nodeset_new(size_t expected_elements) {
81 ir_nodeset_t *res = XMALLOC(ir_nodeset_t);
82 ir_nodeset_init_size(res, expected_elements);
87 * Destroys a nodeset and frees the memory of the nodeset itself.
89 static inline void ir_nodeset_del(ir_nodeset_t *nodeset) {
90 ir_nodeset_destroy(nodeset);
95 * Inserts a node into a nodeset.
97 * @param nodeset Pointer to the nodeset
98 * @param node node to insert into the nodeset
99 * @returns true if the element has been inserted,
100 * false if it was already there
102 bool ir_nodeset_insert(ir_nodeset_t *nodeset, ir_node *node);
106 * Removes a node from a nodeset. Does nothing if the nodeset doesn't contain
109 * @param nodeset Pointer to the nodeset
110 * @param node Node to remove from the nodeset
112 void ir_nodeset_remove(ir_nodeset_t *nodeset, const ir_node *node);
115 * Tests whether a nodeset contains a specific node
117 * @param nodeset Pointer to the nodeset
118 * @param node The pointer to find
120 bool ir_nodeset_contains(const ir_nodeset_t *nodeset, const ir_node *node);
123 * Returns the number of pointers contained in the nodeset
125 * @param nodeset Pointer to the nodeset
126 * @returns Number of pointers contained in the nodeset
128 size_t ir_nodeset_size(const ir_nodeset_t *nodeset);
131 * Initializes a nodeset iterator. Sets the iterator before the first element in
134 * @param iterator Pointer to already allocated iterator memory
135 * @param nodeset Pointer to the nodeset
137 void ir_nodeset_iterator_init(ir_nodeset_iterator_t *iterator,
138 const ir_nodeset_t *nodeset);
141 * Advances the iterator and returns the current element or NULL if all elements
142 * in the nodeset have been processed.
143 * @attention It is not allowed to use nodeset_insert or nodeset_remove while
144 * iterating over a nodeset.
146 * @param iterator Pointer to the nodeset iterator.
147 * @returns Next element in the nodeset or NULL
149 ir_node *ir_nodeset_iterator_next(ir_nodeset_iterator_t *iterator);
152 * Removes the element the iterator currently points to
154 * @param nodeset Pointer to the nodeset
155 * @param iterator Pointer to the nodeset iterator.
157 void ir_nodeset_remove_iterator(ir_nodeset_t *nodeset,
158 const ir_nodeset_iterator_t *iterator);
160 static inline ir_node *ir_nodeset_first(ir_nodeset_t const *const nodeset)
162 ir_nodeset_iterator_t iter;
163 ir_nodeset_iterator_init(&iter, nodeset);
164 return ir_nodeset_iterator_next(&iter);
167 #define foreach_ir_nodeset(nodeset, irn, iter) \
168 for(ir_nodeset_iterator_init(&iter, nodeset), \
169 irn = ir_nodeset_iterator_next(&iter); \
170 irn != NULL; irn = ir_nodeset_iterator_next(&iter))