made work: multi-dimensional lists

This commit is contained in:
Odweta
2023-12-25 01:05:42 +01:00
parent 7233f8c8af
commit a52cdc0c7f
7 changed files with 135 additions and 59 deletions
+64 -12
View File
@@ -1,5 +1,6 @@
#include <odweta/list.h>
#include <odweta/vector.h>
#include <odweta/util.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -48,6 +49,11 @@ listPush(list_t* l, dataType_e type, void* element) {
vectorPush(newVec, element);
vectorPush((vector_t *)l->data, newVec);
break;
case LIST:
newVec = vectorInit(LIST);
vectorPush(newVec, element);
vectorPush((vector_t *)l->data, newVec);
break;
default:
fprintf(stderr, "specify a valid vector type\n");
exit(EXIT_FAILURE);
@@ -59,6 +65,41 @@ listPush(list_t* l, dataType_e type, void* element) {
}
}
void
_listPrintRecursive(list_t* l, int depth) {
fprintf(stdout, "[");
int i;
for (i = 0; i < l->size; i++) {
vector_t *ithVec = (vector_t *)vectorGetAt((vector_t *)l->data, i);
switch (ithVec->type) {
case INT:
printf("%d", *((int *)vectorGetAt(ithVec, 0)));
break;
case FLOAT:
printf("%f", *((float *)vectorGetAt(ithVec, 0)));
break;
case CHAR:
printf("'%c'", *((char *)vectorGetAt(ithVec, 0)));
break;
case VECTOR:
_printVectorRecursive(ithVec->data, 0);
break;
case LIST:
_listPrintRecursive(ithVec->data, 0);
break;
}
if (i < l->size - 1) {
fprintf(stdout, ", ");
}
}
fprintf(stdout, "]");
if (depth != 0) {
fprintf(stdout, "\n");
}
}
void
listPrint(list_t* l) {
fprintf(stdout, "[");
@@ -67,18 +108,21 @@ listPrint(list_t* l) {
vector_t *ithVec = (vector_t *)vectorGetAt((vector_t *)l->data, i);
switch (ithVec->type) {
case INT:
printf("%d", *((int *)vectorGetAt(ithVec, 0)));
break;
case FLOAT:
printf("%f", *((float *)vectorGetAt(ithVec, 0)));
break;
case CHAR:
printf("'%c'", *((char *)vectorGetAt(ithVec, 0)));
break;
case VECTOR:
_printVectorRecursive(ithVec->data, 0);
break;
case INT:
printf("%d", *((int *)vectorGetAt(ithVec, 0)));
break;
case FLOAT:
printf("%f", *((float *)vectorGetAt(ithVec, 0)));
break;
case CHAR:
printf("'%c'", *((char *)vectorGetAt(ithVec, 0)));
break;
case VECTOR:
_printVectorRecursive(ithVec->data, 0);
break;
case LIST:
_listPrintRecursive(ithVec->data, 0);
break;
}
if (i < l->size - 1) {
@@ -127,6 +171,9 @@ listInsertAt(list_t* l, int index, dataType_e type, void* element) {
case VECTOR:
listPush(l, VECTOR, element);
break;
case LIST:
listPush(l, LIST, element);
break;
}
} else {
vector_t* newVec;
@@ -151,6 +198,11 @@ listInsertAt(list_t* l, int index, dataType_e type, void* element) {
vectorPush(newVec, element);
vectorInsertAt((vector_t *)l->data, index, newVec);
break;
case LIST:
newVec = vectorInit(LIST);
vectorPush(newVec, element);
vectorInsertAt((vector_t *)l->data, index, newVec);
break;
default:
fprintf(stderr, "specify a valid vector type\n");
exit(EXIT_FAILURE);