qse/ase/lsp/array.c

102 lines
2.3 KiB
C
Raw Normal View History

2005-02-04 15:39:11 +00:00
/*
2006-10-23 14:57:44 +00:00
* $Id: array.c,v 1.10 2006-10-23 14:49:16 bacon Exp $
2005-02-04 15:39:11 +00:00
*/
2006-10-23 14:57:44 +00:00
#include <sse/lsp/lsp_i.h>
2005-02-04 15:39:11 +00:00
2006-10-22 13:10:46 +00:00
sse_lsp_array_t* sse_lsp_array_new (sse_size_t capacity)
2005-02-04 15:39:11 +00:00
{
2006-10-22 13:10:46 +00:00
sse_lsp_array_t* array;
2005-02-04 15:39:11 +00:00
2006-10-22 13:10:46 +00:00
sse_assert (capacity > 0);
2006-10-23 10:57:59 +00:00
array = (sse_lsp_array_t*) sse_malloc (sizeof(sse_lsp_array_t));
2006-10-22 13:10:46 +00:00
if (array == SSE_NULL) return SSE_NULL;
2005-02-04 15:39:11 +00:00
2006-10-23 10:57:59 +00:00
array->buffer = (void**) sse_malloc (capacity + 1);
2006-10-22 13:10:46 +00:00
if (array->buffer == SSE_NULL) {
2005-02-04 15:39:11 +00:00
free (array);
2006-10-22 13:10:46 +00:00
return SSE_NULL;
2005-02-04 15:39:11 +00:00
}
array->size = 0;
array->capacity = capacity;
2006-10-22 13:10:46 +00:00
array->buffer[0] = SSE_NULL;
2005-02-04 15:39:11 +00:00
return array;
}
2006-10-22 13:10:46 +00:00
void sse_lsp_array_free (sse_lsp_array_t* array)
2005-02-04 15:39:11 +00:00
{
while (array->size > 0)
free (array->buffer[--array->size]);
2006-10-22 13:10:46 +00:00
sse_assert (array->size == 0);
2005-02-04 15:39:11 +00:00
free (array->buffer);
free (array);
}
2006-10-22 13:10:46 +00:00
int sse_lsp_array_add_item (sse_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);
2006-10-22 13:10:46 +00:00
if (new_buffer == SSE_NULL) return -1;
2005-02-04 15:39:11 +00:00
array->buffer = new_buffer;
array->capacity = array->capacity * 2;
}
array->buffer[array->size++] = item;
2006-10-22 13:10:46 +00:00
array->buffer[array->size] = SSE_NULL;
2005-02-04 15:39:11 +00:00
return 0;
}
2006-10-22 13:10:46 +00:00
int sse_lsp_array_insert (sse_lsp_array_t* array, sse_size_t index, void* value)
2005-02-04 15:39:11 +00:00
{
2006-10-22 13:10:46 +00:00
sse_size_t i;
2005-02-04 15:39:11 +00:00
if (index >= array->capacity) {
void* new_buffer = (void**)realloc (
array->buffer, array->capacity * 2 + 1);
2006-10-22 13:10:46 +00:00
if (new_buffer == SSE_NULL) return -1;
2005-02-04 15:39:11 +00:00
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;
}
2006-10-22 13:10:46 +00:00
void sse_lsp_array_delete (sse_lsp_array_t* array, sse_size_t index)
2005-02-04 15:39:11 +00:00
{
2006-10-22 13:10:46 +00:00
sse_assert (index < array->size);
2005-02-04 15:39:11 +00:00
}
2006-10-22 13:10:46 +00:00
void sse_lsp_array_clear (sse_lsp_array_t* array)
2005-02-04 15:39:11 +00:00
{
while (array->size > 0)
free (array->buffer[--array->size]);
2006-10-22 13:10:46 +00:00
sse_assert (array->size == 0);
array->buffer[0] = SSE_NULL;
2005-02-04 15:39:11 +00:00
}
2006-10-22 13:10:46 +00:00
void** sse_lsp_array_yield (sse_lsp_array_t* array, sse_size_t capacity)
2005-02-04 15:39:11 +00:00
{
void** old_buffer, ** new_buffer;
2006-10-23 10:57:59 +00:00
new_buffer = (void**) sse_malloc (capacity + 1);
2006-10-22 13:10:46 +00:00
if (new_buffer == SSE_NULL) return SSE_NULL;
2005-02-04 15:39:11 +00:00
old_buffer = array->buffer;
array->buffer = new_buffer;
array->size = 0;
array->capacity = capacity;
2006-10-22 13:10:46 +00:00
array->buffer[0] = SSE_NULL;
2005-02-04 15:39:11 +00:00
return old_buffer;
}