fixed some errors
This commit is contained in:
+9
-9
@@ -13,28 +13,28 @@ typedef struct {
|
|||||||
|
|
||||||
file_t *file_new(char *path); // initialization function
|
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_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
|
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_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
|
#endif // FILE_H
|
||||||
|
|||||||
+12
-12
@@ -2,12 +2,12 @@
|
|||||||
#define LIST_H
|
#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
|
* is a `vector_t` of other `vector_t`s, where
|
||||||
* every inner `vector_t` can be of any type ->
|
* every inner `vector_t` can be of any type ->
|
||||||
* this means a Python-like list
|
* self means a Python-like list
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <odweta/vector.h>
|
#include <odweta/vector.h>
|
||||||
@@ -21,21 +21,21 @@ typedef struct {
|
|||||||
|
|
||||||
list_t *list_new(); // returns a new list
|
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_show(list_t *self); // prints a list
|
||||||
void _list_print_recursive(list_t *this, int depth);
|
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
|
#endif // LIST_H
|
||||||
+6
-6
@@ -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
|
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_t string_push_chars(string_t *self, 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(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
|
compare_e string_compare(string_t *left, string_t* right); // compare two strings
|
||||||
|
|
||||||
void string_print(string_t *this); // print a string
|
void string_print(string_t *self); // print a string
|
||||||
void string_println(string_t *this); // print a string with a \n
|
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
|
#endif
|
||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#undef bool
|
#undef bool
|
||||||
|
|
||||||
typedef enum { false, true } bool;
|
typedef enum { FALSE, TRUE } bool_t;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
LESS,
|
LESS,
|
||||||
|
|||||||
+12
-12
@@ -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
|
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
|
#endif
|
||||||
|
|||||||
+70
-70
@@ -5,11 +5,11 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int file_count_lines(file_t *this) {
|
int file_count_lines(file_t *self) {
|
||||||
char ch;
|
char ch;
|
||||||
int lines = 0;
|
int lines = 0;
|
||||||
|
|
||||||
FILE *fd = fopen(this->path, "r");
|
FILE *fd = fopen(self->path, "r");
|
||||||
if (fd == NULL) {
|
if (fd == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -25,18 +25,18 @@ int file_count_lines(file_t *this) {
|
|||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _file_load_lines(file_t *this) {
|
void _file_load_lines(file_t *self) {
|
||||||
char ch;
|
char ch;
|
||||||
|
|
||||||
FILE *fd = fopen(this->path, "r");
|
FILE *fd = fopen(self->path, "r");
|
||||||
if (fd == NULL) {
|
if (fd == NULL) {
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
file_cleanup(this);
|
file_cleanup(self);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
char prev;
|
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
|
vector_t *line = vector_new(CHAR); // Create a new vector for each line
|
||||||
|
|
||||||
while ((ch = fgetc(fd)) != '\n' && ch != EOF) {
|
while ((ch = fgetc(fd)) != '\n' && ch != EOF) {
|
||||||
@@ -49,28 +49,28 @@ void _file_load_lines(file_t *this) {
|
|||||||
vector_push(line, empty);
|
vector_push(line, empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
vector_push(this->lines, line);
|
vector_push(self->lines, line);
|
||||||
|
|
||||||
prev = ch;
|
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') {
|
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 {
|
} 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);
|
fclose(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *file_to_string(file_t *this) {
|
char *file_to_string(file_t *self) {
|
||||||
return vector_to_string(this->lines);
|
return vector_to_string(self->lines);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool file_exists(file_t *this) {
|
bool_t file_exists(file_t *self) {
|
||||||
FILE *fd = fopen(this->path, "r");
|
FILE *fd = fopen(self->path, "r");
|
||||||
|
|
||||||
bool retval = (fd == NULL) ? false : true;
|
bool_t retval = (fd == NULL) ? FALSE : TRUE;
|
||||||
|
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
|
|
||||||
@@ -82,50 +82,50 @@ void file_create(char *path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
file_t *file_new(char *path) {
|
file_t *file_new(char *path) {
|
||||||
file_t *this = (file_t *)malloc(sizeof(file_t));
|
file_t *self = (file_t *)malloc(sizeof(file_t));
|
||||||
if (this == NULL) {
|
if (self == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->path = path;
|
self->path = path;
|
||||||
|
|
||||||
if (file_exists(this) == false) {
|
if (file_exists(self) == FALSE) {
|
||||||
// if file 'this' does not exist
|
// if file 'self' does not exist
|
||||||
file_create(this->path);
|
file_create(self->path);
|
||||||
return file_new(this->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
|
self->lines = vector_new(VECTOR); // vector of vectors of chars = vector of strings
|
||||||
_file_load_lines(this);
|
_file_load_lines(self);
|
||||||
|
|
||||||
return this;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_cleanup(file_t *this) {
|
void file_cleanup(file_t *self) {
|
||||||
if (this->not_newline_terminated == 1) {
|
if (self->not_newline_terminated == 1) {
|
||||||
FILE *fd = fopen(this->path, "a");
|
FILE *fd = fopen(self->path, "a");
|
||||||
fprintf(fd, "\n");
|
fprintf(fd, "\n");
|
||||||
fclose(fd);
|
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);
|
vector_cleanup(self->lines);
|
||||||
free(this);
|
free(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_print(file_t *this) {
|
void file_print(file_t *self) {
|
||||||
for (int i = 0; i < this->length; i++) {
|
for (int i = 0; i < self->length; i++) {
|
||||||
vector_print_as_string((vector_t *)vector_get_at(this->lines, i));
|
vector_print_as_string((vector_t *)vector_get_at(self->lines, i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_writeln(file_t *this, char *payload) {
|
void file_writeln(file_t *self, char *payload) {
|
||||||
FILE *fd = fopen(this->path, "a");
|
FILE *fd = fopen(self->path, "a");
|
||||||
if (fd == NULL) {
|
if (fd == NULL) {
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
file_cleanup(this);
|
file_cleanup(self);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,28 +134,28 @@ void file_writeln(file_t *this, char *payload) {
|
|||||||
fclose(fd);
|
fclose(fd);
|
||||||
|
|
||||||
// Update the file structure without re-initializing
|
// 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);
|
vector_t *line = vector_new(CHAR);
|
||||||
for (int i = 0; i < strlen(payload); i++) {
|
for (int i = 0; i < strlen(payload); i++) {
|
||||||
vector_push(line, &payload[i]);
|
vector_push(line, &payload[i]);
|
||||||
}
|
}
|
||||||
vector_push(this->lines, line);
|
vector_push(self->lines, line);
|
||||||
this->length++;
|
self->length++;
|
||||||
this->not_newline_terminated = 0;
|
self->not_newline_terminated = 0;
|
||||||
} else if (this->not_newline_terminated == 1) {
|
} else if (self->not_newline_terminated == 1) {
|
||||||
for (int i = 0; i < strlen(payload); i++) {
|
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) {
|
void file_write(file_t *self, char *payload) {
|
||||||
FILE *fd = fopen(this->path, "a");
|
FILE *fd = fopen(self->path, "a");
|
||||||
if (fd) {
|
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);
|
fclose(fd);
|
||||||
file_cleanup(this);
|
file_cleanup(self);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,9 +168,9 @@ void file_write(file_t *this, char *payload) {
|
|||||||
for (int i = 0; i < strlen(payload); i++) {
|
for (int i = 0; i < strlen(payload); i++) {
|
||||||
vector_push(line, &payload[i]);
|
vector_push(line, &payload[i]);
|
||||||
}
|
}
|
||||||
vector_push(this->lines, line);
|
vector_push(self->lines, line);
|
||||||
this->length++;
|
self->length++;
|
||||||
this->not_newline_terminated = 1;
|
self->not_newline_terminated = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_copy(file_t *src, char *dst) {
|
void file_copy(file_t *src, char *dst) {
|
||||||
@@ -192,9 +192,9 @@ void file_copy(file_t *src, char *dst) {
|
|||||||
fclose(fd);
|
fclose(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_delete(file_t *this) {
|
void file_delete(file_t *self) {
|
||||||
if (remove(this->path) == 0)
|
if (remove(self->path) == 0)
|
||||||
file_cleanup(this);
|
file_cleanup(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_move(file_t *src, char *dst) {
|
void file_move(file_t *src, char *dst) {
|
||||||
@@ -203,45 +203,45 @@ void file_move(file_t *src, char *dst) {
|
|||||||
file_delete(src);
|
file_delete(src);
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_number_lines(file_t *this) {
|
void file_number_lines(file_t *self) {
|
||||||
FILE *fd = fopen(this->path, "w");
|
FILE *fd = fopen(self->path, "w");
|
||||||
if (fd == NULL) {
|
if (fd == NULL) {
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
return;
|
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
|
// 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
|
// print to self->lines
|
||||||
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){' '});
|
||||||
char *string = int_to_string(i+1);
|
char *string = int_to_string(i+1);
|
||||||
for (int j = strlen(string)-1; j >= 0; j--) {
|
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 ' '
|
// prepend the correct amount of ' '
|
||||||
for (int j = 0; j < strlen(int_to_string(mlp))-strlen(int_to_string(i+1)); j++) {
|
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);
|
fclose(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
int file_count_words(file_t *this) {
|
int file_count_words(file_t *self) {
|
||||||
int num_words = 0;
|
int num_words = 0;
|
||||||
char prev;
|
char prev;
|
||||||
for (int i = 0; i < this->length; i++) {
|
for (int i = 0; i < self->length; i++) {
|
||||||
for (int j = 0; j < ((vector_t*)vector_get_at(this->lines, i))->size; j++) {
|
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(this->lines, i), j));
|
char ch = *((char*)vector_get_at((vector_t*)vector_get_at(self->lines, i), j));
|
||||||
if (
|
if (
|
||||||
(ch == ' ' && prev != ' ') ||
|
(ch == ' ' && prev != ' ') ||
|
||||||
(i == this->length-1 && j == ((vector_t*)vector_get_at(this->lines, i))->size-1) ||
|
(i == self->length-1 && j == ((vector_t*)vector_get_at(self->lines, i))->size-1) ||
|
||||||
(j == ((vector_t*)vector_get_at(this->lines, i))->size-1 && j != 0)
|
(j == ((vector_t*)vector_get_at(self->lines, i))->size-1 && j != 0)
|
||||||
) {
|
) {
|
||||||
num_words++;
|
num_words++;
|
||||||
}
|
}
|
||||||
|
|||||||
+61
-61
@@ -9,40 +9,40 @@
|
|||||||
#define init_error_exit(l) fprintf(stderr, "could not init list\n"); free(l); exit(EXIT_FAILURE)
|
#define init_error_exit(l) fprintf(stderr, "could not init list\n"); free(l); exit(EXIT_FAILURE)
|
||||||
|
|
||||||
list_t *list_new() {
|
list_t *list_new() {
|
||||||
list_t *this = (list_t *)malloc(sizeof(list_t));
|
list_t *self = (list_t *)malloc(sizeof(list_t));
|
||||||
if (this == NULL) {
|
if (self == NULL) {
|
||||||
init_error_exit(this);
|
init_error_exit(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
this->data = (vector_t *)vector_new(VECTOR);
|
self->data = (vector_t *)vector_new(VECTOR);
|
||||||
if (this->data == NULL) {
|
if (self->data == NULL) {
|
||||||
init_error_exit(this);
|
init_error_exit(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
this->size = 0;
|
self->size = 0;
|
||||||
this->capacity = 1;
|
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_t *new_vec = vector_new(type);
|
||||||
|
|
||||||
vector_push(new_vec, element);
|
vector_push(new_vec, element);
|
||||||
vector_push((vector_t *)this->data, new_vec);
|
vector_push((vector_t *)self->data, new_vec);
|
||||||
|
|
||||||
this->size += 1;
|
self->size += 1;
|
||||||
if (this->size == this->capacity) {
|
if (self->size == self->capacity) {
|
||||||
this->capacity *= 2;
|
self->capacity *= 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _list_print_recursive(list_t *this, int depth) {
|
void _list_print_recursive(list_t *self, int depth) {
|
||||||
printf("[");
|
printf("[");
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < this->size; i++) {
|
for (i = 0; i < self->size; i++) {
|
||||||
vector_t *ith_vec = (vector_t *)vector_get_at((vector_t *)this->data, i);
|
vector_t *ith_vec = (vector_t *)vector_get_at((vector_t *)self->data, i);
|
||||||
|
|
||||||
switch (ith_vec->type) {
|
switch (ith_vec->type) {
|
||||||
case INT:
|
case INT:
|
||||||
@@ -61,14 +61,14 @@ void _list_print_recursive(list_t *this, int depth) {
|
|||||||
_list_print_recursive(ith_vec->data, 0);
|
_list_print_recursive(ith_vec->data, 0);
|
||||||
break;
|
break;
|
||||||
case BOOL:
|
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;
|
break;
|
||||||
case STRING:
|
case STRING:
|
||||||
string_print((string_t *)vector_get_at(ith_vec, 0));
|
string_print((string_t *)vector_get_at(ith_vec, 0));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < this->size - 1) {
|
if (i < self->size - 1) {
|
||||||
printf(", ");
|
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("[");
|
printf("[");
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < this->size; i++) {
|
for (i = 0; i < self->size; i++) {
|
||||||
vector_t *ith_vec = (vector_t *)vector_get_at((vector_t *)this->data, i);
|
vector_t *ith_vec = (vector_t *)vector_get_at((vector_t *)self->data, i);
|
||||||
|
|
||||||
switch (ith_vec->type) {
|
switch (ith_vec->type) {
|
||||||
case INT:
|
case INT:
|
||||||
@@ -104,14 +104,14 @@ void list_show(list_t *this) {
|
|||||||
_list_print_recursive(ith_vec->data, 0);
|
_list_print_recursive(ith_vec->data, 0);
|
||||||
break;
|
break;
|
||||||
case BOOL:
|
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;
|
break;
|
||||||
case STRING:
|
case STRING:
|
||||||
string_print((string_t *)vector_get_at(ith_vec, 0));
|
string_print((string_t *)vector_get_at(ith_vec, 0));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < this->size - 1) {
|
if (i < self->size - 1) {
|
||||||
printf(", ");
|
printf(", ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,22 +119,22 @@ void list_show(list_t *this) {
|
|||||||
printf("]\n");
|
printf("]\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_cleanup(list_t *this) {
|
void list_cleanup(list_t *self) {
|
||||||
vector_cleanup((vector_t *)this->data);
|
vector_cleanup((vector_t *)self->data);
|
||||||
free(this);
|
free(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *list_pop(list_t *this) {
|
void *list_pop(list_t *self) {
|
||||||
if (this->size != 0) {
|
if (self->size != 0) {
|
||||||
vector_t *first_ptr = (vector_t *)vector_pop((vector_t *)this->data);
|
vector_t *first_ptr = (vector_t *)vector_pop((vector_t *)self->data);
|
||||||
void *ptr = vector_pop(first_ptr);
|
void *ptr = vector_pop(first_ptr);
|
||||||
this->size -= 1;
|
self->size -= 1;
|
||||||
|
|
||||||
if (this->size <= this->capacity / 2)
|
if (self->size <= self->capacity / 2)
|
||||||
this->capacity /= 2;
|
self->capacity /= 2;
|
||||||
|
|
||||||
if (this->capacity == 0)
|
if (self->capacity == 0)
|
||||||
this->capacity = 1;
|
self->capacity = 1;
|
||||||
|
|
||||||
return ptr;
|
return ptr;
|
||||||
} else {
|
} 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) {
|
void list_insert_at(list_t *self, int index, type_e type, void *element) {
|
||||||
if (this->size == 0) {
|
if (self->size == 0) {
|
||||||
list_push(this, type, element);
|
list_push(self, type, element);
|
||||||
} else {
|
} else {
|
||||||
vector_t* new_vec = vector_new(type);
|
vector_t* new_vec = vector_new(type);
|
||||||
|
|
||||||
vector_push(new_vec, element);
|
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;
|
self->size += 1;
|
||||||
if (this->size == this->capacity) {
|
if (self->size == self->capacity) {
|
||||||
this->capacity *= 2;
|
self->capacity *= 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void list_remove_at(list_t *this, int index) {
|
void list_remove_at(list_t *self, int index) {
|
||||||
vector_remove_at(this->data, index);
|
vector_remove_at(self->data, index);
|
||||||
this->size -= 1;
|
self->size -= 1;
|
||||||
if (this->size == this->capacity) {
|
if (self->size == self->capacity) {
|
||||||
this->capacity /= 2;
|
self->capacity /= 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
if (this->size == 0) {
|
if (self->size == 0) {
|
||||||
list_push(this, type, element);
|
list_push(self, type, element);
|
||||||
} else if (index < this->size && index >= 0) {
|
} else if (index < self->size && index >= 0) {
|
||||||
list_remove_at(this, index);
|
list_remove_at(self, index);
|
||||||
if (index == this->size) {
|
if (index == self->size) {
|
||||||
this->data->size += 1;
|
self->data->size += 1;
|
||||||
if (this->data->size == this->data->capacity) {
|
if (self->data->size == self->data->capacity) {
|
||||||
this->data->capacity *= 2;
|
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) {
|
void *list_get_at(list_t *self, int index) {
|
||||||
if (this->size == 0) {
|
if (self->size == 0) {
|
||||||
//fprintf(stderr, "list is empty\n");
|
//fprintf(stderr, "list is empty\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
} else if (index >= this->size || index < 0) {
|
} else if (index >= self->size || index < 0) {
|
||||||
//fprintf(stderr, "index out of range\n");
|
//fprintf(stderr, "index out of range\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
} else {
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-22
@@ -57,27 +57,27 @@ string_t *string_from(char *chars) {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool string_push_chars(string_t *this, char *chars) {
|
bool_t string_push_chars(string_t *self, char *chars) {
|
||||||
int i = this->length;
|
int i = self->length;
|
||||||
int j = 0;
|
int j = 0;
|
||||||
while (*(chars + 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) {
|
if (tmp == NULL) {
|
||||||
return false;
|
return FALSE;
|
||||||
}
|
}
|
||||||
this->content = tmp;
|
self->content = tmp;
|
||||||
*(this->content + i) = *(chars + j);
|
*(self->content + i) = *(chars + j);
|
||||||
i++;
|
i++;
|
||||||
j++;
|
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) {
|
bool_t string_push(string_t *self, string_t* to_be_pushed) {
|
||||||
return string_push_chars(this, to_be_pushed->content);
|
return string_push_chars(self, to_be_pushed->content);
|
||||||
}
|
}
|
||||||
|
|
||||||
compare_e string_compare(string_t* left, string_t *right) {
|
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;
|
return EQUAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void string_print(string_t *this) {
|
void string_print(string_t *self) {
|
||||||
printf("%s", this->content);
|
printf("%s", self->content);
|
||||||
}
|
}
|
||||||
|
|
||||||
void string_println(string_t *this) {
|
void string_println(string_t *self) {
|
||||||
printf("%s\n", this->content);
|
printf("%s\n", self->content);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool string_is_empty(string_t *this) {
|
bool_t string_is_empty(string_t *self) {
|
||||||
compare_e cmp = string_compare(this, string_from(""));
|
compare_e cmp = string_compare(self, string_from(""));
|
||||||
switch (cmp) {
|
switch (cmp) {
|
||||||
case EQUAL:
|
case EQUAL:
|
||||||
return true;
|
return TRUE;
|
||||||
case NOT_EQUAL:
|
case NOT_EQUAL:
|
||||||
return false;
|
return FALSE;
|
||||||
default:
|
default:
|
||||||
return false;
|
return FALSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void string_cleanup(string_t *this) {
|
void string_cleanup(string_t *self) {
|
||||||
free(this->content);
|
free(self->content);
|
||||||
free(this);
|
free(self);
|
||||||
}
|
}
|
||||||
+2
-2
@@ -11,9 +11,9 @@ char *int_to_string(int x) {
|
|||||||
return retval;
|
return retval;
|
||||||
} else {
|
} else {
|
||||||
while (x > 0) {
|
while (x > 0) {
|
||||||
int lastDigit = x % 10;
|
int last_digit = x % 10;
|
||||||
x /= 10;
|
x /= 10;
|
||||||
vector_insert_at(string, 0, &(char){lastDigit+48});
|
vector_insert_at(string, 0, &(char){last_digit+48});
|
||||||
}
|
}
|
||||||
vector_pop(string);
|
vector_pop(string);
|
||||||
char* retval = vector_to_string(string);
|
char* retval = vector_to_string(string);
|
||||||
|
|||||||
+160
-160
@@ -6,44 +6,44 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
void vector_cleanup(vector_t *this) {
|
void vector_cleanup(vector_t *self) {
|
||||||
free(this->data);
|
free(self->data);
|
||||||
free(this);
|
free(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int printNewline = 0;
|
static int printNewline = 0;
|
||||||
|
|
||||||
void vector_reset(vector_t *this) {
|
void vector_reset(vector_t *self) {
|
||||||
while (this->size > 0) {
|
while (self->size > 0) {
|
||||||
vector_pop(this);
|
vector_pop(self);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _vector_print_recursive(vector_t *this, int depth) {
|
void _vector_print_recursive(vector_t *self, int depth) {
|
||||||
int i;
|
int i;
|
||||||
printf("[");
|
printf("[");
|
||||||
for (i = 0; i < this->size; i++) {
|
for (i = 0; i < self->size; i++) {
|
||||||
switch (this->type) {
|
switch (self->type) {
|
||||||
case INT:
|
case INT:
|
||||||
printf("%d", *((int *)(this->data) + i));
|
printf("%d", *((int *)(self->data) + i));
|
||||||
break;
|
break;
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
printf("%f", *((float *)(this->data) + i));
|
printf("%f", *((float *)(self->data) + i));
|
||||||
break;
|
break;
|
||||||
case CHAR:
|
case CHAR:
|
||||||
printf("%c", *((char *)(this->data) + i));
|
printf("%c", *((char *)(self->data) + i));
|
||||||
break;
|
break;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
// Recursively print each level of the 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;
|
break;
|
||||||
case LIST:
|
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:
|
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;
|
break;
|
||||||
case STRING:
|
case STRING:
|
||||||
string_print((string_t *)(this->data + i * this->element_size));
|
string_print((string_t *)(self->data + i * self->element_size));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
//fprintf(stderr, "tried to print unsupported data type\n");
|
//fprintf(stderr, "tried to print unsupported data type\n");
|
||||||
@@ -51,9 +51,9 @@ void _vector_print_recursive(vector_t *this, int depth) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < this->size - 1 && this->type != CHAR) {
|
if (i < self->size - 1 && self->type != CHAR) {
|
||||||
printf(", ");
|
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(", ");
|
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("[");
|
printf("[");
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < this->size; i++) {
|
for (i = 0; i < self->size; i++) {
|
||||||
switch (this->type) {
|
switch (self->type) {
|
||||||
case INT:
|
case INT:
|
||||||
printf("%d", *((int *)(this->data) + i));
|
printf("%d", *((int *)(self->data) + i));
|
||||||
break;
|
break;
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
printf("%f", *((float *)(this->data) + i));
|
printf("%f", *((float *)(self->data) + i));
|
||||||
break;
|
break;
|
||||||
case CHAR:
|
case CHAR:
|
||||||
printf("%c", *((char *)(this->data) + i));
|
printf("%c", *((char *)(self->data) + i));
|
||||||
break;
|
break;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
// Recursively print each level of the 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;
|
break;
|
||||||
case LIST:
|
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:
|
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;
|
break;
|
||||||
case STRING:
|
case STRING:
|
||||||
string_print((string_t *)(this->data + i * this->element_size));
|
string_print((string_t *)(self->data + i * self->element_size));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
//fprintf(stderr, "tried to print unsupported data type\n");
|
//fprintf(stderr, "tried to print unsupported data type\n");
|
||||||
@@ -95,7 +95,7 @@ void vector_print(vector_t *this) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < this->size - 1) {
|
if (i < self->size - 1) {
|
||||||
printf(", ");
|
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;
|
int i;
|
||||||
for (i = 0; i < this->size; i++) {
|
for (i = 0; i < self->size; i++) {
|
||||||
switch (this->type) {
|
switch (self->type) {
|
||||||
case CHAR:
|
case CHAR:
|
||||||
printf("%c", *((char *)(this->data) + i));
|
printf("%c", *((char *)(self->data) + i));
|
||||||
break;
|
break;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
// Recursively print each level of the 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;
|
break;
|
||||||
default:
|
default:
|
||||||
//fprintf(stderr, "tried to print a non-char type vector as string\n");
|
//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
|
printf("\n"); // do not print the last additional new line
|
||||||
}
|
}
|
||||||
|
|
||||||
void vector_print_as_string(vector_t *this) {
|
void vector_print_as_string(vector_t *self) {
|
||||||
switch (this->type) {
|
switch (self->type) {
|
||||||
case CHAR:
|
case CHAR:
|
||||||
for (int i = 0; i < this->size; i++) {
|
for (int i = 0; i < self->size; i++) {
|
||||||
printf("%c", *((char *)(this->data) + i));
|
printf("%c", *((char *)(self->data) + i));
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
break;
|
break;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
_vector_print_as_string_recursive(this, 0);
|
_vector_print_as_string_recursive(self, 0);
|
||||||
break;
|
break;
|
||||||
default:
|
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);
|
//exit(EXIT_FAILURE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vector_t *vector_new(type_e type) {
|
vector_t *vector_new(type_e type) {
|
||||||
vector_t *this = malloc(sizeof(vector_t));
|
vector_t *self = malloc(sizeof(vector_t));
|
||||||
if (this == NULL) {
|
if (self == NULL) {
|
||||||
//fprintf(stderr, "failed to allocate memory for the vector\n");
|
//fprintf(stderr, "failed to allocate memory for the vector\n");
|
||||||
//exit(EXIT_FAILURE);
|
//exit(EXIT_FAILURE);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->type = type;
|
self->type = type;
|
||||||
this->size = 0;
|
self->size = 0;
|
||||||
this->capacity = 1;
|
self->capacity = 1;
|
||||||
switch (this->type) {
|
switch (self->type) {
|
||||||
case INT:
|
case INT:
|
||||||
this->element_size = sizeof(int);
|
self->element_size = sizeof(int);
|
||||||
break;
|
break;
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
this->element_size = sizeof(float);
|
self->element_size = sizeof(float);
|
||||||
break;
|
break;
|
||||||
case CHAR:
|
case CHAR:
|
||||||
this->element_size = sizeof(char);
|
self->element_size = sizeof(char);
|
||||||
break;
|
break;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
this->element_size = sizeof(vector_t);
|
self->element_size = sizeof(vector_t);
|
||||||
break;
|
break;
|
||||||
case LIST:
|
case LIST:
|
||||||
this->element_size = sizeof(list_t);
|
self->element_size = sizeof(list_t);
|
||||||
break;
|
break;
|
||||||
case BOOL:
|
case BOOL:
|
||||||
this->element_size = sizeof(bool);
|
self->element_size = sizeof(bool_t);
|
||||||
break;
|
break;
|
||||||
case STRING:
|
case STRING:
|
||||||
this->element_size = sizeof(string_t);
|
self->element_size = sizeof(string_t);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
//fprintf(stderr, "specify a valid vector type\n");
|
//fprintf(stderr, "specify a valid vector type\n");
|
||||||
@@ -186,92 +186,92 @@ vector_t *vector_new(type_e type) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->data = malloc(this->element_size * this->capacity);
|
self->data = malloc(self->element_size * self->capacity);
|
||||||
if (this->data == NULL) {
|
if (self->data == NULL) {
|
||||||
//fprintf(stderr, "failed to allocate memory for vector data\n");
|
//fprintf(stderr, "failed to allocate memory for vector data\n");
|
||||||
//exit(EXIT_FAILURE);
|
//exit(EXIT_FAILURE);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
void vector_push(vector_t *this, void* element) {
|
void vector_push(vector_t *self, void* element) {
|
||||||
void *tmp = realloc(this->data, (this->size + 1) * this->element_size);
|
void *tmp = realloc(self->data, (self->size + 1) * self->element_size);
|
||||||
this->data = tmp;
|
self->data = tmp;
|
||||||
if (this->data == NULL) {
|
if (self->data == NULL) {
|
||||||
//fprintf(stderr, "failed to allocate memory for vector data\n");
|
//fprintf(stderr, "failed to allocate memory for vector data\n");
|
||||||
//exit(EXIT_FAILURE);
|
//exit(EXIT_FAILURE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (this->type) {
|
switch (self->type) {
|
||||||
case INT:
|
case INT:
|
||||||
*((int *)(this->data) + this->size) = *((int *)element);
|
*((int *)(self->data) + self->size) = *((int *)element);
|
||||||
break;
|
break;
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
*((float *)(this->data) + this->size) = *((float *)element);
|
*((float *)(self->data) + self->size) = *((float *)element);
|
||||||
break;
|
break;
|
||||||
case CHAR:
|
case CHAR:
|
||||||
*((char *)(this->data) + this->size) = *((char *)element);
|
*((char *)(self->data) + self->size) = *((char *)element);
|
||||||
break;
|
break;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
// Copy the vector data from the element to the 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;
|
break;
|
||||||
case LIST:
|
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;
|
break;
|
||||||
case BOOL:
|
case BOOL:
|
||||||
*((bool *)(this->data) + this->size) = *((bool *)element);
|
*((bool_t *)(self->data) + self->size) = *((bool_t *)element);
|
||||||
break;
|
break;
|
||||||
case STRING:
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->size += 1;
|
self->size += 1;
|
||||||
if (this->size == this->capacity) {
|
if (self->size == self->capacity) {
|
||||||
this->capacity *= 2;
|
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)-\_
|
// capacity is always a power of 2, so a rounding error cannot happen i guess _/-(shi)-\_
|
||||||
|
|
||||||
if (this->size != 0) {
|
if (self->size != 0) {
|
||||||
this->size -= 1;
|
self->size -= 1;
|
||||||
|
|
||||||
if (this->size <= this->capacity / 2)
|
if (self->size <= self->capacity / 2)
|
||||||
this->capacity /= 2;
|
self->capacity /= 2;
|
||||||
|
|
||||||
if (this->capacity == 0)
|
if (self->capacity == 0)
|
||||||
this->capacity = 1;
|
self->capacity = 1;
|
||||||
|
|
||||||
return (this->data) + this->size-1;
|
return (self->data) + self->size-1;
|
||||||
} else {
|
} else {
|
||||||
// cannot pop when there is no value in the vector
|
// cannot pop when there is no value in the vector
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char *vector_to_string(vector_t *this) {
|
char *vector_to_string(vector_t *self) {
|
||||||
char* ret_string;
|
char* ret_string;
|
||||||
int total_size = 0;
|
int total_size = 0;
|
||||||
|
|
||||||
// Calculate the total size
|
// Calculate the total size
|
||||||
for (int i = 0; i < this->size; i++) {
|
for (int i = 0; i < self->size; i++) {
|
||||||
if (this->type == CHAR) {
|
if (self->type == CHAR) {
|
||||||
total_size += 1; // For a single character
|
total_size += 1; // For a single character
|
||||||
} else if (this->type == VECTOR) {
|
} else if (self->type == VECTOR) {
|
||||||
vector_t *inner_vector = (vector_t *)vector_get_at(this, i);
|
vector_t *inner_vector = (vector_t *)vector_get_at(self, i);
|
||||||
char *inner_string = vector_to_string(inner_vector);
|
char *inner_string = vector_to_string(inner_vector);
|
||||||
total_size += strlen(inner_string);
|
total_size += strlen(inner_string);
|
||||||
|
|
||||||
// Add newline between vectors (except the last one)
|
// Add newline between vectors (except the last one)
|
||||||
if (i < this->size - 1) {
|
if (i < self->size - 1) {
|
||||||
total_size += 1;
|
total_size += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -284,17 +284,17 @@ char *vector_to_string(vector_t *this) {
|
|||||||
|
|
||||||
// Copy the elements to the string
|
// Copy the elements to the string
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (int i = 0; i < this->size; i++) {
|
for (int i = 0; i < self->size; i++) {
|
||||||
if (this->type == CHAR) {
|
if (self->type == CHAR) {
|
||||||
ret_string[index++] = *((char *)vector_get_at(this, i));
|
ret_string[index++] = *((char *)vector_get_at(self, i));
|
||||||
} else if (this->type == VECTOR) {
|
} else if (self->type == VECTOR) {
|
||||||
vector_t *inner_vector = (vector_t *)vector_get_at(this, i);
|
vector_t *inner_vector = (vector_t *)vector_get_at(self, i);
|
||||||
char *inner_string = vector_to_string(inner_vector);
|
char *inner_string = vector_to_string(inner_vector);
|
||||||
strcpy(ret_string + index, inner_string);
|
strcpy(ret_string + index, inner_string);
|
||||||
index += strlen(inner_string);
|
index += strlen(inner_string);
|
||||||
|
|
||||||
// Add newline between vectors (including the last one)
|
// Add newline between vectors (including the last one)
|
||||||
if (i < this->size) {
|
if (i < self->size) {
|
||||||
ret_string[index++] = '\n';
|
ret_string[index++] = '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -307,102 +307,102 @@ char *vector_to_string(vector_t *this) {
|
|||||||
return ret_string;
|
return ret_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *vector_get_at(vector_t *this, int index) {
|
void *vector_get_at(vector_t *self, int index) {
|
||||||
return (this->data) + index;
|
return (self->data) + index;
|
||||||
}
|
}
|
||||||
|
|
||||||
void vector_set_at(vector_t *this, int index, void *element) {
|
void vector_set_at(vector_t *self, int index, void *element) {
|
||||||
if (this->size == 0) {
|
if (self->size == 0) {
|
||||||
vector_push(this, element);
|
vector_push(self, element);
|
||||||
} else if (index >= this->size) {
|
} else if (index >= self->size) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (this->type) {
|
switch (self->type) {
|
||||||
case INT:
|
case INT:
|
||||||
*((int *)(this->data) + index) = *((int *)element);
|
*((int *)(self->data) + index) = *((int *)element);
|
||||||
break;
|
break;
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
*((float *)(this->data) + index) = *((float *)element);
|
*((float *)(self->data) + index) = *((float *)element);
|
||||||
break;
|
break;
|
||||||
case CHAR:
|
case CHAR:
|
||||||
*((char *)(this->data) + index) = *((char *)element);
|
*((char *)(self->data) + index) = *((char *)element);
|
||||||
break;
|
break;
|
||||||
case VECTOR:
|
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;
|
break;
|
||||||
case LIST:
|
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;
|
break;
|
||||||
case BOOL:
|
case BOOL:
|
||||||
*((bool *)(this->data) + index) = *((bool *)element);
|
*((bool_t *)(self->data) + index) = *((bool_t *)element);
|
||||||
break;
|
break;
|
||||||
case STRING:
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void vector_insert_at(vector_t *this, int index, void *element) {
|
void vector_insert_at(vector_t *self, int index, void *element) {
|
||||||
if (this->size == 0) {
|
if (self->size == 0) {
|
||||||
vector_push(this, element);
|
vector_push(self, element);
|
||||||
} else if (index < this->size && index >= 0) {
|
} else if (index < self->size && index >= 0) {
|
||||||
this->size += 1;
|
self->size += 1;
|
||||||
if (this->size >= this->capacity) {
|
if (self->size >= self->capacity) {
|
||||||
this->capacity *= 2;
|
self->capacity *= 2;
|
||||||
void *tmp = realloc(this->data, this->element_size * this->capacity);
|
void *tmp = realloc(self->data, self->element_size * self->capacity);
|
||||||
this->data = tmp;
|
self->data = tmp;
|
||||||
if (this->data == NULL) {
|
if (self->data == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (this->type) {
|
switch (self->type) {
|
||||||
case INT:
|
case INT:
|
||||||
// move all elements to the right by one
|
// move all elements to the right by one
|
||||||
for (int i = this->size-1; i > index; i--) {
|
for (int i = self->size-1; i > index; i--) {
|
||||||
*((int *)(this->data) + i) = *((int *)(this->data) + i-1);
|
*((int *)(self->data) + i) = *((int *)(self->data) + i-1);
|
||||||
}
|
}
|
||||||
// set the element at the given index
|
// set the element at the given index
|
||||||
*((int *)(this->data) + index) = *((int *)element);
|
*((int *)(self->data) + index) = *((int *)element);
|
||||||
break;
|
break;
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
for (int i = this->size-1; i > index; i--) {
|
for (int i = self->size-1; i > index; i--) {
|
||||||
*((float *)(this->data) + i) = *((float *)(this->data) + i-1);
|
*((float *)(self->data) + i) = *((float *)(self->data) + i-1);
|
||||||
}
|
}
|
||||||
*((float *)(this->data) + index) = *((float *)element);
|
*((float *)(self->data) + index) = *((float *)element);
|
||||||
break;
|
break;
|
||||||
case CHAR:
|
case CHAR:
|
||||||
for (int i = this->size-1; i > index; i--) {
|
for (int i = self->size-1; i > index; i--) {
|
||||||
*((char *)(this->data) + i) = *((char *)(this->data) + i-1);
|
*((char *)(self->data) + i) = *((char *)(self->data) + i-1);
|
||||||
}
|
}
|
||||||
*((char *)(this->data) + index) = *((char *)element);
|
*((char *)(self->data) + index) = *((char *)element);
|
||||||
break;
|
break;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
for (int i = this->size-1; i > index; i--) {
|
for (int i = self->size-1; i > index; i--) {
|
||||||
memcpy((vector_t *)(this->data) + i, (vector_t *)(this->data) + i-1, sizeof(vector_t));
|
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;
|
break;
|
||||||
case LIST:
|
case LIST:
|
||||||
for (int i = this->size-1; i > index; i--) {
|
for (int i = self->size-1; i > index; i--) {
|
||||||
memcpy((vector_t *)(this->data) + i, (list_t *)(this->data) + i-1, sizeof(list_t));
|
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;
|
break;
|
||||||
case BOOL:
|
case BOOL:
|
||||||
for (int i = this->size-1; i > index; i--) {
|
for (int i = self->size-1; i > index; i--) {
|
||||||
*((bool *)(this->data) + i) = *((bool *)(this->data) + i-1);
|
*((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;
|
break;
|
||||||
case STRING:
|
case STRING:
|
||||||
for (int i = this->size-1; i > index; i--) {
|
for (int i = self->size-1; i > index; i--) {
|
||||||
memcpy((vector_t *)(this->data) + i, (string_t *)(this->data) + i-1, sizeof(string_t));
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
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) {
|
void vector_remove_at(vector_t *self, int index) {
|
||||||
if (index < this->size && index >= 0 && this->size != 0) {
|
if (index < self->size && index >= 0 && self->size != 0) {
|
||||||
switch (this->type) {
|
switch (self->type) {
|
||||||
case INT:
|
case INT:
|
||||||
// move all elements to the right by one
|
// move all elements to the right by one
|
||||||
for (int i = index; i < this->size; i++) {
|
for (int i = index; i < self->size; i++) {
|
||||||
*((int *)(this->data) + i) = *((int *)(this->data) + i+1);
|
*((int *)(self->data) + i) = *((int *)(self->data) + i+1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
for (int i = index; i < this->size; i++) {
|
for (int i = index; i < self->size; i++) {
|
||||||
*((float *)(this->data) + i) = *((float *)(this->data) + i+1);
|
*((float *)(self->data) + i) = *((float *)(self->data) + i+1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case CHAR:
|
case CHAR:
|
||||||
for (int i = index; i < this->size; i++) {
|
for (int i = index; i < self->size; i++) {
|
||||||
*((char *)(this->data) + i) = *((char *)(this->data) + i+1);
|
*((char *)(self->data) + i) = *((char *)(self->data) + i+1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
for (int i = index; i < this->size; i++) {
|
for (int i = index; i < self->size; i++) {
|
||||||
memcpy((vector_t *)(this->data) + i, (vector_t *)(this->data) + i+1, sizeof(vector_t));
|
memcpy((vector_t *)(self->data) + i, (vector_t *)(self->data) + i+1, sizeof(vector_t));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case LIST:
|
case LIST:
|
||||||
for (int i = index; i < this->size; i++) {
|
for (int i = index; i < self->size; i++) {
|
||||||
memcpy((vector_t *)(this->data) + i, (list_t *)(this->data) + i+1, sizeof(list_t));
|
memcpy((vector_t *)(self->data) + i, (list_t *)(self->data) + i+1, sizeof(list_t));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BOOL:
|
case BOOL:
|
||||||
for (int i = index; i < this->size; i++) {
|
for (int i = index; i < self->size; i++) {
|
||||||
*((bool *)(this->data) + i) = *((bool *)(this->data) + i+1);
|
*((bool_t *)(self->data) + i) = *((bool_t *)(self->data) + i+1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case STRING:
|
case STRING:
|
||||||
for (int i = index; i < this->size; i++) {
|
for (int i = index; i < self->size; i++) {
|
||||||
memcpy((vector_t *)(this->data) + i, (string_t *)(this->data) + i+1, sizeof(string_t));
|
memcpy((vector_t *)(self->data) + i, (string_t *)(self->data) + i+1, sizeof(string_t));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->size -= 1;
|
self->size -= 1;
|
||||||
if (this->size < this->capacity/2) {
|
if (self->size < self->capacity/2) {
|
||||||
this->capacity /= 2;
|
self->capacity /= 2;
|
||||||
void *tmp = realloc(this->data, this->element_size * this->capacity);
|
void *tmp = realloc(self->data, self->element_size * self->capacity);
|
||||||
this->data = tmp;
|
self->data = tmp;
|
||||||
if (this->data == NULL) {
|
if (self->data == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user