wmii/libcext/vector.c

36 lines
693 B
C
Raw Normal View History

2006-04-03 00:56:25 +04:00
/* extendible vectors. Icarus Sparry 2006. Public domain. */
#include <stdlib.h>
#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
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 *)))) {
fprintf(stderr, "%s\n", "Out of memory in cext_evector_attach\n");
exit(1);
2006-04-03 00:56:25 +04:00
}
v->data[v->size - 1] = p;
2006-04-03 00:56:25 +04:00
}
void
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,
(v->size - i - 1) * sizeof(void *));
v->data[--v->size] = nil;
break;
2006-04-03 00:56:25 +04:00
}
if(v->size == 0) {
free(v->data);
v->data = nil;
}
2006-04-03 00:56:25 +04:00
}