ucx
UAP Common Extensions
Loading...
Searching...
No Matches
collection.h
Go to the documentation of this file.
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
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
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
35
36#ifndef UCX_COLLECTION_H
37#define UCX_COLLECTION_H
38
39#include "allocator.h"
40#include "iterator.h"
41#include "compare.h"
42
46#define CX_STORE_POINTERS 0
47
106
118#define CX_COLLECTION_BASE struct cx_collection_s collection
119
126#define cxCollectionSize(c) ((c)->collection.size)
127
136#define cxCollectionElementSize(c) ((c)->collection.elem_size)
137
145#define cxCollectionStoresPointers(c) ((c)->collection.store_pointer)
146
147
155#define cx_ref(c, elem) (cxCollectionStoresPointers(c) ? ((void*)&(elem)) : (elem))
156
164#define cx_deref(c, elem) (cxCollectionStoresPointers(c) ? *((void**)(elem)) : (elem))
165
178#define cxCollectionSorted(c) ((c)->collection.sorted || (c)->collection.size == 0)
179
190#define cxSetCompareFunc(c, func) \
191 (c)->collection.simple_cmp = (cx_compare_func)(func); \
192 (c)->collection.advanced_cmp = NULL
193
201#define cxSetAdvancedCompareFunc(c, func, data) \
202 (c)->collection.advanced_cmp = (cx_compare_func2) func; \
203 (c)->collection.cmp_data = data
204
215#define cx_invoke_simple_compare_func(c, left, right) \
216 (c)->collection.simple_cmp(left, right)
217
228#define cx_invoke_advanced_compare_func(c, left, right) \
229 (c)->collection.advanced_cmp(left, right, (c)->collection.cmp_data)
230
231
242#define cx_invoke_compare_func(c, left, right) \
243 (((c)->collection.advanced_cmp) ? \
244 cx_invoke_advanced_compare_func(c,left,right) : \
245 cx_invoke_simple_compare_func(c,left,right))
246
253#define cxSetDestructor(c, destr) \
254 (c)->collection.simple_destructor = (cx_destructor_func) destr
255
263#define cxSetAdvancedDestructor(c, destr, data) \
264 (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \
265 (c)->collection.destructor_data = data
266
279#define cx_invoke_simple_destructor(c, e) \
280 (c)->collection.simple_destructor((c)->collection.store_pointer ? (*((void **) (e))) : (e))
281
294#define cx_invoke_advanced_destructor(c, e) \
295 (c)->collection.advanced_destructor((c)->collection.destructor_data, \
296 (c)->collection.store_pointer ? (*((void **) (e))) : (e))
297
298
311#define cx_invoke_destructor(c, e) \
312 if ((c)->collection.simple_destructor) cx_invoke_simple_destructor(c,e); \
313 if ((c)->collection.advanced_destructor) cx_invoke_advanced_destructor(c,e)
314
328#define cx_invoke_destructor_raw(c, e) \
329 if ((c)->collection.simple_destructor) (c)->collection.simple_destructor(e); \
330 if ((c)->collection.advanced_destructor) (c)->collection.advanced_destructor((c)->collection.destructor_data, e)
331
332
333#endif // UCX_COLLECTION_H
Interface for custom allocators.
void(* cx_destructor_func2)(void *data, void *memory)
Function pointer type for destructor functions.
Definition allocator.h:116
struct cx_allocator_s CxAllocator
High-Level type alias for the allocator type.
Definition allocator.h:80
void(* cx_destructor_func)(void *memory)
Function pointer type for destructor functions.
Definition allocator.h:103
A collection of simple compare functions.
int(* cx_compare_func)(const void *left, const void *right)
A comparator function comparing two arbitrary values.
Definition compare.h:53
int(* cx_compare_func2)(const void *left, const void *right, void *data)
A comparator function comparing two arbitrary values.
Definition compare.h:60
Interface for iterator implementations.
Base attributes of a collection.
Definition collection.h:51
bool sorted
Indicates if this collection is guaranteed to be sorted.
Definition collection.h:104
const CxAllocator * allocator
The allocator to use.
Definition collection.h:55
cx_compare_func simple_cmp
A two-argument comparator function for the elements.
Definition collection.h:67
cx_destructor_func simple_destructor
An optional simple destructor for the collection's elements.
Definition collection.h:83
size_t elem_size
The size of each element.
Definition collection.h:59
bool store_pointer
Indicates if this list is supposed to store pointers instead of copies of the actual objects.
Definition collection.h:99
cx_compare_func2 advanced_cmp
A three-argument comparator function for the elements.
Definition collection.h:72
size_t size
The number of currently stored elements.
Definition collection.h:63
cx_destructor_func2 advanced_destructor
An optional advanced destructor for the collection's elements.
Definition collection.h:90
void * cmp_data
A pointer to custom data for the advanced_cmp function.
Definition collection.h:76
void * destructor_data
The pointer to additional data that is passed to the advanced destructor.
Definition collection.h:94