1996-07-09 08:22:35 +02:00
|
|
|
/* ************************************************************************
|
|
|
|
*
|
1999-05-25 18:15:34 +02:00
|
|
|
* Varray.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* routines to provide a generic set of functions to handle variable sized
|
|
|
|
* arrays. originally by Jiang Wu
|
1996-07-09 08:22:35 +02:00
|
|
|
* ************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "Varray.h"
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
Varray *
|
1997-09-07 07:04:48 +02:00
|
|
|
NewVarray(size_t nobj, size_t size)
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
|
|
|
* NewVarray -- allocate a Varray to contain an array of val each of which
|
1997-09-07 07:04:48 +02:00
|
|
|
* is size valSize. Returns the Varray if successful,
|
|
|
|
* returns NULL otherwise.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Varray *result;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (nobj == 0)
|
|
|
|
nobj = VARRAY_INITIAL_SIZE;
|
|
|
|
result = (Varray *) malloc(sizeof(Varray));
|
|
|
|
result->val = (void *) calloc(nobj, size);
|
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
|
|
|
result->size = size;
|
|
|
|
result->nobj = 0;
|
|
|
|
result->maxObj = nobj;
|
|
|
|
return result;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
|
|
|
AppendVarray(Varray * array, void *value, CopyingFunct copy)
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
|
|
|
* AppendVarray -- append value to the end of array. This function
|
1997-09-07 07:04:48 +02:00
|
|
|
* returns the size of the array after the addition of
|
|
|
|
* the new element.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
copy(value, VARRAY_NTH(array->val, array->size, array->nobj));
|
|
|
|
array->nobj++;
|
|
|
|
if (array->nobj >= array->maxObj)
|
|
|
|
ENLARGE_VARRAY(array, array->maxObj / 2);
|
|
|
|
return array->nobj;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|