initial list.h commit
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
CC := gcc
|
CC := gcc
|
||||||
CFLAGS := -Wall -Werror
|
CFLAGS := -Wall -Werror
|
||||||
LIBS := -lvector -lfile
|
LIBS := -lvector -lfile -llist
|
||||||
LIBSPATH := -L/usr/lib/odweta/
|
LIBSPATH := -L/usr/lib/odweta/
|
||||||
INCLUDEDIR := -I/usr/include/odweta/
|
INCLUDEDIR := -I/usr/include/odweta/
|
||||||
PATCH := -Wl,-rpath=/usr/lib/odweta/
|
PATCH := -Wl,-rpath=/usr/lib/odweta/
|
||||||
@@ -9,7 +9,7 @@ PATCH := -Wl,-rpath=/usr/lib/odweta/
|
|||||||
|
|
||||||
default: demo
|
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)
|
sudo $(CC) $(CFLAGS) -o $@ $< $(PATCH) $(INCLUDEDIR) $(LIBSPATH) $(LIBS)
|
||||||
|
|
||||||
libvector.so: src/vector.c
|
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) -c -fpic $< -o /usr/lib/odweta/file.o
|
||||||
sudo $(CC) -shared -o /usr/lib/odweta/$@ /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:
|
install:
|
||||||
sudo mkdir -p /usr/include/odweta/
|
sudo mkdir -p /usr/include/odweta/
|
||||||
sudo mkdir -p /usr/lib/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 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
|
sudo make libvector.so libfile.so liblist.so
|
||||||
sudo ldconfig # update ldconfig cache
|
sudo ldconfig # update ldconfig cache
|
||||||
|
|
||||||
clean:
|
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:
|
cleanrepo:
|
||||||
rm -fr ./demo
|
rm -fr ./demo
|
||||||
|
|||||||
@@ -36,11 +36,19 @@ I got bored with having to use complicated checking and stuff when working with
|
|||||||
|
|
||||||
#### What works so far:
|
#### What works so far:
|
||||||
|
|
||||||
+ not yet implemented
|
+ Creating a list
|
||||||
|
+ Pushing into a list
|
||||||
|
+ Printing a list
|
||||||
|
+ Cleanup of a list
|
||||||
|
|
||||||
#### TODO:
|
#### 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
|
## Installation
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
#include <odweta/vector.h>
|
#include <odweta/list.h>
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
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});
|
listPrint(l);
|
||||||
vectorPush(floatv, &(float){55.5});
|
|
||||||
|
|
||||||
vectorPush(v, floatv);
|
listCleanup(l);
|
||||||
|
|
||||||
vectorPrint(v);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 <odweta/vector.h>
|
||||||
|
|
||||||
|
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
|
||||||
+7
-1
@@ -51,4 +51,10 @@ vectorToString(vector_t* v); // convert the vector into a regular string
|
|||||||
void*
|
void*
|
||||||
vectorGetAt(vector_t* v, int index); // get an element at the given index
|
vectorGetAt(vector_t* v, int index); // get an element at the given index
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void _printVectorRecursive(vector_t* v, int depth);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
+108
@@ -0,0 +1,108 @@
|
|||||||
|
#include <odweta/list.h>
|
||||||
|
#include <odweta/vector.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define init_error_exit(l) fprintf(stderr, "could not init list\n"); free(l); exit(EXIT_FAILURE)
|
||||||
|
|
||||||
|
list_t*
|
||||||
|
listInit() {
|
||||||
|
list_t* l = malloc(sizeof(list_t));
|
||||||
|
if (!l) {
|
||||||
|
init_error_exit(l);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user