2 * Copyright (C) 1995-2007 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)
27 * @note Actually the bits to make the behaviour deterministic are not
30 #ifndef _FIRM_IRNODESET_H_
31 #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*
43 #undef HashSetIterator
46 typedef struct ir_nodeset_t ir_nodeset_t;
47 typedef struct ir_nodeset_iterator_t ir_nodeset_iterator_t;
50 * Initializes a nodeset with default size.
52 * @param nodeset Pointer to allocated space for the nodeset
54 void ir_nodeset_init(ir_nodeset_t *nodeset);
57 * Initializes a nodeset
59 * @param nodeset Pointer to allocated space for the nodeset
60 * @param expected_elements Number of elements expected in the nodeset (roughly)
62 void ir_nodeset_init_size(ir_nodeset_t *nodeset, size_t expected_elements);
65 * Destroys a nodeset and frees the memory allocated for hashtable. The memory of
66 * the nodeset itself is not freed.
68 * @param nodeset Pointer to the nodeset
70 void ir_nodeset_destroy(ir_nodeset_t *nodeset);
73 * Allocates memory for a nodeset and initializes the set.
75 * @param expected_elements Number of elements expected in the nodeset (roughly)
76 * @return The initialized nodeset
78 static INLINE ir_nodeset_t *ir_nodeset_new(size_t expected_elements) {
79 ir_nodeset_t *res = xmalloc(sizeof(*res));
80 ir_nodeset_init_size(res, expected_elements);
85 * Destroys a nodeset and frees the memory of the nodeset itself.
87 static INLINE void ir_nodeset_del(ir_nodeset_t *nodeset) {
88 ir_nodeset_destroy(nodeset);
93 * Inserts a node into a nodeset.
95 * @param nodeset Pointer to the nodeset
96 * @param node node to insert into the nodeset
97 * @returns 1 if the element has been inserted,
98 * 0 if it was already there
100 int ir_nodeset_insert(ir_nodeset_t *nodeset, ir_node *node);
103 * Removes a node from a nodeset. Does nothing if the nodeset doesn't contain
106 * @param nodeset Pointer to the nodeset
107 * @param node Node to remove from the nodeset
109 void ir_nodeset_remove(ir_nodeset_t *nodeset, const ir_node *node);
112 * Tests whether a nodeset contains a specific node
114 * @param nodeset Pointer to the nodeset
115 * @param node The pointer to find
116 * @returns 1 if nodeset contains the node, 0 else
118 int ir_nodeset_contains(const ir_nodeset_t *nodeset, const ir_node *node);
121 * Returns the number of pointers contained in the nodeset
123 * @param nodeset Pointer to the nodeset
124 * @returns Number of pointers contained in the nodeset
126 size_t ir_nodeset_size(const ir_nodeset_t *nodeset);
129 * Initializes a nodeset iterator. Sets the iterator before the first element in
132 * @param iterator Pointer to already allocated iterator memory
133 * @param nodeset Pointer to the nodeset
135 void ir_nodeset_iterator_init(ir_nodeset_iterator_t *iterator,
136 const ir_nodeset_t *nodeset);
139 * Advances the iterator and returns the current element or NULL if all elements
140 * in the nodeset have been processed.
141 * @attention It is not allowed to use nodeset_insert or nodeset_remove while
142 * iterating over a nodeset.
144 * @param iterator Pointer to the nodeset iterator.
145 * @returns Next element in the nodeset or NULL
147 ir_node *ir_nodeset_iterator_next(ir_nodeset_iterator_t *iterator);
150 * Removes the element the iterator currently points to
152 * @param nodeset Pointer to the nodeset
153 * @param iterator Pointer to the nodeset iterator.
155 void ir_nodeset_remove_iterator(ir_nodeset_t *nodeset,
156 const ir_nodeset_iterator_t *iterator);
158 #define foreach_ir_nodeset(nodeset, irn, iter) \
159 for(ir_nodeset_iterator_init(&iter, nodeset), \
160 irn = ir_nodeset_iterator_next(&iter); \
161 irn != NULL; irn = ir_nodeset_iterator_next(&iter))