4 * @brief a set of pointers with a custom compare function
5 * @author Matthias Braun
12 * The type of a cpset compare function.
14 * @param p1 pointer to an element
15 * @param p2 pointer to another element
17 * @return 1 if the elements are identically, zero else
19 typedef int (*cpset_cmp_function) (const void *p1, const void *p2);
22 * The type of a cpset hash function.
24 * @param p1 pointer to an element
25 * @param p2 pointer to another element
27 * @return 1 if the elements are identically, zero else
29 typedef unsigned (*cpset_hash_function) (const void *obj);
31 #define HashSet cpset_t
32 #define HashSetIterator cpset_iterator_t
33 #define HashSetEntry cpset_hashset_entry_t
34 #define ValueType void*
35 #define ADDITIONAL_DATA cpset_cmp_function cmp_function; cpset_hash_function hash_function;
37 #undef ADDITIONAL_DATA
40 #undef HashSetIterator
46 * @param cpset Pointer to allocated space for the cpset
47 * @param cmp_function The compare function to use
49 void cpset_init(cpset_t *cpset, cpset_hash_function hash_function,
50 cpset_cmp_function cmp_function);
55 * @param cpset Pointer to allocated space for the cpset
56 * @param cmp_function The compare function to use
57 * @param expected_elements Number of elements expected in the cpset (rougly)
59 void cpset_init_size(cpset_t *cpset, cpset_hash_function hash_function,
60 cpset_cmp_function cmp_function,
61 size_t expected_elements);
64 * Destroys a cpset and frees the memory allocated for hashtable. The memory of
65 * the cpset itself is not freed.
67 * @param cpset Pointer to the cpset
69 void cpset_destroy(cpset_t *cpset);
72 * Inserts an element into a cpset.
74 * @param cpset Pointer to the cpset
75 * @param obj Element to insert into the cpset
76 * @returns The element itself or a pointer to an existing element
78 void* cpset_insert(cpset_t *cpset, void *obj);
81 * Removes an element from a cpset. Does nothing if the cpset doesn't contain the
84 * @param cpset Pointer to the cpset
85 * @param obj Pointer to remove from the cpset
87 void cpset_remove(cpset_t *cpset, const void *obj);
90 * Tests whether a cpset contains a pointer
92 * @param cpset Pointer to the cpset
93 * @param obj The pointer to find
94 * @returns An equivalent object to @p obj or NULL
96 void *cpset_find(const cpset_t *cpset, const void *obj);
99 * Returns the number of pointers contained in the cpset
101 * @param cpset Pointer to the cpset
102 * @returns Number of pointers contained in the cpset
104 size_t cpset_size(const cpset_t *cpset);
107 * Initializes a cpset iterator. Sets the iterator before the first element in
110 * @param iterator Pointer to already allocated iterator memory
111 * @param cpset Pointer to the cpset
113 void cpset_iterator_init(cpset_iterator_t *iterator, const cpset_t *cpset);
116 * Advances the iterator and returns the current element or NULL if all elements
117 * in the cpset have been processed.
118 * @attention It is not allowed to use cpset_insert or cpset_remove while
119 * iterating over a cpset.
121 * @param iterator Pointer to the cpset iterator.
122 * @returns Next element in the cpset or NULL
124 void *cpset_iterator_next(cpset_iterator_t *iterator);
127 * Removed the element the iterator currently points to
129 * @param cpset Pointer to the cpset
130 * @param iterator Pointer to the cpset iterator.
132 void cpset_remove_iterator(cpset_t *cpset, const cpset_iterator_t *iterator);