Loading...
Searching...
No Matches
Vector.h
Go to the documentation of this file.
/***************************************************************************************************
Zyan Core Library (Zycore-C)
Original Author : Florian Bernd
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
***************************************************************************************************/
#ifndef ZYCORE_VECTOR_H
#define ZYCORE_VECTOR_H
#include <Zycore/Allocator.h>
#include <Zycore/Comparison.h>
#include <Zycore/Object.h>
#include <Zycore/Status.h>
#include <Zycore/Types.h>
#ifdef __cplusplus
extern "C" {
#endif
/* ============================================================================================== */
/* Constants */
/* ============================================================================================== */
#define ZYAN_VECTOR_MIN_CAPACITY 1
#define ZYAN_VECTOR_DEFAULT_GROWTH_FACTOR 2
#define ZYAN_VECTOR_DEFAULT_SHRINK_THRESHOLD 4
/* ============================================================================================== */
/* Enums and types */
/* ============================================================================================== */
typedef struct ZyanVector_
{
ZyanAllocator* allocator;
ZyanU8 growth_factor;
ZyanU8 shrink_threshold;
ZyanUSize size;
ZyanUSize capacity;
ZyanUSize element_size;
ZyanMemberProcedure destructor;
void* data;
} ZyanVector;
/* ============================================================================================== */
/* Macros */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* General */
/* ---------------------------------------------------------------------------------------------- */
#define ZYAN_VECTOR_INITIALIZER \
{ \
/* allocator */ ZYAN_NULL, \
/* growth_factor */ 0, \
/* shrink_threshold */ 0, \
/* size */ 0, \
/* capacity */ 0, \
/* element_size */ 0, \
/* destructor */ ZYAN_NULL, \
/* data */ ZYAN_NULL \
}
/* ---------------------------------------------------------------------------------------------- */
/* Helper macros */
/* ---------------------------------------------------------------------------------------------- */
#ifdef __cplusplus
#define ZYAN_VECTOR_GET(type, vector, index) \
(*reinterpret_cast<const type*>(ZyanVectorGet(vector, index)))
#else
#define ZYAN_VECTOR_GET(type, vector, index) \
(*(const type*)ZyanVectorGet(vector, index))
#endif
#define ZYAN_VECTOR_FOREACH(type, vector, item_name, body) \
{ \
const ZyanUSize ZYAN_MACRO_CONCAT_EXPAND(size_d50d3303, item_name) = (vector)->size; \
for (ZyanUSize ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name) = 0; \
ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name) < \
ZYAN_MACRO_CONCAT_EXPAND(size_d50d3303, item_name); \
++ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name)) \
{ \
const type item_name = ZYAN_VECTOR_GET(type, vector, \
ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name)); \
body \
} \
}
#define ZYAN_VECTOR_FOREACH_MUTABLE(type, vector, item_name, body) \
{ \
const ZyanUSize ZYAN_MACRO_CONCAT_EXPAND(size_d50d3303, item_name) = (vector)->size; \
for (ZyanUSize ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name) = 0; \
ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name) < \
ZYAN_MACRO_CONCAT_EXPAND(size_d50d3303, item_name); \
++ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name)) \
{ \
type* const item_name = ZyanVectorGetMutable(vector, \
ZYAN_MACRO_CONCAT_EXPAND(i_bfd62679, item_name)); \
body \
} \
}
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
/* Exported functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Constructor and destructor */
/* ---------------------------------------------------------------------------------------------- */
#ifndef ZYAN_NO_LIBC
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanVectorInit(ZyanVector* vector,
ZyanUSize element_size, ZyanUSize capacity, ZyanMemberProcedure destructor);
#endif // ZYAN_NO_LIBC
ZYCORE_EXPORT ZyanStatus ZyanVectorInitEx(ZyanVector* vector, ZyanUSize element_size,
ZyanUSize capacity, ZyanMemberProcedure destructor, ZyanAllocator* allocator,
ZyanU8 growth_factor, ZyanU8 shrink_threshold);
ZYCORE_EXPORT ZyanStatus ZyanVectorInitCustomBuffer(ZyanVector* vector, ZyanUSize element_size,
void* buffer, ZyanUSize capacity, ZyanMemberProcedure destructor);
ZYCORE_EXPORT ZyanStatus ZyanVectorDestroy(ZyanVector* vector);
/* ---------------------------------------------------------------------------------------------- */
/* Duplication */
/* ---------------------------------------------------------------------------------------------- */
#ifndef ZYAN_NO_LIBC
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanVectorDuplicate(ZyanVector* destination,
const ZyanVector* source, ZyanUSize capacity);
#endif // ZYAN_NO_LIBC
ZYCORE_EXPORT ZyanStatus ZyanVectorDuplicateEx(ZyanVector* destination, const ZyanVector* source,
ZyanUSize capacity, ZyanAllocator* allocator, ZyanU8 growth_factor, ZyanU8 shrink_threshold);
ZYCORE_EXPORT ZyanStatus ZyanVectorDuplicateCustomBuffer(ZyanVector* destination,
const ZyanVector* source, void* buffer, ZyanUSize capacity);
/* ---------------------------------------------------------------------------------------------- */
/* Element access */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT const void* ZyanVectorGet(const ZyanVector* vector, ZyanUSize index);
ZYCORE_EXPORT void* ZyanVectorGetMutable(const ZyanVector* vector, ZyanUSize index);
ZYCORE_EXPORT ZyanStatus ZyanVectorGetPointer(const ZyanVector* vector, ZyanUSize index,
const void** value);
ZYCORE_EXPORT ZyanStatus ZyanVectorGetPointerMutable(const ZyanVector* vector, ZyanUSize index,
void** value);
ZYCORE_EXPORT ZyanStatus ZyanVectorSet(ZyanVector* vector, ZyanUSize index,
const void* value);
/* ---------------------------------------------------------------------------------------------- */
/* Insertion */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanVectorPushBack(ZyanVector* vector, const void* element);
ZYCORE_EXPORT ZyanStatus ZyanVectorInsert(ZyanVector* vector, ZyanUSize index,
const void* element);
ZYCORE_EXPORT ZyanStatus ZyanVectorInsertRange(ZyanVector* vector, ZyanUSize index,
const void* elements, ZyanUSize count);
ZYCORE_EXPORT ZyanStatus ZyanVectorEmplace(ZyanVector* vector, void** element,
ZyanMemberFunction constructor);
ZYCORE_EXPORT ZyanStatus ZyanVectorEmplaceEx(ZyanVector* vector, ZyanUSize index,
void** element, ZyanMemberFunction constructor);
/* ---------------------------------------------------------------------------------------------- */
/* Utils */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanVectorSwapElements(ZyanVector* vector, ZyanUSize index_first,
ZyanUSize index_second);
/* ---------------------------------------------------------------------------------------------- */
/* Deletion */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanVectorDelete(ZyanVector* vector, ZyanUSize index);
ZYCORE_EXPORT ZyanStatus ZyanVectorDeleteRange(ZyanVector* vector, ZyanUSize index,
ZyanUSize count);
ZYCORE_EXPORT ZyanStatus ZyanVectorPopBack(ZyanVector* vector);
ZYCORE_EXPORT ZyanStatus ZyanVectorClear(ZyanVector* vector);
/* ---------------------------------------------------------------------------------------------- */
/* Searching */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanVectorFind(const ZyanVector* vector, const void* element,
ZyanISize* found_index, ZyanEqualityComparison comparison);
ZYCORE_EXPORT ZyanStatus ZyanVectorFindEx(const ZyanVector* vector, const void* element,
ZyanISize* found_index, ZyanEqualityComparison comparison, ZyanUSize index, ZyanUSize count);
ZYCORE_EXPORT ZyanStatus ZyanVectorBinarySearch(const ZyanVector* vector, const void* element,
ZyanUSize* found_index, ZyanComparison comparison);
ZYCORE_EXPORT ZyanStatus ZyanVectorBinarySearchEx(const ZyanVector* vector, const void* element,
ZyanUSize* found_index, ZyanComparison comparison, ZyanUSize index, ZyanUSize count);
/* ---------------------------------------------------------------------------------------------- */
/* Memory management */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanVectorResize(ZyanVector* vector, ZyanUSize size);
ZYCORE_EXPORT ZyanStatus ZyanVectorResizeEx(ZyanVector* vector, ZyanUSize size,
const void* initializer);
ZYCORE_EXPORT ZyanStatus ZyanVectorReserve(ZyanVector* vector, ZyanUSize capacity);
ZYCORE_EXPORT ZyanStatus ZyanVectorShrinkToFit(ZyanVector* vector);
/* ---------------------------------------------------------------------------------------------- */
/* Information */
/* ---------------------------------------------------------------------------------------------- */
ZYCORE_EXPORT ZyanStatus ZyanVectorGetCapacity(const ZyanVector* vector, ZyanUSize* capacity);
ZYCORE_EXPORT ZyanStatus ZyanVectorGetSize(const ZyanVector* vector, ZyanUSize* size);
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
#ifdef __cplusplus
}
#endif
#endif /* ZYCORE_VECTOR_H */
Allocator.h
ZyanAllocator
struct ZyanAllocator_ ZyanAllocator
Comparison.h
ZyanComparison
ZyanI32(* ZyanComparison)(const void *left, const void *right)
Definition Comparison.h:68
ZyanEqualityComparison
ZyanBool(* ZyanEqualityComparison)(const void *left, const void *right)
Definition Comparison.h:55
ZYAN_REQUIRES_LIBC
#define ZYAN_REQUIRES_LIBC
Definition Defines.h:382
ZYCORE_EXPORT
#define ZYCORE_EXPORT
Definition Defines.h:244
Object.h
ZyanMemberProcedure
void(* ZyanMemberProcedure)(void *object)
Definition Object.h:51
ZyanMemberFunction
ZyanStatus(* ZyanMemberFunction)(void *object)
Definition Object.h:67
Status.h
ZyanStatus
ZyanU32 ZyanStatus
Definition Status.h:48
Types.h
ZyanVectorDeleteRange
ZYCORE_EXPORT ZyanStatus ZyanVectorDeleteRange(ZyanVector *vector, ZyanUSize index, ZyanUSize count)
ZyanVectorEmplace
ZYCORE_EXPORT ZyanStatus ZyanVectorEmplace(ZyanVector *vector, void **element, ZyanMemberFunction constructor)
ZyanVectorDuplicateEx
ZYCORE_EXPORT ZyanStatus ZyanVectorDuplicateEx(ZyanVector *destination, const ZyanVector *source, ZyanUSize capacity, ZyanAllocator *allocator, ZyanU8 growth_factor, ZyanU8 shrink_threshold)
ZyanVectorInitEx
ZYCORE_EXPORT ZyanStatus ZyanVectorInitEx(ZyanVector *vector, ZyanUSize element_size, ZyanUSize capacity, ZyanMemberProcedure destructor, ZyanAllocator *allocator, ZyanU8 growth_factor, ZyanU8 shrink_threshold)
ZyanVectorSwapElements
ZYCORE_EXPORT ZyanStatus ZyanVectorSwapElements(ZyanVector *vector, ZyanUSize index_first, ZyanUSize index_second)
ZyanVectorPopBack
ZYCORE_EXPORT ZyanStatus ZyanVectorPopBack(ZyanVector *vector)
ZyanVectorGetPointerMutable
ZYCORE_EXPORT ZyanStatus ZyanVectorGetPointerMutable(const ZyanVector *vector, ZyanUSize index, void **value)
ZyanVectorGet
ZYCORE_EXPORT const void * ZyanVectorGet(const ZyanVector *vector, ZyanUSize index)
ZyanVectorInsertRange
ZYCORE_EXPORT ZyanStatus ZyanVectorInsertRange(ZyanVector *vector, ZyanUSize index, const void *elements, ZyanUSize count)
ZyanVectorSet
ZYCORE_EXPORT ZyanStatus ZyanVectorSet(ZyanVector *vector, ZyanUSize index, const void *value)
ZyanVectorGetPointer
ZYCORE_EXPORT ZyanStatus ZyanVectorGetPointer(const ZyanVector *vector, ZyanUSize index, const void **value)
ZyanVectorFind
ZYCORE_EXPORT ZyanStatus ZyanVectorFind(const ZyanVector *vector, const void *element, ZyanISize *found_index, ZyanEqualityComparison comparison)
ZyanVectorGetCapacity
ZYCORE_EXPORT ZyanStatus ZyanVectorGetCapacity(const ZyanVector *vector, ZyanUSize *capacity)
ZyanVectorResize
ZYCORE_EXPORT ZyanStatus ZyanVectorResize(ZyanVector *vector, ZyanUSize size)
ZyanVectorGetMutable
ZYCORE_EXPORT void * ZyanVectorGetMutable(const ZyanVector *vector, ZyanUSize index)
ZyanVectorPushBack
ZYCORE_EXPORT ZyanStatus ZyanVectorPushBack(ZyanVector *vector, const void *element)
ZyanVectorInitCustomBuffer
ZYCORE_EXPORT ZyanStatus ZyanVectorInitCustomBuffer(ZyanVector *vector, ZyanUSize element_size, void *buffer, ZyanUSize capacity, ZyanMemberProcedure destructor)
ZyanVectorFindEx
ZYCORE_EXPORT ZyanStatus ZyanVectorFindEx(const ZyanVector *vector, const void *element, ZyanISize *found_index, ZyanEqualityComparison comparison, ZyanUSize index, ZyanUSize count)
ZyanVectorBinarySearch
ZYCORE_EXPORT ZyanStatus ZyanVectorBinarySearch(const ZyanVector *vector, const void *element, ZyanUSize *found_index, ZyanComparison comparison)
ZyanVectorBinarySearchEx
ZYCORE_EXPORT ZyanStatus ZyanVectorBinarySearchEx(const ZyanVector *vector, const void *element, ZyanUSize *found_index, ZyanComparison comparison, ZyanUSize index, ZyanUSize count)
ZyanVectorInsert
ZYCORE_EXPORT ZyanStatus ZyanVectorInsert(ZyanVector *vector, ZyanUSize index, const void *element)
ZyanVectorDestroy
ZYCORE_EXPORT ZyanStatus ZyanVectorDestroy(ZyanVector *vector)
ZyanVectorDuplicate
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanVectorDuplicate(ZyanVector *destination, const ZyanVector *source, ZyanUSize capacity)
ZyanVectorInit
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanVectorInit(ZyanVector *vector, ZyanUSize element_size, ZyanUSize capacity, ZyanMemberProcedure destructor)
ZyanVectorEmplaceEx
ZYCORE_EXPORT ZyanStatus ZyanVectorEmplaceEx(ZyanVector *vector, ZyanUSize index, void **element, ZyanMemberFunction constructor)
ZyanVectorReserve
ZYCORE_EXPORT ZyanStatus ZyanVectorReserve(ZyanVector *vector, ZyanUSize capacity)
ZyanVectorDuplicateCustomBuffer
ZYCORE_EXPORT ZyanStatus ZyanVectorDuplicateCustomBuffer(ZyanVector *destination, const ZyanVector *source, void *buffer, ZyanUSize capacity)
ZyanVectorClear
ZYCORE_EXPORT ZyanStatus ZyanVectorClear(ZyanVector *vector)
ZyanVectorShrinkToFit
ZYCORE_EXPORT ZyanStatus ZyanVectorShrinkToFit(ZyanVector *vector)
ZyanVector
struct ZyanVector_ ZyanVector
ZyanVectorDelete
ZYCORE_EXPORT ZyanStatus ZyanVectorDelete(ZyanVector *vector, ZyanUSize index)
ZyanVectorResizeEx
ZYCORE_EXPORT ZyanStatus ZyanVectorResizeEx(ZyanVector *vector, ZyanUSize size, const void *initializer)
ZyanVectorGetSize
ZYCORE_EXPORT ZyanStatus ZyanVectorGetSize(const ZyanVector *vector, ZyanUSize *size)
ZyanVector_
Definition Vector.h:76
ZyanVector_::size
ZyanUSize size
Definition Vector.h:92
ZyanVector_::shrink_threshold
ZyanU8 shrink_threshold
Definition Vector.h:88
ZyanVector_::element_size
ZyanUSize element_size
Definition Vector.h:100
ZyanVector_::capacity
ZyanUSize capacity
Definition Vector.h:96
ZyanVector_::data
void * data
Definition Vector.h:108
ZyanVector_::destructor
ZyanMemberProcedure destructor
Definition Vector.h:104
ZyanVector_::allocator
ZyanAllocator * allocator
Definition Vector.h:80
ZyanVector_::growth_factor
ZyanU8 growth_factor
Definition Vector.h:84