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

56 lines
1.2 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/map.h"
typedef vtype_map map_t;
int print_value(const void* key, vtype key_type, void* value, vtype value_type, void* data) {
const char *n = data;
vtype_int32 *k = (void*)key;
assert(key_type == VTYPE_INT32);
printf("%s %d: ", n, *k);
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) {
map_t map;
vtype_float fl = 0.0;
map_init(&map, VTYPE_INT32);
for (size_t i = 0; i < 28; ++i) {
if (i%2) {
map_update(&map, i, (vtype_int32)i);
} else {
map_update(&map, i, (vtype_float)fl);
fl += 0.05;
}
}
map_get(&map, 13, "Get value:", print_value);
map_pop(&map, 18, "Pop value:", print_value);
map_foreach(&map, "Foreach loop:", print_value);
map_free(&map);
}