refactoring done, small bugfixes included, vectors and lists of non-primitive types are not working though
This commit is contained in:
+112
-221
@@ -1,152 +1,140 @@
|
||||
#include <odweta/list.h>
|
||||
#include <odweta/vector.h>
|
||||
#include <odweta/util.h>
|
||||
#include <odweta/string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define init_error_exit(l) fprintf(stderr, "could not init list\n"); free(l); exit(EXIT_FAILURE)
|
||||
|
||||
list_t*
|
||||
listInit() {
|
||||
list_t* l = malloc(sizeof(list_t));
|
||||
if (!l) {
|
||||
init_error_exit(l);
|
||||
list_t *list_new() {
|
||||
list_t *this = (list_t *)malloc(sizeof(list_t));
|
||||
if (this == NULL) {
|
||||
init_error_exit(this);
|
||||
}
|
||||
|
||||
l->data = (vector_t *)vectorInit(VECTOR);
|
||||
if (!l->data) {
|
||||
init_error_exit(l);
|
||||
this->data = (vector_t *)vector_new(VECTOR);
|
||||
if (this->data == NULL) {
|
||||
init_error_exit(this);
|
||||
}
|
||||
|
||||
l->size = 0;
|
||||
l->capacity = 1;
|
||||
this->size = 0;
|
||||
this->capacity = 1;
|
||||
|
||||
return l;
|
||||
return this;
|
||||
}
|
||||
|
||||
void
|
||||
listPush(list_t* l, dataType_e type, void* element) {
|
||||
vector_t* newVec;
|
||||
switch (type) {
|
||||
case INT:
|
||||
newVec = vectorInit(INT);
|
||||
vectorPush(newVec, element);
|
||||
vectorPush((vector_t *)l->data, newVec);
|
||||
break;
|
||||
case FLOAT:
|
||||
newVec = vectorInit(FLOAT);
|
||||
vectorPush(newVec, element);
|
||||
vectorPush((vector_t *)l->data, newVec);
|
||||
break;
|
||||
case CHAR:
|
||||
newVec = vectorInit(CHAR);
|
||||
vectorPush(newVec, element);
|
||||
vectorPush((vector_t *)l->data, newVec);
|
||||
break;
|
||||
case VECTOR:
|
||||
newVec = vectorInit(VECTOR);
|
||||
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);
|
||||
}
|
||||
void list_push(list_t *this, type_e type, void *element) {
|
||||
vector_t *new_vec = vector_new(type);
|
||||
|
||||
l->size += 1;
|
||||
if (l->size == l->capacity) {
|
||||
l->capacity *= 2;
|
||||
vector_push(new_vec, element);
|
||||
vector_push((vector_t *)this->data, new_vec);
|
||||
|
||||
this->size += 1;
|
||||
if (this->size == this->capacity) {
|
||||
this->capacity *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
_listPrintRecursive(list_t* l, int depth) {
|
||||
fprintf(stdout, "[");
|
||||
void _list_print_recursive(list_t *this, int depth) {
|
||||
printf("[");
|
||||
|
||||
int i;
|
||||
for (i = 0; i < l->size; i++) {
|
||||
vector_t *ithVec = (vector_t *)vectorGetAt((vector_t *)l->data, i);
|
||||
for (i = 0; i < this->size; i++) {
|
||||
vector_t *ith_vec = (vector_t *)vector_get_at((vector_t *)this->data, i);
|
||||
|
||||
switch (ithVec->type) {
|
||||
switch (ith_vec->type) {
|
||||
case INT:
|
||||
printf("%d", *((int *)vectorGetAt(ithVec, 0)));
|
||||
printf("%d", *((int *)vector_get_at(ith_vec, 0)));
|
||||
break;
|
||||
case FLOAT:
|
||||
printf("%f", *((float *)vectorGetAt(ithVec, 0)));
|
||||
printf("%f", *((float *)vector_get_at(ith_vec, 0)));
|
||||
break;
|
||||
case CHAR:
|
||||
printf("'%c'", *((char *)vectorGetAt(ithVec, 0)));
|
||||
printf("'%c'", *((char *)vector_get_at(ith_vec, 0)));
|
||||
break;
|
||||
case VECTOR:
|
||||
_printVectorRecursive(ithVec->data, 0);
|
||||
_vector_print_recursive(ith_vec->data, 0);
|
||||
break;
|
||||
case LIST:
|
||||
_listPrintRecursive(ithVec->data, 0);
|
||||
_list_print_recursive(ith_vec->data, 0);
|
||||
break;
|
||||
case BOOL:
|
||||
printf("%s", (*((bool *)vector_get_at(ith_vec, 0)) == false) ? "false" : "true");
|
||||
break;
|
||||
case STRING:
|
||||
string_print((string_t *)vector_get_at(ith_vec, 0));
|
||||
break;
|
||||
}
|
||||
|
||||
if (i < l->size - 1) {
|
||||
fprintf(stdout, ", ");
|
||||
if (i < this->size - 1) {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
fprintf(stdout, "]");
|
||||
|
||||
printf("]");
|
||||
|
||||
if (depth != 0) {
|
||||
fprintf(stdout, "\n");
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
listPrint(list_t* l) {
|
||||
fprintf(stdout, "[");
|
||||
int i;
|
||||
for (i = 0; i < l->size; i++) {
|
||||
vector_t *ithVec = (vector_t *)vectorGetAt((vector_t *)l->data, i);
|
||||
void list_show(list_t *this) {
|
||||
printf("[");
|
||||
|
||||
switch (ithVec->type) {
|
||||
int i;
|
||||
for (i = 0; i < this->size; i++) {
|
||||
vector_t *ith_vec = (vector_t *)vector_get_at((vector_t *)this->data, i);
|
||||
|
||||
switch (ith_vec->type) {
|
||||
case INT:
|
||||
printf("%d", *((int *)vectorGetAt(ithVec, 0)));
|
||||
printf("%d", *((int *)vector_get_at(ith_vec, 0)));
|
||||
break;
|
||||
case FLOAT:
|
||||
printf("%f", *((float *)vectorGetAt(ithVec, 0)));
|
||||
printf("%f", *((float *)vector_get_at(ith_vec, 0)));
|
||||
break;
|
||||
case CHAR:
|
||||
printf("'%c'", *((char *)vectorGetAt(ithVec, 0)));
|
||||
printf("'%c'", *((char *)vector_get_at(ith_vec, 0)));
|
||||
break;
|
||||
case VECTOR:
|
||||
_printVectorRecursive(ithVec->data, 0);
|
||||
_vector_print_recursive(ith_vec->data, 0);
|
||||
break;
|
||||
case LIST:
|
||||
_listPrintRecursive(ithVec->data, 0);
|
||||
_list_print_recursive(ith_vec->data, 0);
|
||||
break;
|
||||
case BOOL:
|
||||
printf("%s", (*((bool *)vector_get_at(ith_vec, 0)) == false) ? "false" : "true");
|
||||
break;
|
||||
case STRING:
|
||||
string_print((string_t *)vector_get_at(ith_vec, 0));
|
||||
break;
|
||||
}
|
||||
|
||||
if (i < l->size - 1) {
|
||||
fprintf(stdout, ", ");
|
||||
if (i < this->size - 1) {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
fprintf(stdout, "]\n");
|
||||
|
||||
printf("]\n");
|
||||
}
|
||||
|
||||
void
|
||||
listCleanup(list_t* l) {
|
||||
vectorCleanup((vector_t *)l->data);
|
||||
free(l);
|
||||
void list_cleanup(list_t *this) {
|
||||
vector_cleanup((vector_t *)this->data);
|
||||
free(this);
|
||||
}
|
||||
|
||||
void*
|
||||
listPop(list_t* l) {
|
||||
if (l->size != 0) {
|
||||
vector_t *first_ptr = (vector_t *)vectorPop((vector_t *)l->data);
|
||||
void *ptr = vectorPop(first_ptr);
|
||||
l->size -= 1;
|
||||
void *list_pop(list_t *this) {
|
||||
if (this->size != 0) {
|
||||
vector_t *first_ptr = (vector_t *)vector_pop((vector_t *)this->data);
|
||||
void *ptr = vector_pop(first_ptr);
|
||||
this->size -= 1;
|
||||
|
||||
if (l->size <= l->capacity / 2) l->capacity /= 2;
|
||||
if (l->capacity == 0) l->capacity = 1;
|
||||
if (this->size <= this->capacity / 2)
|
||||
this->capacity /= 2;
|
||||
|
||||
if (this->capacity == 0)
|
||||
this->capacity = 1;
|
||||
|
||||
return ptr;
|
||||
} else {
|
||||
@@ -155,150 +143,53 @@ listPop(list_t* l) {
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
listInsertAt(list_t* l, int index, dataType_e type, void* element) {
|
||||
if (l->size == 0) {
|
||||
switch (type) {
|
||||
case INT:
|
||||
listPush(l, INT, element);
|
||||
break;
|
||||
case FLOAT:
|
||||
listPush(l, FLOAT, element);
|
||||
break;
|
||||
case CHAR:
|
||||
listPush(l, CHAR, element);
|
||||
break;
|
||||
case VECTOR:
|
||||
listPush(l, VECTOR, element);
|
||||
break;
|
||||
case LIST:
|
||||
listPush(l, LIST, element);
|
||||
break;
|
||||
}
|
||||
void list_insert_at(list_t *this, int index, type_e type, void *element) {
|
||||
if (this->size == 0) {
|
||||
list_push(this, type, element);
|
||||
} else {
|
||||
vector_t* newVec;
|
||||
switch (type) {
|
||||
case INT:
|
||||
newVec = vectorInit(INT);
|
||||
vectorPush(newVec, element);
|
||||
vectorInsertAt((vector_t *)l->data, index, newVec);
|
||||
break;
|
||||
case FLOAT:
|
||||
newVec = vectorInit(FLOAT);
|
||||
vectorPush(newVec, element);
|
||||
vectorInsertAt((vector_t *)l->data, index, newVec);
|
||||
break;
|
||||
case CHAR:
|
||||
newVec = vectorInit(CHAR);
|
||||
vectorPush(newVec, element);
|
||||
vectorInsertAt((vector_t *)l->data, index, newVec);
|
||||
break;
|
||||
case VECTOR:
|
||||
newVec = vectorInit(VECTOR);
|
||||
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);
|
||||
}
|
||||
vector_t* new_vec = vector_new(type);
|
||||
|
||||
vector_push(new_vec, element);
|
||||
vector_insert_at((vector_t *)this->data, index, new_vec);
|
||||
|
||||
l->size += 1;
|
||||
if (l->size == l->capacity) {
|
||||
l->capacity *= 2;
|
||||
this->size += 1;
|
||||
if (this->size == this->capacity) {
|
||||
this->capacity *= 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
listRemoveAt(list_t* l, int index) {
|
||||
vectorRemoveAt(l->data, index);
|
||||
l->size -= 1;
|
||||
if (l->size == l->capacity) {
|
||||
l->capacity /= 2;
|
||||
void list_remove_at(list_t *this, int index) {
|
||||
vector_remove_at(this->data, index);
|
||||
this->size -= 1;
|
||||
if (this->size == this->capacity) {
|
||||
this->capacity /= 2;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
listSetAt(list_t* l, int index, dataType_e type, void* element) {
|
||||
if (l->size == 0) {
|
||||
listPush(l, type, element);
|
||||
} else if (index >= l->size || index < 0) {
|
||||
fprintf(stderr, "index out of range\nmust be between 0 and %d, is %d\n", l->size-1, index);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case INT:
|
||||
listRemoveAt(l, index);
|
||||
if (index == l->size) {
|
||||
l->data->size += 1;
|
||||
if (l->data->size == l->data->capacity) {
|
||||
l->data->capacity *= 2;
|
||||
}
|
||||
void list_set_at(list_t *this, int index, type_e type, void *element) {
|
||||
if (this->size == 0) {
|
||||
list_push(this, type, element);
|
||||
} else if (index < this->size && index >= 0) {
|
||||
list_remove_at(this, index);
|
||||
if (index == this->size) {
|
||||
this->data->size += 1;
|
||||
if (this->data->size == this->data->capacity) {
|
||||
this->data->capacity *= 2;
|
||||
}
|
||||
listInsertAt(l, index, INT, element);
|
||||
break;
|
||||
case FLOAT:
|
||||
listRemoveAt(l, index);
|
||||
if (index == l->size) {
|
||||
l->data->size += 1;
|
||||
if (l->data->size == l->data->capacity) {
|
||||
l->data->capacity *= 2;
|
||||
}
|
||||
}
|
||||
listInsertAt(l, index, FLOAT, element);
|
||||
break;
|
||||
case CHAR:
|
||||
listRemoveAt(l, index);
|
||||
if (index == l->size) {
|
||||
l->data->size += 1;
|
||||
if (l->data->size == l->data->capacity) {
|
||||
l->data->capacity *= 2;
|
||||
}
|
||||
}
|
||||
listInsertAt(l, index, CHAR, element);
|
||||
break;
|
||||
case VECTOR:
|
||||
listRemoveAt(l, index);
|
||||
if (index == l->size) {
|
||||
l->data->size += 1;
|
||||
if (l->data->size == l->data->capacity) {
|
||||
l->data->capacity *= 2;
|
||||
}
|
||||
}
|
||||
listInsertAt(l, index, VECTOR, element);
|
||||
break;
|
||||
case LIST:
|
||||
listRemoveAt(l, index);
|
||||
if (index == l->size) {
|
||||
l->data->size += 1;
|
||||
if (l->data->size == l->data->capacity) {
|
||||
l->data->capacity *= 2;
|
||||
}
|
||||
}
|
||||
listInsertAt(l, index, LIST, element);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "specify a valid vector type\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
list_insert_at(this, index, type, element);
|
||||
}
|
||||
}
|
||||
|
||||
void*
|
||||
listGetAt(list_t* l, int index) {
|
||||
if (l->size == 0) {
|
||||
fprintf(stderr, "list is empty\n");
|
||||
exit(EXIT_FAILURE);
|
||||
} else if (index >= l->size || index < 0) {
|
||||
fprintf(stderr, "index out of range\n");
|
||||
exit(EXIT_FAILURE);
|
||||
void *list_get_at(list_t *this, int index) {
|
||||
if (this->size == 0) {
|
||||
//fprintf(stderr, "list is empty\n");
|
||||
return NULL;
|
||||
} else if (index >= this->size || index < 0) {
|
||||
//fprintf(stderr, "index out of range\n");
|
||||
return NULL;
|
||||
} else {
|
||||
return vector_get_at((vector_t *)vector_get_at(this->data, index), 0);
|
||||
}
|
||||
|
||||
return vectorGetAt((vector_t*)vectorGetAt(l->data, index), 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user