diff --git a/Makefile b/Makefile index d6d1775..7739fd1 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC := gcc CFLAGS := -Wall -Werror -LIBS := -lvector -lfile +LIBS := -lvector -lfile -llist LIBSPATH := -L/usr/lib/odweta/ INCLUDEDIR := -I/usr/include/odweta/ PATCH := -Wl,-rpath=/usr/lib/odweta/ @@ -9,7 +9,7 @@ PATCH := -Wl,-rpath=/usr/lib/odweta/ default: demo -demo: demo.c libvector.so libfile.so +demo: demo.c libvector.so libfile.so liblist.so sudo $(CC) $(CFLAGS) -o $@ $< $(PATCH) $(INCLUDEDIR) $(LIBSPATH) $(LIBS) libvector.so: src/vector.c @@ -20,15 +20,18 @@ libfile.so: src/file.c sudo $(CC) -c -fpic $< -o /usr/lib/odweta/file.o sudo $(CC) -shared -o /usr/lib/odweta/$@ /usr/lib/odweta/file.o +liblist.so: src/list.c + sudo $(CC) -c -fpic $< -o /usr/lib/odweta/list.o + sudo $(CC) -shared -o /usr/lib/odweta/$@ /usr/lib/odweta/list.o install: sudo mkdir -p /usr/include/odweta/ sudo mkdir -p /usr/lib/odweta/ - sudo cp src/vector.c src/file.c include/vector.h include/file.h /usr/include/odweta/ - sudo make libvector.so libfile.so + sudo cp src/vector.c src/file.c src/list.c include/vector.h include/file.h include/list.h /usr/include/odweta/ + sudo make libvector.so libfile.so liblist.so sudo ldconfig # update ldconfig cache clean: - sudo rm -f main /usr/lib/odweta/vector.o /usr/lib/odweta/file.o /usr/lib/odweta/libvector.so /usr/lib/odweta/libfile.so + sudo rm -f main /usr/lib/odweta/vector.o /usr/lib/odweta/file.o /usr/lib/odweta/libvector.so /usr/lib/odweta/libfile.so /usr/lib/odweta/list.o /usr/lib/odweta/liblist.so cleanrepo: rm -fr ./demo diff --git a/README.md b/README.md index 6eea58e..960e726 100644 --- a/README.md +++ b/README.md @@ -36,11 +36,19 @@ I got bored with having to use complicated checking and stuff when working with #### What works so far: -+ not yet implemented ++ Creating a list ++ Pushing into a list ++ Printing a list ++ Cleanup of a list #### TODO: -+ everything ++ Inserting at a given index ++ Verify multi-dimensional lists working ++ Get/set at a given index ++ Popping ++ Remove at a given index ++ Verify vectors working inside of lists ## Installation diff --git a/demo.c b/demo.c index 6cbfe7b..905112e 100644 --- a/demo.c +++ b/demo.c @@ -1,16 +1,15 @@ -#include +#include int main(int argc, char **argv) { - vector_t *v = vectorInit(VECTOR); + list_t *l = listInit(); - vector_t *floatv = vectorInit(FLOAT); + listPush(l, FLOAT, &(float){3.14}); + listPush(l, CHAR, &(char){'x'}); + listPush(l, INT, &(int){123}); - vectorPush(floatv, &(float){3.14}); - vectorPush(floatv, &(float){55.5}); + listPrint(l); - vectorPush(v, floatv); - - vectorPrint(v); + listCleanup(l); return 0; } diff --git a/include/list.h b/include/list.h new file mode 100644 index 0000000..efa7897 --- /dev/null +++ b/include/list.h @@ -0,0 +1,26 @@ +/* + * What is this? + * + * A list (as I interpret it in this 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 + */ + +#include + +typedef long unsigned int size_t; + +typedef struct { + size_t size; // number of elements that are inside the list + size_t capacity; // how many elements can be put inside + vector_t* data; // vector of vectors +} list_t; + +list_t* listInit(); // returns a new list + +void listPush(list_t* l, dataType_e type, void* element); // appends an element at the end of a list + +void listPrint(list_t* l); // prints a list + +void listCleanup(list_t* l); // freeing pointers diff --git a/include/vector.h b/include/vector.h index 55dc37f..b473d0b 100644 --- a/include/vector.h +++ b/include/vector.h @@ -51,4 +51,10 @@ vectorToString(vector_t* v); // convert the vector into a regular string void* vectorGetAt(vector_t* v, int index); // get an element at the given index -#endif \ No newline at end of file + + + + +void _printVectorRecursive(vector_t* v, int depth); + +#endif diff --git a/src/list.c b/src/list.c new file mode 100644 index 0000000..5fc1e85 --- /dev/null +++ b/src/list.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include + +#define init_error_exit(l) fprintf(stderr, "could not init list\n"); free(l); exit(EXIT_FAILURE) + +list_t* +listInit() { + list_t* l = malloc(sizeof(list_t)); + if (!l) { + init_error_exit(l); + } + + l->data = (vector_t *)vectorInit(VECTOR); + if (!l->data) { + init_error_exit(l); + } + + l->size = 0; + l->capacity = 1; + + return l; +} + +void +listPush(list_t* l, dataType_e type, void* element) { + vector_t* newVec; + switch (type) { + case INT: + newVec = vectorInit(INT); + vectorPush(newVec, element); + vectorPush((vector_t *)l->data, newVec); + break; + case FLOAT: + newVec = vectorInit(FLOAT); + vectorPush(newVec, element); + vectorPush((vector_t *)l->data, newVec); + break; + case CHAR: + newVec = vectorInit(CHAR); + vectorPush(newVec, element); + vectorPush((vector_t *)l->data, newVec); + break; + case VECTOR: + // Instead of reallocating memory for vector_t pointers, allocate memory for the vector data + if (l->size == 0) { + // Allocate memory for the vector data for the first time + l->data = malloc(sizeof(vector_t) * l->capacity); + } else { + // Reallocate memory for the vector data + l->data = realloc((vector_t *)l->data, sizeof(vector_t) * l->capacity); + } + if (!l->data) { + perror("failed to allocate memory for vector data\n"); + exit(EXIT_FAILURE); + } + + // Copy the vector data from the element to the vector + memcpy((vector_t *)(l->data) + l->size, (vector_t *)element, sizeof(vector_t)); + + break; + default: + perror("specify a valid vector type\n"); + exit(EXIT_FAILURE); + } + + l->size += 1; + if (l->size == l->capacity) { + l->capacity *= 2; + } +} + +void +listPrint(list_t* l) { + fprintf(stdout, "["); + int i; + for (i = 0; i < l->size; i++) { + vector_t *ithVec = (vector_t *)vectorGetAt((vector_t *)l->data, i); + + switch (ithVec->type) { + case INT: + printf("%d", *((int *)vectorGetAt(ithVec, 0))); + break; + case FLOAT: + printf("%f", *((float *)vectorGetAt(ithVec, 0))); + break; + case CHAR: + printf("'%c'", *((char *)vectorGetAt(ithVec, 0))); + break; + case VECTOR: + _printVectorRecursive((vector_t *)l->data, 0); + break; + } + + if (i < l->size - 1) { + fprintf(stdout, ", "); + } + } + fprintf(stdout, "]\n"); +} + +void +listCleanup(list_t* l) { + vectorCleanup((vector_t *)l->data); + free(l); +}