made popping with lists work. yay! (also new demo to show the thing)

This commit is contained in:
Odweta
2023-12-22 14:27:07 +01:00
parent ea527405e5
commit 45732c4b22
5 changed files with 47 additions and 14 deletions
+12
View File
@@ -106,3 +106,15 @@ listCleanup(list_t* l) {
vectorCleanup((vector_t *)l->data);
free(l);
}
void*
listPop(list_t* l) {
vector_t *first_ptr = (vector_t *)vectorPop((vector_t *)l->data);
void *ptr = vectorPop(first_ptr);
l->size -= 1;
if (l->size <= l->capacity / 2) l->capacity /= 2;
if (l->capacity == 0) l->capacity = 1;
return ptr;
}
+6 -9
View File
@@ -216,30 +216,27 @@ vectorPop(vector_t* v) {
int *iretval = (int *)(v->data) + v->size-1;
v->size -= 1;
if (v->size <= v->capacity / 2) v->capacity /= 2;
v->data = realloc(v->data, v->elementSize * v->capacity);
if (v->capacity == 0) v->capacity = 1;
return iretval;
if (v->capacity == 0) v->capacity = 1;
return iretval;
case FLOAT:
float *fretval = (float *)(v->data) + v->size-1;
v->size -= 1;
if (v->size <= v->capacity / 2) v->capacity /= 2;
if (v->capacity == 0) v->capacity = 1;
v->data = realloc(v->data, v->elementSize * v->capacity);
return fretval;
return fretval;
case CHAR:
char *cretval = (char *)(v->data) + v->size-1;
v->size -= 1;
if (v->size <= v->capacity / 2) v->capacity /= 2;
if (v->capacity == 0) v->capacity = 1;
v->data = realloc(v->data, v->elementSize * v->capacity);
return cretval;
return cretval;
case VECTOR:
vector_t *vretval = (vector_t *)(v->data) + v->size-1;
v->size -= 1;
if (v->size <= v->capacity / 2) v->capacity /= 2;
if (v->capacity == 0) v->capacity = 1;
v->data = realloc(v->data, v->elementSize * v->capacity);
return vretval;
return vretval;
default:
perror("specify a valid vector type\n");
exit(EXIT_FAILURE);