libFirm
set.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5 
11 #ifndef FIRM_ADT_SET_H
12 #define FIRM_ADT_SET_H
13 
14 #include <stddef.h>
15 
16 #include "../begin.h"
17 
34 typedef struct set set;
35 
37 typedef struct set_entry {
38  unsigned hash;
39  size_t size;
40  int dptr[1];
42 } set_entry;
43 
59 typedef int (*set_cmp_fun) (void const *elt, void const *key, size_t size);
60 
71 FIRM_API set *new_set(set_cmp_fun func, size_t slots);
72 
78 FIRM_API void del_set(set *set);
79 
85 FIRM_API size_t set_count(set const *set);
86 
97 FIRM_API void *set_find(set *set, void const *key, size_t size, unsigned hash);
98 
114 FIRM_API void *set_insert(set *set, void const *key, size_t size, unsigned hash);
115 
131 FIRM_API set_entry *set_hinsert(set *set, void const *key, size_t size,
132  unsigned hash);
133 
149 FIRM_API set_entry *set_hinsert0(set *set, void const *key, size_t size,
150  unsigned hash);
151 
159 FIRM_API void *set_first(set *set);
160 
171 #define set_first(type, set) ((type*)set_first((set)))
172 
181 FIRM_API void *set_next(set *set);
182 
194 #define set_next(type, set) ((type*)set_next((set)))
195 
203 FIRM_API void set_break(set *set);
204 
212 #define foreach_set(set, type, entry) for (type *entry = set_first(type, set); entry; entry = set_next(type, set))
213 
216 /* implementation specific */
217 #define new_set(cmp, slots) ((new_set) ((cmp), (slots)))
218 #define set_find(type, set, key, size, hash) \
219  ((type*)_set_search((set), 1 ? (key) : (type*)0 /* type check */, (size), (hash), _set_find))
220 #define set_insert(type, set, key, size, hash) \
221  ((type*)_set_search((set), 1 ? (key) : (type*)0 /* type check */, (size), (hash), _set_insert))
222 #define set_hinsert(set, key, size, hash) \
223  ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert))
224 #define set_hinsert0(set, key, size, hash) \
225  ((set_entry *)_set_search ((set), (key), (size), (hash), _set_hinsert0))
226 
227 typedef enum { _set_find, _set_insert, _set_hinsert, _set_hinsert0 } _set_action;
228 
229 FIRM_API void *_set_search(set *set, void const *key, size_t size,
230  unsigned hash, _set_action action);
231 
236 #include "../end.h"
237 
238 #endif
void * set_find(set *set, void const *key, size_t size, unsigned hash)
Searches an element in a set.
set * new_set(set_cmp_fun func, size_t slots)
Creates a new set.
void del_set(set *set)
Deletes a set and all elements of it.
size_t set_count(set const *set)
Returns the number of elements in a set.
unsigned hash
the hash value of the element
Definition: set.h:38
The entry of a set, representing an element in the set and its meta-information.
Definition: set.h:37
int dptr[1]
the element itself, data copied in must not need more alignment than this
Definition: set.h:40
struct set set
The abstract type of a set.
Definition: set.h:34
set_entry * set_hinsert(set *set, void const *key, size_t size, unsigned hash)
Inserts an element into a set and returns its set_entry.
void * set_insert(set *set, void const *key, size_t size, unsigned hash)
Inserts an element into a set.
size_t size
the size of the element
Definition: set.h:39
#define set_first(type, set)
Returns the first element of a set.
Definition: set.h:171
set_entry * set_hinsert0(set *set, void const *key, size_t size, unsigned hash)
Inserts an element into a set, zero-terminate it and returns its set_entry.
#define set_next(type, set)
Returns the next element of a set.
Definition: set.h:194
void set_break(set *set)
Breaks the iteration of a set.
int(* set_cmp_fun)(void const *elt, void const *key, size_t size)
The type of a set compare function.
Definition: set.h:59