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