2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
9 * @brief hashset containing pointers
10 * @author Matthias Braun
12 * @note This has been named pset_new_new for now until all code has been
13 * changed to use this instead of the old deprecated pset_new functions!
14 * This version performs better than pset in terms of speed and memory
15 * usage and allows multiple iterators over the set
17 #ifndef FIRM_ADT_PSET_NEW_H
18 #define FIRM_ADT_PSET_NEW_H
24 #define HashSet pset_new_t
25 #define HashSetIterator pset_new_iterator_t
26 #define ValueType void*
31 #undef HashSetIterator
36 /** a pointer (hash)set */
37 typedef struct pset_new_t pset_new_t;
38 /** iterator over a pointer set.
40 typedef struct pset_new_iterator_t pset_new_iterator_t;
43 * Initializes a pset_new
45 * @param pset_new Pointer to allocated space for the pset_new
47 void pset_new_init(pset_new_t *pset_new);
50 * Initializes a pset_new
52 * @param pset_new Pointer to allocated space for the pset_new
53 * @param expected_elements Number of elements expected in the pset_new (roughly)
55 void pset_new_init_size(pset_new_t *pset_new, size_t expected_elements);
58 * Destroys a pset_new and frees the memory allocated for hashtable. The memory of
59 * the pset_new itself is not freed.
61 * @param pset_new Pointer to the pset_new
63 void pset_new_destroy(pset_new_t *pset_new);
66 * Inserts an element into a pset_new.
68 * @param pset_new Pointer to the pset_new
69 * @param ptr Pointer to insert into the pset_new
70 * @returns true if the pointer was inserted, false if it was already there
72 bool pset_new_insert(pset_new_t *pset_new, void *ptr);
75 * Removes an element from a pset_new. Does nothing if the pset_new doesn't contain the
78 * @param pset_new Pointer to the pset_new
79 * @param ptr Pointer to remove from the pset_new
81 void pset_new_remove(pset_new_t *pset_new, const void *ptr);
84 * Tests whether a pset_new contains a pointer
86 * @param pset_new Pointer to the pset_new
87 * @param ptr The pointer to test
89 bool pset_new_contains(const pset_new_t *pset_new, const void *ptr);
92 * Returns the number of pointers contained in the pset_new
94 * @param pset_new Pointer to the pset_new
95 * @returns Number of pointers contained in the pset_new
97 size_t pset_new_size(const pset_new_t *pset_new);
100 * Initializes a pset_new iterator. Sets the iterator before the first element in
103 * @param iterator Pointer to already allocated iterator memory
104 * @param pset_new Pointer to the pset_new
106 void pset_new_iterator_init(pset_new_iterator_t *iterator, const pset_new_t *pset_new);
109 * Advances the iterator and returns the current element or NULL if all elements
110 * in the pset_new have been processed.
111 * @attention It is not allowed to use pset_new_insert or pset_new_remove while
112 * iterating over a pset_new; pset_new_remove_iter is allowed.
114 * @param iterator Pointer to the pset_new iterator.
115 * @returns Next element in the pset_new or NULL
117 void* pset_new_iterator_next(pset_new_iterator_t *iterator);
120 * Removes the element that the iterator currently points to from the hashset.
122 * @param pset_new Pointer to the pset_new
123 * @param iterator Pointer to the iterator
125 void pset_new_remove_iterator(pset_new_t *pset_new, const pset_new_iterator_t *iterator);
128 * Convenience macro for iterating over a pset_new.
130 #define foreach_pset_new(pset_new, type, ptr, iter) \
131 for(pset_new_iterator_init(&iter, pset_new), \
132 ptr = (type) pset_new_iterator_next(&iter); \
133 ptr != NULL; ptr = (type) pset_new_iterator_next(&iter))