fixed some errors

This commit is contained in:
Odweta
2024-02-06 22:04:47 +01:00
parent e40b61a21b
commit f9e820efbc
10 changed files with 355 additions and 355 deletions
+61 -61
View File
@@ -9,40 +9,40 @@
#define init_error_exit(l) fprintf(stderr, "could not init list\n"); free(l); exit(EXIT_FAILURE)
list_t *list_new() {
list_t *this = (list_t *)malloc(sizeof(list_t));
if (this == NULL) {
init_error_exit(this);
list_t *self = (list_t *)malloc(sizeof(list_t));
if (self == NULL) {
init_error_exit(self);
}
this->data = (vector_t *)vector_new(VECTOR);
if (this->data == NULL) {
init_error_exit(this);
self->data = (vector_t *)vector_new(VECTOR);
if (self->data == NULL) {
init_error_exit(self);
}
this->size = 0;
this->capacity = 1;
self->size = 0;
self->capacity = 1;
return this;
return self;
}
void list_push(list_t *this, type_e type, void *element) {
void list_push(list_t *self, type_e type, void *element) {
vector_t *new_vec = vector_new(type);
vector_push(new_vec, element);
vector_push((vector_t *)this->data, new_vec);
vector_push((vector_t *)self->data, new_vec);
this->size += 1;
if (this->size == this->capacity) {
this->capacity *= 2;
self->size += 1;
if (self->size == self->capacity) {
self->capacity *= 2;
}
}
void _list_print_recursive(list_t *this, int depth) {
void _list_print_recursive(list_t *self, int depth) {
printf("[");
int i;
for (i = 0; i < this->size; i++) {
vector_t *ith_vec = (vector_t *)vector_get_at((vector_t *)this->data, i);
for (i = 0; i < self->size; i++) {
vector_t *ith_vec = (vector_t *)vector_get_at((vector_t *)self->data, i);
switch (ith_vec->type) {
case INT:
@@ -61,14 +61,14 @@ void _list_print_recursive(list_t *this, int depth) {
_list_print_recursive(ith_vec->data, 0);
break;
case BOOL:
printf("%s", (*((bool *)vector_get_at(ith_vec, 0)) == false) ? "false" : "true");
printf("%s", (*((bool_t *)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 < this->size - 1) {
if (i < self->size - 1) {
printf(", ");
}
}
@@ -80,12 +80,12 @@ void _list_print_recursive(list_t *this, int depth) {
}
}
void list_show(list_t *this) {
void list_show(list_t *self) {
printf("[");
int i;
for (i = 0; i < this->size; i++) {
vector_t *ith_vec = (vector_t *)vector_get_at((vector_t *)this->data, i);
for (i = 0; i < self->size; i++) {
vector_t *ith_vec = (vector_t *)vector_get_at((vector_t *)self->data, i);
switch (ith_vec->type) {
case INT:
@@ -104,14 +104,14 @@ void list_show(list_t *this) {
_list_print_recursive(ith_vec->data, 0);
break;
case BOOL:
printf("%s", (*((bool *)vector_get_at(ith_vec, 0)) == false) ? "false" : "true");
printf("%s", (*((bool_t *)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 < this->size - 1) {
if (i < self->size - 1) {
printf(", ");
}
}
@@ -119,22 +119,22 @@ void list_show(list_t *this) {
printf("]\n");
}
void list_cleanup(list_t *this) {
vector_cleanup((vector_t *)this->data);
free(this);
void list_cleanup(list_t *self) {
vector_cleanup((vector_t *)self->data);
free(self);
}
void *list_pop(list_t *this) {
if (this->size != 0) {
vector_t *first_ptr = (vector_t *)vector_pop((vector_t *)this->data);
void *list_pop(list_t *self) {
if (self->size != 0) {
vector_t *first_ptr = (vector_t *)vector_pop((vector_t *)self->data);
void *ptr = vector_pop(first_ptr);
this->size -= 1;
self->size -= 1;
if (this->size <= this->capacity / 2)
this->capacity /= 2;
if (self->size <= self->capacity / 2)
self->capacity /= 2;
if (this->capacity == 0)
this->capacity = 1;
if (self->capacity == 0)
self->capacity = 1;
return ptr;
} else {
@@ -143,53 +143,53 @@ void *list_pop(list_t *this) {
}
}
void list_insert_at(list_t *this, int index, type_e type, void *element) {
if (this->size == 0) {
list_push(this, type, element);
void list_insert_at(list_t *self, int index, type_e type, void *element) {
if (self->size == 0) {
list_push(self, type, element);
} else {
vector_t* new_vec = vector_new(type);
vector_push(new_vec, element);
vector_insert_at((vector_t *)this->data, index, new_vec);
vector_insert_at((vector_t *)self->data, index, new_vec);
this->size += 1;
if (this->size == this->capacity) {
this->capacity *= 2;
self->size += 1;
if (self->size == self->capacity) {
self->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 list_remove_at(list_t *self, int index) {
vector_remove_at(self->data, index);
self->size -= 1;
if (self->size == self->capacity) {
self->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;
void list_set_at(list_t *self, int index, type_e type, void *element) {
if (self->size == 0) {
list_push(self, type, element);
} else if (index < self->size && index >= 0) {
list_remove_at(self, index);
if (index == self->size) {
self->data->size += 1;
if (self->data->size == self->data->capacity) {
self->data->capacity *= 2;
}
}
list_insert_at(this, index, type, element);
list_insert_at(self, index, type, element);
}
}
void *list_get_at(list_t *this, int index) {
if (this->size == 0) {
void *list_get_at(list_t *self, int index) {
if (self->size == 0) {
//fprintf(stderr, "list is empty\n");
return NULL;
} else if (index >= this->size || index < 0) {
} else if (index >= self->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 vector_get_at((vector_t *)vector_get_at(self->data, index), 0);
}
}