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 hashset: datastructure containing objects accessible by their key
23 * @author Markus Armbruster
25 #ifndef FIRM_ADT_SET_H
26 #define FIRM_ADT_SET_H
33 * The abstract type of a set.
35 * This sets stores copies of its elements, so there is no need
36 * to store the elements after they were added to a set.
40 typedef struct set set;
42 /** The entry of a set, representing an element in the set and its meta-information */
43 typedef struct set_entry {
44 unsigned hash; /**< the hash value of the element */
45 size_t size; /**< the size of the element */
46 int dptr[1]; /**< the element itself, data copied in must not need more
47 alignment than this */
51 * The type of a set compare function.
53 * @param elt pointer to an element
54 * @param key pointer to another element
55 * @param size size of the elements
58 * 0 if the elements are identically, non-zero else
61 * Although it is possible to define different meanings of equality
62 * of two elements of a set, they can be only equal if their sizes are
63 * are equal. This is checked before the compare function is called.
65 typedef int (*set_cmp_fun) (const void *elt, const void *key, size_t size);
70 * @param func The compare function of this set.
71 * @param slots Initial number of collision chains. I.e., \#slots
72 * different keys can be hashed without collisions.
77 FIRM_API set *new_set(set_cmp_fun func, size_t slots);
80 * Deletes a set and all elements of it.
82 * @param set the set to delete
84 FIRM_API void del_set(set *set);
87 * Returns the number of elements in a set.
91 FIRM_API size_t set_count(set *set);
94 * Searches an element in a set.
96 * @param set the set to search in
97 * @param key the element to is searched
98 * @param size the size of key
99 * @param hash the hash value of key
102 * The address of the found element in the set or NULL if it was not found.
104 FIRM_API void *set_find(set *set, const void *key, size_t size, unsigned hash);
107 * Inserts an element into a set.
109 * @param set the set to insert in
110 * @param key a pointer to the element to be inserted. Element is copied!
111 * @param size the size of the element that should be inserted
112 * @param hash the hash-value of the element
114 * @return a pointer to the inserted element
117 * It is not possible to insert one element more than once. If an element
118 * that should be inserted is already in the set, this functions does
119 * nothing but returning its pointer.
121 FIRM_API void *set_insert(set *set, const void *key, size_t size, unsigned hash);
124 * Inserts an element into a set and returns its set_entry.
126 * @param set the set to insert in
127 * @param key a pointer to the element to be inserted. Element is copied!
128 * @param size the size of the element that should be inserted
129 * @param hash the hash-value of the element
131 * @return a pointer to the set_entry of the inserted element
134 * It is not possible to insert an element more than once. If an element
135 * that should be inserted is already in the set, this functions does
136 * nothing but returning its set_entry.
138 FIRM_API set_entry *set_hinsert(set *set, const void *key, size_t size, unsigned hash);
141 * Inserts an element into a set, zero-terminate it and returns its set_entry.
143 * @param set the set to insert in
144 * @param key a pointer to the element to be inserted. Element is copied!
145 * @param size the size of the element that should be inserted
146 * @param hash the hash-value of the element
148 * @return a pointer to the set_entry of the inserted element
151 * It is not possible to insert on element more than once. If an element
152 * that should be inserted is already in the set, this functions does
153 * nothing but returning its set_entry.
155 FIRM_API set_entry *set_hinsert0(set *set, const void *key, size_t size, unsigned hash);
158 * Returns the first element of a set.
160 * @param set the set to iterate
162 * @return a pointer to the element or NULL if the set is empty
164 FIRM_API void *set_first(set *set);
167 * Returns the next element of a set.
169 * @param set the set to iterate
171 * @return a pointer to the next element or NULL if the
172 * iteration is finished
174 FIRM_API void *set_next(set *set);
177 * Breaks the iteration of a set. Must be called before
178 * the next set_first() call if the iteration was NOT
183 FIRM_API void set_break(set *set);
186 * Iterates over an set.
189 * @param type type of iterator variable
190 * @param entry the iterator
192 #define foreach_set(set, type, entry) for (entry = (type) set_first(set); entry; entry = (type) set_next(set))
194 /* implementation specific */
195 #define new_set(cmp, slots) ((new_set) ((cmp), (slots)))
196 #define set_find(set, key, size, hash) \
197 _set_search ((set), (key), (size), (hash), _set_find)
198 #define set_insert(set, key, size, hash) \
199 _set_search ((set), (key), (size), (hash), _set_insert)
200 #define set_hinsert(set, key, size, hash) \
201 ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert))
202 #define set_hinsert0(set, key, size, hash) \
203 ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert0))
205 #define SET_VRFY(set) (void)0
209 * Prints statistics on a set to stdout.
213 void set_stats (set *set);
215 # define set_stats(s) ((void)0)
220 typedef enum { _set_find, _set_insert, _set_hinsert, _set_hinsert0 } _set_action;
222 FIRM_API void *_set_search(set *, const void *, size_t, unsigned, _set_action);