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
45 typedef int (*set_cmp_fun) (const void *elt, const void *key, size_t size);
50 * @param func the compare function of this set
51 * @param slots number of slots
56 set *new_set (set_cmp_fun func, int slots);
58 /** Deletes a set and all elements of it. */
59 void del_set (set *set);
62 * Searches an element in a set.
64 * @param set the set to search in
65 * @param key the element to is searched
66 * @param size the size of key
67 * @param hash the hash value of key
70 * the address of the found element in the set of NULL if it was not found
72 void *set_find (set *set, const void *key, size_t size, unsigned hash);
75 * Inserts an element into a set.
77 * @param set the set to insert in
78 * @param key a pointer to the element to be inserted
79 * @param size the size of the element that should be inserted
80 * @param hash the hash-value of the element
82 * @return a pointer to the inserted element
85 * It is not possible to insert on element more than once. If a element
86 * that should be inserted is already in the set, this functions does
87 * nothing but returning its pointer.
89 void *set_insert (set *set, const void *key, size_t size, unsigned hash);
92 * Inserts an element into a set and returns its set_entry.
94 * @param set the set to insert in
95 * @param key a pointer to the element to be inserted
96 * @param size the size of the element that should be inserted
97 * @param hash the hash-value of the element
99 * @return a pointer to the set_entry of the inserted element
102 * It is not possible to insert on element more than once. If a element
103 * that should be inserted is already in the set, this functions does
104 * nothing but returning its set_entry.
106 set_entry *set_hinsert (set *set, const void *key, size_t size, unsigned hash);
108 /** Returns the first element of a set. */
109 void *set_first (set *set);
111 /** Returns the next element of a set. */
112 void *set_next (set *set);
114 /** Breaks the iteration of a set. Must be called before the next set_first() call */
115 void set_break (set *set);
117 /* implementation specific */
118 #define new_set(cmp, slots) (SET_TRACE (new_set) ((cmp), (slots)))
119 #define set_find(set, key, size, hash) \
120 _set_search ((set), (key), (size), (hash), _set_find)
121 #define set_insert(set, key, size, hash) \
122 _set_search ((set), (key), (size), (hash), _set_insert)
123 #define set_hinsert(set, key, size, hash) \
124 ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert))
126 #define SET_VRFY(set) (void)0
129 void set_stats (set *);
131 # define set_stats(s) ((void)0)
135 void set_describe (set *);
141 typedef enum { _set_find, _set_insert, _set_hinsert } _set_action;
143 void *_set_search (set *, const void *, size_t, unsigned, _set_action);
145 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
146 extern const char *set_tag;
148 # define SET_TRACE set_tag = SET_ID,
150 # define SET_TRACE set_tag = __FILE__,
152 #else /* !(DEBUG && HAVE_GNU_MALLOC) */
154 #endif /* !(DEBUG && HAVE_GNU_MALLOC) */