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