src/ucx/cx/collection.h

Sat, 30 Mar 2024 12:35:29 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 30 Mar 2024 12:35:29 +0100
changeset 514
922bfe380c8e
parent 504
c094afcdfb27
permissions
-rw-r--r--

merge

490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28 /**
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 * \file collection.h
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 * \brief Common definitions for various collection implementations.
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 * \author Mike Becker
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 * \author Olaf Wintermann
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 * \version 3.0
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 * \copyright 2-Clause BSD License
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 */
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #ifndef UCX_COLLECTION_H
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #define UCX_COLLECTION_H
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #include "allocator.h"
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #include "iterator.h"
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #ifdef __cplusplus
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 extern "C" {
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 #endif
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 /**
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 * Special constant used for creating collections that are storing pointers.
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 */
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 #define CX_STORE_POINTERS 0
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 /**
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 * A comparator function comparing two collection elements.
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 */
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 typedef int(*cx_compare_func)(
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 void const *left,
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 void const *right
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 );
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 /**
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 * Use this macro to declare common members for a collection structure.
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 */
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 #define CX_COLLECTION_MEMBERS \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 /** \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 * The allocator to use. \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 */ \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 CxAllocator const *allocator; \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 /** \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 * The comparator function for the elements. \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 */ \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 cx_compare_func cmpfunc; \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 /** \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 * The size of each element. \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 */ \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 size_t item_size; \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 /** \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 * The number of currently stored elements. \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 */ \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 size_t size; \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 /** \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 * An optional simple destructor for the collection's elements. \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 * \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 * @attention Read the documentation of the particular collection implementation \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 * whether this destructor shall only destroy the contents or also free the memory. \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 */ \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 cx_destructor_func simple_destructor; \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 /** \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 * An optional advanced destructor for the collection's elements. \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 * \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 * @attention Read the documentation of the particular collection implementation \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 * whether this destructor shall only destroy the contents or also free the memory. \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 */ \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 cx_destructor_func2 advanced_destructor; \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 /** \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 * The pointer to additional data that is passed to the advanced destructor. \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 */ \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 void *destructor_data; \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 /** \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 * Indicates if this instance of a collection is supposed to store pointers \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 * instead of copies of the actual objects. \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 */ \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 bool store_pointer;
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 /**
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 * Invokes the simple destructor function for a specific element.
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 *
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 * Usually only used by collection implementations. There should be no need
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 * to invoke this macro manually.
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 *
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 * @param c the collection
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 * @param e the element
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 */
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 #define cx_invoke_simple_destructor(c, e) \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 (c)->simple_destructor((c)->store_pointer ? (*((void **) (e))) : (e))
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 /**
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 * Invokes the advanced destructor function for a specific element.
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 *
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 * Usually only used by collection implementations. There should be no need
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 * to invoke this macro manually.
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 *
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 * @param c the collection
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 * @param e the element
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 */
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 #define cx_invoke_advanced_destructor(c, e) \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 (c)->advanced_destructor((c)->destructor_data, \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 (c)->store_pointer ? (*((void **) (e))) : (e))
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129
504
c094afcdfb27 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
130 /**
c094afcdfb27 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
131 * Invokes all available destructor functions for a specific element.
c094afcdfb27 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
132 *
c094afcdfb27 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
133 * Usually only used by collection implementations. There should be no need
c094afcdfb27 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
134 * to invoke this macro manually.
c094afcdfb27 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
135 *
c094afcdfb27 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
136 * @param c the collection
c094afcdfb27 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
137 * @param e the element
c094afcdfb27 update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
138 */
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 #define cx_invoke_destructor(c, e) \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 if ((c)->simple_destructor) cx_invoke_simple_destructor(c,e); \
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 if ((c)->advanced_destructor) cx_invoke_advanced_destructor(c,e)
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 #ifdef __cplusplus
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 } // extern "C"
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 #endif
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 #endif // UCX_COLLECTION_H

mercurial