2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @author Matthias Braun
10 * @brief A nodemap. This should be preferred over a simple pset, because it
11 * tries to guarantee deterministic behavior. (and is faster)
13 #ifndef _FIRM_IRNODEHASHMAP_H_
14 #define _FIRM_IRNODEHASHMAP_H_
18 typedef struct ir_nodehashmap_entry_t {
21 } ir_nodehashmap_entry_t;
23 #define HashSet ir_nodehashmap_t
24 #define HashSetIterator ir_nodehashmap_iterator_t
25 #define ValueType ir_nodehashmap_entry_t
30 #undef HashSetIterator
33 typedef struct ir_nodehashmap_t ir_nodehashmap_t;
34 typedef struct ir_nodehashmap_iterator_t ir_nodehashmap_iterator_t;
37 * Initializes a nodehashmap with default size.
39 * @param nodehashmap Pointer to allocated space for the nodehashmap
41 void ir_nodehashmap_init(ir_nodehashmap_t *nodehashmap);
44 * Initializes a nodehashmap
46 * @param nodehashmap Pointer to allocated space for the nodehashmap
47 * @param expected_elements Number of elements expected in the nodehashmap
50 void ir_nodehashmap_init_size(ir_nodehashmap_t *nodehashmap,
51 size_t expected_elements);
54 * Destroys a nodehashmap and frees the memory allocated for hashtable. The
55 * memory of the nodehashmap itself is not freed.
57 * @param nodehashmap Pointer to the nodehashmap
59 void ir_nodehashmap_destroy(ir_nodehashmap_t *nodehashmap);
62 * Inserts a node into a nodehashmap.
64 * @param nodehashmap Pointer to the nodehashmap
65 * @param node node to insert into the nodehashmap
66 * @param data data to associate with the node
68 void ir_nodehashmap_insert(ir_nodehashmap_t *nodehashmap, ir_node *node,
72 * Removes a node from a nodehashmap. Does nothing if the nodehashmap doesn't
75 * @param nodehashmap Pointer to the nodehashmap
76 * @param node Node to remove from the nodehashmap
78 void ir_nodehashmap_remove(ir_nodehashmap_t *nodehashmap, const ir_node *node);
81 * Tests whether a nodehashmap contains a specific node
83 * @param nodehashmap Pointer to the nodehashmap
84 * @param node The pointer to find
85 * @returns the associated data of the node or NULL
87 void *ir_nodehashmap_get(const ir_nodehashmap_t *nodehashmap,
90 #define ir_nodehashmap_get(type, self, node) ((type*)ir_nodehashmap_get((self), (node)))
93 * Returns the number of pointers contained in the nodehashmap
95 * @param nodehashmap Pointer to the nodehashmap
96 * @returns Number of pointers contained in the nodehashmap
98 size_t ir_nodehashmap_size(const ir_nodehashmap_t *nodehashmap);
101 * Initializes a nodehashmap iterator. Sets the iterator before the first
102 * element in the nodehashmap.
104 * @param iterator Pointer to already allocated iterator memory
105 * @param nodehashmap Pointer to the nodehashmap
107 void ir_nodehashmap_iterator_init(ir_nodehashmap_iterator_t *iterator,
108 const ir_nodehashmap_t *nodehashmap);
111 * Advances the iterator and returns the current element or NULL if all elements
112 * in the nodehashmap have been processed.
113 * @attention It is not allowed to use nodehashmap_insert or nodehashmap_remove
114 * while iterating over a nodehashmap.
116 * @param iterator Pointer to the nodehashmap iterator.
117 * @returns Next element in the nodehashmap or NULL
119 ir_nodehashmap_entry_t ir_nodehashmap_iterator_next(
120 ir_nodehashmap_iterator_t *iterator);
123 * Removes the element the iterator currently points to
125 * @param nodehashmap Pointer to the nodehashmap
126 * @param iterator Pointer to the nodehashmap iterator.
128 void ir_nodehashmap_remove_iterator(ir_nodehashmap_t *nodehashmap,
129 const ir_nodehashmap_iterator_t *iterator);
131 #define foreach_ir_nodehashmap(nodehashmap, entry, iter) \
132 for (ir_nodehashmap_iterator_init(&iter, nodehashmap), \
133 entry = ir_nodehashmap_iterator_next(&iter); \
134 entry.node != NULL; entry = ir_nodehashmap_iterator_next(&iter))