Zycore 1.5.2
Zyan Core Library for C
Loading...
Searching...
No Matches
List.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_LIST_H
33#define ZYCORE_LIST_H
34
35#include <Zycore/Allocator.h>
36#include <Zycore/Object.h>
37#include <Zycore/Status.h>
38#include <Zycore/Types.h>
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/* ============================================================================================== */
45/* Enums and types */
46/* ============================================================================================== */
47
65
126
127/* ============================================================================================== */
128/* Macros */
129/* ============================================================================================== */
130
131/* ---------------------------------------------------------------------------------------------- */
132/* General */
133/* ---------------------------------------------------------------------------------------------- */
134
138#define ZYAN_LIST_INITIALIZER \
139 { \
140 /* allocator */ ZYAN_NULL, \
141 /* size */ 0, \
142 /* element_size */ 0, \
143 /* head */ ZYAN_NULL, \
144 /* destructor */ ZYAN_NULL, \
145 /* tail */ ZYAN_NULL, \
146 /* buffer */ ZYAN_NULL, \
147 /* capacity */ 0, \
148 /* first_unused */ ZYAN_NULL \
149 }
150
151/* ---------------------------------------------------------------------------------------------- */
152/* Helper macros */
153/* ---------------------------------------------------------------------------------------------- */
154
165#ifdef __cplusplus
166#define ZYAN_LIST_GET(type, node) \
167 (*reinterpret_cast<const type*>(ZyanListGetNodeData(node)))
168#else
169#define ZYAN_LIST_GET(type, node) \
170 (*(const type*)ZyanListGetNodeData(node))
171#endif
172
173/* ---------------------------------------------------------------------------------------------- */
174
175/* ============================================================================================== */
176/* Exported functions */
177/* ============================================================================================== */
178
179/* ---------------------------------------------------------------------------------------------- */
180/* Constructor and destructor */
181/* ---------------------------------------------------------------------------------------------- */
182
183#ifndef ZYAN_NO_LIBC
184
200 ZyanMemberProcedure destructor);
201
202#endif // ZYAN_NO_LIBC
203
218 ZyanMemberProcedure destructor, ZyanAllocator* allocator);
219
240 ZyanMemberProcedure destructor, void* buffer, ZyanUSize capacity);
241
250
251/* ---------------------------------------------------------------------------------------------- */
252/* Duplication */
253/* ---------------------------------------------------------------------------------------------- */
254
255#ifndef ZYAN_NO_LIBC
256
270 const ZyanList* source);
271
272#endif // ZYAN_NO_LIBC
273
287 ZyanAllocator* allocator);
288
310 const ZyanList* source, void* buffer, ZyanUSize capacity);
311
312/* ---------------------------------------------------------------------------------------------- */
313/* Item access */
314/* ---------------------------------------------------------------------------------------------- */
315
325
335
345
354
366
378
391
404
415 const void* value);
416
417/* ---------------------------------------------------------------------------------------------- */
418/* Insertion */
419/* ---------------------------------------------------------------------------------------------- */
420
430
440
452 ZyanMemberFunction constructor);
453
465 ZyanMemberFunction constructor);
466
467/* ---------------------------------------------------------------------------------------------- */
468/* Deletion */
469/* ---------------------------------------------------------------------------------------------- */
470
479
488
498
509 const ZyanListNode* last);
510
519
520/* ---------------------------------------------------------------------------------------------- */
521/* Searching */
522/* ---------------------------------------------------------------------------------------------- */
523
524// TODO:
525
526/* ---------------------------------------------------------------------------------------------- */
527/* Memory management */
528/* ---------------------------------------------------------------------------------------------- */
529
539
549ZYCORE_EXPORT ZyanStatus ZyanListResizeEx(ZyanList* list, ZyanUSize size, const void* initializer);
550
551/* ---------------------------------------------------------------------------------------------- */
552/* Information */
553/* ---------------------------------------------------------------------------------------------- */
554
564
565/* ---------------------------------------------------------------------------------------------- */
566
567/* ============================================================================================== */
568
569#ifdef __cplusplus
570}
571#endif
572
573#endif /* ZYCORE_VECTOR_H */
struct ZyanAllocator_ ZyanAllocator
Defines the ZyanAllocator struct.
#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
ZYCORE_EXPORT ZyanStatus ZyanListInitEx(ZyanList *list, ZyanUSize element_size, ZyanMemberProcedure destructor, ZyanAllocator *allocator)
Initializes the given ZyanList instance and sets a custom allocator.
ZYCORE_EXPORT ZyanStatus ZyanListPushFront(ZyanList *list, const void *item)
Adds a new item to the beginning of the list.
struct ZyanList_ ZyanList
Defines the ZyanList struct.
ZYCORE_EXPORT ZyanStatus ZyanListDestroy(ZyanList *list)
Destroys the given ZyanList instance.
ZYCORE_EXPORT ZyanStatus ZyanListGetNodeDataMutableEx(const ZyanListNode *node, void **value)
Returns a mutable pointer to the data of the given node.
ZYCORE_EXPORT ZyanStatus ZyanListPopBack(ZyanList *list)
Removes the last element of the list.
ZYCORE_EXPORT ZyanStatus ZyanListRemove(ZyanList *list, const ZyanListNode *node)
Removes the given node from the list.
ZYCORE_EXPORT ZyanStatus ZyanListPushBack(ZyanList *list, const void *item)
Adds a new item to the end of the list.
ZYCORE_EXPORT ZyanStatus ZyanListEmplaceBack(ZyanList *list, void **item, ZyanMemberFunction constructor)
Constructs an item in-place at the end of the list.
struct ZyanListNode_ ZyanListNode
Defines the ZyanListNode struct.
ZYCORE_EXPORT ZyanStatus ZyanListEmplaceFront(ZyanList *list, void **item, ZyanMemberFunction constructor)
Constructs an item in-place at the beginning of the list.
ZYCORE_EXPORT ZyanStatus ZyanListInitCustomBuffer(ZyanList *list, ZyanUSize element_size, ZyanMemberProcedure destructor, void *buffer, ZyanUSize capacity)
Initializes the given ZyanList instance and configures it to use a custom user defined buffer with a ...
ZYCORE_EXPORT ZyanStatus ZyanListPopFront(ZyanList *list)
Removes the firstelement of the list.
ZYCORE_EXPORT ZyanStatus ZyanListSetNodeData(const ZyanList *list, const ZyanListNode *node, const void *value)
Assigns a new data value to the given node.
ZYCORE_EXPORT ZyanStatus ZyanListGetTailNode(const ZyanList *list, const ZyanListNode **node)
Returns a pointer to the last ZyanListNode struct of the given list.
ZYCORE_EXPORT const void * ZyanListGetNodeData(const ZyanListNode *node)
Returns a constant pointer to the data of the given node.
ZYCORE_EXPORT ZyanStatus ZyanListGetNextNode(const ZyanListNode **node)
Receives a pointer to the next ZyanListNode struct linked to the passed one.
ZYCORE_EXPORT ZyanStatus ZyanListDuplicateEx(ZyanList *destination, const ZyanList *source, ZyanAllocator *allocator)
Initializes a new ZyanList instance by duplicating an existing list and sets a custom allocator.
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanListDuplicate(ZyanList *destination, const ZyanList *source)
Initializes a new ZyanList instance by duplicating an existing list.
ZYCORE_EXPORT ZYAN_REQUIRES_LIBC ZyanStatus ZyanListInit(ZyanList *list, ZyanUSize element_size, ZyanMemberProcedure destructor)
Initializes the given ZyanList instance.
ZYCORE_EXPORT ZyanStatus ZyanListGetHeadNode(const ZyanList *list, const ZyanListNode **node)
Returns a pointer to the first ZyanListNode struct of the given list.
ZYCORE_EXPORT ZyanStatus ZyanListRemoveRange(ZyanList *list, const ZyanListNode *first, const ZyanListNode *last)
Removes multiple nodes from the list.
ZYCORE_EXPORT ZyanStatus ZyanListResize(ZyanList *list, ZyanUSize size)
Resizes the given ZyanList instance.
ZYCORE_EXPORT ZyanStatus ZyanListDuplicateCustomBuffer(ZyanList *destination, const ZyanList *source, void *buffer, ZyanUSize capacity)
Initializes a new ZyanList instance by duplicating an existing list and configures it to use a custom...
ZYCORE_EXPORT ZyanStatus ZyanListGetPrevNode(const ZyanListNode **node)
Receives a pointer to the previous ZyanListNode struct linked to the passed one.
ZYCORE_EXPORT ZyanStatus ZyanListResizeEx(ZyanList *list, ZyanUSize size, const void *initializer)
Resizes the given ZyanList instance.
ZYCORE_EXPORT ZyanStatus ZyanListGetSize(const ZyanList *list, ZyanUSize *size)
Returns the current size of the list.
ZYCORE_EXPORT ZyanStatus ZyanListClear(ZyanList *list)
Erases all elements of the list.
ZYCORE_EXPORT ZyanStatus ZyanListGetNodeDataEx(const ZyanListNode *node, const void **value)
Returns a constant pointer to the data of the given node.
ZYCORE_EXPORT void * ZyanListGetNodeDataMutable(const ZyanListNode *node)
Returns a mutable pointer to the data of the given node.
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.
size_t ZyanUSize
Definition Types.h:224
Defines the ZyanListNode struct.
Definition List.h:55
struct ZyanListNode_ * prev
A pointer to the previous list node.
Definition List.h:59
struct ZyanListNode_ * next
A pointer to the next list node.
Definition List.h:63
Defines the ZyanList struct.
Definition List.h:73
ZyanAllocator * allocator
The memory allocator.
Definition List.h:77
ZyanUSize element_size
The size of a single element in bytes.
Definition List.h:85
ZyanListNode * head
The head node.
Definition List.h:93
ZyanListNode * tail
The tail node.
Definition List.h:97
void * buffer
The data buffer.
Definition List.h:103
ZyanUSize size
The current number of elements in the list.
Definition List.h:81
ZyanListNode * first_unused
The first unused node.
Definition List.h:124
ZyanUSize capacity
The data buffer capacity (number of bytes).
Definition List.h:109
ZyanMemberProcedure destructor
The element destructor callback.
Definition List.h:89