2005-02-04 15:39:11 +00:00
|
|
|
/*
|
2006-10-24 04:22:40 +00:00
|
|
|
* $Id: array.c,v 1.11 2006-10-24 04:22:39 bacon Exp $
|
2005-02-04 15:39:11 +00:00
|
|
|
*/
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
#include <ase/lsp/lsp_i.h>
|
2005-02-04 15:39:11 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_array_t* ase_lsp_array_new (ase_size_t capacity)
|
2005-02-04 15:39:11 +00:00
|
|
|
{
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_lsp_array_t* array;
|
2005-02-04 15:39:11 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_assert (capacity > 0);
|
|
|
|
array = (ase_lsp_array_t*) ase_malloc (sizeof(ase_lsp_array_t));
|
|
|
|
if (array == ASE_NULL) return ASE_NULL;
|
2005-02-04 15:39:11 +00:00
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
array->buffer = (void**) ase_malloc (capacity + 1);
|
|
|
|
if (array->buffer == ASE_NULL) {
|
2005-02-04 15:39:11 +00:00
|
|
|
free (array);
|
2006-10-24 04:22:40 +00:00
|
|
|
return ASE_NULL;
|
2005-02-04 15:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
array->size = 0;
|
|
|
|
array->capacity = capacity;
|
2006-10-24 04:22:40 +00:00
|
|
|
array->buffer[0] = ASE_NULL;
|
2005-02-04 15:39:11 +00:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
void ase_lsp_array_free (ase_lsp_array_t* array)
|
2005-02-04 15:39:11 +00:00
|
|
|
{
|
|
|
|
while (array->size > 0)
|
|
|
|
free (array->buffer[--array->size]);
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_assert (array->size == 0);
|
2005-02-04 15:39:11 +00:00
|
|
|
|
|
|
|
free (array->buffer);
|
|
|
|
free (array);
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
int ase_lsp_array_add_item (ase_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-24 04:22:40 +00:00
|
|
|
if (new_buffer == ASE_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-24 04:22:40 +00:00
|
|
|
array->buffer[array->size] = ASE_NULL;
|
2005-02-04 15:39:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
int ase_lsp_array_insert (ase_lsp_array_t* array, ase_size_t index, void* value)
|
2005-02-04 15:39:11 +00:00
|
|
|
{
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_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-24 04:22:40 +00:00
|
|
|
if (new_buffer == ASE_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-24 04:22:40 +00:00
|
|
|
void ase_lsp_array_delete (ase_lsp_array_t* array, ase_size_t index)
|
2005-02-04 15:39:11 +00:00
|
|
|
{
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_assert (index < array->size);
|
2005-02-04 15:39:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
void ase_lsp_array_clear (ase_lsp_array_t* array)
|
2005-02-04 15:39:11 +00:00
|
|
|
{
|
|
|
|
while (array->size > 0)
|
|
|
|
free (array->buffer[--array->size]);
|
2006-10-24 04:22:40 +00:00
|
|
|
ase_assert (array->size == 0);
|
|
|
|
array->buffer[0] = ASE_NULL;
|
2005-02-04 15:39:11 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
void** ase_lsp_array_yield (ase_lsp_array_t* array, ase_size_t capacity)
|
2005-02-04 15:39:11 +00:00
|
|
|
{
|
|
|
|
void** old_buffer, ** new_buffer;
|
|
|
|
|
2006-10-24 04:22:40 +00:00
|
|
|
new_buffer = (void**) ase_malloc (capacity + 1);
|
|
|
|
if (new_buffer == ASE_NULL) return ASE_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-24 04:22:40 +00:00
|
|
|
array->buffer[0] = ASE_NULL;
|
2005-02-04 15:39:11 +00:00
|
|
|
|
|
|
|
return old_buffer;
|
|
|
|
}
|