libcdsb/examples/dict.c

67 lines
1.4 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 <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "../include/dict.h"
2022-08-16 21:11:07 +03:00
typedef vtype_dict dict_t;
int print_value(const void* key, vtype key_type, void* value, vtype value_type, void* data) {
const char *n = data;
switch (key_type) {
default: abort();
case VTYPE_INT32:
printf("%s %d: ", n, *(vtype_int32*)key);
break;
case VTYPE_FLOAT:
printf("%s %f: ", n, *(vtype_float*)key);
break;
}
switch (value_type) {
default: abort();
case VTYPE_INT32:
printf("%d\n", *(vtype_int32*)value);
break;
case VTYPE_FLOAT:
printf("%f\n", *(vtype_float*)value);
break;
}
return 0;
}
int main(int argc, char** argv) {
dict_t dict;
vtype_float fl = 0.0;
int a;
vtype_float b;
2022-08-16 21:11:07 +03:00
dict_init(&dict);
for (vtype_int32 i = 0; i < 28; ++i) {
2022-08-16 21:11:07 +03:00
if (i%2) {
dict_update(&dict, fl, i);
2022-08-16 21:11:07 +03:00
} else {
dict_update(&dict, i, fl);
2022-08-16 21:11:07 +03:00
}
fl += 0.05;
}
a = 13;
b = 0.25;
dict_get(&dict, a, "Get value:", print_value);
dict_pop(&dict, b, "Pop value:", print_value);
2022-08-16 21:11:07 +03:00
dict_foreach(&dict, "Foreach loop:", print_value);
dict_free(&dict);
}