libcdsb/examples/dict.c

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(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;
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);
}