forked from lda/telodendria
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:
parent
b8d00bc8bf
commit
e6f3dfad18
2 changed files with 32 additions and 3 deletions
30
src/Array.c
30
src/Array.c
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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 *);
|
||||
|
|
Loading…
Reference in a new issue