3 * @author Matthias Braun
5 * @brief A nodeset. This should be prefered over a simple pset, because it
6 * tries to guarantee deterministic behavior. (and is faster)
9 #ifndef _FIRM_IRNODESET_H_
10 #define _FIRM_IRNODESET_H_
15 #define HashSet ir_nodeset_t
16 #define HashSetIterator ir_nodeset_iterator_t
17 #define ValueType ir_node*
22 #undef HashSetIterator
26 * Initializes a nodeset with default size.
28 * @param nodeset Pointer to allocated space for the nodeset
30 void ir_nodeset_init(ir_nodeset_t *nodeset);
33 * Initializes a nodeset
35 * @param nodeset Pointer to allocated space for the nodeset
36 * @param expected_elements Number of elements expected in the nodeset (roughly)
38 void ir_nodeset_init_size(ir_nodeset_t *nodeset, size_t expected_elements);
41 * Destroys a nodeset and frees the memory allocated for hashtable. The memory of
42 * the nodeset itself is not freed.
44 * @param nodeset Pointer to the nodeset
46 void ir_nodeset_destroy(ir_nodeset_t *nodeset);
49 * Allocates memory for a nodeset and initializes the set.
51 * @param expected_elements Number of elements expected in the nodeset (roughly)
52 * @return The initialized nodeset
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);
61 * Destroys a nodeset and frees the memory of the nodeset itself.
63 static INLINE void ir_nodeset_del(ir_nodeset_t *nodeset) {
64 ir_nodeset_destroy(nodeset);
69 * Inserts a node into a nodeset.
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
76 int ir_nodeset_insert(ir_nodeset_t *nodeset, ir_node *node);
79 * Removes a node from a nodeset. Does nothing if the nodeset doesn't contain
82 * @param nodeset Pointer to the nodeset
83 * @param node Node to remove from the nodeset
85 void ir_nodeset_remove(ir_nodeset_t *nodeset, const ir_node *node);
88 * Tests whether a nodeset contains a specific node
90 * @param nodeset Pointer to the nodeset
91 * @param node The pointer to find
92 * @returns 1 if nodeset contains the node, 0 else
94 int ir_nodeset_contains(const ir_nodeset_t *nodeset, const ir_node *node);
97 * Returns the number of pointers contained in the nodeset
99 * @param nodeset Pointer to the nodeset
100 * @returns Number of pointers contained in the nodeset
102 size_t ir_nodeset_size(const ir_nodeset_t *nodeset);
105 * Initializes a nodeset iterator. Sets the iterator before the first element in
108 * @param iterator Pointer to already allocated iterator memory
109 * @param nodeset Pointer to the nodeset
111 void ir_nodeset_iterator_init(ir_nodeset_iterator_t *iterator,
112 const ir_nodeset_t *nodeset);
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.
120 * @param iterator Pointer to the nodeset iterator.
121 * @returns Next element in the nodeset or NULL
123 ir_node *ir_nodeset_iterator_next(ir_nodeset_iterator_t *iterator);
126 * Removes the element the iterator currently points to
128 * @param nodeset Pointer to the nodeset
129 * @param iterator Pointer to the nodeset iterator.
131 void ir_nodeset_remove_iterator(ir_nodeset_t *nodeset,
132 const ir_nodeset_iterator_t *iterator);
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))