libcdsb/examples/list.c

52 lines
1.1 KiB
C
Raw Normal View History

2022-08-16 21:11:07 +03:00
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include <stdio.h>
#include <stdlib.h>
#include "../include/list.h"
2022-08-16 21:11:07 +03:00
typedef vtype_list list_t;
2023-03-23 18:18:51 +03:00
int print_value(vtype_variable value, ssize_t index, void* data) {
2022-08-16 21:11:07 +03:00
const char *n = data;
2023-03-23 18:18:51 +03:00
switch (value.type) {
2022-08-16 21:11:07 +03:00
default: abort();
case VTYPE_INT32:
2023-03-23 18:18:51 +03:00
printf("%s %d (index: %ld)\n", n, *(vtype_int32*)value.pointer, index);
2022-08-16 21:11:07 +03:00
break;
case VTYPE_FLOAT:
2023-03-23 18:18:51 +03:00
printf("%s %f (index: %ld)\n", n, *(vtype_float*)value.pointer, index);
2022-08-16 21:11:07 +03:00
break;
}
return 0;
}
int main(int argc, char** argv) {
list_t list;
vtype_float fl = 0.0;
list_init(&list);
for (vtype_int32 i = 0; i < 28; ++i) {
2022-08-16 21:11:07 +03:00
if (i%2) {
list_push_back(&list, i);
2022-08-16 21:11:07 +03:00
} else {
list_push_back(&list, fl);
2022-08-16 21:11:07 +03:00
fl += 0.05;
}
}
list_get_by_index(&list, 13, "Get value:", print_value);
list_pop_by_index(&list, 18, "Pop value:", print_value);
list_foreach(&list, "Foreach loop:", print_value);
list_free(&list);
}