src/ucx/cx/collection.h

changeset 579
e10457d74fe1
parent 504
c094afcdfb27
equal deleted inserted replaced
578:eb48f716b31c 579:e10457d74fe1
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 /** 28 /**
29 * \file collection.h 29 * @file collection.h
30 * \brief Common definitions for various collection implementations. 30 * @brief Common definitions for various collection implementations.
31 * \author Mike Becker 31 * @author Mike Becker
32 * \author Olaf Wintermann 32 * @author Olaf Wintermann
33 * \version 3.0 33 * @copyright 2-Clause BSD License
34 * \copyright 2-Clause BSD License
35 */ 34 */
36 35
37 #ifndef UCX_COLLECTION_H 36 #ifndef UCX_COLLECTION_H
38 #define UCX_COLLECTION_H 37 #define UCX_COLLECTION_H
39 38
40 #include "allocator.h" 39 #include "allocator.h"
41 #include "iterator.h" 40 #include "iterator.h"
41 #include "compare.h"
42 42
43 #ifdef __cplusplus 43 #ifdef __cplusplus
44 extern "C" { 44 extern "C" {
45 #endif 45 #endif
46 46
48 * Special constant used for creating collections that are storing pointers. 48 * Special constant used for creating collections that are storing pointers.
49 */ 49 */
50 #define CX_STORE_POINTERS 0 50 #define CX_STORE_POINTERS 0
51 51
52 /** 52 /**
53 * A comparator function comparing two collection elements. 53 * Base attributes of a collection.
54 */ 54 */
55 typedef int(*cx_compare_func)( 55 struct cx_collection_s {
56 void const *left, 56 /**
57 void const *right 57 * The allocator to use.
58 ); 58 */
59 const CxAllocator *allocator;
60 /**
61 * The comparator function for the elements.
62 */
63 cx_compare_func cmpfunc;
64 /**
65 * The size of each element.
66 */
67 size_t elem_size;
68 /**
69 * The number of currently stored elements.
70 */
71 size_t size;
72 /**
73 * An optional simple destructor for the collection's elements.
74 *
75 * @attention Read the documentation of the particular collection implementation
76 * whether this destructor shall only destroy the contents or also free the memory.
77 */
78 cx_destructor_func simple_destructor;
79 /**
80 * An optional advanced destructor for the collection's elements.
81 *
82 * @attention Read the documentation of the particular collection implementation
83 * whether this destructor shall only destroy the contents or also free the memory.
84 */
85 cx_destructor_func2 advanced_destructor;
86 /**
87 * The pointer to additional data that is passed to the advanced destructor.
88 */
89 void *destructor_data;
90 /**
91 * Indicates if this list is supposed to store pointers
92 * instead of copies of the actual objects.
93 */
94 bool store_pointer;
95 /**
96 * Indicates if this collection is guaranteed to be sorted.
97 * Note that the elements can still be sorted, even when the collection is not aware of that.
98 */
99 bool sorted;
100 };
59 101
60 /** 102 /**
61 * Use this macro to declare common members for a collection structure. 103 * Use this macro to declare common members for a collection structure.
62 */ 104 *
63 #define CX_COLLECTION_MEMBERS \ 105 * @par Example Use
64 /** \ 106 * @code
65 * The allocator to use. \ 107 * struct MyCustomSet {
66 */ \ 108 * CX_COLLECTION_BASE;
67 CxAllocator const *allocator; \ 109 * MySetElements *data;
68 /** \ 110 * }
69 * The comparator function for the elements. \ 111 * @endcode
70 */ \ 112 */
71 cx_compare_func cmpfunc; \ 113 #define CX_COLLECTION_BASE struct cx_collection_s collection
72 /** \ 114
73 * The size of each element. \ 115 /**
74 */ \ 116 * Returns the number of elements currently stored.
75 size_t item_size; \ 117 *
76 /** \ 118 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
77 * The number of currently stored elements. \ 119 * @return (@c size_t) the number of currently stored elements
78 */ \ 120 */
79 size_t size; \ 121 #define cxCollectionSize(c) ((c)->collection.size)
80 /** \ 122
81 * An optional simple destructor for the collection's elements. \ 123 /**
82 * \ 124 * Returns the size of one element.
83 * @attention Read the documentation of the particular collection implementation \ 125 *
84 * whether this destructor shall only destroy the contents or also free the memory. \ 126 * If #cxCollectionStoresPointers() returns true, this is the size of a pointer.
85 */ \ 127 *
86 cx_destructor_func simple_destructor; \ 128 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
87 /** \ 129 * @return (@c size_t) the size of one element in bytes
88 * An optional advanced destructor for the collection's elements. \ 130 */
89 * \ 131 #define cxCollectionElementSize(c) ((c)->collection.elem_size)
90 * @attention Read the documentation of the particular collection implementation \ 132
91 * whether this destructor shall only destroy the contents or also free the memory. \ 133 /**
92 */ \ 134 * Indicates whether this collection only stores pointers instead of the actual data.
93 cx_destructor_func2 advanced_destructor; \ 135 *
94 /** \ 136 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
95 * The pointer to additional data that is passed to the advanced destructor. \ 137 * @retval true if this collection stores only pointers to data
96 */ \ 138 * @retval false if this collection stores the actual element's data
97 void *destructor_data; \ 139 */
98 /** \ 140 #define cxCollectionStoresPointers(c) ((c)->collection.store_pointer)
99 * Indicates if this instance of a collection is supposed to store pointers \ 141
100 * instead of copies of the actual objects. \ 142 /**
101 */ \ 143 * Indicates whether the collection can guarantee that the stored elements are currently sorted.
102 bool store_pointer; 144 *
145 * This may return false even when the elements are sorted.
146 * It is totally up to the implementation of the collection whether it keeps track of the order of its elements.
147 *
148 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
149 * @retval true if the elements are currently sorted wrt. the collection's compare function
150 * @retval false if the order of elements is unknown
151 */
152 #define cxCollectionSorted(c) ((c)->collection.sorted)
153
154 /**
155 * Sets a simple destructor function for this collection.
156 *
157 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
158 * @param destr the destructor function
159 */
160 #define cxDefineDestructor(c, destr) \
161 (c)->collection.simple_destructor = (cx_destructor_func) destr
162
163 /**
164 * Sets a simple destructor function for this collection.
165 *
166 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
167 * @param destr the destructor function
168 */
169 #define cxDefineAdvancedDestructor(c, destr, data) \
170 (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \
171 (c)->collection.destructor_data = data
103 172
104 /** 173 /**
105 * Invokes the simple destructor function for a specific element. 174 * Invokes the simple destructor function for a specific element.
106 * 175 *
107 * Usually only used by collection implementations. There should be no need 176 * Usually only used by collection implementations. There should be no need
108 * to invoke this macro manually. 177 * to invoke this macro manually.
109 * 178 *
110 * @param c the collection 179 * When the collection stores pointers, those pointers are directly passed
111 * @param e the element 180 * to the destructor. Otherwise, a pointer to the element is passed.
181 *
182 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
183 * @param e the element (the type is @c void* or @c void** depending on context)
112 */ 184 */
113 #define cx_invoke_simple_destructor(c, e) \ 185 #define cx_invoke_simple_destructor(c, e) \
114 (c)->simple_destructor((c)->store_pointer ? (*((void **) (e))) : (e)) 186 (c)->collection.simple_destructor((c)->collection.store_pointer ? (*((void **) (e))) : (e))
115 187
116 /** 188 /**
117 * Invokes the advanced destructor function for a specific element. 189 * Invokes the advanced destructor function for a specific element.
118 * 190 *
119 * Usually only used by collection implementations. There should be no need 191 * Usually only used by collection implementations. There should be no need
120 * to invoke this macro manually. 192 * to invoke this macro manually.
121 * 193 *
122 * @param c the collection 194 * When the collection stores pointers, those pointers are directly passed
123 * @param e the element 195 * to the destructor. Otherwise, a pointer to the element is passed.
196 *
197 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
198 * @param e the element (the type is @c void* or @c void** depending on context)
124 */ 199 */
125 #define cx_invoke_advanced_destructor(c, e) \ 200 #define cx_invoke_advanced_destructor(c, e) \
126 (c)->advanced_destructor((c)->destructor_data, \ 201 (c)->collection.advanced_destructor((c)->collection.destructor_data, \
127 (c)->store_pointer ? (*((void **) (e))) : (e)) 202 (c)->collection.store_pointer ? (*((void **) (e))) : (e))
128 203
129 204
130 /** 205 /**
131 * Invokes all available destructor functions for a specific element. 206 * Invokes all available destructor functions for a specific element.
132 * 207 *
133 * Usually only used by collection implementations. There should be no need 208 * Usually only used by collection implementations. There should be no need
134 * to invoke this macro manually. 209 * to invoke this macro manually.
135 * 210 *
136 * @param c the collection 211 * When the collection stores pointers, those pointers are directly passed
137 * @param e the element 212 * to the destructor. Otherwise, a pointer to the element is passed.
213 *
214 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE
215 * @param e the element (the type is @c void* or @c void** depending on context)
138 */ 216 */
139 #define cx_invoke_destructor(c, e) \ 217 #define cx_invoke_destructor(c, e) \
140 if ((c)->simple_destructor) cx_invoke_simple_destructor(c,e); \ 218 if ((c)->collection.simple_destructor) cx_invoke_simple_destructor(c,e); \
141 if ((c)->advanced_destructor) cx_invoke_advanced_destructor(c,e) 219 if ((c)->collection.advanced_destructor) cx_invoke_advanced_destructor(c,e)
142 220
143 #ifdef __cplusplus 221 #ifdef __cplusplus
144 } // extern "C" 222 } // extern "C"
145 #endif 223 #endif
146 224

mercurial