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