2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief optimized version of set for sets containing only pointers
24 * @author Markus Armbruster
26 #ifndef FIRM_ADT_PSET_H
27 #define FIRM_ADT_PSET_H
37 * @defgroup pset Pointer Set
38 * (Hash)sets containing pointers.
39 * @note This code has been deprecated. Use pset_new or cpset for new code.
44 * The default comparison function for pointers.
47 * @return 0 if @p x and @p y are equal. Some value != 0 otherwise.
49 FIRM_API int pset_default_ptr_cmp(const void *x, const void *y);
52 * The abstract type of a pset (Set of pointers).
54 * This kind of sets stores only pointer to elements, the elements itself
55 * must be stored somewhere else.
59 typedef struct pset pset;
61 /** Inserts into pointer set with default hash function. */
62 #define pset_insert_ptr(set,key) pset_insert(set, key, hash_ptr(key))
63 /** Inserts into pointer set with default hash function and return entry */
64 #define pset_hinsert_ptr(set,key) pset_hinsert(set, key, hash_ptr(key))
65 /** Removes pointer from pointer set with default hash function */
66 #define pset_remove_ptr(set,key) pset_remove(set, key, hash_ptr(key))
67 /** Finds pointer in pointer set with default hash function */
68 #define pset_find_ptr(set,key) pset_find(set, key, hash_ptr(key))
69 /** Creates new pointer set with default compare function */
70 #define pset_new_ptr(slots) new_pset(pset_default_ptr_cmp, slots)
71 /** Creates new pointer set with default compare function and default size */
72 #define pset_new_ptr_default() pset_new_ptr(64)
74 /** The entry of a pset, representing an element pointer in the set and its meta-information */
76 unsigned hash; /**< hash value of element */
77 void *dptr; /**< pointer to element data */
81 * The type of a set compare function.
83 * @param elt pointer to an element
84 * @param key pointer to another element
87 * 0 if the elements are identically, non-zero else
89 typedef int (*pset_cmp_fun) (const void *elt, const void *key);
94 * @param func The compare function of this pset.
95 * @param slots Initial number of collision chains. I.e., \#slots
96 * different keys can be hashed without collisions.
101 FIRM_API pset *new_pset(pset_cmp_fun func, size_t slots);
106 * @param pset the pset
109 * This does NOT delete the elements of this pset, just its pointers!
111 FIRM_API void del_pset(pset *pset);
114 * Returns the number of elements in a pset.
116 * @param pset the pset
118 FIRM_API size_t pset_count(pset *pset);
121 * Searches an element pointer in a pset.
123 * @param pset the pset to search in
124 * @param key the element to search
125 * @param hash the hash value of key
128 * the pointer of the found element in the pset or NULL if it was not found
130 FIRM_API void *pset_find(pset *pset, const void *key, unsigned hash);
133 * Inserts an element pointer into a pset.
135 * @param pset the pset to insert in
136 * @param key a pointer to the element to be inserted
137 * @param hash the hash-value of the element
139 * @return a pointer to the inserted element
142 * It is not possible to insert an element more than once. If an element
143 * that should be inserted is already in the set, this functions does
144 * nothing but returning its already existing set_entry.
147 FIRM_API void *pset_insert(pset *pset, const void *key, unsigned hash);
150 * Inserts an element pointer into a pset and returns its pset_entry.
152 * @param pset the pset to insert in
153 * @param key a pointer to the element to be inserted
154 * @param hash the hash-value of the element
156 * @return a pointer to the pset_entry of the inserted element
159 * It is not possible to insert an element more than once. If an element
160 * that should be inserted is already in the pset, this functions does
161 * nothing but returning its pset_entry.
163 FIRM_API pset_entry *pset_hinsert(pset *pset, const void *key, unsigned hash);
166 * Removes an element from a pset.
168 * @param pset the pset to delete in
169 * @param key a pointer to the element to be deleted
170 * @param hash the hash-value of the element
173 * the pointer to the removed element
176 * The current implementation did not allow to remove non-existing elements.
177 * @@@ so, does it do now?
178 * Further, it is allowed to remove elements during an iteration
179 * including the current one.
181 FIRM_API void *pset_remove(pset *pset, const void *key, unsigned hash);
184 * Returns the first element of a pset.
186 * @param pset the pset to iterate
188 * @return a pointer to the element or NULL if the set is empty
190 FIRM_API void *pset_first(pset *pset);
192 #define pset_first(type, pset) ((type*)pset_first((pset)))
195 * Returns the next element of a pset.
197 * @param pset the pset to iterate
199 * @return a pointer to the next element or NULL if the
200 * iteration is finished
202 FIRM_API void *pset_next(pset *pset);
204 #define pset_next(type, pset) ((type*)pset_next((pset)))
207 * Breaks the iteration of a set. Must be called before
208 * the next pset_first() call if the iteration was NOT
211 * @param pset the pset
213 FIRM_API void pset_break(pset *pset);
216 * Iterates oven an pset.
218 * @param pset the pset
219 * @param type type of iterator variable
220 * @param entry the iterator
222 #define foreach_pset(pset, type, entry) for (type *entry = pset_first(type, pset); entry; entry = pset_next(type, pset))
225 * Inserts all elements of the pointer set src into
226 * the set target (union).
228 * @param target the target set, will contain the union
229 * @param src a set, will not be changed
231 FIRM_API void pset_insert_pset_ptr(pset *target, pset *src);
235 #define new_pset(cmp, slots) ((new_pset) ((cmp), (slots)))
236 #define pset_find(pset, key, hash) \
237 _pset_search ((pset), (key), (hash), _pset_find)
238 #define pset_insert(pset, key, hash) \
239 _pset_search ((pset), (key), (hash), _pset_insert)
240 #define pset_hinsert(pset, key, hash) \
241 ((pset_entry *)_pset_search ((pset), (key), (hash), _pset_hinsert))
243 typedef enum { _pset_find, _pset_insert, _pset_hinsert } _pset_action;
245 FIRM_API void *_pset_search(pset *, const void *, unsigned, _pset_action);