arrays
This commit is contained in:
+111
@@ -0,0 +1,111 @@
|
||||
/* - | 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.
|
||||
|
||||
* --------------------------------------------------------------------------*/
|
||||
#include "../include/platform/dsa/ci2_array.h"
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void
|
||||
ci2_array_init(CI2_Array* v, int i)
|
||||
{
|
||||
v->cap = i;
|
||||
v->len = 0;
|
||||
v->items = malloc(sizeof(void*) * v->cap);
|
||||
}
|
||||
|
||||
int
|
||||
ci2_array_len(CI2_Array* v)
|
||||
{
|
||||
return v->len;
|
||||
}
|
||||
|
||||
static void
|
||||
ci2_array_resize(CI2_Array* v, int cap)
|
||||
{
|
||||
#ifdef DEBUG_ON
|
||||
printf("ci2_array_resize: %d to %d\n", v->cap, cap);
|
||||
#endif
|
||||
void** items = realloc(v->items, sizeof(void*) * cap);
|
||||
|
||||
if (items) {
|
||||
v->items = items;
|
||||
v->cap = cap;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ci2_array_add(CI2_Array* v, void* item)
|
||||
{
|
||||
if (v->cap == v->len)
|
||||
ci2_array_resize(v, v->cap * 2);
|
||||
|
||||
v->items[v->len++] = item;
|
||||
}
|
||||
|
||||
void
|
||||
ci2_array_set(CI2_Array* v, int index, void* item)
|
||||
{
|
||||
if (index >= 0 && index < v->len)
|
||||
v->items[index] = item;
|
||||
}
|
||||
|
||||
void*
|
||||
ci2_array_get(CI2_Array* v, int index)
|
||||
{
|
||||
if (index >= 0 && index < v->len)
|
||||
return v->items[index];
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
ci2_array_delete(struct CI2_Array* v, int index)
|
||||
{
|
||||
|
||||
if (index < 0 || index >= v->len)
|
||||
|
||||
return;
|
||||
|
||||
v->items[index] = NULL;
|
||||
|
||||
int i;
|
||||
|
||||
for (i = index; i < v->len - 1; i++) {
|
||||
|
||||
v->items[i] = v->items[i + 1];
|
||||
}
|
||||
|
||||
v->items[i] = NULL;
|
||||
|
||||
v->len--;
|
||||
|
||||
if (v->len > 0 && v->len == v->cap / 4)
|
||||
|
||||
ci2_array_resize(v, v->cap / 2);
|
||||
}
|
||||
|
||||
void
|
||||
ci2_array_free(struct CI2_Array* v)
|
||||
{
|
||||
|
||||
free(v->items);
|
||||
}
|
||||
Reference in New Issue
Block a user