made work: get/set at index in list
This commit is contained in:
@@ -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
|
5. Pushing a value at the end of a vector
|
||||||
6. Inserting a value into a vector at a given index
|
6. Inserting a value into a vector at a given index
|
||||||
7. Insertion of a vector into another vector
|
7. Insertion of a vector into another vector
|
||||||
|
8. Vectors of lists
|
||||||
|
9. Remove a value at a given index
|
||||||
|
|
||||||
#### TODO:
|
#### TODO:
|
||||||
|
|
||||||
1. Vectors of lists
|
|
||||||
2. Remove a value at a given index
|
|
||||||
|
|
||||||
+ You tell me
|
+ You tell me
|
||||||
|
|
||||||
## Lists (vectors with dynamic types)
|
## 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
|
6. Vectors inside of a list
|
||||||
7. Inserting at a given index
|
7. Inserting at a given index
|
||||||
8. Lists of lists
|
8. Lists of lists
|
||||||
|
9. Remove at a given index
|
||||||
|
|
||||||
#### TODO:
|
#### TODO:
|
||||||
|
|
||||||
1. Get/set at a given index
|
1. Get/set at a given index
|
||||||
2. Remove at a given index
|
|
||||||
|
|
||||||
+ You tell me
|
+ You tell me
|
||||||
|
|
||||||
|
|||||||
@@ -1,41 +1,30 @@
|
|||||||
#include <odweta/list.h>
|
#include <odweta/list.h>
|
||||||
#include <odweta/vector.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int main() {
|
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();
|
list_t* l = listInit();
|
||||||
|
|
||||||
listPush(l, INT, &(int){5+2});
|
listPush(l, INT, &(int){5});
|
||||||
listPush(l, INT, &(int){5*2});
|
listPush(l, INT, &(int){2});
|
||||||
listPush(l, FLOAT, &(float){5/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);
|
listPush(m, CHAR, &(char){'a'});
|
||||||
//
|
listPush(m, CHAR, &(char){'b'});
|
||||||
// vectorPush(v, &(int){123});
|
listPush(m, CHAR, &(char){'c'});
|
||||||
// vectorPush(v, &(int){456});
|
|
||||||
// vectorPush(v, &(int){789});
|
listSetAt(l, 2, LIST, m);
|
||||||
//
|
|
||||||
// vectorRemoveAt(v, 1);
|
listPrint(l);
|
||||||
//
|
|
||||||
// vectorPrint(v);
|
listCleanup(l);
|
||||||
//
|
|
||||||
// vectorCleanup(v);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 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 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);
|
||||||
|
|||||||
+79
@@ -223,3 +223,82 @@ listRemoveAt(list_t* l, int index) {
|
|||||||
l->capacity /= 2;
|
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);
|
||||||
|
}
|
||||||
|
|||||||
+2
-2
@@ -381,8 +381,8 @@ void
|
|||||||
vectorInsertAt(vector_t* v, int index, void* element) {
|
vectorInsertAt(vector_t* v, int index, void* element) {
|
||||||
if (v->size == 0) {
|
if (v->size == 0) {
|
||||||
vectorPush(v, element);
|
vectorPush(v, element);
|
||||||
} else if (index >= v->size) {
|
} else if (index >= v->size || index < 0) {
|
||||||
fprintf(stderr, "index out of range\n");
|
fprintf(stderr, "index out of range\nmust be between 0 and %d, is %d\n", v->size-1, index);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user