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