libcdsb/examples/dict.c
2023-03-23 18:18:51 +03:00

67 lines
1.4 KiB
C

/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "../include/dict.h"
typedef vtype_dict dict_t;
int print_value(vtype_variable key, vtype_variable value, void* data) {
const char *n = data;
switch (key.type) {
default: abort();
case VTYPE_INT32:
printf("%s %d: ", n, *(vtype_int32*)key.pointer);
break;
case VTYPE_FLOAT:
printf("%s %f: ", n, *(vtype_float*)key.pointer);
break;
}
switch (value.type) {
default: abort();
case VTYPE_INT32:
printf("%d\n", *(vtype_int32*)value.pointer);
break;
case VTYPE_FLOAT:
printf("%f\n", *(vtype_float*)value.pointer);
break;
}
return 0;
}
int main(int argc, char** argv) {
dict_t dict;
vtype_float fl = 0.0;
int a;
vtype_float b;
dict_init(&dict);
for (vtype_int32 i = 0; i < 28; ++i) {
if (i%2) {
dict_update(&dict, fl, i);
} else {
dict_update(&dict, i, fl);
}
fl += 0.05;
}
a = 13;
b = 0.25;
dict_get(&dict, a, "Get value:", print_value);
dict_pop(&dict, b, "Pop value:", print_value);
dict_foreach(&dict, "Foreach loop:", print_value);
dict_free(&dict);
}