libcdsb/examples/map.c
2023-03-23 18:18:51 +03:00

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(vtype_variable key, vtype_variable value, void* data) {
const char *n = data;
vtype_int32 *k = (void*)key.pointer;
assert(key.type == VTYPE_INT32);
printf("%s %d: ", n, *k);
switch (value.type) {
default: abort();
case VTYPE_INT32:
printf("%d\n", *(vtype_int32*)value.pointer);
break;
case VTYPE_FLOAT:
printf("%f\n", *(vtype_float*)value.pointer);
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);
}