made vectors in lists work

This commit is contained in:
Odweta
2023-12-22 16:19:47 +01:00
parent 45732c4b22
commit b68e6c4d2b
3 changed files with 9 additions and 18 deletions
+1 -1
View File
@@ -41,6 +41,7 @@ I got bored with having to use complicated checking and stuff when working with
+ Printing a list
+ Popping the last value of a list
+ Cleanup of a list
+ Vectors inside of a list
#### TODO:
@@ -48,7 +49,6 @@ I got bored with having to use complicated checking and stuff when working with
+ Verify multi-dimensional lists working
+ Get/set at a given index
+ Remove at a given index
+ Verify vectors working inside of lists
## Installation
+4
View File
@@ -1,9 +1,13 @@
#include <odweta/list.h>
#include <odweta/vector.h>
#include <stdio.h>
int main(int argc, char **argv) {
list_t *l = listInit();
vector_t *to_be_pushed_vec = vectorInit(INT);
vectorPush(to_be_pushed_vec, &(int){1620});
listPush(l, VECTOR, (vector_t *)to_be_pushed_vec);
listPush(l, FLOAT, &(float){3.14});
listPush(l, CHAR, &(char){'x'});
listPush(l, INT, &(int){123});
+4 -17
View File
@@ -44,22 +44,9 @@ listPush(list_t* l, dataType_e type, void* element) {
vectorPush((vector_t *)l->data, newVec);
break;
case VECTOR:
// Instead of reallocating memory for vector_t pointers, allocate memory for the vector data
if (l->size == 0) {
// Allocate memory for the vector data for the first time
l->data = malloc(sizeof(vector_t) * l->capacity);
} else {
// Reallocate memory for the vector data
l->data = realloc((vector_t *)l->data, sizeof(vector_t) * l->capacity);
}
if (!l->data) {
perror("failed to allocate memory for vector data\n");
exit(EXIT_FAILURE);
}
// Copy the vector data from the element to the vector
memcpy((vector_t *)(l->data) + l->size, (vector_t *)element, sizeof(vector_t));
newVec = vectorInit(VECTOR);
vectorPush(newVec, element);
vectorPush((vector_t *)l->data, newVec);
break;
default:
perror("specify a valid vector type\n");
@@ -90,7 +77,7 @@ listPrint(list_t* l) {
printf("'%c'", *((char *)vectorGetAt(ithVec, 0)));
break;
case VECTOR:
_printVectorRecursive((vector_t *)l->data, 0);
_printVectorRecursive(ithVec->data, 0);
break;
}