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 + Printing a list
+ Popping the last value of a list + Popping the last value of a list
+ Cleanup of a list + Cleanup of a list
+ Vectors inside of a list
#### TODO: #### TODO:
@@ -48,7 +49,6 @@ I got bored with having to use complicated checking and stuff when working with
+ Verify multi-dimensional lists working + Verify multi-dimensional lists working
+ Get/set at a given index + Get/set at a given index
+ Remove at a given index + Remove at a given index
+ Verify vectors working inside of lists
## Installation ## Installation
+4
View File
@@ -1,9 +1,13 @@
#include <odweta/list.h> #include <odweta/list.h>
#include <odweta/vector.h>
#include <stdio.h> #include <stdio.h>
int main(int argc, char **argv) { int main(int argc, char **argv) {
list_t *l = listInit(); 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, FLOAT, &(float){3.14});
listPush(l, CHAR, &(char){'x'}); listPush(l, CHAR, &(char){'x'});
listPush(l, INT, &(int){123}); 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); vectorPush((vector_t *)l->data, newVec);
break; break;
case VECTOR: case VECTOR:
// Instead of reallocating memory for vector_t pointers, allocate memory for the vector data newVec = vectorInit(VECTOR);
if (l->size == 0) { vectorPush(newVec, element);
// Allocate memory for the vector data for the first time vectorPush((vector_t *)l->data, newVec);
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));
break; break;
default: default:
perror("specify a valid vector type\n"); perror("specify a valid vector type\n");
@@ -90,7 +77,7 @@ listPrint(list_t* l) {
printf("'%c'", *((char *)vectorGetAt(ithVec, 0))); printf("'%c'", *((char *)vectorGetAt(ithVec, 0)));
break; break;
case VECTOR: case VECTOR:
_printVectorRecursive((vector_t *)l->data, 0); _printVectorRecursive(ithVec->data, 0);
break; break;
} }