3 * File name: ir/adt/set.h
4 * Purpose: Declarations for set.
5 * Author: Markus Armbruster
7 * Created: 1999 by getting from fiasco
9 * Copyright: (c) 1995, 1996 Markus Armbruster
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 * Declarations for set.
25 * The abstract type of a set.
27 * This sets stores copies of its elements, so there is no need
28 * to store the elements after they were added to a set.
32 typedef struct set set;
34 /** The entry of a set, representing an element in the set and it's meta-information */
35 typedef struct set_entry {
36 unsigned hash; /**< the hash value of the element */
37 size_t size; /**< the size of the element */
38 int dptr[1]; /**< the element itself, data copied in must not need more
39 alignment than this */
43 * The type of a set compare function.
45 * @param elt pointer to an element
46 * @param key pointer to another element
47 * @param size size of the elements
50 * 0 if the elements are identically, non-zero else
53 * Although it is possible to define different meanings of equality
54 * of two elements of a set, they can be only equal if their sizes are
55 * are equal. This is checked before the compare function is called.
57 typedef int (*set_cmp_fun) (const void *elt, const void *key, size_t size);
62 * @param func The compare function of this set.
63 * @param slots Initial number of collision chains. I.e., #slots
64 * different keys can be hashed without collisions.
69 set *new_set (set_cmp_fun func, int slots);
72 * Deletes a set and all elements of it.
74 void del_set (set *set);
77 * Returns the number of elements in a set.
81 int set_count (set *set);
84 * Searches an element in a set.
86 * @param set the set to search in
87 * @param key the element to is searched
88 * @param size the size of key
89 * @param hash the hash value of key
92 * The address of the found element in the set or NULL if it was not found.
94 void *set_find (set *set, const void *key, size_t size, unsigned hash);
97 * Inserts an element into a set.
99 * @param set the set to insert in
100 * @param key a pointer to the element to be inserted. Element is copied!
101 * @param size the size of the element that should be inserted
102 * @param hash the hash-value of the element
104 * @return a pointer to the inserted element
107 * It is not possible to insert one element more than once. If an element
108 * that should be inserted is already in the set, this functions does
109 * nothing but returning its pointer.
111 void *set_insert (set *set, const void *key, size_t size, unsigned hash);
114 * Inserts an element into a set and returns its set_entry.
116 * @param set the set to insert in
117 * @param key a pointer to the element to be inserted. Element is copied!
118 * @param size the size of the element that should be inserted
119 * @param hash the hash-value of the element
121 * @return a pointer to the set_entry of the inserted element
124 * It is not possible to insert an element more than once. If an element
125 * that should be inserted is already in the set, this functions does
126 * nothing but returning its set_entry.
128 set_entry *set_hinsert (set *set, const void *key, size_t size, unsigned hash);
131 * Inserts an element into a set, zero-terminate it and returns its set_entry.
133 * @param set the set to insert in
134 * @param key a pointer to the element to be inserted. Element is copied!
135 * @param size the size of the element that should be inserted
136 * @param hash the hash-value of the element
138 * @return a pointer to the set_entry of the inserted element
141 * It is not possible to insert on element more than once. If an element
142 * that should be inserted is already in the set, this functions does
143 * nothing but returning its set_entry.
145 set_entry *set_hinsert0 (set *set, const void *key, size_t size, unsigned hash);
148 * Returns the first element of a set.
150 * @param set the set to iterate
152 * @return a pointer to the element or NULL if the set is empty
154 void *set_first (set *set);
157 * Returns the next element of a set.
159 * @param set the set to iterate
161 * @return a pointer to the next element or NULL if the
162 * iteration is finished
164 void *set_next (set *set);
167 * Breaks the iteration of a set. Must be called before
168 * the next pset_first() call if the iteration was NOT
171 * @param pset the pset
173 void set_break (set *set);
175 /* implementation specific */
176 #define new_set(cmp, slots) (SET_TRACE (new_set) ((cmp), (slots)))
177 #define set_find(set, key, size, hash) \
178 _set_search ((set), (key), (size), (hash), _set_find)
179 #define set_insert(set, key, size, hash) \
180 _set_search ((set), (key), (size), (hash), _set_insert)
181 #define set_hinsert(set, key, size, hash) \
182 ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert))
183 #define set_hinsert0(set, key, size, hash) \
184 ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert0))
186 #define SET_VRFY(set) (void)0
190 * Prints statistics on a set to stdout.
194 void set_stats (set *set);
196 # define set_stats(s) ((void)0)
203 * Writes a description of a set to stdout. The description includes:
204 * - a header telling how many elements (nkey) and segments (nseg) are in use
205 * - for every collision chain the number of element with its hash values
209 void set_describe (set *set);
215 typedef enum { _set_find, _set_insert, _set_hinsert, _set_hinsert0 } _set_action;
217 void *_set_search (set *, const void *, size_t, unsigned, _set_action);
219 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
220 extern const char *set_tag;
222 # define SET_TRACE set_tag = SET_ID,
224 # define SET_TRACE set_tag = __FILE__,
226 #else /* !(DEBUG && HAVE_GNU_MALLOC) */
228 #endif /* !(DEBUG && HAVE_GNU_MALLOC) */