remove old #undef
[libfirm] / ir / ir / irlinkednodeset.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    Michael Beck
23  * @brief     A linked nodeset.
24  */
25 #ifndef _FIRM_IRLINKEDNODESET_H_
26 #define _FIRM_IRLINKEDNODESET_H_
27
28 #include "firm_types.h"
29 #include "xmalloc.h"
30 #include "list.h"
31
32 typedef struct ir_lnk_nodeset_entry_t {
33         ir_node     *node;  /**< the node itself */
34         list_head   list;   /**< link field for the list iterator */
35 } ir_lnk_nodeset_entry_t;
36
37 #define HashSet          ir_lnk_nodeset_t
38 #define HashSetIterator  ir_lnk_nodeset_iterator_t
39 #define ValueType        ir_lnk_nodeset_entry_t
40 #define ADDITIONAL_DATA  list_head elem_list; list_head all_iters;
41 #define DO_REHASH
42 #define NO_ITERATOR
43
44 #include "hashset.h"
45
46 #undef NO_ITERATOR
47 #undef DO_REHASH
48 #undef ADDITIONAL_DATA
49 #undef ValueType
50 #undef HashSetIterator
51 #undef HashSet
52
53 typedef struct ir_lnk_nodeset_t ir_lnk_nodeset_t;
54 typedef struct ir_lnk_nodeset_iterator_t {
55         list_head              *iter;       /**< points to the list head of the last element */
56         const ir_lnk_nodeset_t *nodeset;    /**< lithe nodeset of this iterator. */
57 } ir_lnk_nodeset_iterator_t;
58
59 /**
60  * Initializes a linked nodeset with default size.
61  *
62  * @param nodeset      Pointer to allocated space for the nodeset
63  */
64 void ir_lnk_nodeset_init(ir_lnk_nodeset_t *nodeset);
65
66 /**
67  * Initializes a linked nodeset.
68  *
69  * @param nodeset             Pointer to allocated space for the nodeset
70  * @param expected_elements   Number of elements expected in the nodeset (roughly)
71  */
72 void ir_lnk_nodeset_init_size(ir_lnk_nodeset_t *nodeset, size_t expected_elements);
73
74 /**
75  * Destroys a nodeset and frees the memory allocated for hashtable. The memory of
76  * the nodeset itself is not freed.
77  *
78  * @param nodeset   Pointer to the nodeset
79  */
80 void ir_lnk_nodeset_destroy(ir_lnk_nodeset_t *nodeset);
81
82 /**
83  * Allocates memory for a linked nodeset and initializes the set.
84  *
85  * @param expected_elements   Number of elements expected in the nodeset (roughly)
86  * @return The initialized nodeset
87  */
88 static inline ir_lnk_nodeset_t *ir_lnk_nodeset_new(size_t expected_elements) {
89         ir_lnk_nodeset_t *res = XMALLOC(ir_lnk_nodeset_t);
90         ir_lnk_nodeset_init_size(res, expected_elements);
91         return res;
92 }
93
94 /**
95  * Destroys a linked nodeset and frees the memory of the nodeset itself.
96  */
97 static inline void ir_lnk_nodeset_del(ir_lnk_nodeset_t *nodeset) {
98         ir_lnk_nodeset_destroy(nodeset);
99         xfree(nodeset);
100 }
101
102 /**
103  * Inserts a node into a linked nodeset.
104  *
105  * @param nodeset   Pointer to the nodeset
106  * @param node      node to insert into the nodeset
107  * @returns         1 if the element has been inserted,
108  *                  0 if it was already there
109  */
110 int ir_lnk_nodeset_insert(ir_lnk_nodeset_t *nodeset, ir_node *node);
111
112
113 /**
114  * Removes a node from a linked nodeset. Does nothing if the nodeset doesn't contain
115  * the node.
116  *
117  * @param nodeset  Pointer to the nodeset
118  * @param node     Node to remove from the nodeset
119  */
120 void ir_lnk_nodeset_remove(ir_lnk_nodeset_t *nodeset, const ir_node *node);
121
122 /**
123  * Tests whether a linked nodeset contains a specific node.
124  *
125  * @param nodeset   Pointer to the nodeset
126  * @param node      The pointer to find
127  * @returns         1 if nodeset contains the node, 0 else
128  */
129 int ir_lnk_nodeset_contains(const ir_lnk_nodeset_t *nodeset, const ir_node *node);
130
131 /**
132  * Returns the number of nodes contained in the linked nodeset.
133  *
134  * @param nodeset   Pointer to the nodeset
135  * @returns         Number of nodes contained in the linked nodeset
136  */
137 size_t ir_lnk_nodeset_size(const ir_lnk_nodeset_t *nodeset);
138
139 /**
140  * Initializes a nodeset iterator. Sets the iterator before the first element in
141  * the linked nodeset.
142  *
143  * @param iterator   Pointer to already allocated iterator memory
144  * @param nodeset       Pointer to the nodeset
145  */
146 void ir_lnk_nodeset_iterator_init(ir_lnk_nodeset_iterator_t *iterator,
147                                   const ir_lnk_nodeset_t *nodeset);
148
149 /**
150  * Advances the iterator and returns the current element or NULL if all elements
151  * in the linked nodeset have been processed.
152  * @attention It is not allowed to use ir_lnk_nodeset_insert or ir_lnk_nodeset_remove while
153  *            iterating over a nodeset.
154  *
155  * @param iterator  Pointer to the nodeset iterator.
156  * @returns         Next element in the nodeset or NULL
157  */
158 ir_node *ir_lnk_nodeset_iterator_next(ir_lnk_nodeset_iterator_t *iterator);
159
160 /**
161  * Removes the element the iterator currently points to.
162  *
163  * @param nodeset   Pointer to the linked nodeset
164  * @param iterator  Pointer to the linked nodeset iterator.
165  */
166 void ir_lnk_nodeset_remove_iterator(ir_lnk_nodeset_t *nodeset,
167                                     ir_lnk_nodeset_iterator_t *iterator);
168
169 #define foreach_ir_lnk_nodeset(nodeset, irn, iter) \
170         for (ir_lnk_nodeset_iterator_init(&iter, nodeset), \
171         irn = ir_lnk_nodeset_iterator_next(&iter);    \
172                 irn != NULL; irn = ir_lnk_nodeset_iterator_next(&iter))
173
174 #endif