diff --git a/README.md b/README.md index 9bb02c0..5a9a490 100644 --- a/README.md +++ b/README.md @@ -36,12 +36,11 @@ Examples of this work of art are in the `/examples/` directory of this repo. 5. Pushing a value at the end of a vector 6. Inserting a value into a vector at a given index 7. Insertion of a vector into another vector +8. Vectors of lists +9. Remove a value at a given index #### TODO: -1. Vectors of lists -2. Remove a value at a given index - + You tell me ## Lists (vectors with dynamic types) @@ -56,11 +55,11 @@ Examples of this work of art are in the `/examples/` directory of this repo. 6. Vectors inside of a list 7. Inserting at a given index 8. Lists of lists +9. Remove at a given index #### TODO: 1. Get/set at a given index -2. Remove at a given index + You tell me diff --git a/demo.c b/demo.c index 9afaf39..cdb6869 100644 --- a/demo.c +++ b/demo.c @@ -1,41 +1,30 @@ #include -#include #include int main() { - list_t* another_list = listInit(); - - listPush(another_list, CHAR, &(char){'x'}); - listPush(another_list, INT, &(int){123}); - listPush(another_list, FLOAT, &(float){3.14}); - printf("do not put the list into itself, it does weird stuff\n"); - // BIG NONO -> listPush(another_list, LIST, another_list); - - listRemoveAt(another_list, 1); - list_t* l = listInit(); - listPush(l, INT, &(int){5+2}); - listPush(l, INT, &(int){5*2}); - listPush(l, FLOAT, &(float){5/2}); + listPush(l, INT, &(int){5}); + listPush(l, INT, &(int){2}); + listPush(l, INT, &(int){1}); - listPush(another_list, LIST, l); + listSetAt(l, 1, FLOAT, &(float){3.14}); - listPrint(another_list); + printf("value at index 2 in list l:\n"); + int val = *((int*)listGetAt(l, 2)); + printf("%d\n", val); - listCleanup(another_list); + list_t* m = listInit(); - // vector_t* v = vectorInit(INT); - // - // vectorPush(v, &(int){123}); - // vectorPush(v, &(int){456}); - // vectorPush(v, &(int){789}); - // - // vectorRemoveAt(v, 1); - // - // vectorPrint(v); - // - // vectorCleanup(v); + listPush(m, CHAR, &(char){'a'}); + listPush(m, CHAR, &(char){'b'}); + listPush(m, CHAR, &(char){'c'}); + + listSetAt(l, 2, LIST, m); + + listPrint(l); + + listCleanup(l); return 0; } diff --git a/include/list.h b/include/list.h index 18a75f8..a47fcdb 100644 --- a/include/list.h +++ b/include/list.h @@ -30,3 +30,7 @@ void* listPop(list_t* l); // p void listInsertAt(list_t* l, int index, dataType_e type, void* element); // inserts an element at a given index void listRemoveAt(list_t* l, int index); + +void listSetAt(list_t* l, int index, dataType_e type, void* element); + +void* listGetAt(list_t* l, int index); diff --git a/src/list.c b/src/list.c index eeea065..4af6a2b 100644 --- a/src/list.c +++ b/src/list.c @@ -223,3 +223,82 @@ listRemoveAt(list_t* l, int index) { l->capacity /= 2; } } + +void +listSetAt(list_t* l, int index, dataType_e type, void* element) { + if (l->size == 0) { + listPush(l, type, element); + } else if (index >= l->size || index < 0) { + fprintf(stderr, "index out of range\nmust be between 0 and %d, is %d\n", l->size-1, index); + exit(EXIT_FAILURE); + } + + switch (type) { + case INT: + listRemoveAt(l, index); + if (index == l->size) { + l->data->size += 1; + if (l->data->size == l->data->capacity) { + l->data->capacity *= 2; + } + } + listInsertAt(l, index, INT, element); + break; + case FLOAT: + listRemoveAt(l, index); + if (index == l->size) { + l->data->size += 1; + if (l->data->size == l->data->capacity) { + l->data->capacity *= 2; + } + } + listInsertAt(l, index, FLOAT, element); + break; + case CHAR: + listRemoveAt(l, index); + if (index == l->size) { + l->data->size += 1; + if (l->data->size == l->data->capacity) { + l->data->capacity *= 2; + } + } + listInsertAt(l, index, CHAR, element); + break; + case VECTOR: + listRemoveAt(l, index); + if (index == l->size) { + l->data->size += 1; + if (l->data->size == l->data->capacity) { + l->data->capacity *= 2; + } + } + listInsertAt(l, index, VECTOR, element); + break; + case LIST: + listRemoveAt(l, index); + if (index == l->size) { + l->data->size += 1; + if (l->data->size == l->data->capacity) { + l->data->capacity *= 2; + } + } + listInsertAt(l, index, LIST, element); + break; + default: + fprintf(stderr, "specify a valid vector type\n"); + exit(EXIT_FAILURE); + } +} + +void* +listGetAt(list_t* l, int index) { + if (l->size == 0) { + fprintf(stderr, "list is empty\n"); + exit(EXIT_FAILURE); + } else if (index >= l->size || index < 0) { + fprintf(stderr, "index out of range\n"); + exit(EXIT_FAILURE); + } + + return vectorGetAt((vector_t*)vectorGetAt(l->data, index), 0); +} diff --git a/src/vector.c b/src/vector.c index f9e8ae8..9581507 100644 --- a/src/vector.c +++ b/src/vector.c @@ -381,8 +381,8 @@ void vectorInsertAt(vector_t* v, int index, void* element) { if (v->size == 0) { vectorPush(v, element); - } else if (index >= v->size) { - fprintf(stderr, "index out of range\n"); + } else if (index >= v->size || index < 0) { + fprintf(stderr, "index out of range\nmust be between 0 and %d, is %d\n", v->size-1, index); exit(EXIT_FAILURE); }