diff --git a/include/file.h b/include/file.h index f345478..4c6e206 100644 --- a/include/file.h +++ b/include/file.h @@ -13,28 +13,28 @@ typedef struct { file_t *file_new(char *path); // initialization function -void file_cleanup(file_t *this); // just to be memory safe... +void file_cleanup(file_t *self); // just to be memory safe... -void file_print(file_t *this); // prints the file as a string +void file_print(file_t *self); // prints the file as a string -char *file_to_string(file_t *this); // returns a string with the file contents +char *file_to_string(file_t *self); // returns a string with the file contents -void file_writeln(file_t *this, char *payload); // appends a given string to the file on a newline +void file_writeln(file_t *self, char *payload); // appends a given string to the file on a newline -void file_write(file_t *this, char *payload); // appends a given string to the file (not on a newline!) +void file_write(file_t *self, char *payload); // appends a given string to the file (not on a newline!) void file_copy(file_t* src, char *dst); // copy file src into file dst -void file_delete(file_t *this); // deletes a file +void file_delete(file_t *self); // deletes a file void file_move(file_t* src, char *dst); // moves/renames a file -bool file_exists(file_t *this); // 1 = yes, 0 = no +bool_t file_exists(file_t *self); // TRUE = yes, FALSE = no void file_create(char *path); // create an empty file with the given name -void file_number_lines(file_t *this); // prepend line numbers in front of each line in a file +void file_number_lines(file_t *self); // prepend line numbers in front of each line in a file -int file_count_words(file_t *this); // counts the number of words in a file +int file_count_words(file_t *self); // counts the number of words in a file #endif // FILE_H diff --git a/include/list.h b/include/list.h index 82eaa71..720ce66 100644 --- a/include/list.h +++ b/include/list.h @@ -2,12 +2,12 @@ #define LIST_H /* - * What is this? + * What is self? * - * A list (as I interpret it in this library) + * A list (as I interpret it in self library) * is a `vector_t` of other `vector_t`s, where * every inner `vector_t` can be of any type -> - * this means a Python-like list + * self means a Python-like list */ #include @@ -21,21 +21,21 @@ typedef struct { list_t *list_new(); // returns a new list -void list_push(list_t *this, type_e type, void *element); // appends an element at the end of a list +void list_push(list_t *self, type_e type, void *element); // appends an element at the end of a list -void list_show(list_t *this); // prints a list -void _list_print_recursive(list_t *this, int depth); +void list_show(list_t *self); // prints a list +void _list_print_recursive(list_t *self, int depth); -void list_cleanup(list_t *this); // freeing pointers +void list_cleanup(list_t *self); // freeing pointers -void *list_pop(list_t *this); // popping the last value of the list and returning it +void *list_pop(list_t *self); // popping the last value of the list and returning it -void list_insert_at(list_t *this, int index, type_e type, void *element); // inserts an element at a given index +void list_insert_at(list_t *self, int index, type_e type, void *element); // inserts an element at a given index -void list_remove_at(list_t *this, int index); +void list_remove_at(list_t *self, int index); -void list_set_at(list_t *this, int index, type_e type, void *element); +void list_set_at(list_t *self, int index, type_e type, void *element); -void *list_get_at(list_t *this, int index); +void *list_get_at(list_t *self, int index); #endif // LIST_H \ No newline at end of file diff --git a/include/string.h b/include/string.h index f782acc..63b8d4a 100644 --- a/include/string.h +++ b/include/string.h @@ -12,16 +12,16 @@ string_t *string_new(); // create a new e string_t *string_from(char *chars); // create a new string from a given char_sequence -bool string_push_chars(string_t *this, char *chars); // append a char_sequence to a string -bool string_push(string_t *this, string_t *other); // append a string to a string +bool_t string_push_chars(string_t *self, char *chars); // append a char_sequence to a string +bool_t string_push(string_t *self, string_t *other); // append a string to a string compare_e string_compare(string_t *left, string_t* right); // compare two strings -void string_print(string_t *this); // print a string -void string_println(string_t *this); // print a string with a \n +void string_print(string_t *self); // print a string +void string_println(string_t *self); // print a string with a \n -bool string_is_empty(string_t *this); // return true when s->content == "" +bool_t string_is_empty(string_t *self); // return true when s->content == "" -void string_cleanup(string_t *this); // memory cleanup +void string_cleanup(string_t *self); // memory cleanup #endif \ No newline at end of file diff --git a/include/util.h b/include/util.h index b81deec..1555350 100644 --- a/include/util.h +++ b/include/util.h @@ -3,7 +3,7 @@ #undef bool -typedef enum { false, true } bool; +typedef enum { FALSE, TRUE } bool_t; typedef enum { LESS, diff --git a/include/vector.h b/include/vector.h index 0835d72..d3ab42e 100644 --- a/include/vector.h +++ b/include/vector.h @@ -13,30 +13,30 @@ typedef struct { vector_t *vector_new(type_e type); // size gets determined by the dataType and the capacity starts at 1 and x1.5 every time the vector gets filled up -void vector_reset(vector_t *this); // sets the vector to its original state +void vector_reset(vector_t *self); // sets the vector to its original state -void *vector_pop(vector_t *this); // remove one value from the end of the vector and return it +void *vector_pop(vector_t *self); // remove one value from the end of the vector and return it -void vector_push(vector_t *this, void *element); // push one value at the end of the vector +void vector_push(vector_t *self, void *element); // push one value at the end of the vector -void vector_cleanup(vector_t *this); // freeing pointers +void vector_cleanup(vector_t *self); // freeing pointers -void vector_print(vector_t *this); // shows the vector +void vector_print(vector_t *self); // shows the vector -void vector_print_as_string(vector_t *this); // show the vector as a string if the v->type is CHAR +void vector_print_as_string(vector_t *self); // show the vector as a string if the v->type is CHAR -void vector_insert_at(vector_t *this, int index, void *element); // inserts an element 'element' at index 'index' +void vector_insert_at(vector_t *self, int index, void *element); // inserts an element 'element' at index 'index' -void vector_set_at(vector_t *this, int index, void *element); // set an element's value at a given index +void vector_set_at(vector_t *self, int index, void *element); // set an element's value at a given index -char *vector_to_string(vector_t *this); // convert the vector into a regular string +char *vector_to_string(vector_t *self); // convert the vector into a regular string -void *vector_get_at(vector_t *this, int index); // get an element at the given index +void *vector_get_at(vector_t *self, int index); // get an element at the given index -void vector_remove_at(vector_t *this, int index); +void vector_remove_at(vector_t *self, int index); -void _vector_print_recursive(vector_t *this, int depth); +void _vector_print_recursive(vector_t *self, int depth); #endif diff --git a/src/file.c b/src/file.c index 58be7f9..4490334 100644 --- a/src/file.c +++ b/src/file.c @@ -5,11 +5,11 @@ #include #include -int file_count_lines(file_t *this) { +int file_count_lines(file_t *self) { char ch; int lines = 0; - FILE *fd = fopen(this->path, "r"); + FILE *fd = fopen(self->path, "r"); if (fd == NULL) { return -1; } @@ -25,18 +25,18 @@ int file_count_lines(file_t *this) { return lines; } -void _file_load_lines(file_t *this) { +void _file_load_lines(file_t *self) { char ch; - FILE *fd = fopen(this->path, "r"); + FILE *fd = fopen(self->path, "r"); if (fd == NULL) { fclose(fd); - file_cleanup(this); + file_cleanup(self); return; } char prev; - for (int i = 0; i < this->length; i++) { + for (int i = 0; i < self->length; i++) { vector_t *line = vector_new(CHAR); // Create a new vector for each line while ((ch = fgetc(fd)) != '\n' && ch != EOF) { @@ -49,28 +49,28 @@ void _file_load_lines(file_t *this) { vector_push(line, empty); } - vector_push(this->lines, line); + vector_push(self->lines, line); prev = ch; - // Don't free the line vector here, as it's now owned by the this->lines vector + // Don't free the line vector here, as it's now owned by the self->lines vector } if (prev != '\n') { - this->not_newline_terminated = 1; // \n is NOT at the end of a file + self->not_newline_terminated = 1; // \n is NOT at the end of a file } else { - this->not_newline_terminated = 0; // \n IS at the end of a file + self->not_newline_terminated = 0; // \n IS at the end of a file } fclose(fd); } -char *file_to_string(file_t *this) { - return vector_to_string(this->lines); +char *file_to_string(file_t *self) { + return vector_to_string(self->lines); } -bool file_exists(file_t *this) { - FILE *fd = fopen(this->path, "r"); +bool_t file_exists(file_t *self) { + FILE *fd = fopen(self->path, "r"); - bool retval = (fd == NULL) ? false : true; + bool_t retval = (fd == NULL) ? FALSE : TRUE; fclose(fd); @@ -82,50 +82,50 @@ void file_create(char *path) { } file_t *file_new(char *path) { - file_t *this = (file_t *)malloc(sizeof(file_t)); - if (this == NULL) { + file_t *self = (file_t *)malloc(sizeof(file_t)); + if (self == NULL) { return NULL; } - this->path = path; + self->path = path; - if (file_exists(this) == false) { - // if file 'this' does not exist - file_create(this->path); - return file_new(this->path); + if (file_exists(self) == FALSE) { + // if file 'self' does not exist + file_create(self->path); + return file_new(self->path); } - this->length = file_count_lines(this); + self->length = file_count_lines(self); - this->lines = vector_new(VECTOR); // vector of vectors of chars = vector of strings - _file_load_lines(this); + self->lines = vector_new(VECTOR); // vector of vectors of chars = vector of strings + _file_load_lines(self); - return this; + return self; } -void file_cleanup(file_t *this) { - if (this->not_newline_terminated == 1) { - FILE *fd = fopen(this->path, "a"); +void file_cleanup(file_t *self) { + if (self->not_newline_terminated == 1) { + FILE *fd = fopen(self->path, "a"); fprintf(fd, "\n"); fclose(fd); - this->not_newline_terminated = 0; // just for piece of mind + self->not_newline_terminated = 0; // just for piece of mind } - vector_cleanup(this->lines); - free(this); + vector_cleanup(self->lines); + free(self); } -void file_print(file_t *this) { - for (int i = 0; i < this->length; i++) { - vector_print_as_string((vector_t *)vector_get_at(this->lines, i)); +void file_print(file_t *self) { + for (int i = 0; i < self->length; i++) { + vector_print_as_string((vector_t *)vector_get_at(self->lines, i)); } } -void file_writeln(file_t *this, char *payload) { - FILE *fd = fopen(this->path, "a"); +void file_writeln(file_t *self, char *payload) { + FILE *fd = fopen(self->path, "a"); if (fd == NULL) { fclose(fd); - file_cleanup(this); + file_cleanup(self); return; } @@ -134,28 +134,28 @@ void file_writeln(file_t *this, char *payload) { fclose(fd); // Update the file structure without re-initializing - if (this->not_newline_terminated == 0) { + if (self->not_newline_terminated == 0) { vector_t *line = vector_new(CHAR); for (int i = 0; i < strlen(payload); i++) { vector_push(line, &payload[i]); } - vector_push(this->lines, line); - this->length++; - this->not_newline_terminated = 0; - } else if (this->not_newline_terminated == 1) { + vector_push(self->lines, line); + self->length++; + self->not_newline_terminated = 0; + } else if (self->not_newline_terminated == 1) { for (int i = 0; i < strlen(payload); i++) { - vector_push((vector_t *)vector_get_at(this->lines, this->length-1), &payload[i]); + vector_push((vector_t *)vector_get_at(self->lines, self->length-1), &payload[i]); } - this->not_newline_terminated = 0; + self->not_newline_terminated = 0; } } -void file_write(file_t *this, char *payload) { - FILE *fd = fopen(this->path, "a"); +void file_write(file_t *self, char *payload) { + FILE *fd = fopen(self->path, "a"); if (fd) { - fprintf(stderr, "unable to open file %s with mode \"a\"\n", this->path); + fprintf(stderr, "unable to open file %s with mode \"a\"\n", self->path); fclose(fd); - file_cleanup(this); + file_cleanup(self); exit(EXIT_FAILURE); } @@ -168,9 +168,9 @@ void file_write(file_t *this, char *payload) { for (int i = 0; i < strlen(payload); i++) { vector_push(line, &payload[i]); } - vector_push(this->lines, line); - this->length++; - this->not_newline_terminated = 1; + vector_push(self->lines, line); + self->length++; + self->not_newline_terminated = 1; } void file_copy(file_t *src, char *dst) { @@ -192,9 +192,9 @@ void file_copy(file_t *src, char *dst) { fclose(fd); } -void file_delete(file_t *this) { - if (remove(this->path) == 0) - file_cleanup(this); +void file_delete(file_t *self) { + if (remove(self->path) == 0) + file_cleanup(self); } void file_move(file_t *src, char *dst) { @@ -203,45 +203,45 @@ void file_move(file_t *src, char *dst) { file_delete(src); } -void file_number_lines(file_t *this) { - FILE *fd = fopen(this->path, "w"); +void file_number_lines(file_t *self) { + FILE *fd = fopen(self->path, "w"); if (fd == NULL) { fclose(fd); return; } - int mlp = max_lower_power_of_10(this->length); + int mlp = max_lower_power_of_10(self->length); - for (int i = 0; i < this->length; i++) { + for (int i = 0; i < self->length; i++) { // print to the file descriptor - fprintf(fd, "%d %s\n", i+1, vector_to_string((vector_t *)vector_get_at(this->lines, i))); + fprintf(fd, "%d %s\n", i+1, vector_to_string((vector_t *)vector_get_at(self->lines, i))); - // print to this->lines - vector_insert_at((vector_t*)vector_get_at(this->lines, i), 0, &(char){' '}); + // print to self->lines + vector_insert_at((vector_t*)vector_get_at(self->lines, i), 0, &(char){' '}); char *string = int_to_string(i+1); for (int j = strlen(string)-1; j >= 0; j--) { - vector_insert_at((vector_t*)vector_get_at(this->lines, i), 0, &(char){string[j]}); + vector_insert_at((vector_t*)vector_get_at(self->lines, i), 0, &(char){string[j]}); } // prepend the correct amount of ' ' for (int j = 0; j < strlen(int_to_string(mlp))-strlen(int_to_string(i+1)); j++) { - vector_insert_at((vector_t*)vector_get_at(this->lines, i), 0, &(char){' '}); + vector_insert_at((vector_t*)vector_get_at(self->lines, i), 0, &(char){' '}); } } fclose(fd); } -int file_count_words(file_t *this) { +int file_count_words(file_t *self) { int num_words = 0; char prev; - for (int i = 0; i < this->length; i++) { - for (int j = 0; j < ((vector_t*)vector_get_at(this->lines, i))->size; j++) { - char ch = *((char*)vector_get_at((vector_t*)vector_get_at(this->lines, i), j)); + for (int i = 0; i < self->length; i++) { + for (int j = 0; j < ((vector_t*)vector_get_at(self->lines, i))->size; j++) { + char ch = *((char*)vector_get_at((vector_t*)vector_get_at(self->lines, i), j)); if ( (ch == ' ' && prev != ' ') || - (i == this->length-1 && j == ((vector_t*)vector_get_at(this->lines, i))->size-1) || - (j == ((vector_t*)vector_get_at(this->lines, i))->size-1 && j != 0) + (i == self->length-1 && j == ((vector_t*)vector_get_at(self->lines, i))->size-1) || + (j == ((vector_t*)vector_get_at(self->lines, i))->size-1 && j != 0) ) { num_words++; } diff --git a/src/list.c b/src/list.c index e3a0a65..258ed0a 100644 --- a/src/list.c +++ b/src/list.c @@ -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); } } diff --git a/src/string.c b/src/string.c index 0167c2e..60a3cac 100644 --- a/src/string.c +++ b/src/string.c @@ -57,27 +57,27 @@ string_t *string_from(char *chars) { return s; } -bool string_push_chars(string_t *this, char *chars) { - int i = this->length; +bool_t string_push_chars(string_t *self, char *chars) { + int i = self->length; int j = 0; while (*(chars + j) != '\0') { - char *tmp = realloc(this->content, sizeof(this->content) + sizeof(char)); + char *tmp = realloc(self->content, sizeof(self->content) + sizeof(char)); if (tmp == NULL) { - return false; + return FALSE; } - this->content = tmp; - *(this->content + i) = *(chars + j); + self->content = tmp; + *(self->content + i) = *(chars + j); i++; j++; } - this->length = (int)chars_length(this->content)-1; + self->length = (int)chars_length(self->content)-1; - return true; + return TRUE; } -bool string_push(string_t *this, string_t* to_be_pushed) { - return string_push_chars(this, to_be_pushed->content); +bool_t string_push(string_t *self, string_t* to_be_pushed) { + return string_push_chars(self, to_be_pushed->content); } compare_e string_compare(string_t* left, string_t *right) { @@ -97,27 +97,27 @@ compare_e string_compare(string_t* left, string_t *right) { return EQUAL; } -void string_print(string_t *this) { - printf("%s", this->content); +void string_print(string_t *self) { + printf("%s", self->content); } -void string_println(string_t *this) { - printf("%s\n", this->content); +void string_println(string_t *self) { + printf("%s\n", self->content); } -bool string_is_empty(string_t *this) { - compare_e cmp = string_compare(this, string_from("")); +bool_t string_is_empty(string_t *self) { + compare_e cmp = string_compare(self, string_from("")); switch (cmp) { case EQUAL: - return true; + return TRUE; case NOT_EQUAL: - return false; + return FALSE; default: - return false; + return FALSE; } } -void string_cleanup(string_t *this) { - free(this->content); - free(this); +void string_cleanup(string_t *self) { + free(self->content); + free(self); } \ No newline at end of file diff --git a/src/util.c b/src/util.c index e8bc58c..109a47e 100644 --- a/src/util.c +++ b/src/util.c @@ -11,9 +11,9 @@ char *int_to_string(int x) { return retval; } else { while (x > 0) { - int lastDigit = x % 10; + int last_digit = x % 10; x /= 10; - vector_insert_at(string, 0, &(char){lastDigit+48}); + vector_insert_at(string, 0, &(char){last_digit+48}); } vector_pop(string); char* retval = vector_to_string(string); diff --git a/src/vector.c b/src/vector.c index 7f7dc22..1f2033e 100644 --- a/src/vector.c +++ b/src/vector.c @@ -6,44 +6,44 @@ #include #include -void vector_cleanup(vector_t *this) { - free(this->data); - free(this); +void vector_cleanup(vector_t *self) { + free(self->data); + free(self); } static int printNewline = 0; -void vector_reset(vector_t *this) { - while (this->size > 0) { - vector_pop(this); +void vector_reset(vector_t *self) { + while (self->size > 0) { + vector_pop(self); } } -void _vector_print_recursive(vector_t *this, int depth) { +void _vector_print_recursive(vector_t *self, int depth) { int i; printf("["); - for (i = 0; i < this->size; i++) { - switch (this->type) { + for (i = 0; i < self->size; i++) { + switch (self->type) { case INT: - printf("%d", *((int *)(this->data) + i)); + printf("%d", *((int *)(self->data) + i)); break; case FLOAT: - printf("%f", *((float *)(this->data) + i)); + printf("%f", *((float *)(self->data) + i)); break; case CHAR: - printf("%c", *((char *)(this->data) + i)); + printf("%c", *((char *)(self->data) + i)); break; case VECTOR: // Recursively print each level of the vector - _vector_print_recursive((vector_t *)(this->data + i * this->element_size), depth + 1); + _vector_print_recursive((vector_t *)(self->data + i * self->element_size), depth + 1); break; case LIST: - _list_print_recursive((list_t *)(this->data + i * this->element_size), depth + 1); + _list_print_recursive((list_t *)(self->data + i * self->element_size), depth + 1); case BOOL: - printf("%s", (*((bool *)(this->data + i * this->element_size)) == false) ? "false" : "true"); + printf("%s", (*((bool_t *)(self->data + i * self->element_size)) == FALSE) ? "false" : "true"); break; case STRING: - string_print((string_t *)(this->data + i * this->element_size)); + string_print((string_t *)(self->data + i * self->element_size)); break; default: //fprintf(stderr, "tried to print unsupported data type\n"); @@ -51,9 +51,9 @@ void _vector_print_recursive(vector_t *this, int depth) { return; } - if (i < this->size - 1 && this->type != CHAR) { + if (i < self->size - 1 && self->type != CHAR) { printf(", "); - } else if (this->type == CHAR && i < this->size - 2 && this->size != 1) { + } else if (self->type == CHAR && i < self->size - 2 && self->size != 1) { printf(", "); } } @@ -63,31 +63,31 @@ void _vector_print_recursive(vector_t *this, int depth) { } } -void vector_print(vector_t *this) { +void vector_print(vector_t *self) { printf("["); int i; - for (i = 0; i < this->size; i++) { - switch (this->type) { + for (i = 0; i < self->size; i++) { + switch (self->type) { case INT: - printf("%d", *((int *)(this->data) + i)); + printf("%d", *((int *)(self->data) + i)); break; case FLOAT: - printf("%f", *((float *)(this->data) + i)); + printf("%f", *((float *)(self->data) + i)); break; case CHAR: - printf("%c", *((char *)(this->data) + i)); + printf("%c", *((char *)(self->data) + i)); break; case VECTOR: // Recursively print each level of the vector - _vector_print_recursive((vector_t *)(this->data + i * this->element_size), 0); + _vector_print_recursive((vector_t *)(self->data + i * self->element_size), 0); break; case LIST: - _list_print_recursive((list_t *)(this->data + i * this->element_size), 0); + _list_print_recursive((list_t *)(self->data + i * self->element_size), 0); case BOOL: - printf("%s", (*((bool *)(this->data + i * this->element_size)) == false) ? "false" : "true"); + printf("%s", (*((bool_t *)(self->data + i * self->element_size)) == FALSE) ? "false" : "true"); break; case STRING: - string_print((string_t *)(this->data + i * this->element_size)); + string_print((string_t *)(self->data + i * self->element_size)); break; default: //fprintf(stderr, "tried to print unsupported data type\n"); @@ -95,7 +95,7 @@ void vector_print(vector_t *this) { return; } - if (i < this->size - 1) { + if (i < self->size - 1) { printf(", "); } } @@ -107,16 +107,16 @@ void vector_print(vector_t *this) { } } -void _vector_print_as_string_recursive(vector_t *this, int depth) { +void _vector_print_as_string_recursive(vector_t *self, int depth) { int i; - for (i = 0; i < this->size; i++) { - switch (this->type) { + for (i = 0; i < self->size; i++) { + switch (self->type) { case CHAR: - printf("%c", *((char *)(this->data) + i)); + printf("%c", *((char *)(self->data) + i)); break; case VECTOR: // Recursively print each level of the vector - _vector_print_as_string_recursive((vector_t *)(this->data + i * this->element_size), depth + 1); + _vector_print_as_string_recursive((vector_t *)(self->data + i * self->element_size), depth + 1); break; default: //fprintf(stderr, "tried to print a non-char type vector as string\n"); @@ -129,56 +129,56 @@ void _vector_print_as_string_recursive(vector_t *this, int depth) { printf("\n"); // do not print the last additional new line } -void vector_print_as_string(vector_t *this) { - switch (this->type) { +void vector_print_as_string(vector_t *self) { + switch (self->type) { case CHAR: - for (int i = 0; i < this->size; i++) { - printf("%c", *((char *)(this->data) + i)); + for (int i = 0; i < self->size; i++) { + printf("%c", *((char *)(self->data) + i)); } printf("\n"); break; case VECTOR: - _vector_print_as_string_recursive(this, 0); + _vector_print_as_string_recursive(self, 0); break; default: - //fprintf(stderr, "tried to print a non-char type vector as string | type: %d\n", this->type); + //fprintf(stderr, "tried to print a non-char type vector as string | type: %d\n", self->type); //exit(EXIT_FAILURE); return; } } vector_t *vector_new(type_e type) { - vector_t *this = malloc(sizeof(vector_t)); - if (this == NULL) { + vector_t *self = malloc(sizeof(vector_t)); + if (self == NULL) { //fprintf(stderr, "failed to allocate memory for the vector\n"); //exit(EXIT_FAILURE); return NULL; } - this->type = type; - this->size = 0; - this->capacity = 1; - switch (this->type) { + self->type = type; + self->size = 0; + self->capacity = 1; + switch (self->type) { case INT: - this->element_size = sizeof(int); + self->element_size = sizeof(int); break; case FLOAT: - this->element_size = sizeof(float); + self->element_size = sizeof(float); break; case CHAR: - this->element_size = sizeof(char); + self->element_size = sizeof(char); break; case VECTOR: - this->element_size = sizeof(vector_t); + self->element_size = sizeof(vector_t); break; case LIST: - this->element_size = sizeof(list_t); + self->element_size = sizeof(list_t); break; case BOOL: - this->element_size = sizeof(bool); + self->element_size = sizeof(bool_t); break; case STRING: - this->element_size = sizeof(string_t); + self->element_size = sizeof(string_t); break; default: //fprintf(stderr, "specify a valid vector type\n"); @@ -186,92 +186,92 @@ vector_t *vector_new(type_e type) { return NULL; } - this->data = malloc(this->element_size * this->capacity); - if (this->data == NULL) { + self->data = malloc(self->element_size * self->capacity); + if (self->data == NULL) { //fprintf(stderr, "failed to allocate memory for vector data\n"); //exit(EXIT_FAILURE); return NULL; } - return this; + return self; } -void vector_push(vector_t *this, void* element) { - void *tmp = realloc(this->data, (this->size + 1) * this->element_size); - this->data = tmp; - if (this->data == NULL) { +void vector_push(vector_t *self, void* element) { + void *tmp = realloc(self->data, (self->size + 1) * self->element_size); + self->data = tmp; + if (self->data == NULL) { //fprintf(stderr, "failed to allocate memory for vector data\n"); //exit(EXIT_FAILURE); return; } - switch (this->type) { + switch (self->type) { case INT: - *((int *)(this->data) + this->size) = *((int *)element); + *((int *)(self->data) + self->size) = *((int *)element); break; case FLOAT: - *((float *)(this->data) + this->size) = *((float *)element); + *((float *)(self->data) + self->size) = *((float *)element); break; case CHAR: - *((char *)(this->data) + this->size) = *((char *)element); + *((char *)(self->data) + self->size) = *((char *)element); break; case VECTOR: // Copy the vector data from the element to the vector - memcpy((vector_t *)(this->data) + this->size, (vector_t *)element, sizeof(vector_t)); + memcpy((vector_t *)(self->data) + self->size, (vector_t *)element, sizeof(vector_t)); break; case LIST: - memcpy((vector_t*)(this->data) + this->size, (list_t*)element, sizeof(list_t)); + memcpy((vector_t*)(self->data) + self->size, (list_t*)element, sizeof(list_t)); break; case BOOL: - *((bool *)(this->data) + this->size) = *((bool *)element); + *((bool_t *)(self->data) + self->size) = *((bool_t *)element); break; case STRING: - memcpy((vector_t*)(this->data) + this->size, (string_t*)element, sizeof(string_t)); + memcpy((vector_t*)(self->data) + self->size, (string_t*)element, sizeof(string_t)); break; default: return; } - this->size += 1; - if (this->size == this->capacity) { - this->capacity *= 2; + self->size += 1; + if (self->size == self->capacity) { + self->capacity *= 2; } } -void *vector_pop(vector_t *this) { +void *vector_pop(vector_t *self) { // capacity is always a power of 2, so a rounding error cannot happen i guess _/-(shi)-\_ - if (this->size != 0) { - this->size -= 1; + if (self->size != 0) { + 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 (this->data) + this->size-1; + return (self->data) + self->size-1; } else { // cannot pop when there is no value in the vector return NULL; } } -char *vector_to_string(vector_t *this) { +char *vector_to_string(vector_t *self) { char* ret_string; int total_size = 0; // Calculate the total size - for (int i = 0; i < this->size; i++) { - if (this->type == CHAR) { + for (int i = 0; i < self->size; i++) { + if (self->type == CHAR) { total_size += 1; // For a single character - } else if (this->type == VECTOR) { - vector_t *inner_vector = (vector_t *)vector_get_at(this, i); + } else if (self->type == VECTOR) { + vector_t *inner_vector = (vector_t *)vector_get_at(self, i); char *inner_string = vector_to_string(inner_vector); total_size += strlen(inner_string); // Add newline between vectors (except the last one) - if (i < this->size - 1) { + if (i < self->size - 1) { total_size += 1; } @@ -284,17 +284,17 @@ char *vector_to_string(vector_t *this) { // Copy the elements to the string int index = 0; - for (int i = 0; i < this->size; i++) { - if (this->type == CHAR) { - ret_string[index++] = *((char *)vector_get_at(this, i)); - } else if (this->type == VECTOR) { - vector_t *inner_vector = (vector_t *)vector_get_at(this, i); + for (int i = 0; i < self->size; i++) { + if (self->type == CHAR) { + ret_string[index++] = *((char *)vector_get_at(self, i)); + } else if (self->type == VECTOR) { + vector_t *inner_vector = (vector_t *)vector_get_at(self, i); char *inner_string = vector_to_string(inner_vector); strcpy(ret_string + index, inner_string); index += strlen(inner_string); // Add newline between vectors (including the last one) - if (i < this->size) { + if (i < self->size) { ret_string[index++] = '\n'; } @@ -307,102 +307,102 @@ char *vector_to_string(vector_t *this) { return ret_string; } -void *vector_get_at(vector_t *this, int index) { - return (this->data) + index; +void *vector_get_at(vector_t *self, int index) { + return (self->data) + index; } -void vector_set_at(vector_t *this, int index, void *element) { - if (this->size == 0) { - vector_push(this, element); - } else if (index >= this->size) { +void vector_set_at(vector_t *self, int index, void *element) { + if (self->size == 0) { + vector_push(self, element); + } else if (index >= self->size) { return; } - switch (this->type) { + switch (self->type) { case INT: - *((int *)(this->data) + index) = *((int *)element); + *((int *)(self->data) + index) = *((int *)element); break; case FLOAT: - *((float *)(this->data) + index) = *((float *)element); + *((float *)(self->data) + index) = *((float *)element); break; case CHAR: - *((char *)(this->data) + index) = *((char *)element); + *((char *)(self->data) + index) = *((char *)element); break; case VECTOR: - memcpy((vector_t*)(this->data) + index, (vector_t*)element, sizeof(vector_t)); + memcpy((vector_t*)(self->data) + index, (vector_t*)element, sizeof(vector_t)); break; case LIST: - memcpy((vector_t*)(this->data) + index, (list_t*)element, sizeof(list_t)); + memcpy((vector_t*)(self->data) + index, (list_t*)element, sizeof(list_t)); break; case BOOL: - *((bool *)(this->data) + index) = *((bool *)element); + *((bool_t *)(self->data) + index) = *((bool_t *)element); break; case STRING: - memcpy((vector_t*)(this->data) + index, (string_t*)element, sizeof(string_t)); + memcpy((vector_t*)(self->data) + index, (string_t*)element, sizeof(string_t)); break; default: return; } } -void vector_insert_at(vector_t *this, int index, void *element) { - if (this->size == 0) { - vector_push(this, element); - } else if (index < this->size && index >= 0) { - this->size += 1; - if (this->size >= this->capacity) { - this->capacity *= 2; - void *tmp = realloc(this->data, this->element_size * this->capacity); - this->data = tmp; - if (this->data == NULL) { +void vector_insert_at(vector_t *self, int index, void *element) { + if (self->size == 0) { + vector_push(self, element); + } else if (index < self->size && index >= 0) { + self->size += 1; + if (self->size >= self->capacity) { + self->capacity *= 2; + void *tmp = realloc(self->data, self->element_size * self->capacity); + self->data = tmp; + if (self->data == NULL) { return; } } - switch (this->type) { + switch (self->type) { case INT: // move all elements to the right by one - for (int i = this->size-1; i > index; i--) { - *((int *)(this->data) + i) = *((int *)(this->data) + i-1); + for (int i = self->size-1; i > index; i--) { + *((int *)(self->data) + i) = *((int *)(self->data) + i-1); } // set the element at the given index - *((int *)(this->data) + index) = *((int *)element); + *((int *)(self->data) + index) = *((int *)element); break; case FLOAT: - for (int i = this->size-1; i > index; i--) { - *((float *)(this->data) + i) = *((float *)(this->data) + i-1); + for (int i = self->size-1; i > index; i--) { + *((float *)(self->data) + i) = *((float *)(self->data) + i-1); } - *((float *)(this->data) + index) = *((float *)element); + *((float *)(self->data) + index) = *((float *)element); break; case CHAR: - for (int i = this->size-1; i > index; i--) { - *((char *)(this->data) + i) = *((char *)(this->data) + i-1); + for (int i = self->size-1; i > index; i--) { + *((char *)(self->data) + i) = *((char *)(self->data) + i-1); } - *((char *)(this->data) + index) = *((char *)element); + *((char *)(self->data) + index) = *((char *)element); break; case VECTOR: - for (int i = this->size-1; i > index; i--) { - memcpy((vector_t *)(this->data) + i, (vector_t *)(this->data) + i-1, sizeof(vector_t)); + for (int i = self->size-1; i > index; i--) { + memcpy((vector_t *)(self->data) + i, (vector_t *)(self->data) + i-1, sizeof(vector_t)); } - memcpy((vector_t*)(this->data) + index, (vector_t *)element, sizeof(vector_t)); + memcpy((vector_t*)(self->data) + index, (vector_t *)element, sizeof(vector_t)); break; case LIST: - for (int i = this->size-1; i > index; i--) { - memcpy((vector_t *)(this->data) + i, (list_t *)(this->data) + i-1, sizeof(list_t)); + for (int i = self->size-1; i > index; i--) { + memcpy((vector_t *)(self->data) + i, (list_t *)(self->data) + i-1, sizeof(list_t)); } - memcpy((vector_t *)(this->data) + index, (list_t *)element, sizeof(list_t)); + memcpy((vector_t *)(self->data) + index, (list_t *)element, sizeof(list_t)); break; case BOOL: - for (int i = this->size-1; i > index; i--) { - *((bool *)(this->data) + i) = *((bool *)(this->data) + i-1); + for (int i = self->size-1; i > index; i--) { + *((bool_t *)(self->data) + i) = *((bool_t *)(self->data) + i-1); } - *((bool *)(this->data) + index) = *((bool *)element); + *((bool_t *)(self->data) + index) = *((bool_t *)element); break; case STRING: - for (int i = this->size-1; i > index; i--) { - memcpy((vector_t *)(this->data) + i, (string_t *)(this->data) + i-1, sizeof(string_t)); + for (int i = self->size-1; i > index; i--) { + memcpy((vector_t *)(self->data) + i, (string_t *)(self->data) + i-1, sizeof(string_t)); } - memcpy((vector_t *)(this->data) + index, (string_t *)element, sizeof(string_t)); + memcpy((vector_t *)(self->data) + index, (string_t *)element, sizeof(string_t)); break; default: return; @@ -410,55 +410,55 @@ void vector_insert_at(vector_t *this, int index, void *element) { } } -void vector_remove_at(vector_t *this, int index) { - if (index < this->size && index >= 0 && this->size != 0) { - switch (this->type) { +void vector_remove_at(vector_t *self, int index) { + if (index < self->size && index >= 0 && self->size != 0) { + switch (self->type) { case INT: // move all elements to the right by one - for (int i = index; i < this->size; i++) { - *((int *)(this->data) + i) = *((int *)(this->data) + i+1); + for (int i = index; i < self->size; i++) { + *((int *)(self->data) + i) = *((int *)(self->data) + i+1); } break; case FLOAT: - for (int i = index; i < this->size; i++) { - *((float *)(this->data) + i) = *((float *)(this->data) + i+1); + for (int i = index; i < self->size; i++) { + *((float *)(self->data) + i) = *((float *)(self->data) + i+1); } break; case CHAR: - for (int i = index; i < this->size; i++) { - *((char *)(this->data) + i) = *((char *)(this->data) + i+1); + for (int i = index; i < self->size; i++) { + *((char *)(self->data) + i) = *((char *)(self->data) + i+1); } break; case VECTOR: - for (int i = index; i < this->size; i++) { - memcpy((vector_t *)(this->data) + i, (vector_t *)(this->data) + i+1, sizeof(vector_t)); + for (int i = index; i < self->size; i++) { + memcpy((vector_t *)(self->data) + i, (vector_t *)(self->data) + i+1, sizeof(vector_t)); } break; case LIST: - for (int i = index; i < this->size; i++) { - memcpy((vector_t *)(this->data) + i, (list_t *)(this->data) + i+1, sizeof(list_t)); + for (int i = index; i < self->size; i++) { + memcpy((vector_t *)(self->data) + i, (list_t *)(self->data) + i+1, sizeof(list_t)); } break; case BOOL: - for (int i = index; i < this->size; i++) { - *((bool *)(this->data) + i) = *((bool *)(this->data) + i+1); + for (int i = index; i < self->size; i++) { + *((bool_t *)(self->data) + i) = *((bool_t *)(self->data) + i+1); } break; case STRING: - for (int i = index; i < this->size; i++) { - memcpy((vector_t *)(this->data) + i, (string_t *)(this->data) + i+1, sizeof(string_t)); + for (int i = index; i < self->size; i++) { + memcpy((vector_t *)(self->data) + i, (string_t *)(self->data) + i+1, sizeof(string_t)); } break; default: return; } - this->size -= 1; - if (this->size < this->capacity/2) { - this->capacity /= 2; - void *tmp = realloc(this->data, this->element_size * this->capacity); - this->data = tmp; - if (this->data == NULL) { + self->size -= 1; + if (self->size < self->capacity/2) { + self->capacity /= 2; + void *tmp = realloc(self->data, self->element_size * self->capacity); + self->data = tmp; + if (self->data == NULL) { return; } }