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