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