ir_visibility cleanup
[libfirm] / include / libfirm / adt / cpset.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    16.03.2007
23  * @brief   a set of pointers with a custom compare function
24  * @author  Matthias Braun
25  */
26 #ifndef FIRM_ADT_CPSET_H
27 #define FIRM_ADT_CPSET_H
28
29 #include "../begin.h"
30
31 /**
32  * @ingroup adt
33  * @defgroup Pointer Set (custom Compare)
34  * A pointer set with user-definable compare function
35  * @{
36  */
37
38 /**
39  * The type of a cpset compare function.
40  *
41  * @param p1   pointer to an element
42  * @param p2   pointer to another element
43  *
44  * @return  1 if the elements are identically, zero else
45  */
46 typedef int (*cpset_cmp_function) (const void *p1, const void *p2);
47
48 /**
49  * The type of a cpset hash function.
50  */
51 typedef unsigned (*cpset_hash_function) (const void *obj);
52
53 /** @cond PRIVATE */
54
55 #define HashSet          cpset_t
56 #define HashSetIterator  cpset_iterator_t
57 #define HashSetEntry     cpset_hashset_entry_t
58 #define ValueType        void*
59 #define ADDITIONAL_DATA  cpset_cmp_function cmp_function; cpset_hash_function hash_function;
60 #include "hashset.h"
61 #undef ADDITIONAL_DATA
62 #undef ValueType
63 #undef HashSetEntry
64 #undef HashSetIterator
65 #undef HashSet
66
67 /** @endcond */
68
69 /** a pointer set with custom compare function */
70 typedef struct cpset_t          cpset_t;
71 /** iterator over a pointer set with custom compare function
72  * @see #cpset_t */
73 typedef struct cpset_iterator_t cpset_iterator_t;
74
75 /**
76  * Initializes a cpset
77  *
78  * @param cpset           Pointer to allocated space for the cpset
79  * @param hash_function   The hash function to use
80  * @param cmp_function    The compare function to use
81  */
82 FIRM_API void cpset_init(cpset_t *cpset, cpset_hash_function hash_function,
83                 cpset_cmp_function cmp_function);
84
85 /**
86  * Initializes a cpset
87  *
88  * @param cpset              Pointer to allocated space for the cpset
89  * @param hash_function      The hash function to use
90  * @param cmp_function       The compare function to use
91  * @param expected_elements  Number of elements expected in the cpset (roughly)
92  */
93 FIRM_API void cpset_init_size(cpset_t *cpset, cpset_hash_function hash_function,
94                      cpset_cmp_function cmp_function,
95                      size_t expected_elements);
96
97 /**
98  * Destroys a cpset and frees the memory allocated for hashtable. The memory of
99  * the cpset itself is not freed.
100  *
101  * @param cpset   Pointer to the cpset
102  */
103 FIRM_API void cpset_destroy(cpset_t *cpset);
104
105 /**
106  * Inserts an element into a cpset.
107  *
108  * @param cpset   Pointer to the cpset
109  * @param obj     Element to insert into the cpset
110  * @returns       The element itself or a pointer to an existing element
111  */
112 FIRM_API void* cpset_insert(cpset_t *cpset, void *obj);
113
114 /**
115  * Removes an element from a cpset. Does nothing if the cpset doesn't contain the
116  * element.
117  *
118  * @param cpset   Pointer to the cpset
119  * @param obj     Pointer to remove from the cpset
120  */
121 FIRM_API void cpset_remove(cpset_t *cpset, const void *obj);
122
123 /**
124  * Tests whether a cpset contains a pointer
125  *
126  * @param cpset   Pointer to the cpset
127  * @param obj     The pointer to find
128  * @returns       An equivalent object to @p obj or NULL
129  */
130 FIRM_API void *cpset_find(const cpset_t *cpset, const void *obj);
131
132 /**
133  * Returns the number of pointers contained in the cpset
134  *
135  * @param cpset   Pointer to the cpset
136  * @returns       Number of pointers contained in the cpset
137  */
138 FIRM_API size_t cpset_size(const cpset_t *cpset);
139
140 /**
141  * Initializes a cpset iterator. Sets the iterator before the first element in
142  * the cpset.
143  *
144  * @param iterator   Pointer to already allocated iterator memory
145  * @param cpset       Pointer to the cpset
146  */
147 FIRM_API void cpset_iterator_init(cpset_iterator_t *iterator, const cpset_t *cpset);
148
149 /**
150  * Advances the iterator and returns the current element or NULL if all elements
151  * in the cpset have been processed.
152  * @attention It is not allowed to use cpset_insert or cpset_remove while
153  *            iterating over a cpset.
154  *
155  * @param iterator  Pointer to the cpset iterator.
156  * @returns         Next element in the cpset or NULL
157  */
158 FIRM_API void *cpset_iterator_next(cpset_iterator_t *iterator);
159
160 /**
161  * Removed the element the iterator currently points to
162  *
163  * @param cpset     Pointer to the cpset
164  * @param iterator  Pointer to the cpset iterator.
165  */
166 FIRM_API void cpset_remove_iterator(cpset_t *cpset, const cpset_iterator_t *iterator);
167
168 /** @} */
169
170 #include "../end.h"
171
172 #endif