3 * File name: ir/adt/pset.h
4 * Purpose: Declarations for pset.
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.
21 * Define some convenience macros using the predefined hash function.
23 #define pset_insert_ptr(set,key) pset_insert(set, key, HASH_PTR(key))
24 #define pset_hinsert_ptr(set,key) pset_hinsert(set, key, HASH_PTR(key))
25 #define pset_remove_ptr(set,key) pset_remove(set, key, HASH_PTR(key))
26 #define pset_find_ptr(set,key) pset_find(set, key, HASH_PTR(key))
29 * The abstract type of a pset (Set of pointers).
31 * This kind of sets stores only pointer to elements, the elements itself
32 * must be stored somewere else.
36 typedef struct pset pset;
38 /** The entry of a pset, representing an element pointer in the set and it's meta-information */
45 * The type of a set compare function.
47 * @param elt pointer to an element
48 * @param key pointer to another element
51 * 0 if the elements are identically, non-zero else
53 typedef int (*pset_cmp_fun) (const void *elt, const void *key);
58 * @param func The compare function of this pset.
59 * @param slots Initial number of collision chains. I.e., #slots
60 * different keys can be hashed without collisions.
65 pset *new_pset (pset_cmp_fun func, int slots);
70 * @param pset the pset
73 * This does NOT delete the elements of this pset, just it's pointers!
75 void del_pset (pset *pset);
78 * Returns the number of elements in a pset.
80 * @param pset the pset
82 int pset_count (pset *pset);
85 * Searches an element pointer in a pset.
87 * @param pset the pset to search in
88 * @param key the element to search
89 * @param hash the hash value of key
92 * the pointer of the found element in the pset of NULL if it was not found
94 void *pset_find (pset *pset, const void *key, unsigned hash);
97 * Inserts an element pointer into a pset.
99 * @param pset the pset to insert in
100 * @param key a pointer to the element to 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 a element
107 * that should be inserted is already in the set, this functions does
108 * nothing but returning its already existing set_entry.
111 void *pset_insert (pset *pset, const void *key, unsigned hash);
114 * Inserts an element pointer into a pset and returns its pset_entry.
116 * @param pset the pset to insert in
117 * @param key a pointer to the element to be inserted
118 * @param hash the hash-value of the element
120 * @return a pointer to the pset_entry of the inserted element
123 * It is not possible to insert on element more than once. If a element
124 * that should be inserted is already in the pset, this functions does
125 * nothing but returning its pset_entry.
127 pset_entry *pset_hinsert (pset *pset, const void *key, unsigned hash);
130 * Removes an element from a pset.
132 * @param pset the pset to insert in
133 * @param key a pointer to the element to be inserted
134 * @param hash the hash-value of the element
137 * the pointer to the removed element
140 * The current implementation did not allow to remove non-existing elements.
141 * Further, it is allowed to remove elements during an iteration
142 * including the current one.
144 void *pset_remove (pset *pset, const void *key, unsigned hash);
147 * Returns the first element of a pset.
149 * @param pset the pset to iterate
151 * @return a pointer to the element or NULL if the set is empty
153 void *pset_first (pset *pset);
156 * Returns the next element of a pset.
158 * @param pset the pset to iterate
160 * @return a pointer to the next element or NULL if the
161 * iteration is finished
163 void *pset_next (pset *pset);
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 pset_break (pset *pset);
174 #define new_pset(cmp, slots) (PSET_TRACE (new_pset) ((cmp), (slots)))
175 #define pset_find(pset, key, hash) \
176 _pset_search ((pset), (key), (hash), _pset_find)
177 #define pset_insert(pset, key, hash) \
178 _pset_search ((pset), (key), (hash), _pset_insert)
179 #define pset_hinsert(pset, key, hash) \
180 ((pset_entry *)_pset_search ((pset), (key), (hash), _pset_hinsert))
184 * Prints statistics on a set to stdout.
186 * @param pset the pset
188 void pset_stats (pset *pset);
190 # define pset_stats(s) ((void)0)
197 * Writes a description of a set to stdout. The description includes:
198 * - a header telling how many elements (nkey) and segments (nseg) are in use
199 * - for every collision chain the number of element with its hash values
201 * @param pset the pset
203 void pset_describe (pset *pset);
207 #define PSET_VRFY(pset) (void)0
212 typedef enum { _pset_find, _pset_insert, _pset_hinsert } _pset_action;
214 void *_pset_search (pset *, const void *, unsigned, _pset_action);
216 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
217 extern const char *pset_tag;
219 # define PSET_TRACE pset_tag = SET_ID,
221 # define PSET_TRACE pset_tag = __FILE__,
223 #else /* !(DEBUG && HAVE_GNU_MALLOC) */
225 #endif /* !(DEBUG && HAVE_GNU_MALLOC) */