32#ifndef ZYCORE_VECTOR_H
33#define ZYCORE_VECTOR_H
53#define ZYAN_VECTOR_MIN_CAPACITY 1
58#define ZYAN_VECTOR_DEFAULT_GROWTH_FACTOR 2
63#define ZYAN_VECTOR_DEFAULT_SHRINK_THRESHOLD 4
122#define ZYAN_VECTOR_INITIALIZER \
150#define ZYAN_VECTOR_GET(type, vector, index) \
151 (*reinterpret_cast<const type*>(ZyanVectorGet(vector, index)))
153#define ZYAN_VECTOR_GET(type, vector, index) \
154 (*(const type*)ZyanVectorGet(vector, index))
165#define ZYAN_VECTOR_FOREACH(type, vector, item_name, body) \
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)) \
173 const type item_name = ZYAN_VECTOR_GET(type, vector, \
174 ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name)); \
187#define ZYAN_VECTOR_FOREACH_MUTABLE(type, vector, item_name, body) \
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)) \
195 type* const item_name = ZyanVectorGetMutable(vector, \
196 ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name)); \
460 const void* element);
669 const void* initializer);
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