-
Notifications
You must be signed in to change notification settings - Fork 0
/
da.h
114 lines (98 loc) · 3.29 KB
/
da.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#ifndef DYNAMICARRAY_H_
#define DYNAMICARRAY_H_
#include <stdlib.h>
#include <stdbool.h>
#define DA_ERROR SIZE_MAX
#define DA_INVALID_ARRAY SIZE_MAX
#define DA_NOT_FOUND SIZE_MAX
#define DA_INVALID_INDICES SIZE_MAX
/**
* @brief A dynamic array data structure.
*
* @warning Always zero-initialize!
*/
typedef struct daArray
{
void* data; /**< Pointer to the array data */ // TODO: Add a bucketing system for faster insertion and removal.
void* tempElement; /**< Pointer to temporary element for use in swapping & sorting. */
size_t allocatedSize; /**< Allocated size of the array */
size_t elementCount; /**< Number of elements in the array */
size_t elementByteSize; /**< Size of each array element in bytes */
bool zeroInitialize; /**< Whether to zero-initialize memory */
} daArray;
/**
* @brief Initialize a dynamic array.
*
* @param array The dynamic array to initialize.
* @param size Initial size of the array.
* @param elementByteSize Size of each element in bytes.
* @param zeroInitialize Whether to zero-initialize memory.
* @return Returns true if initialization is successful, false otherwise.
*/
bool daInit(daArray* array, size_t size, size_t elementByteSize, bool zeroInitialize);
/**
* @brief Insert an element at a specific index.
*
* @param array The dynamic array.
* @param element Pointer to the element to insert.
* @param index Index at which to insert the element.
* @return New size of the array after insertion.
*/
size_t daInsert(daArray* array, void* element, size_t index);
/**
* @brief Add an element to the end of the array.
*
* @param array The dynamic array.
* @param element Pointer to the element to add.
* @return Index of the newly added element.
*/
size_t daPushBack(daArray* array, void* element);
/**
* @brief Get an element from the array at a specific index.
*
* @param array The dynamic array.
* @param index Index of the element to retrieve.
* @return Pointer to the element at the given index.
*/
void* daGet(daArray* array, size_t index);
/**
* @brief Replace an element at a specific index.
*
* @param array The dynamic array.
* @param element Pointer to the new element.
* @param index Index of the element to replace.
* @return Returns true if the element was replaced successfully, false otherwise.
*/
bool daReplace(daArray* array, void* element, size_t index);
/**
* @brief Remove an element from the array at a specific index.
*
* @param array The dynamic array.
* @param index Index of the element to remove.
* @return New size of the array after removal.
*/
size_t daRemove(daArray* array, size_t index);
/**
* @brief Resize the dynamic array.
*
* @param array The dynamic array.
* @param newSize New size for the array.
* @return Returns true if resizing is successful, false otherwise.
*/
bool daResize(daArray* array, size_t newSize);
/**
* @brief Free the memory used by the dynamic array.
*
* @param array The dynamic array to free.
* @return Returns true if memory is successfully freed, false otherwise.
*/
bool daFree(daArray* array);
/**
* @brief Get the index of the last element in the array.
*
* @param array The dynamic array.
*
* @return Index of the last element (0 is returned if the array is empty).
*/
size_t daGetFinalElementIndex(daArray* array);
#endif