e20e99d4f6610b9e1d239805bb50a659dc34e305
[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
14 #define HashSet          ir_nodeset_t
15 #define HashSetIterator  ir_nodeset_iterator_t
16 #define ValueType        ir_node*
17 #define DO_REHASH
18 #include "hashset.h"
19 #undef DO_REHASH
20 #undef ValueType
21 #undef HashSetIterator
22 #undef HashSet
23
24 /**
25  * Initializes a nodeset
26  *
27  * @param nodeset      Pointer to allocated space for the nodeset
28  */
29 void ir_nodeset_init(ir_nodeset_t *nodeset);
30
31 /**
32  * Initializes a nodeset
33  *
34  * @param nodeset             Pointer to allocated space for the nodeset
35  * @param expected_elements   Number of elements expected in the nodeset (rougly)
36  */
37 void ir_nodeset_init_size(ir_nodeset_t *nodeset, size_t expected_elements);
38
39 /**
40  * Destroys a nodeset and frees the memory allocated for hashtable. The memory of
41  * the nodeset itself is not freed.
42  *
43  * @param nodeset   Pointer to the nodeset
44  */
45 void ir_nodeset_destroy(ir_nodeset_t *nodeset);
46
47 /**
48  * Inserts a node into a nodeset.
49  *
50  * @param nodeset   Pointer to the nodeset
51  * @param node      node to insert into the nodeset
52  * @returns         1 if the element has been inserted,
53  *                  0 if it was already there
54  */
55 int ir_nodeset_insert(ir_nodeset_t *nodeset, ir_node *node);
56
57 /**
58  * Removes a node from a nodeset. Does nothing if the nodeset doesn't contain
59  * the node.
60  *
61  * @param nodeset  Pointer to the nodeset
62  * @param node     Node to remove from the nodeset
63  */
64 void ir_nodeset_remove(ir_nodeset_t *nodeset, const ir_node *node);
65
66 /**
67  * Tests whether a nodeset contains a specific node
68  *
69  * @param nodeset   Pointer to the nodeset
70  * @param node      The pointer to find
71  * @returns         1 if nodeset contains the node, 0 else
72  */
73 int ir_nodeset_contains(const ir_nodeset_t *nodeset, const ir_node *node);
74
75 /**
76  * Returns the number of pointers contained in the nodeset
77  *
78  * @param nodeset   Pointer to the nodeset
79  * @returns       Number of pointers contained in the nodeset
80  */
81 size_t ir_nodeset_size(const ir_nodeset_t *nodeset);
82
83 /**
84  * Initializes a nodeset iterator. Sets the iterator before the first element in
85  * the nodeset.
86  *
87  * @param iterator   Pointer to already allocated iterator memory
88  * @param nodeset       Pointer to the nodeset
89  */
90 void ir_nodeset_iterator_init(ir_nodeset_iterator_t *iterator,
91                               const ir_nodeset_t *nodeset);
92
93 /**
94  * Advances the iterator and returns the current element or NULL if all elements
95  * in the nodeset have been processed.
96  * @attention It is not allowed to use nodeset_insert or nodeset_remove while
97  *            iterating over a nodeset.
98  *
99  * @param iterator  Pointer to the nodeset iterator.
100  * @returns         Next element in the nodeset or NULL
101  */
102 ir_node *ir_nodeset_iterator_next(ir_nodeset_iterator_t *iterator);
103
104 /**
105  * Removes the element the iterator currently points to
106  *
107  * @param nodeset   Pointer to the nodeset
108  * @param iterator  Pointer to the nodeset iterator.
109  */
110 void ir_nodeset_remove_iterator(ir_nodeset_t *nodeset,
111                                 const ir_nodeset_iterator_t *iterator);
112
113 #define foreach_ir_nodeset(nodeset, irn, iter) \
114         for(ir_nodeset_iterator_init(&iter, nodeset), \
115         irn = ir_nodeset_iterator_next(&iter);    \
116                 irn != NULL; irn = ir_nodeset_iterator_next(&iter))
117
118 #endif