4de77f89df911337b1dd8d24ea84b5a414e653af
[libfirm] / ir / adt / pset_new.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  * @date    17.03.2007
23  * @brief   hashset containing pointers
24  * @author  Matthias Braun
25  *
26  * @note This has been named pset_new_new for now until all code has been
27  *       changed to use this instead of the old deprecated pset_new functions!
28  *       This version performs better than pset in terms of speed and memory
29  *       usage and allows multiple iterators over the set
30  */
31 #ifndef FIRM_ADT_PSET_NEW_H
32 #define FIRM_ADT_PSET_NEW_H
33
34 /** @cond PRIVATE */
35
36 #define HashSet          pset_new_t
37 #define HashSetIterator  pset_new_iterator_t
38 #define ValueType        void*
39 #define DO_REHASH
40 #include "hashset.h"
41 #undef DO_REHASH
42 #undef HashSet
43 #undef HashSetIterator
44 #undef ValueType
45
46 /** @endcond */
47
48 /** a pointer (hash)set */
49 typedef struct pset_new_t           pset_new_t;
50 /** iterator over a pointer set.
51  * @see #pset_new_t */
52 typedef struct pset_new_iterator_t  pset_new_iterator_t;
53
54 /**
55  * Initializes a pset_new
56  *
57  * @param pset_new   Pointer to allocated space for the pset_new
58  */
59 void pset_new_init(pset_new_t *pset_new);
60
61 /**
62  * Initializes a pset_new
63  *
64  * @param pset_new            Pointer to allocated space for the pset_new
65  * @param expected_elements   Number of elements expected in the pset_new (roughly)
66  */
67 void pset_new_init_size(pset_new_t *pset_new, size_t expected_elements);
68
69 /**
70  * Destroys a pset_new and frees the memory allocated for hashtable. The memory of
71  * the pset_new itself is not freed.
72  *
73  * @param pset_new   Pointer to the pset_new
74  */
75 void pset_new_destroy(pset_new_t *pset_new);
76
77 /**
78  * Inserts an element into a pset_new.
79  *
80  * @param pset_new   Pointer to the pset_new
81  * @param ptr    Pointer to insert into the pset_new
82  * @returns      1 if the pointer was inserted, 0 if it was already there
83  */
84 int pset_new_insert(pset_new_t *pset_new, void *ptr);
85
86 /**
87  * Removes an element from a pset_new. Does nothing if the pset_new doesn't contain the
88  * element.
89  *
90  * @param pset_new   Pointer to the pset_new
91  * @param ptr    Pointer to remove from the pset_new
92  */
93 void pset_new_remove(pset_new_t *pset_new, const void *ptr);
94
95 /**
96  * Tests whether a pset_new contains a pointer
97  *
98  * @param pset_new   Pointer to the pset_new
99  * @param ptr    The pointer to test
100  * @returns      1 @p pset_new contains the @p ptr, 0 otherwise
101  */
102 int pset_new_contains(const pset_new_t *pset_new, const void *ptr);
103
104 /**
105  * Returns the number of pointers contained in the pset_new
106  *
107  * @param pset_new   Pointer to the pset_new
108  * @returns      Number of pointers contained in the pset_new
109  */
110 size_t pset_new_size(const pset_new_t *pset_new);
111
112 /**
113  * Initializes a pset_new iterator. Sets the iterator before the first element in
114  * the pset_new.
115  *
116  * @param iterator   Pointer to already allocated iterator memory
117  * @param pset_new       Pointer to the pset_new
118  */
119 void pset_new_iterator_init(pset_new_iterator_t *iterator, const pset_new_t *pset_new);
120
121 /**
122  * Advances the iterator and returns the current element or NULL if all elements
123  * in the pset_new have been processed.
124  * @attention It is not allowed to use pset_new_insert or pset_new_remove while
125  *            iterating over a pset_new; pset_new_remove_iter is allowed.
126  *
127  * @param iterator  Pointer to the pset_new iterator.
128  * @returns         Next element in the pset_new or NULL
129  */
130 void* pset_new_iterator_next(pset_new_iterator_t *iterator);
131
132 /**
133  * Removes the element that the iterator currently points to from the hashset.
134  *
135  * @param pset_new      Pointer to the pset_new
136  * @param iterator  Pointer to the iterator
137  */
138 void pset_new_remove_iterator(pset_new_t *pset_new, const pset_new_iterator_t *iterator);
139
140 /**
141  * Convenience macro for iterating over a pset_new.
142  */
143 #define foreach_pset_new(pset_new, type, ptr, iter)    \
144         for(pset_new_iterator_init(&iter, pset_new), \
145                 ptr = (type) pset_new_iterator_next(&iter);     \
146                 ptr != NULL; ptr = (type) pset_new_iterator_next(&iter))
147
148 #endif