libcdsb/examples/dict.c
2022-08-16 21:11:07 +03:00

62 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/extra/dict.h"
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;
dict_init(&dict);
for (size_t i = 0; i < 28; ++i) {
if (i%2) {
dict_update(&dict, (vtype_float)fl, (vtype_int32)i);
} else {
dict_update(&dict, (vtype_int32)i, (vtype_float)fl);
}
fl += 0.05;
}
dict_get(&dict, 14, "Get value:", print_value);
dict_pop(&dict, 0.25, "Pop value:", print_value);
dict_foreach(&dict, "Foreach loop:", print_value);
dict_free(&dict);
}