CArray v1.0.0-build
Minimal and Extensible Array Implementation for C
Loading...
Searching...
No Matches
carr.h
Go to the documentation of this file.
1#ifndef INCLUDE_VECTOR_VECTOR_H_
2#define INCLUDE_VECTOR_VECTOR_H_
3
4#include <stddef.h>
5#include <stdbool.h>
6
7#ifdef __cplusplus
8# define CARRVECTOR_OPEN extern "C" {
9# define CARRVECTOR_CLOSE }
10#else
11# define CARRVECTOR_OPEN
12# define CARRVECTOR_CLOSE
13#endif
14
15CARRVECTOR_OPEN
16
17/**
18 * @def CARR_VECTOR_CAP_ADDITION
19 * @brief Default capacity increment used by the vector implementation.
20 *
21 * Defines the default capacity growth increment as 16 elements.
22 * The implementation may use this value when increasing capacity.
23 *
24 * Override this macro before including the header to customize the value.
25 */
26#ifndef CARR_VECTOR_CAP_ADDITION
27# define CARR_VECTOR_CAP_ADDITION 16
28#endif
29
30/**
31 * @struct CARR
32 * @brief Represents a pointer-based vector container.
33 *
34 * Stores pointers to elements in a contiguous array. Tracks the number
35 * of elements currently stored and the allocated capacity.
36 *
37 * @warning Avoid modifying the internal fields directly
38 */
39typedef struct CARR
40{
41 /**
42 * @brief Pointer to the array of stored element pointers.
43 *
44 * Each element is a `void *`. The array itself is managed by the
45 * CARR implementation.
46 */
47 void **vec;
48
49 /**
50 * @brief Maximum number of elements that can be stored without resizing.
51 */
52 size_t cap;
53
54 /**
55 * @brief Number of elements currently stored in the vector.
56 */
57 size_t size;
58
59 /**
60 * @brief Indicates whether the vector supports dynamic resizing.
61 *
62 * When true, the vector may grow or shrink through resizing operations.
63 * When false, capacity is intended to remain fixed.
64 */
65 bool dynamic;
66
67 /**
68 * @brief Internal initialization-state flag.
69 *
70 * Used by the implementation to track whether the vector has been
71 * initialized.
72 *
73 * @internal
74 */
77
78/**
79 * @enum CARRError
80 * @brief Error codes returned by CARR operations.
81 */
82typedef enum CARRError
83{
84 /** @brief The operation completed successfully. */
86
87 /** @brief The requested index is outside the valid range. */
89
90 /** @brief The requested resize operation could not be completed. */
92
93 /** @brief Memory allocation failed. */
95
96 /** @brief A required pointer argument was NULL. */
99
100/**
101 * @brief Initializes a new CARR vector.
102 *
103 * Creates a vector with the requested initial capacity and resizing mode.
104 *
105 * @param[in] cap Initial capacity of the vector.
106 * @param[in] dynamic Whether the vector supports dynamic resizing.
107 *
108 * @return An initialized CARR instance.
109 *
110 * @note Check the returned instance according to the implementation's
111 * initialization and allocation-failure conventions.
112 */
113CARR carr_init(const size_t cap, const bool dynamic);
114
115/**
116 * @brief Changes the capacity of a vector.
117 *
118 * Requests a new capacity for the vector's underlying pointer array.
119 *
120 * @param[in,out] vec Pointer to the vector to resize.
121 * @param[in] newCap Desired capacity.
122 *
123 * @return CARR_VEC_OK if successful.
124 * @return CARR_VEC_NULL if a required pointer is NULL.
125 * @return CARR_VEC_ResizeError if resizing fails or is not permitted.
126 * @return CARR_VEC_OOM if memory allocation fails.
127 *
128 * @note The exact behavior when `newCap` is smaller than the current
129 * size depends on the implementation.
130 */
131CARRError carr_resize(CARR *vec, const size_t newCap);
132
133/**
134 * @brief Deletes an element at a specified index.
135 *
136 * Removes the element at `index` from the vector.
137 *
138 * @param[in,out] vec Pointer to the vector to modify.
139 * @param[in] index Index of the element to delete.
140 * @param[in] fall Controls the deletion behavior, as defined by the
141 * implementation.
142 *
143 * @return CARR_VEC_OK if successful.
144 * @return CARR_VEC_OutOfBounds if `index` is invalid.
145 * @return CARR_VEC_NULL if a required pointer is NULL.
146 *
147 * @note The meaning of `fall` and whether the removed element's pointer
148 * is freed must be verified in the implementation.
149 */
150CARRError carr_deleteIndex(CARR *vec, const size_t index, const bool fall);
151
152/**
153 * @brief Replaces an element at a specified index.
154 *
155 * Assigns a new pointer to the element at `idx`.
156 *
157 * @param[in,out] vec Pointer to the vector to modify.
158 * @param[in] item Pointer to the replacement element.
159 * @param[in] idx Index of the element to replace.
160 *
161 * @return CARR_VEC_OK if successful.
162 * @return CARR_VEC_OutOfBounds if `idx` is invalid.
163 * @return CARR_VEC_NULL if a required pointer is NULL.
164 *
165 * @note This function replaces the stored pointer. Ownership and cleanup
166 * of the previous element are determined by the implementation.
167 */
168CARRError carr_replaceIndex(CARR *vec, void *item, const size_t idx);
169
170/**
171 * @brief Appends an element to the end of the vector.
172 *
173 * Stores `item` after the current last element. Depending on the vector's
174 * configuration, the operation may require additional capacity.
175 *
176 * @param[in,out] vec Pointer to the vector to modify.
177 * @param[in] item Pointer to the element to append.
178 *
179 * @return CARR_VEC_OK if successful.
180 * @return CARR_VEC_NULL if a required pointer is NULL.
181 * @return CARR_VEC_ResizeError if the required resize cannot be performed.
182 * @return CARR_VEC_OOM if memory allocation fails.
183 */
184CARRError carr_pushBack(CARR *vec, void *item);
185
186/**
187 * @brief Inserts an element at the beginning of the vector.
188 *
189 * Stores `item` at index zero and shifts existing elements as necessary.
190 *
191 * @param[in,out] vec Pointer to the vector to modify.
192 * @param[in] item Pointer to the element to insert.
193 *
194 * @return CARR_VEC_OK if successful.
195 * @return CARR_VEC_NULL if a required pointer is NULL.
196 * @return CARR_VEC_ResizeError if the required resize cannot be performed.
197 * @return CARR_VEC_OOM if memory allocation fails.
198 */
199CARRError carr_pushFront(CARR *vec, void *item);
200
201/**
202 * @brief Reverses the order of elements in the vector.
203 *
204 * Reorders the stored pointers so that the first element becomes the last,
205 * the second becomes the second-to-last, and so on.
206 *
207 * @param[in,out] vec Pointer to the vector to reverse.
208 *
209 * @return CARR_VEC_OK if successful.
210 * @return CARR_VEC_NULL if a required pointer is NULL.
211 *
212 * @note The elements themselves are not copied or modified; only their
213 * positions in the vector are changed.
214 */
216
217/**
218 * @brief Destroys a vector and releases its internal resources.
219 *
220 * Releases resources owned by the vector implementation and invalidates
221 * the vector's internal storage.
222 *
223 * @param[in,out] vec Pointer to the vector to destroy.
224 *
225 * @note This function does not return an error code.
226 * @note Whether the structure is reset to a safe, reusable state depends
227 * on the implementation.
228 * @note The pointed-to elements are not assumed to be freed.
229 */
231
232CARRVECTOR_CLOSE
233
234#endif // INCLUDE_VECTOR_VECTOR_H_
CARRError carr_deleteIndex(CARR *vec, const size_t index, const bool fall)
Deletes an element at a specified index.
CARRError carr_resize(CARR *vec, const size_t newCap)
Changes the capacity of a vector.
void carr_destroy(CARR *vec)
Destroys a vector and releases its internal resources.
CARR carr_init(const size_t cap, const bool dynamic)
Initializes a new CARR vector.
CARRError
Error codes returned by CARR operations.
Definition carr.h:83
@ CARR_VEC_ResizeError
The requested resize operation could not be completed.
Definition carr.h:91
@ CARR_VEC_OutOfBounds
The requested index is outside the valid range.
Definition carr.h:88
@ CARR_VEC_NULL
A required pointer argument was NULL.
Definition carr.h:97
@ CARR_VEC_OOM
Memory allocation failed.
Definition carr.h:94
@ CARR_VEC_OK
The operation completed successfully.
Definition carr.h:85
CARRError carr_pushFront(CARR *vec, void *item)
Inserts an element at the beginning of the vector.
CARRError carr_reverse(CARR *vec)
Reverses the order of elements in the vector.
CARRError carr_pushBack(CARR *vec, void *item)
Appends an element to the end of the vector.
CARRError carr_replaceIndex(CARR *vec, void *item, const size_t idx)
Replaces an element at a specified index.
Represents a pointer-based vector container.
Definition carr.h:40
bool __initialized
Internal initialization-state flag.
Definition carr.h:75
size_t cap
Maximum number of elements that can be stored without resizing.
Definition carr.h:52
size_t size
Number of elements currently stored in the vector.
Definition carr.h:57
bool dynamic
Indicates whether the vector supports dynamic resizing.
Definition carr.h:65
void ** vec
Pointer to the array of stored element pointers.
Definition carr.h:47