2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @author Michael Beck
23 * @brief A linked nodemap.
25 #ifndef _FIRM_IRLINKEDNODEMAP_H_
26 #define _FIRM_IRLINKEDNODEMAP_H_
28 #include "firm_types.h"
33 * sebastian experimental:
34 * use ordered arrays as node sets.
35 * the guys here have made good experiences with that.
36 * Internally we use normal Firm arrays and binary
37 * search for locating the elements. Using arrays should
38 * give the sets a small footprint.
40 #undef IR_nodemap_USE_ORDERED_SETS
42 typedef struct ir_lnk_nodemap_entry_t {
43 ir_node *node; /**< the node itself */
44 void *data; /**< associated data */
45 list_head list; /**< link field for the list iterator */
46 } ir_lnk_nodemap_entry_t;
48 #define HashSet ir_lnk_nodemap_t
49 #define HashSetIterator ir_lnk_nodemap_iterator_t
50 #define ValueType ir_lnk_nodemap_entry_t
51 #define ADDITIONAL_DATA list_head elem_list; list_head all_iters;
59 #undef ADDITIONAL_DATA
61 #undef HashSetIterator
64 typedef struct ir_lnk_nodemap_t ir_lnk_nodemap_t;
65 typedef struct ir_lnk_nodemap_iterator_t {
66 list_head *iter; /**< points to the list head of the last element */
67 const ir_lnk_nodemap_t *nodemap; /**< lithe nodemap of this iterator. */
68 } ir_lnk_nodemap_iterator_t;
71 * Initializes a linked nodemap with default size.
73 * @param nodemap Pointer to allocated space for the nodemap
75 void ir_lnk_nodemap_init(ir_lnk_nodemap_t *nodemap);
78 * Initializes a linked nodemap.
80 * @param nodemap Pointer to allocated space for the nodemap
81 * @param expected_elements Number of elements expected in the nodemap (roughly)
83 void ir_lnk_nodemap_init_size(ir_lnk_nodemap_t *nodemap, size_t expected_elements);
86 * Destroys a nodemap and frees the memory allocated for hashtable. The memory of
87 * the nodemap itself is not freed.
89 * @param nodemap Pointer to the nodemap
91 void ir_lnk_nodemap_destroy(ir_lnk_nodemap_t *nodemap);
94 * Allocates memory for a linked nodemap and initializes the set.
96 * @param expected_elements Number of elements expected in the nodemap (roughly)
97 * @return The initialized nodemap
99 static inline ir_lnk_nodemap_t *ir_lnk_nodemap_new(size_t expected_elements) {
100 ir_lnk_nodemap_t *res = XMALLOC(ir_lnk_nodemap_t);
101 ir_lnk_nodemap_init_size(res, expected_elements);
106 * Destroys a linked nodemap and frees the memory of the nodemap itself.
108 static inline void ir_lnk_nodemap_del(ir_lnk_nodemap_t *nodemap) {
109 ir_lnk_nodemap_destroy(nodemap);
114 * Inserts a node into a linked nodemap.
116 * @param nodemap Pointer to the nodemap
117 * @param node node to insert into the nodemap
118 * @param data data to associate with the node
119 * @returns 1 if the element has been inserted,
120 * 0 if it was already there
122 int ir_lnk_nodemap_put(ir_lnk_nodemap_t *nodemap, ir_node *node, void *data);
125 * Get the associated value of a specific node
127 * @param nodemap Pointer to the nodemap
128 * @param node The pointer to find
129 * @returns the associated data of the node or NULL
131 void *ir_lnk_nodemap_get(const ir_lnk_nodemap_t *nodemap, const ir_node *node);
134 * Removes a node from a linked nodemap. Does nothing if the nodemap doesn't contain
137 * @param nodemap Pointer to the nodemap
138 * @param node Node to remove from the nodemap
140 void ir_lnk_nodemap_remove(ir_lnk_nodemap_t *nodemap, const ir_node *node);
143 * Returns the number of nodes contained in the linked nodemap.
145 * @param nodemap Pointer to the nodemap
146 * @returns Number of nodes contained in the linked nodemap
148 size_t ir_lnk_nodemap_size(const ir_lnk_nodemap_t *nodemap);
151 * Initializes a nodemap iterator. Sets the iterator before the first element in
152 * the linked nodemap.
154 * @param iterator Pointer to already allocated iterator memory
155 * @param nodemap Pointer to the nodemap
157 void ir_lnk_nodemap_iterator_init(ir_lnk_nodemap_iterator_t *iterator,
158 const ir_lnk_nodemap_t *nodemap);
161 * Advances the iterator and returns the current element or NULL if all elements
162 * in the linked nodemap have been processed.
163 * @attention It is not allowed to use ir_lnk_nodemap_insert or ir_lnk_nodemap_remove while
164 * iterating over a nodemap.
166 * @param iterator Pointer to the nodemap iterator.
167 * @returns Next element in the nodemap or NULL
169 ir_node *ir_lnk_nodemap_iterator_next(ir_lnk_nodemap_iterator_t *iterator);
172 * Removes the element the iterator currently points to.
174 * @param nodemap Pointer to the linked nodemap
175 * @param iterator Pointer to the linked nodemap iterator.
177 void ir_lnk_nodemap_remove_iterator(ir_lnk_nodemap_t *nodemap,
178 ir_lnk_nodemap_iterator_t *iterator);
180 #define foreach_ir_lnk_nodemap(nodemap, irn, iter) \
181 for (ir_lnk_nodemap_iterator_init(&iter, nodemap), \
182 irn = ir_lnk_nodemap_iterator_next(&iter); \
183 irn != NULL; irn = ir_lnk_nodemap_iterator_next(&iter))