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 ArrayInsert(array, value, array->size);
return ArrayInsert(array, array->size, value);
}
Array *
@ -123,7 +123,7 @@ ArrayGet(Array * array, size_t index)
extern int
ArrayInsert(Array * array, void *value, size_t index)
ArrayInsert(Array * array, size_t index, void *value)
{
size_t i;
@ -163,6 +163,32 @@ ArrayInsert(Array * array, void *value, size_t index)
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
ArraySize(Array * array)
{

View file

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