rework hashset
[libfirm] / ir / ir / valueset.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 value set, containing expression for values.
24  */
25 #ifndef _FIRM_VALUESET_H_
26 #define _FIRM_VALUESET_H_
27
28 #include "firm_types.h"
29 #include "xmalloc.h"
30 #include "list.h"
31
32 typedef struct ir_valueset_entry_t {
33         ir_node     *value;  /**< the represented value */
34         ir_node     *expr;   /**< the leader expression for the value in the current set */
35         list_head   list;    /**< link field for the list iterator */
36 } ir_valueset_entry_t;
37
38 #define HashSet          ir_valueset_t
39 #define ValueType        ir_valueset_entry_t
40 #define ADDITIONAL_DATA  list_head elem_list; list_head all_iters;
41 #undef DO_REHASH
42
43 #include "hashset.h"
44
45 #undef ADDITIONAL_DATA
46 #undef ValueType
47 #undef HashSet
48
49 typedef struct ir_valueset_t ir_valueset_t;
50 typedef struct ir_valueset_iterator_t {
51         list_head           *iter;       /**< points to the list head of the last element */
52         const ir_valueset_t *valueset;   /**< the value set of this iterator. */
53 } ir_valueset_iterator_t;
54
55 /**
56  * Initializes a value set with default size.
57  *
58  * @param valueset      Pointer to allocated space for the value set
59  */
60 void ir_valueset_init(ir_valueset_t *valueset);
61
62 /**
63  * Initializes a value set.
64  *
65  * @param valueset            Pointer to allocated space for the value set
66  * @param expected_elements   Number of elements expected in the value set (roughly)
67  */
68 void ir_valueset_init_size(ir_valueset_t *valueset, size_t expected_elements);
69
70 /**
71  * Destroys a value set and frees the memory allocated for hashtable. The memory of
72  * the value set itself is not freed.
73  *
74  * @param valueset   Pointer to the value set
75  */
76 void ir_valueset_destroy(ir_valueset_t *valueset);
77
78 /**
79  * Allocates memory for a value set and initializes it.
80  *
81  * @param expected_elements   Number of elements expected in the value set (roughly)
82  * @return The initialized value set
83  */
84 static inline ir_valueset_t *ir_valueset_new(size_t expected_elements) {
85         ir_valueset_t *res = XMALLOC(ir_valueset_t);
86         ir_valueset_init_size(res, expected_elements);
87         return res;
88 }
89
90 /**
91  * Destroys a value set and frees the memory of the set itself.
92  */
93 static inline void ir_valueset_del(ir_valueset_t *valueset) {
94         ir_valueset_destroy(valueset);
95         xfree(valueset);
96 }
97
98 /**
99  * Inserts a (value, expression) pair into a valueset if the value is not already
100  * known, else does nothing.
101  *
102  * @param valueset  Pointer to the value set
103  * @param value     the value to insert into the value set
104  * @param expr      the expression to associate with the value
105  * @returns         1 if the value has been inserted,
106  *                  0 if it was already there
107  */
108 int ir_valueset_insert(ir_valueset_t *valueset, ir_node *value, ir_node *expr);
109
110 /**
111  * Inserts a (value, expression) pair into a valueset if the value is not already
112  * known, else replace the expression for the given value.
113  *
114  * @param valueset  Pointer to the value set
115  * @param value     the value to insert into the value set
116  * @param expr      the expression to associate with the value
117  * @returns         1 if the value has been inserted,
118  *                  0 if it was already there
119  */
120 int ir_valueset_replace(ir_valueset_t *valueset, ir_node *value, ir_node *expr);
121
122 /**
123  * Get the leader expression of a specific value from the value set.
124  *
125  * @param valueset  Pointer to the value set
126  * @param value     The value to find
127  * @returns         the associated expression of the value or NULL
128  */
129 void *ir_valueset_lookup(const ir_valueset_t *valueset, const ir_node *value);
130
131 /**
132  * Removes a value from a value set. Does nothing if the value set doesn't contain
133  * the value.
134  *
135  * @param valueset  Pointer to the value set
136  * @param value     value to remove from the values et
137  */
138 void ir_valueset_remove(ir_valueset_t *valueset, const ir_node *value);
139
140 /**
141  * Returns the number of values contained in the value set.
142  *
143  * @param valueset  Pointer to the value set
144  * @returns         Number of values contained in the value set
145  */
146 size_t ir_valueset_size(const ir_valueset_t *valueset);
147
148 /**
149  * Initializes a value set iterator. Sets the iterator before the first element in
150  * the value set.
151  *
152  * @param iterator   Pointer to already allocated iterator memory
153  * @param valueset   Pointer to the value set
154  */
155 void ir_valueset_iterator_init(ir_valueset_iterator_t *iterator,
156                                const ir_valueset_t *valueset);
157
158 /**
159  * Advances the iterator and returns the current element or NULL if all elements
160  * in the value set have been processed.
161  * @note It is not allowed to use ir_valueset_insert() or ir_valueset_remove() while
162  *            iterating over a nodemap.
163  *
164  * @param iterator  Pointer to the value set iterator.
165  * @param expr      After return contains the associated expression for the value or NULL
166  * @returns         Next element in the value set or NULL
167  */
168 ir_node *ir_valueset_iterator_next(ir_valueset_iterator_t *iterator, ir_node **expr);
169
170 /**
171  * Removes the element the iterator currently points to.
172  *
173  * @param valueset  Pointer to the value set
174  * @param iterator  Pointer to the value set iterator.
175  */
176 void ir_valueset_remove_iterator(ir_valueset_t *valueset, ir_valueset_iterator_t *iterator);
177
178 #define foreach_valueset(valueset, value, expr, iter) \
179         for (ir_valueset_iterator_init(&iter, valueset), \
180         value = ir_valueset_iterator_next(&iter, &expr);    \
181                 value != NULL; value = ir_valueset_iterator_next(&iter, &expr))
182
183 #endif