2006-04-03 00:56:25 +04:00
|
|
|
/* extendible vectors. Icarus Sparry 2006. Public domain. */
|
|
|
|
#include <stdlib.h>
|
2006-04-03 15:05:40 +04:00
|
|
|
#include <stdio.h>
|
2006-04-05 20:26:15 +04:00
|
|
|
#include <string.h>
|
2006-04-03 00:56:25 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "cext.h"
|
|
|
|
|
|
|
|
void
|
2006-04-03 15:40:34 +04:00
|
|
|
cext_vattach(Vector *v, void *p)
|
2006-04-03 00:56:25 +04:00
|
|
|
{
|
|
|
|
++v->size;
|
|
|
|
if (!(v->data = realloc(v->data, v->size * sizeof(void *)))) {
|
2006-04-03 15:05:40 +04:00
|
|
|
fprintf(stderr, "%s\n", "Out of memory in cext_evector_attach\n");
|
|
|
|
exit(1);
|
2006-04-03 00:56:25 +04:00
|
|
|
}
|
2006-04-10 12:25:44 +04:00
|
|
|
v->data[v->size - 1] = p;
|
2006-04-03 00:56:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-04-03 15:40:34 +04:00
|
|
|
cext_vdetach(Vector *v, void *data)
|
2006-04-03 00:56:25 +04:00
|
|
|
{
|
2006-04-05 20:26:15 +04:00
|
|
|
unsigned int i;
|
|
|
|
for(i = 0; i < v->size; i++)
|
|
|
|
if (v->data[i] == data) {
|
|
|
|
memmove(v->data + i, v->data + i + 1,
|
2006-04-07 22:17:11 +04:00
|
|
|
(v->size - i - 1) * sizeof(void *));
|
2006-04-10 12:25:44 +04:00
|
|
|
v->data[--v->size] = nil;
|
|
|
|
break;
|
2006-04-03 00:56:25 +04:00
|
|
|
}
|
2006-04-10 12:25:44 +04:00
|
|
|
if(v->size == 0) {
|
2006-04-10 10:44:18 +04:00
|
|
|
free(v->data);
|
2006-04-10 12:25:44 +04:00
|
|
|
v->data = nil;
|
|
|
|
}
|
2006-04-03 00:56:25 +04:00
|
|
|
}
|