2012-01-29 17:39:56 +00:00
|
|
|
#include "array.h"
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2013-04-16 20:10:03 +00:00
|
|
|
#include <assert.h>
|
2011-12-10 13:25:22 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-04-16 20:10:03 +00:00
|
|
|
void* naiveArrayInsert(void** array, size_t item_size, int item_count, int location)
|
|
|
|
{
|
|
|
|
assert(location >= 0);
|
|
|
|
assert(location <= item_count);
|
|
|
|
|
|
|
|
*array = realloc(*array, item_size * (item_count + 1));
|
|
|
|
if (location < item_count)
|
|
|
|
{
|
|
|
|
memmove(*array + item_size * (location + 1), *array + item_size * location, item_size * (item_count - location));
|
|
|
|
}
|
|
|
|
|
|
|
|
return *array + item_size * location;
|
|
|
|
}
|
|
|
|
|
2011-12-10 13:25:22 +00:00
|
|
|
void arrayCreate(Array* array, int item_size)
|
|
|
|
{
|
|
|
|
array->length = 0;
|
2012-04-29 15:14:37 +00:00
|
|
|
array->alloc_length = 1;
|
2011-12-10 13:25:22 +00:00
|
|
|
array->item_size = item_size;
|
|
|
|
array->dirty = 1;
|
2012-02-12 16:57:29 +00:00
|
|
|
array->data = malloc((size_t)item_size * array->alloc_length);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void arrayDelete(Array* array)
|
|
|
|
{
|
|
|
|
free(array->data);
|
|
|
|
array->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* arrayAppend(Array* array, void* item)
|
|
|
|
{
|
|
|
|
void* dest;
|
|
|
|
size_t item_size = (size_t)array->item_size;
|
|
|
|
|
|
|
|
if (array->length >= array->alloc_length)
|
|
|
|
{
|
2012-04-29 15:14:37 +00:00
|
|
|
array->alloc_length += 1;
|
2011-12-10 13:25:22 +00:00
|
|
|
array->data = realloc(array->data, item_size * array->alloc_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
dest = array->data + item_size * array->length;
|
|
|
|
memcpy(dest, item, item_size);
|
|
|
|
array->length++;
|
|
|
|
|
|
|
|
array->dirty = 1;
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
void arrayInsert(Array* array, void* item, int position)
|
|
|
|
{
|
|
|
|
size_t item_size;
|
|
|
|
void* dest;
|
|
|
|
|
|
|
|
if (position >= array->length)
|
|
|
|
{
|
|
|
|
arrayAppend(array, item);
|
|
|
|
}
|
|
|
|
else if (position >= 0)
|
|
|
|
{
|
|
|
|
item_size = (size_t)array->item_size;
|
|
|
|
|
|
|
|
if (array->length >= array->alloc_length)
|
|
|
|
{
|
|
|
|
array->alloc_length += 10;
|
|
|
|
array->data = realloc(array->data, item_size * array->alloc_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
dest = array->data + item_size * position;
|
|
|
|
memmove(dest + item_size, dest, array->length - position);
|
|
|
|
memcpy(array->data + item_size * position, item, item_size);
|
|
|
|
array->length++;
|
|
|
|
|
|
|
|
array->dirty = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void arrayReplace(Array* array, void* item, int position)
|
|
|
|
{
|
|
|
|
size_t item_size;
|
|
|
|
|
|
|
|
if (position >= 0 && position < array->length)
|
|
|
|
{
|
|
|
|
item_size = (size_t)array->item_size;
|
|
|
|
memcpy(array->data + item_size * position, item, item_size);
|
|
|
|
|
|
|
|
array->dirty = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void arrayLStrip(Array* array, int count)
|
|
|
|
{
|
|
|
|
size_t item_size;
|
|
|
|
|
|
|
|
if (count >= array->length)
|
|
|
|
{
|
|
|
|
arrayClear(array);
|
|
|
|
}
|
|
|
|
else if (count >= 0)
|
|
|
|
{
|
|
|
|
item_size = (size_t)array->item_size;
|
|
|
|
memmove(array->data, array->data + item_size * count, item_size * (array->length - count));
|
|
|
|
array->length -= count;
|
|
|
|
array->dirty = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void arrayClear(Array* array)
|
|
|
|
{
|
|
|
|
free(array->data);
|
|
|
|
array->length = 0;
|
2012-04-29 15:14:37 +00:00
|
|
|
array->alloc_length = 1;
|
|
|
|
array->data = malloc((size_t)array->item_size * array->alloc_length);
|
2011-12-10 13:25:22 +00:00
|
|
|
array->dirty = 1;
|
|
|
|
}
|