libcdsb/examples/map.c

60 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/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;
int a, b;
map_init(&map, VTYPE_INT32);
for (vtype_int32 i = 0; i < 28; ++i) {
if (i%2) {
map_update(&map, i, i);
} else {
map_update(&map, i, fl);
fl += 0.05;
}
}
a = 13;
b = 18;
map_get(&map, a, "Get value:", print_value);
map_pop(&map, b, "Pop value:", print_value);
map_foreach(&map, "Foreach loop:", print_value);
map_free(&map);
}