verify: Clarify assertion message.
[libfirm] / ir / ir / irnodehashmap.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    Matthias Braun
23  * @date      30.03.2007
24  * @brief     A nodemap. This should be preferred over a simple pset, because it
25  *            tries to guarantee deterministic behavior. (and is faster)
26  */
27 #ifndef _FIRM_IRNODEHASHMAP_H_
28 #define _FIRM_IRNODEHASHMAP_H_
29
30 #include "irnode.h"
31
32 typedef struct ir_nodehashmap_entry_t {
33         ir_node *node;
34         void    *data;
35 } ir_nodehashmap_entry_t;
36
37 #define HashSet          ir_nodehashmap_t
38 #define HashSetIterator  ir_nodehashmap_iterator_t
39 #define ValueType        ir_nodehashmap_entry_t
40 #define DO_REHASH
41 #include "hashset.h"
42 #undef DO_REHASH
43 #undef ValueType
44 #undef HashSetIterator
45 #undef HashSet
46
47 typedef struct ir_nodehashmap_t           ir_nodehashmap_t;
48 typedef struct ir_nodehashmap_iterator_t  ir_nodehashmap_iterator_t;
49
50 /**
51  * Initializes a nodehashmap with default size.
52  *
53  * @param nodehashmap      Pointer to allocated space for the nodehashmap
54  */
55 void ir_nodehashmap_init(ir_nodehashmap_t *nodehashmap);
56
57 /**
58  * Initializes a nodehashmap
59  *
60  * @param nodehashmap         Pointer to allocated space for the nodehashmap
61  * @param expected_elements   Number of elements expected in the nodehashmap
62  *                            (roughly)
63  */
64 void ir_nodehashmap_init_size(ir_nodehashmap_t *nodehashmap,
65                               size_t expected_elements);
66
67 /**
68  * Destroys a nodehashmap and frees the memory allocated for hashtable. The
69  * memory of the nodehashmap itself is not freed.
70  *
71  * @param nodehashmap   Pointer to the nodehashmap
72  */
73 void ir_nodehashmap_destroy(ir_nodehashmap_t *nodehashmap);
74
75 /**
76  * Inserts a node into a nodehashmap.
77  *
78  * @param nodehashmap  Pointer to the nodehashmap
79  * @param node         node to insert into the nodehashmap
80  * @param data         data to associate with the node
81  */
82 void ir_nodehashmap_insert(ir_nodehashmap_t *nodehashmap, ir_node *node,
83                            void *data);
84
85 /**
86  * Removes a node from a nodehashmap. Does nothing if the nodehashmap doesn't
87  * contain the node.
88  *
89  * @param nodehashmap  Pointer to the nodehashmap
90  * @param node         Node to remove from the nodehashmap
91  */
92 void ir_nodehashmap_remove(ir_nodehashmap_t *nodehashmap, const ir_node *node);
93
94 /**
95  * Tests whether a nodehashmap contains a specific node
96  *
97  * @param nodehashmap   Pointer to the nodehashmap
98  * @param node          The pointer to find
99  * @returns             the associated data of the node or NULL
100  */
101 void *ir_nodehashmap_get(const ir_nodehashmap_t *nodehashmap,
102                          const ir_node *node);
103
104 #define ir_nodehashmap_get(type, self, node) ((type*)ir_nodehashmap_get((self), (node)))
105
106 /**
107  * Returns the number of pointers contained in the nodehashmap
108  *
109  * @param nodehashmap   Pointer to the nodehashmap
110  * @returns             Number of pointers contained in the nodehashmap
111  */
112 size_t ir_nodehashmap_size(const ir_nodehashmap_t *nodehashmap);
113
114 /**
115  * Initializes a nodehashmap iterator. Sets the iterator before the first
116  * element in the nodehashmap.
117  *
118  * @param iterator   Pointer to already allocated iterator memory
119  * @param nodehashmap       Pointer to the nodehashmap
120  */
121 void ir_nodehashmap_iterator_init(ir_nodehashmap_iterator_t *iterator,
122                                   const ir_nodehashmap_t *nodehashmap);
123
124 /**
125  * Advances the iterator and returns the current element or NULL if all elements
126  * in the nodehashmap have been processed.
127  * @attention It is not allowed to use nodehashmap_insert or nodehashmap_remove
128  * while iterating over a nodehashmap.
129  *
130  * @param iterator  Pointer to the nodehashmap iterator.
131  * @returns         Next element in the nodehashmap or NULL
132  */
133 ir_nodehashmap_entry_t ir_nodehashmap_iterator_next(
134                 ir_nodehashmap_iterator_t *iterator);
135
136 /**
137  * Removes the element the iterator currently points to
138  *
139  * @param nodehashmap  Pointer to the nodehashmap
140  * @param iterator     Pointer to the nodehashmap iterator.
141  */
142 void ir_nodehashmap_remove_iterator(ir_nodehashmap_t *nodehashmap,
143                                     const ir_nodehashmap_iterator_t *iterator);
144
145 #define foreach_ir_nodehashmap(nodehashmap, entry, iter)                  \
146         for (ir_nodehashmap_iterator_init(&iter, nodehashmap),                \
147                 entry = ir_nodehashmap_iterator_next(&iter);                      \
148                 entry.node != NULL; entry = ir_nodehashmap_iterator_next(&iter))
149
150 #endif