Zycore 1.5.2
Zyan Core Library for C
Loading...
Searching...
No Matches
Vector.h
Go to the documentation of this file.
1/***************************************************************************************************
2
3 Zyan Core Library (Zycore-C)
4
5 Original Author : Florian Bernd
6
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24
25***************************************************************************************************/
26
31
32#ifndef ZYCORE_VECTOR_H
33#define ZYCORE_VECTOR_H
34
35#include <Zycore/Allocator.h>
36#include <Zycore/Comparison.h>
37#include <Zycore/Object.h>
38#include <Zycore/Status.h>
39#include <Zycore/Types.h>
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/* ============================================================================================== */
46/* Constants */
47/* ============================================================================================== */
48
53#define ZYAN_VECTOR_MIN_CAPACITY 1
54
58#define ZYAN_VECTOR_DEFAULT_GROWTH_FACTOR 2
59
63#define ZYAN_VECTOR_DEFAULT_SHRINK_THRESHOLD 4
64
65/* ============================================================================================== */
66/* Enums and types */
67/* ============================================================================================== */
68
110
111/* ============================================================================================== */
112/* Macros */
113/* ============================================================================================== */
114
115/* ---------------------------------------------------------------------------------------------- */
116/* General */
117/* ---------------------------------------------------------------------------------------------- */
118
122#define ZYAN_VECTOR_INITIALIZER \
123 { \
124 /* allocator */ ZYAN_NULL, \
125 /* growth_factor */ 0, \
126 /* shrink_threshold */ 0, \
127 /* size */ 0, \
128 /* capacity */ 0, \
129 /* element_size */ 0, \
130 /* destructor */ ZYAN_NULL, \
131 /* data */ ZYAN_NULL \
132 }
133
134/* ---------------------------------------------------------------------------------------------- */
135/* Helper macros */
136/* ---------------------------------------------------------------------------------------------- */
137
149#ifdef __cplusplus
150#define ZYAN_VECTOR_GET(type, vector, index) \
151 (*reinterpret_cast<const type*>(ZyanVectorGet(vector, index)))
152#else
153#define ZYAN_VECTOR_GET(type, vector, index) \
154 (*(const type*)ZyanVectorGet(vector, index))
155#endif
156
165#define ZYAN_VECTOR_FOREACH(type, vector, item_name, body) \
166 { \
167 const ZyanUSize ZYAN_MACRO_CONCAT_EXPAND(size_d50d3303, item_name) = (vector)->size; \
168 for (ZyanUSize ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name) = 0; \
169 ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name) < \
170 ZYAN_MACRO_CONCAT_EXPAND(size_d50d3303, item_name); \
171 ++ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name)) \
172 { \
173 const type item_name = ZYAN_VECTOR_GET(type, vector, \
174 ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name)); \
175 body \
176 } \
177 }
178
187#define ZYAN_VECTOR_FOREACH_MUTABLE(type, vector, item_name, body) \
188 { \
189 const ZyanUSize ZYAN_MACRO_CONCAT_EXPAND(size_d50d3303, item_name) = (vector)->size; \
190 for (ZyanUSize ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name) = 0; \
191 ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name) < \
192 ZYAN_MACRO_CONCAT_EXPAND(size_d50d3303, item_name); \
193 ++ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name)) \
194 { \
195 type* const item_name = ZyanVectorGetMutable(vector, \
196 ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name)); \
197 body \
198 } \
199 }
200
201/* ---------------------------------------------------------------------------------------------- */
202
203/* ============================================================================================== */
204/* Exported functions */
205/* ============================================================================================== */
206
207/* ---------------------------------------------------------------------------------------------- */
208/* Constructor and destructor */
209/* ---------------------------------------------------------------------------------------------- */
210
211#ifndef ZYAN_NO_LIBC
212
230 ZyanUSize element_size, ZyanUSize capacity, ZyanMemberProcedure destructor);
231
232#endif // ZYAN_NO_LIBC
233
255 ZyanUSize capacity, ZyanMemberProcedure destructor, ZyanAllocator* allocator,
256 ZyanU8 growth_factor, ZyanU8 shrink_threshold);
257
274 void* buffer, ZyanUSize capacity, ZyanMemberProcedure destructor);
275
284
285/* ---------------------------------------------------------------------------------------------- */
286/* Duplication */
287/* ---------------------------------------------------------------------------------------------- */
288
289#ifndef ZYAN_NO_LIBC
290
309 const ZyanVector* source, ZyanUSize capacity);
310
311#endif // ZYAN_NO_LIBC
312
335 ZyanUSize capacity, ZyanAllocator* allocator, ZyanU8 growth_factor, ZyanU8 shrink_threshold);
336
354 const ZyanVector* source, void* buffer, ZyanUSize capacity);
355
356/* ---------------------------------------------------------------------------------------------- */
357/* Element access */
358/* ---------------------------------------------------------------------------------------------- */
359
375ZYCORE_EXPORT const void* ZyanVectorGet(const ZyanVector* vector, ZyanUSize index);
376
393
407 const void** value);
408
422 void** value);
423
434 const void* value);
435
436/* ---------------------------------------------------------------------------------------------- */
437/* Insertion */
438/* ---------------------------------------------------------------------------------------------- */
439
449
460 const void* element);
461
473 const void* elements, ZyanUSize count);
474
486 ZyanMemberFunction constructor);
487
500 void** element, ZyanMemberFunction constructor);
501
502/* ---------------------------------------------------------------------------------------------- */
503/* Utils */
504/* ---------------------------------------------------------------------------------------------- */
505
519 ZyanUSize index_second);
520
521/* ---------------------------------------------------------------------------------------------- */
522/* Deletion */
523/* ---------------------------------------------------------------------------------------------- */
524
534
545 ZyanUSize count);
546
555
564
565/* ---------------------------------------------------------------------------------------------- */
566/* Searching */
567/* ---------------------------------------------------------------------------------------------- */
568
582ZYCORE_EXPORT ZyanStatus ZyanVectorFind(const ZyanVector* vector, const void* element,
583 ZyanISize* found_index, ZyanEqualityComparison comparison);
584
600ZYCORE_EXPORT ZyanStatus ZyanVectorFindEx(const ZyanVector* vector, const void* element,
601 ZyanISize* found_index, ZyanEqualityComparison comparison, ZyanUSize index, ZyanUSize count);
602
621 ZyanUSize* found_index, ZyanComparison comparison);
622
643 ZyanUSize* found_index, ZyanComparison comparison, ZyanUSize index, ZyanUSize count);
644
645/* ---------------------------------------------------------------------------------------------- */
646/* Memory management */
647/* ---------------------------------------------------------------------------------------------- */
648
658
669 const void* initializer);
670
680
689
690/* ---------------------------------------------------------------------------------------------- */
691/* Information */
692/* ---------------------------------------------------------------------------------------------- */
693
703
713
714/* ---------------------------------------------------------------------------------------------- */
715
716/* ============================================================================================== */
717
718#ifdef __cplusplus
719}
720#endif
721
722#endif /* ZYCORE_VECTOR_H */
struct ZyanAllocator_ ZyanAllocator
Defines the ZyanAllocator struct.
Defines prototypes of general-purpose comparison functions.
ZyanI32(* ZyanComparison)(const void *left, const void *right)
Defines the ZyanComparison function prototype.
Definition Comparison.h:68
ZyanBool(* ZyanEqualityComparison)(const void *left, const void *right)
Defines the ZyanEqualityComparison function prototype.
Definition Comparison.h:55
#define ZYAN_REQUIRES_LIBC
Marks functions that require libc (cannot be used with ZYAN_NO_LIBC).
Definition Defines.h:468
#define ZYCORE_EXPORT
Symbol is exported in shared library builds.
Definition Defines.h:330
Defines some generic object-related datatypes.
void(* ZyanMemberProcedure)(void *object)
Defines the ZyanMemberProcedure function prototype.
Definition Object.h:51
ZyanStatus(* ZyanMemberFunction)(void *object)
Defines the ZyanMemberFunction function prototype.
Definition Object.h:67
Status code definitions and check macros.
ZyanU32 ZyanStatus
Defines the ZyanStatus data type.
Definition Status.h:48
Includes and defines some default data types.
ptrdiff_t ZyanISize
Definition Types.h:225
size_t ZyanUSize
Definition Types.h:224
uint8_t ZyanU8
Definition Types.h:216
ZYCORE_EXPORT ZyanStatus ZyanVectorDeleteRange(ZyanVector *vector, ZyanUSize index, ZyanUSize count)
Deletes multiple elements from the given vector, starting at index.
ZYCORE_EXPORT ZyanStatus ZyanVectorEmplace(ZyanVector *vector, void **element, ZyanMemberFunction constructor)
Constructs an element in-place at the end of the vector.
ZYCORE_EXPORT ZyanStatus ZyanVectorDuplicateEx(ZyanVector *destination, const ZyanVector *source, ZyanUSize capacity, ZyanAllocator *allocator, ZyanU8 growth_factor, ZyanU8 shrink_threshold)
Initializes a new ZyanVector instance by duplicating an existing vector and sets a custom allocator a...
ZYCORE_EXPORT ZyanStatus ZyanVectorInitEx(ZyanVector *vector, ZyanUSize element_size, ZyanUSize capacity, ZyanMemberProcedure destructor, ZyanAllocator *allocator, ZyanU8 growth_factor, ZyanU8 shrink_threshold)
Initializes the given ZyanVector instance and sets a custom allocator and memory allocation/deallocat...
ZYCORE_EXPORT ZyanStatus ZyanVectorSwapElements(ZyanVector *vector, ZyanUSize index_first, ZyanUSize index_second)
Swaps the element at index_first with the element at index_second.
ZYCORE_EXPORT ZyanStatus ZyanVectorPopBack(ZyanVector *vector)
Removes the last element of the vector.
ZYCORE_EXPORT ZyanStatus ZyanVectorGetPointerMutable(const ZyanVector *vector, ZyanUSize index, void **value)
Returns a mutable pointer to the element at the given index.
ZYCORE_EXPORT const void * ZyanVectorGet(const ZyanVector *vector, ZyanUSize index)
Returns a constant pointer to the element at the given index.
ZYCORE_EXPORT ZyanStatus ZyanVectorInsertRange(ZyanVector *vector, ZyanUSize index, const void *elements, ZyanUSize count)
Inserts multiple elements at the given index of the vector.
ZYCORE_EXPORT ZyanStatus ZyanVectorSet(ZyanVector *vector, ZyanUSize index, const void *value)
Assigns a new value to the element at the given index.
ZYCORE_EXPORT ZyanStatus ZyanVectorGetPointer(const ZyanVector *vector, ZyanUSize index, const void **value)
Returns a constant pointer to the element at the given index.
ZYCORE_EXPORT ZyanStatus ZyanVectorFind(const ZyanVector *vector, const void *element, ZyanISize *found_index, ZyanEqualityComparison comparison)
Sequentially searches for the first occurrence of element in the given vector.
ZYCORE_EXPORT ZyanStatus ZyanVectorGetCapacity(const ZyanVector *vector, ZyanUSize *capacity)
Returns the current capacity of the vector.
ZYCORE_EXPORT ZyanStatus ZyanVectorResize(ZyanVector *vector, ZyanUSize size)
Resizes the given ZyanVector instance.
ZYCORE_EXPORT void * ZyanVectorGetMutable(const ZyanVector *vector, ZyanUSize index)
Returns a mutable pointer to the element at the given index.
ZYCORE_EXPORT ZyanStatus ZyanVectorPushBack(ZyanVector *vector, const void *element)
Adds a new element to the end of the vector.
ZYCORE_EXPORT ZyanStatus ZyanVectorInitCustomBuffer(ZyanVector *vector, ZyanUSize element_size, void *buffer, ZyanUSize capacity, ZyanMemberProcedure destructor)
Initializes the given ZyanVector instance and configures it to use a custom user defined buffer with ...
ZYCORE_EXPORT ZyanStatus ZyanVectorFindEx(const ZyanVector *vector, const void *element, ZyanISize *found_index, ZyanEqualityComparison comparison, ZyanUSize index, ZyanUSize count)
Sequentially searches for the first occurrence of element in the given vector.
ZYCORE_EXPORT ZyanStatus ZyanVectorBinarySearch(const ZyanVector *vector, const void *element, ZyanUSize *found_index, ZyanComparison comparison)
Searches for the first occurrence of element in the given vector using a binary- search algorithm.
ZYCORE_EXPORT ZyanStatus ZyanVectorBinarySearchEx(const ZyanVector *vector, const void *element, ZyanUSize *found_index, ZyanComparison comparison, ZyanUSize index, ZyanUSize count)
Searches for the first occurrence of element in the given vector using a binary- search algorithm.
ZYCORE_EXPORT ZyanStatus ZyanVectorInsert(ZyanVector *vector, ZyanUSize index, const void *element)
Inserts an element at the given index of the vector.
ZYCORE_EXPORT ZyanStatus ZyanVectorDestroy(ZyanVector *vector)
Destroys the given ZyanVector instance.
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanVectorDuplicate(ZyanVector *destination, const ZyanVector *source, ZyanUSize capacity)
Initializes a new ZyanVector instance by duplicating an existing vector.
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanVectorInit(ZyanVector *vector, ZyanUSize element_size, ZyanUSize capacity, ZyanMemberProcedure destructor)
Initializes the given ZyanVector instance.
ZYCORE_EXPORT ZyanStatus ZyanVectorEmplaceEx(ZyanVector *vector, ZyanUSize index, void **element, ZyanMemberFunction constructor)
Constructs an element in-place and inserts it at the given index of the vector.
ZYCORE_EXPORT ZyanStatus ZyanVectorReserve(ZyanVector *vector, ZyanUSize capacity)
Changes the capacity of the given ZyanVector instance.
ZYCORE_EXPORT ZyanStatus ZyanVectorDuplicateCustomBuffer(ZyanVector *destination, const ZyanVector *source, void *buffer, ZyanUSize capacity)
Initializes a new ZyanVector instance by duplicating an existing vector and configures it to use a cu...
ZYCORE_EXPORT ZyanStatus ZyanVectorClear(ZyanVector *vector)
Erases all elements of the given vector.
ZYCORE_EXPORT ZyanStatus ZyanVectorShrinkToFit(ZyanVector *vector)
Shrinks the capacity of the given vector to match it's size.
struct ZyanVector_ ZyanVector
Defines the ZyanVector struct.
ZYCORE_EXPORT ZyanStatus ZyanVectorDelete(ZyanVector *vector, ZyanUSize index)
Deletes the element at the given index of the vector.
ZYCORE_EXPORT ZyanStatus ZyanVectorResizeEx(ZyanVector *vector, ZyanUSize size, const void *initializer)
Resizes the given ZyanVector instance.
ZYCORE_EXPORT ZyanStatus ZyanVectorGetSize(const ZyanVector *vector, ZyanUSize *size)
Returns the current size of the vector.
Defines the ZyanVector struct.
Definition Vector.h:76
ZyanAllocator * allocator
The memory allocator.
Definition Vector.h:80
ZyanUSize element_size
The size of a single element in bytes.
Definition Vector.h:100
void * data
The data pointer.
Definition Vector.h:108
ZyanUSize size
The current number of elements in the vector.
Definition Vector.h:92
ZyanUSize capacity
The maximum capacity (number of elements).
Definition Vector.h:96
ZyanMemberProcedure destructor
The element destructor callback.
Definition Vector.h:104
ZyanU8 growth_factor
The growth factor.
Definition Vector.h:84
ZyanU8 shrink_threshold
The shrink threshold.
Definition Vector.h:88