Add an ArraySet() method for replacing entries in an array.

This is much more efficient than using ArrayDelete() and ArrayInsert(),
and will serve us well in the future.
This commit is contained in:
Jordan Bancino 2023-03-22 16:31:06 +00:00
parent b8d00bc8bf
commit e6f3dfad18
2 changed files with 32 additions and 3 deletions

View file

@ -46,7 +46,7 @@ ArrayAdd(Array * array, void *value)
return 0; return 0;
} }
return ArrayInsert(array, value, array->size); return ArrayInsert(array, array->size, value);
} }
Array * Array *
@ -123,7 +123,7 @@ ArrayGet(Array * array, size_t index)
extern int extern int
ArrayInsert(Array * array, void *value, size_t index) ArrayInsert(Array * array, size_t index, void *value)
{ {
size_t i; size_t i;
@ -163,6 +163,32 @@ ArrayInsert(Array * array, void *value, size_t index)
return 1; return 1;
} }
extern void *
ArraySet(Array * array, size_t index, void * value)
{
void *oldValue;
if (!value)
{
return ArrayDelete(array, index);
}
if (!array)
{
return NULL;
}
if (index >= array->size)
{
return NULL;
}
oldValue = array->entries[index];
array->entries[index] = value;
return oldValue;
}
size_t size_t
ArraySize(Array * array) ArraySize(Array * array)
{ {

View file

@ -40,7 +40,10 @@ extern void *
ArrayGet(Array *, size_t); ArrayGet(Array *, size_t);
extern int extern int
ArrayInsert(Array *, void *, size_t); ArrayInsert(Array *, size_t, void *);
extern void *
ArraySet(Array *, size_t, void *);
extern int extern int
ArrayAdd(Array *, void *); ArrayAdd(Array *, void *);