This commit is contained in:
2026-06-16 14:53:28 -05:00
parent 9ee1496340
commit cfc33fca38
3 changed files with 220 additions and 0 deletions
+79
View File
@@ -0,0 +1,79 @@
/* - | Copyright | ------------------------------------------------------------
Copyright (c) 2026 Randy Jordan
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 CI2_ARRAY_H
#define CI2_ARRAY_H
#include "../ci2_platform.h"
#include <stdbool.h>
#include <stddef.h> /* size_t */
#include <stdint.h> /* uintptr_t */
#define ARRAY_INIT_CAPACITY 4
typedef struct Array
{
void** items;
size_t cap;
size_t len;
} Array;
/* Initialize an array */
CI2_DEF void
ci2_array_init(struct Array* v);
/* Free the array items and set to 0. */
CI2_DEF void
ci2_array_free(struct Array* v);
/* Get the current number of items. */
CI2_DEF size_t
ci2_array_len(const struct Array* v);
/* Get the current capacity of the array. */
CI2_DEF size_t
ci2_array_capacity(const struct Array* v);
CI2_DEF int
ci2_array_shrink_to_fit(Array* v);
/* Add item to end of array. */
CI2_DEF int
ci2_array_add(struct Array* v, void* item);
/* Set the item of an array at the given index */
CI2_DEF int
ci2_array_set(Array* v, size_t index, void* item);
/* Get an item of an array at an index. */
CI2_DEF void*
ci2_array_get(const struct Array* v, size_t index);
/* Remove element (does not free). Returns 0 on success, -1 out-of-range */
CI2_DEF int
ci2_array_remove(struct Array* v, size_t index);
/* Clear all elements; if destructor non-NULL it will be called on each item */
CI2_DEF void
ci2_array_destroy(struct Array* v, void (*destructor)(void*));
#endif // ci2_array.h