8fc0663affceb0e5ebb168b27c2c00bc1a69fc6e
10 #include "cgds/safe_alloc.h"
11 #include "cgds/types.h"
12 #include "cgds/Vector.h"
15 * @brief Cell of a set.
17 typedef struct SetCell
{
18 void* item
; ///< Generic data (key) contained in this cell.
19 struct SetCell
* next
; ///< Pointer to next cell in the list.
23 * @brief Generic set containing any data (of same size).
26 UInt size
; ///< Count elements in the set.
27 size_t dataSize
; ///< Size of a set cell element in bytes.
28 size_t hashSize
; ///< (Maximum) Number of stored hash keys.
29 SetCell
** head
; ///< Pointers to the first cell in a list.
30 UInt (*getHash
)(void*, size_t); ///< Custom hash function (optional)
34 * @brief Initialize an empty set.
37 Set
* set
, ///< "this" pointer.
38 size_t dataSize
, ///< Size in bytes of a set element.
39 size_t hashSize
, ///< (Maximum) Number of stored hash keys.
40 UInt (*getHash
)(void*, size_t); ///< Custom hash function (optional)
44 * @brief Return an allocated and initialized set.
47 size_t dataSize
, ///< Size in bytes of a set element.
48 size_t hashSize
, ///< (Maximum) Number of stored hash keys.
49 UInt (*getHash
)(void*, size_t) ///< Custom hash function (nullable)
53 * @brief Return an allocated and initialized set.
54 * @param type Type of a set element (int, char*, ...).
55 * @param hsize Size of the internal pointers array.
56 * @param getHash Custom hash function (nullable)
58 * Usage: Set* set_new(<Type> type, UInt hash_size, UInt (*getHash)(void*, size_t))
60 #define set_new(type, hsize, getHash) \
61 _set_new(sizeof(type), hsize, getHash)
64 * @brief Copy constructor (shallow copy, ok for basic types).
67 Set
* set
///< "this" pointer.
71 * @brief Check if the set is empty.
74 Set
* set
///< "this" pointer.
78 * @brief Return current size.
81 Set
* set
///< "this" pointer.
85 * @brief Lookup given element.
87 * Usage: bool set_has(Set* set, void* item)
90 Set
* set
, ///< "this" pointer.
91 void* item
///< Element to search.
95 * @brief Add an item to the set.
98 Set
* set
, ///< "this" pointer.
99 void* item
///< Element to add.
103 * @brief Add a key to the set.
104 * @param set "this" pointer.
105 * @param item Element to add.
107 * Usage: void set_add(Set* set, void item)
109 #define set_add(set, item) \
111 typeof(item) tmp = item; \
112 _set_add(set, &tmp); \
116 * @brief Remove the given item.
119 Set
* set
, ///< "this" pointer.
120 void* item
///< Element to delete.
124 * @brief Remove the given item.
125 * @param item Element to remove.
127 * Usage: void set_delete(Set* set, void item)
129 #define set_delete(set, item) \
131 typeof(item) tmp = item; \
132 _set_delete(set, &tmp); \
136 * @brief Initialize a vector with (pointers to) set elements.
138 Vector
* set_to_vector(
139 Set
* set
///< "this" pointer.
143 * @brief Clear the entire set.
146 Set
* set
///< "this" pointer.
150 * @brief Destroy the set: clear it, and free hashes array.
153 Set
* set
///< "this" pointer.