updated Header
[libfirm] / ir / be / benodesets.h
1 /*
2  * Copyright (C) 1995-2007 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  * A lightweight wrapper around pset to store IR nodes.
22  * In some algorithms we want a more deterministic behavior
23  * which the pset_ptr did not guarantee due to it's hash function
24  */
25 #ifndef _BENODESETS_H
26 #define _BENODESETS_H
27
28 #include "firm_types.h"
29 #include "pset.h"
30
31 typedef struct pset nodeset;
32
33 /**
34  * Calculates a hash value for a node.
35  */
36 unsigned nodeset_hash(const ir_node *n);
37
38 /**
39  * Creates a new nodeset.
40  *
41  * @param slots   Initial number of collision chains.  I.e., #slots
42  *                different keys can be hashed without collisions.
43  *
44  * @returns
45  *    created nodeset
46  */
47 static INLINE nodeset *new_nodeset(int slots)
48 {
49         return new_pset(pset_default_ptr_cmp, slots);
50 }
51
52 /*
53  * Define some convenience macros.
54  */
55 #define new_nodeset_default()    new_nodeset(64)
56
57 /**
58  * Deletes a nodeset.
59  *
60  * @param nset   the nodeset
61  *
62  * @note
63  *    This does NOT delete the elements of this node set, just it's pointers!
64  */
65 static INLINE void del_nodeset(nodeset *nset)
66 {
67         del_pset(nset);
68 }
69
70 /**
71  * Returns the number of nodes in a nodeset.
72  *
73  * @param nset   the nodeset
74  */
75 static INLINE int nodeset_count(nodeset *nset)
76 {
77         return pset_count(nset);
78 }
79
80 /**
81  * Searches a node in a node set.
82  *
83  * @param nset  the pset to search in
84  * @param key   the node to search
85  *
86  * @return
87  *    the pointer of the found node in the nodeset or NULL if it was not found
88  */
89 static INLINE ir_node *nodeset_find(nodeset *nset, ir_node *key)
90 {
91         return (ir_node *) pset_find(nset, key, nodeset_hash(key));
92 }
93
94 /**
95  * Inserts a node into a pset.
96  *
97  * @param nset  the nodeset to insert in
98  * @param key   a pointer to the element to be inserted
99  *
100  * @return a pointer to the inserted element
101  *
102  * @note
103  *    It is not possible to insert an element more than once. If an element
104  *    that should be inserted is already in the set, this functions does
105  *    nothing but returning its already existing set_entry.
106  */
107 static INLINE ir_node *nodeset_insert(nodeset *nset, ir_node *key)
108 {
109         return (ir_node *) pset_insert(nset, key, nodeset_hash(key));
110 }
111
112 /**
113  * Removes a node from a nodeset.
114  *
115  * @param nset  the nodeset to delete in
116  * @param key   a pointer to the element to be deleted
117  *
118  * @return
119  *    the pointer to the removed element
120  *
121  * @remark
122  *    The current implementation did not allow to remove non-existing elements.
123  *    @@@ so, does it do now?
124  *    Further, it is allowed to remove elements during an iteration
125  *    including the current one.
126  */
127 static INLINE ir_node *nodeset_remove(nodeset *nset, ir_node *key)
128 {
129         return (ir_node *) pset_remove(nset, key, nodeset_hash(key));
130 }
131
132 /**
133  * Returns the first node of a nodeset.
134  *
135  * @param nset  the nodeset to iterate
136  *
137  * @return a node or NULL if the set is empty
138  */
139 static INLINE ir_node *nodeset_first(nodeset *nset)
140 {
141         return (ir_node *) pset_first(nset);
142 }
143
144 /**
145  * Returns the next node of a nodeset.
146  *
147  * @param nset  the nodeset to iterate
148  *
149  * @return a node or NULL if the iteration is finished
150  */
151 static INLINE ir_node *nodeset_next(nodeset *nset)
152 {
153         return (ir_node *) pset_next(nset);
154 }
155
156 /**
157  * Breaks the iteration of a set. Must be called before
158  * the next nodeset_first() call if the iteration was NOT
159  * finished.
160  *
161  * @param nset  the nodeset
162  */
163 static INLINE void nodeset_break(nodeset *nset)
164 {
165         pset_break(nset);
166 }
167
168 /**
169  * Iterate over a node set.
170  *
171  * @param nset  the nodeset
172  * @param irn   the iterator node
173  */
174 #define foreach_nodeset(nset, irn)      for (irn = nodeset_first(nset); irn; irn = nodeset_next(nset))
175
176 #endif /* _BENODESETS_H */