diff --git a/demo.c b/demo.c index 286b195..9afaf39 100644 --- a/demo.c +++ b/demo.c @@ -8,12 +8,34 @@ int main() { listPush(another_list, CHAR, &(char){'x'}); listPush(another_list, INT, &(int){123}); listPush(another_list, FLOAT, &(float){3.14}); - listPush(another_list, LIST, another_list); - listPush(another_list, LIST, another_list); + printf("do not put the list into itself, it does weird stuff\n"); + // BIG NONO -> listPush(another_list, LIST, another_list); + + listRemoveAt(another_list, 1); + + list_t* l = listInit(); + + listPush(l, INT, &(int){5+2}); + listPush(l, INT, &(int){5*2}); + listPush(l, FLOAT, &(float){5/2}); + + listPush(another_list, LIST, l); listPrint(another_list); listCleanup(another_list); + // vector_t* v = vectorInit(INT); + // + // vectorPush(v, &(int){123}); + // vectorPush(v, &(int){456}); + // vectorPush(v, &(int){789}); + // + // vectorRemoveAt(v, 1); + // + // vectorPrint(v); + // + // vectorCleanup(v); + return 0; } diff --git a/include/list.h b/include/list.h index a02ef68..18a75f8 100644 --- a/include/list.h +++ b/include/list.h @@ -28,3 +28,5 @@ void listCleanup(list_t* l); // f void* listPop(list_t* l); // popping the last value of the list and returning it void listInsertAt(list_t* l, int index, dataType_e type, void* element); // inserts an element at a given index + +void listRemoveAt(list_t* l, int index); diff --git a/include/vector.h b/include/vector.h index 7ccb1b5..f0ec003 100644 --- a/include/vector.h +++ b/include/vector.h @@ -44,7 +44,8 @@ vectorToString(vector_t* v); // convert the vector into a regular string void* vectorGetAt(vector_t* v, int index); // get an element at the given index - +void +vectorRemoveAt(vector_t* v, int index); diff --git a/src/list.c b/src/list.c index 46b106c..eeea065 100644 --- a/src/list.c +++ b/src/list.c @@ -214,3 +214,12 @@ listInsertAt(list_t* l, int index, dataType_e type, void* element) { } } } + +void +listRemoveAt(list_t* l, int index) { + vectorRemoveAt(l->data, index); + l->size -= 1; + if (l->size == l->capacity) { + l->capacity /= 2; + } +} diff --git a/src/vector.c b/src/vector.c index a63e021..f9e8ae8 100644 --- a/src/vector.c +++ b/src/vector.c @@ -430,3 +430,52 @@ vectorInsertAt(vector_t* v, int index, void* element) { exit(EXIT_FAILURE); } } + +void +vectorRemoveAt(vector_t* v, int index) { + if (v->size == 0) { + fprintf(stderr, "cannot remove from empty vector\n"); + exit(EXIT_FAILURE); + } else if (index >= v->size || index < 0) { + fprintf(stderr, "index out of range\n"); + exit(EXIT_FAILURE); + } + + switch (v->type) { + case INT: + // move all elements to the right by one + for (int i = index; i < v->size; i++) { + *((int *)(v->data) + i) = *((int *)(v->data) + i+1); + } + break; + case FLOAT: + for (int i = index; i < v->size; i++) { + *((float *)(v->data) + i) = *((float *)(v->data) + i+1); + } + break; + case CHAR: + for (int i = index; i < v->size; i++) { + *((char *)(v->data) + i) = *((char *)(v->data) + i+1); + } + break; + case VECTOR: + for (int i = index; i < v->size; i++) { + *((vector_t*)(v->data) + i) = *((vector_t*)(v->data) + i+1); + } + break; + case LIST: + for (int i = index; i < v->size; i++) { + *((list_t*)(v->data) + i) = *((list_t*)(v->data) + i+1); + } + break; + default: + fprintf(stderr, "specify a valid vector type\n"); + exit(EXIT_FAILURE); + } + + v->size -= 1; + if (v->size < v->capacity/2) { + v->capacity /= 2; + v->data = realloc(v->data, v->elementSize * v->capacity); + } +}