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 for equality of two
54 * elements of a sets, they can be only equal if there sizes are
55 * 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 number of initial slots
68 set *new_set (set_cmp_fun func, int slots);
71 * Deletes a set and all elements of it.
73 void del_set (set *set);
76 * Returns the number of elements in a set.
80 int set_count (set *set);
83 * Searches an element in a set.
85 * @param set the set to search in
86 * @param key the element to is searched
87 * @param size the size of key
88 * @param hash the hash value of key
91 * The address of the found element in the set or NULL if it was not found.
93 void *set_find (set *set, const void *key, size_t size, unsigned hash);
96 * Inserts an element into a set.
98 * @param set the set to insert in
99 * @param key a pointer to the element to be inserted. Element is copied!
100 * @param size the size of the element that should be inserted
101 * @param hash the hash-value of the element
103 * @return a pointer to the inserted element
106 * It is not possible to insert on element more than once. If an element
107 * that should be inserted is already in the set, this functions does
108 * nothing but returning its pointer.
110 void *set_insert (set *set, const void *key, size_t size, unsigned hash);
113 * Inserts an element into a set and returns its set_entry.
115 * @param set the set to insert in
116 * @param key a pointer to the element to be inserted. Element is copied!
117 * @param size the size of the element that should be inserted
118 * @param hash the hash-value of the element
120 * @return a pointer to the set_entry of the inserted element
123 * It is not possible to insert an element more than once. If an element
124 * that should be inserted is already in the set, this functions does
125 * nothing but returning its set_entry.
127 set_entry *set_hinsert (set *set, const void *key, size_t size, unsigned hash);
130 * Inserts an element into a set, zero-terminate it and returns its set_entry.
132 * @param set the set to insert in
133 * @param key a pointer to the element to be inserted. Element is copied!
134 * @param size the size of the element that should be inserted
135 * @param hash the hash-value of the element
137 * @return a pointer to the set_entry of the inserted element
140 * It is not possible to insert on element more than once. If an element
141 * that should be inserted is already in the set, this functions does
142 * nothing but returning its set_entry.
144 set_entry *set_hinsert0 (set *set, const void *key, size_t size, unsigned hash);
147 * Returns the first element of a set.
149 * @param set the set to iterate
151 * @return a pointer to the element or NULL if the set is empty
153 void *set_first (set *set);
156 * Returns the next element of a set.
158 * @param set the set to iterate
160 * @return a pointer to the next element or NULL if the
161 * iteration is finished
163 void *set_next (set *set);
166 * Breaks the iteration of a set. Must be called before
167 * the next pset_first() call if the iteration was NOT
170 * @param pset the pset
172 void set_break (set *set);
174 /* implementation specific */
175 #define new_set(cmp, slots) (SET_TRACE (new_set) ((cmp), (slots)))
176 #define set_find(set, key, size, hash) \
177 _set_search ((set), (key), (size), (hash), _set_find)
178 #define set_insert(set, key, size, hash) \
179 _set_search ((set), (key), (size), (hash), _set_insert)
180 #define set_hinsert(set, key, size, hash) \
181 ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert))
182 #define set_hinsert0(set, key, size, hash) \
183 ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert0))
185 #define SET_VRFY(set) (void)0
189 * Prints statistics on a set to stdout.
193 void set_stats (set *set);
195 # define set_stats(s) ((void)0)
199 void set_describe (set *);
205 typedef enum { _set_find, _set_insert, _set_hinsert, _set_hinsert0 } _set_action;
207 void *_set_search (set *, const void *, size_t, unsigned, _set_action);
209 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
210 extern const char *set_tag;
212 # define SET_TRACE set_tag = SET_ID,
214 # define SET_TRACE set_tag = __FILE__,
216 #else /* !(DEBUG && HAVE_GNU_MALLOC) */
218 #endif /* !(DEBUG && HAVE_GNU_MALLOC) */