qse/ase/lsp/array.c

104 lines
2.3 KiB
C
Raw Normal View History

2005-02-04 15:39:11 +00:00
/*
2005-09-18 11:34:35 +00:00
* $Id: array.c,v 1.7 2005-09-18 11:34:35 bacon Exp $
2005-02-04 15:39:11 +00:00
*/
2005-05-28 13:34:26 +00:00
#include <xp/lsp/array.h>
2005-04-24 07:48:16 +00:00
#include <xp/bas/memory.h>
#include <xp/bas/assert.h>
2005-02-04 15:39:11 +00:00
2005-09-18 11:34:35 +00:00
xp_lsp_array_t* xp_lsp_array_new (xp_size_t capacity)
2005-02-04 15:39:11 +00:00
{
2005-09-18 11:34:35 +00:00
xp_lsp_array_t* array;
2005-02-04 15:39:11 +00:00
2005-02-04 16:00:37 +00:00
xp_assert (capacity > 0);
2005-09-18 11:34:35 +00:00
array = (xp_lsp_array_t*)malloc (sizeof(xp_lsp_array_t));
2005-02-04 15:39:11 +00:00
if (array == XP_NULL) return XP_NULL;
array->buffer = (void**)malloc (capacity + 1);
if (array->buffer == XP_NULL) {
free (array);
return XP_NULL;
}
array->size = 0;
array->capacity = capacity;
array->buffer[0] = XP_NULL;
return array;
}
2005-09-18 11:34:35 +00:00
void xp_lsp_array_free (xp_lsp_array_t* array)
2005-02-04 15:39:11 +00:00
{
while (array->size > 0)
free (array->buffer[--array->size]);
2005-02-04 16:00:37 +00:00
xp_assert (array->size == 0);
2005-02-04 15:39:11 +00:00
free (array->buffer);
free (array);
}
2005-09-18 11:34:35 +00:00
int xp_lsp_array_add_item (xp_lsp_array_t* array, void* item)
2005-02-04 15:39:11 +00:00
{
if (array->size >= array->capacity) {
void* new_buffer = (void**)realloc (
array->buffer, array->capacity * 2 + 1);
if (new_buffer == XP_NULL) return -1;
array->buffer = new_buffer;
array->capacity = array->capacity * 2;
}
array->buffer[array->size++] = item;
array->buffer[array->size] = XP_NULL;
return 0;
}
2005-09-18 11:34:35 +00:00
int xp_lsp_array_insert (xp_lsp_array_t* array, xp_size_t index, void* value)
2005-02-04 15:39:11 +00:00
{
xp_size_t i;
if (index >= array->capacity) {
void* new_buffer = (void**)realloc (
array->buffer, array->capacity * 2 + 1);
if (new_buffer == XP_NULL) return -1;
array->buffer = new_buffer;
array->capacity = array->capacity * 2;
}
for (i = array->size; i > index; i--) {
array->buffer[i] = array->buffer[i - 1];
}
array->buffer[index] = value;
array->size = (index > array->size)? index + 1: array->size + 1;
return 0;
}
2005-09-18 11:34:35 +00:00
void xp_lsp_array_delete (xp_lsp_array_t* array, xp_size_t index)
2005-02-04 15:39:11 +00:00
{
2005-02-04 16:00:37 +00:00
xp_assert (index < array->size);
2005-02-04 15:39:11 +00:00
}
2005-09-18 11:34:35 +00:00
void xp_lsp_array_clear (xp_lsp_array_t* array)
2005-02-04 15:39:11 +00:00
{
while (array->size > 0)
free (array->buffer[--array->size]);
2005-02-04 16:00:37 +00:00
xp_assert (array->size == 0);
2005-02-04 15:39:11 +00:00
array->buffer[0] = XP_NULL;
}
2005-09-18 11:34:35 +00:00
void** xp_lsp_array_yield (xp_lsp_array_t* array, xp_size_t capacity)
2005-02-04 15:39:11 +00:00
{
void** old_buffer, ** new_buffer;
new_buffer = (void**)malloc(capacity + 1);
if (new_buffer == XP_NULL) return XP_NULL;
old_buffer = array->buffer;
array->buffer = new_buffer;
array->size = 0;
array->capacity = capacity;
array->buffer[0] = XP_NULL;
return old_buffer;
}