1 /* Declarations for set.
2 Copyright (C) 1995, 1996 Markus Armbruster */
9 * Declarations for set.
18 * The abstract type of a set.
20 * This sets stores copies of its elements, so there is no need
21 * to store the elements after there were added to a set.
25 typedef struct set set;
27 /** The entry of a set, representing an element in the set and it's meta-information */
28 typedef struct set_entry {
29 unsigned hash; /**< the hash value of the element */
30 size_t size; /**< the size of the element */
31 int dptr[1]; /**< the element itself, data copied in must not need more
32 alignment than this */
36 * The type of a set compare function.
38 * @param elt pointer to an element
39 * @param key pointer to another element
40 * @param size size of the elements
43 * 0 if the elements are identically, non-zero else
46 * Although it is possible to define different meanings for equality of two
47 * elements of a sets, they can be only equal if there sizes are
48 * equal. This is checked before the compare function is called.
50 typedef int (*set_cmp_fun) (const void *elt, const void *key, size_t size);
55 * @param func the compare function of this set
56 * @param slots number of slots
61 set *new_set (set_cmp_fun func, int slots);
63 /** Deletes a set and all elements of it. */
64 void del_set (set *set);
67 * Searches an element in a set.
69 * @param set the set to search in
70 * @param key the element to is searched
71 * @param size the size of key
72 * @param hash the hash value of key
75 * the address of the found element in the set of NULL if it was not found
77 void *set_find (set *set, const void *key, size_t size, unsigned hash);
80 * Inserts an element into a set.
82 * @param set the set to insert in
83 * @param key a pointer to the element to be inserted
84 * @param size the size of the element that should be inserted
85 * @param hash the hash-value of the element
87 * @return a pointer to the inserted element
90 * It is not possible to insert on element more than once. If a element
91 * that should be inserted is already in the set, this functions does
92 * nothing but returning its pointer.
94 void *set_insert (set *set, const void *key, size_t size, unsigned hash);
97 * Inserts an element into a set and returns its set_entry.
99 * @param set the set to insert in
100 * @param key a pointer to the element to be inserted
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 set_entry of the inserted element
107 * It is not possible to insert on element more than once. If a element
108 * that should be inserted is already in the set, this functions does
109 * nothing but returning its set_entry.
111 set_entry *set_hinsert (set *set, const void *key, size_t size, unsigned hash);
113 /** Returns the first element of a set. */
114 void *set_first (set *set);
116 /** Returns the next element of a set. */
117 void *set_next (set *set);
119 /** Breaks the iteration of a set. Must be called before the next set_first() call */
120 void set_break (set *set);
122 /* implementation specific */
123 #define new_set(cmp, slots) (SET_TRACE (new_set) ((cmp), (slots)))
124 #define set_find(set, key, size, hash) \
125 _set_search ((set), (key), (size), (hash), _set_find)
126 #define set_insert(set, key, size, hash) \
127 _set_search ((set), (key), (size), (hash), _set_insert)
128 #define set_hinsert(set, key, size, hash) \
129 ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert))
131 #define SET_VRFY(set) (void)0
134 void set_stats (set *);
136 # define set_stats(s) ((void)0)
140 void set_describe (set *);
146 typedef enum { _set_find, _set_insert, _set_hinsert } _set_action;
148 void *_set_search (set *, const void *, size_t, unsigned, _set_action);
150 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
151 extern const char *set_tag;
153 # define SET_TRACE set_tag = SET_ID,
155 # define SET_TRACE set_tag = __FILE__,
157 #else /* !(DEBUG && HAVE_GNU_MALLOC) */
159 #endif /* !(DEBUG && HAVE_GNU_MALLOC) */