71e48fb5ae18be4f40e67aeba561ec860bf8385e
[libfirm] / ir / ir / irnodeset.h
1 /**
2  * @file
3  * @author    Matthias Braun
4  * @date      30.03.2007
5  * @brief     A nodeset. This should be prefered over a simple pset, because it
6  *            tries to guarantee deterministic behavior. (and is faster)
7  * @version   $Id$
8  */
9 #ifndef _FIRM_IRNODESET_H_
10 #define _FIRM_IRNODESET_H_
11
12 #include "irnode.h"
13 #include "xmalloc.h"
14
15 #define HashSet          ir_nodeset_t
16 #define HashSetIterator  ir_nodeset_iterator_t
17 #define ValueType        ir_node*
18 #define DO_REHASH
19 #include "hashset.h"
20 #undef DO_REHASH
21 #undef ValueType
22 #undef HashSetIterator
23 #undef HashSet
24
25 /**
26  * Initializes a nodeset with default size.
27  *
28  * @param nodeset      Pointer to allocated space for the nodeset
29  */
30 void ir_nodeset_init(ir_nodeset_t *nodeset);
31
32 /**
33  * Initializes a nodeset
34  *
35  * @param nodeset             Pointer to allocated space for the nodeset
36  * @param expected_elements   Number of elements expected in the nodeset (roughly)
37  */
38 void ir_nodeset_init_size(ir_nodeset_t *nodeset, size_t expected_elements);
39
40 /**
41  * Destroys a nodeset and frees the memory allocated for hashtable. The memory of
42  * the nodeset itself is not freed.
43  *
44  * @param nodeset   Pointer to the nodeset
45  */
46 void ir_nodeset_destroy(ir_nodeset_t *nodeset);
47
48 /**
49  * Allocates memory for a nodeset and initializes the set.
50  *
51  * @param expected_elements   Number of elements expected in the nodeset (roughly)
52  * @return The initialized nodeset
53  */
54 static INLINE ir_nodeset_t *ir_nodeset_new(size_t expected_elements) {
55         ir_nodeset_t *res = xmalloc(sizeof(*res));
56         ir_nodeset_init_size(res, expected_elements);
57         return res;
58 }
59
60 /**
61  * Destroys a nodeset and frees the memory of the nodeset itself.
62  */
63 static INLINE void ir_nodeset_del(ir_nodeset_t *nodeset) {
64         ir_nodeset_destroy(nodeset);
65         xfree(nodeset);
66 }
67
68 /**
69  * Inserts a node into a nodeset.
70  *
71  * @param nodeset   Pointer to the nodeset
72  * @param node      node to insert into the nodeset
73  * @returns         1 if the element has been inserted,
74  *                  0 if it was already there
75  */
76 int ir_nodeset_insert(ir_nodeset_t *nodeset, ir_node *node);
77
78 /**
79  * Removes a node from a nodeset. Does nothing if the nodeset doesn't contain
80  * the node.
81  *
82  * @param nodeset  Pointer to the nodeset
83  * @param node     Node to remove from the nodeset
84  */
85 void ir_nodeset_remove(ir_nodeset_t *nodeset, const ir_node *node);
86
87 /**
88  * Tests whether a nodeset contains a specific node
89  *
90  * @param nodeset   Pointer to the nodeset
91  * @param node      The pointer to find
92  * @returns         1 if nodeset contains the node, 0 else
93  */
94 int ir_nodeset_contains(const ir_nodeset_t *nodeset, const ir_node *node);
95
96 /**
97  * Returns the number of pointers contained in the nodeset
98  *
99  * @param nodeset   Pointer to the nodeset
100  * @returns       Number of pointers contained in the nodeset
101  */
102 size_t ir_nodeset_size(const ir_nodeset_t *nodeset);
103
104 /**
105  * Initializes a nodeset iterator. Sets the iterator before the first element in
106  * the nodeset.
107  *
108  * @param iterator   Pointer to already allocated iterator memory
109  * @param nodeset       Pointer to the nodeset
110  */
111 void ir_nodeset_iterator_init(ir_nodeset_iterator_t *iterator,
112                               const ir_nodeset_t *nodeset);
113
114 /**
115  * Advances the iterator and returns the current element or NULL if all elements
116  * in the nodeset have been processed.
117  * @attention It is not allowed to use nodeset_insert or nodeset_remove while
118  *            iterating over a nodeset.
119  *
120  * @param iterator  Pointer to the nodeset iterator.
121  * @returns         Next element in the nodeset or NULL
122  */
123 ir_node *ir_nodeset_iterator_next(ir_nodeset_iterator_t *iterator);
124
125 /**
126  * Removes the element the iterator currently points to
127  *
128  * @param nodeset   Pointer to the nodeset
129  * @param iterator  Pointer to the nodeset iterator.
130  */
131 void ir_nodeset_remove_iterator(ir_nodeset_t *nodeset,
132                                 const ir_nodeset_iterator_t *iterator);
133
134 #define foreach_ir_nodeset(nodeset, irn, iter) \
135         for(ir_nodeset_iterator_init(&iter, nodeset), \
136         irn = ir_nodeset_iterator_next(&iter);    \
137                 irn != NULL; irn = ir_nodeset_iterator_next(&iter))
138
139 #endif