Examples for inserting, iterating, and deleting hashmaps.
Examples for inserting, iterating, and deleting hashmaps.
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KEY uint32_t
#define KEY_EQ uint32_t_eq
#define KEY_HASH hash_id_uint32_t
#define VALUE char const*
#define NAME id_to_name
printf("Map has items:\n");
size_t pos = 0;
printf("position: %zu key: %" PRIu32 " string: %s\n", pos, *entry.key, *entry.value);
pos++;
}
}
printf("Id to Name Map Example:\n");
id_to_name map = id_to_name_new(stdalloc_get());
id_to_name_insert(&map, 23, "hello");
id_to_name_insert(&map, 10, "bob");
id_to_name_insert(&map, 42, "meaning");
DC_ASSERT(strcmp(*id_to_name_read(&map, 42),
"meaning") == 0);
char const** entry = id_to_name_write(&map, 23);
*entry = "a different string!";
id_to_name_delete(&map);
}
};
(void)fmt;
fprintf(
stream,
" report_id@%p { name: \"%s\", section: %d}", self, self->
name, self->
section);
}
}
}
};
(void)fmt;
fprintf(
stream,
" report@%p { description: \"%s\", value: %d}", self, self->
description,
}
#define KEY struct report_id
#define KEY_EQ report_id_equality
#define KEY_HASH report_id_hash
#define KEY_DELETE report_id_delete
#define KEY_DEBUG report_id_debug
#define VALUE struct report
#define VALUE_DELETE report_delete
#define VALUE_DEBUG report_debug
#define NAME report_map
printf("Report Map Example:\n");
report_map map = report_map_new(stdalloc_get());
struct report_id id1 = {.name = strdup(
"Report A"), .section = 1};
struct report_id id2 = {.name = strdup(
"Report B"), .section = 2};
report_map_insert(&map, id1,
(
struct report){.description = strdup(
"Description A"), .value = 100});
report_map_insert(&map, id2,
(
struct report){.description = strdup(
"Description B"), .value = 200});
DC_ASSERT(strcmp(report_map_read(&map, id1)->description,
"Description A") == 0);
{
size_t pos = 0;
printf("Position: %zu Key: %s Section: %u Value: %d\n", pos, entry.key->name,
entry.key->section, entry.value->value);
pos++;
}
}
struct report entry = report_map_remove(&map, id1);
report_map_delete(&map);
}
};
(void)fmt;
fprintf(
stream,
"fixed_string@%p { value: \"%.*s\" }", self, 4, self->
value);
}
}
return hash_murmurhash_string_4(str->
value);
}
#define KEY struct fixed_string
#define KEY_EQ fixed_string_eq
#define KEY_HASH fixed_string_hash
#define KEY_DEBUG fixed_string_debug
#define VALUE uint32_t
#define NAME fixed_string_map
printf("Fixed Strings Example:\n");
fixed_string_map map = fixed_string_map_new(stdalloc_get());
fixed_string_map_insert(&map, key1, 123);
fixed_string_map_insert(&map, key2, 456);
fixed_string_map_insert(&map, key3, 789);
DC_ASSERT(*fixed_string_map_read(&map, key1) == 123);
DC_ASSERT(*fixed_string_map_read(&map, key2) == 456);
DC_ASSERT(*fixed_string_map_read(&map, key3) == 789);
size_t pos = 0;
FOR_CONST(fixed_string_map, &map, iter, entry) {
printf(
"Position: %zu Key: %.3s Value: %u\n", pos, entry.key->
value, *entry.
value);
pos++;
}
fixed_string_map_delete(&map);
}
}
static void free(SELF *self, void *ptr)
void report_debug(struct report const *self, dc_debug_fmt fmt, FILE *stream)
void id_to_name_example()
void print_map(id_to_name const *map)
void report_delete(struct report *self)
void report_id_debug(struct report_id const *self, dc_debug_fmt fmt, FILE *stream)
void report_map_example()
void fixed_string_debug(struct fixed_string const *self, dc_debug_fmt fmt, FILE *stream)
bool report_id_equality(struct report_id const *report_1, struct report_id const *report_2)
void fixed_string_example()
bool fixed_string_eq(struct fixed_string const *str1, struct fixed_string const *str2)
void report_id_delete(struct report_id *self)
size_t report_id_hash(struct report_id const *report_id)
size_t fixed_string_hash(struct fixed_string const *str)
static dc_debug_fmt dc_debug_fmt_new()
#define FOR_CONST(TYPE, INSTANCE, ITER, ITEM)
size_t hash_murmurhash_string(const char *str)
static size_t hash_combine(size_t seed, size_t h)
#define DC_ASSERT(expr,...)
Debug format helpers for debug printin data structures.
static FILE * stream(SELF *self)
Opens a file for.