2 * Copyright (C) 1995-2007 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 hashset: datastructure containing objects accessible by their key
23 * @author Markus Armbruster
26 #ifndef FIRM_ADT_SET_H
27 #define FIRM_ADT_SET_H
32 * The abstract type of a set.
34 * This sets stores copies of its elements, so there is no need
35 * to store the elements after they were added to a set.
39 typedef struct set set;
41 /** The entry of a set, representing an element in the set and it's meta-information */
42 typedef struct set_entry {
43 unsigned hash; /**< the hash value of the element */
44 size_t size; /**< the size of the element */
45 int dptr[1]; /**< the element itself, data copied in must not need more
46 alignment than this */
50 * The type of a set compare function.
52 * @param elt pointer to an element
53 * @param key pointer to another element
54 * @param size size of the elements
57 * 0 if the elements are identically, non-zero else
60 * Although it is possible to define different meanings of equality
61 * of two elements of a set, they can be only equal if their sizes are
62 * are equal. This is checked before the compare function is called.
64 typedef int (*set_cmp_fun) (const void *elt, const void *key, size_t size);
69 * @param func The compare function of this set.
70 * @param slots Initial number of collision chains. I.e., #slots
71 * different keys can be hashed without collisions.
76 set *new_set (set_cmp_fun func, int slots);
79 * Deletes a set and all elements of it.
81 void del_set (set *set);
84 * Returns the number of elements in a set.
88 int set_count (set *set);
91 * Searches an element in a set.
93 * @param set the set to search in
94 * @param key the element to is searched
95 * @param size the size of key
96 * @param hash the hash value of key
99 * The address of the found element in the set or NULL if it was not found.
101 void *set_find (set *set, const void *key, size_t size, unsigned hash);
104 * Inserts an element into a set.
106 * @param set the set to insert in
107 * @param key a pointer to the element to be inserted. Element is copied!
108 * @param size the size of the element that should be inserted
109 * @param hash the hash-value of the element
111 * @return a pointer to the inserted element
114 * It is not possible to insert one element more than once. If an element
115 * that should be inserted is already in the set, this functions does
116 * nothing but returning its pointer.
118 void *set_insert (set *set, const void *key, size_t size, unsigned hash);
121 * Inserts an element into a set and returns its set_entry.
123 * @param set the set to insert in
124 * @param key a pointer to the element to be inserted. Element is copied!
125 * @param size the size of the element that should be inserted
126 * @param hash the hash-value of the element
128 * @return a pointer to the set_entry of the inserted element
131 * It is not possible to insert an element more than once. If an element
132 * that should be inserted is already in the set, this functions does
133 * nothing but returning its set_entry.
135 set_entry *set_hinsert (set *set, const void *key, size_t size, unsigned hash);
138 * Inserts an element into a set, zero-terminate it and returns its set_entry.
140 * @param set the set to insert in
141 * @param key a pointer to the element to be inserted. Element is copied!
142 * @param size the size of the element that should be inserted
143 * @param hash the hash-value of the element
145 * @return a pointer to the set_entry of the inserted element
148 * It is not possible to insert on element more than once. If an element
149 * that should be inserted is already in the set, this functions does
150 * nothing but returning its set_entry.
152 set_entry *set_hinsert0 (set *set, const void *key, size_t size, unsigned hash);
155 * Returns the first element of a set.
157 * @param set the set to iterate
159 * @return a pointer to the element or NULL if the set is empty
161 void *set_first (set *set);
164 * Returns the next element of a set.
166 * @param set the set to iterate
168 * @return a pointer to the next element or NULL if the
169 * iteration is finished
171 void *set_next (set *set);
174 * Breaks the iteration of a set. Must be called before
175 * the next set_first() call if the iteration was NOT
180 void set_break (set *set);
183 * Iterates over an set.
186 * @param entry the iterator
188 #define foreach_set(set, entry) for (entry = set_first(set); entry; entry = set_next(set))
190 /* implementation specific */
191 #define new_set(cmp, slots) (SET_TRACE (new_set) ((cmp), (slots)))
192 #define set_find(set, key, size, hash) \
193 _set_search ((set), (key), (size), (hash), _set_find)
194 #define set_insert(set, key, size, hash) \
195 _set_search ((set), (key), (size), (hash), _set_insert)
196 #define set_hinsert(set, key, size, hash) \
197 ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert))
198 #define set_hinsert0(set, key, size, hash) \
199 ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert0))
201 #define SET_VRFY(set) (void)0
205 * Prints statistics on a set to stdout.
209 void set_stats (set *set);
211 # define set_stats(s) ((void)0)
218 * Writes a description of a set to stdout. The description includes:
219 * - a header telling how many elements (nkey) and segments (nseg) are in use
220 * - for every collision chain the number of element with its hash values
224 void set_describe (set *set);
230 typedef enum { _set_find, _set_insert, _set_hinsert, _set_hinsert0 } _set_action;
232 void *_set_search (set *, const void *, size_t, unsigned, _set_action);
234 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
235 extern const char *set_tag;
237 # define SET_TRACE set_tag = SET_ID,
239 # define SET_TRACE set_tag = __FILE__,
241 #else /* !(DEBUG && HAVE_GNU_MALLOC) */
243 #endif /* !(DEBUG && HAVE_GNU_MALLOC) */