2004-09-15 14:54:34 +08:00
|
|
|
/*
|
|
|
|
* collectn.c - implements variable length pointer arrays [collections].
|
2002-05-01 04:51:32 +08:00
|
|
|
*
|
|
|
|
* This file is public domain.
|
|
|
|
*/
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
2002-05-01 04:51:32 +08:00
|
|
|
#include <stdlib.h>
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "collectn.h"
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
void collection_init(Collection * c)
|
|
|
|
{
|
2004-09-15 14:54:34 +08:00
|
|
|
int i;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2004-09-15 14:54:34 +08:00
|
|
|
for (i = 0; i < 32; i++)
|
2005-01-16 06:15:51 +08:00
|
|
|
c->p[i] = NULL;
|
2004-09-15 14:54:34 +08:00
|
|
|
c->next = NULL;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2004-09-15 14:54:34 +08:00
|
|
|
void **colln(Collection * c, int index)
|
2002-05-01 04:51:32 +08:00
|
|
|
{
|
2004-09-15 14:54:34 +08:00
|
|
|
while (index >= 32) {
|
2005-01-16 06:15:51 +08:00
|
|
|
index -= 32;
|
|
|
|
if (c->next == NULL) {
|
|
|
|
c->next = malloc(sizeof(Collection));
|
|
|
|
collection_init(c->next);
|
|
|
|
}
|
|
|
|
c = c->next;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2004-09-15 14:54:34 +08:00
|
|
|
return &(c->p[index]);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2004-09-15 14:54:34 +08:00
|
|
|
void collection_reset(Collection * c)
|
2002-05-01 04:51:32 +08:00
|
|
|
{
|
2004-09-15 14:54:34 +08:00
|
|
|
int i;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2004-09-15 14:54:34 +08:00
|
|
|
if (c->next) {
|
2005-01-16 06:15:51 +08:00
|
|
|
collection_reset(c->next);
|
|
|
|
free(c->next);
|
2004-09-15 14:54:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c->next = NULL;
|
|
|
|
for (i = 0; i < 32; i++)
|
2005-01-16 06:15:51 +08:00
|
|
|
c->p[i] = NULL;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|