Examples for using map containers.
Examples for using map containers.
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
};
}
const char* username_ptr = self->
username;
return hash;
}
fprintf(
stream,
"user_id@%p {\n", (
void*)self);
}
};
fprintf(
stream,
"userdata@%p {\n", (
void*)self);
}
#define KEY struct user_id
#define KEY_EQ user_id_eq
#define KEY_HASH user_id_hash
#define KEY_DEBUG user_id_debug
#define VALUE struct userdata
#define VALUE_DEBUG userdata_debug
#define NAME user_map
DC_SCOPED(user_map) map = user_map_new(stdalloc_get_ref());
struct user_id user1 = {.username =
"alice", .uuid = 1001};
struct user_id user2 = {.username =
"bob", .uuid = 1002};
struct userdata data1 = {.score = 1500, .hashed_password =
"hash1234567890"};
struct userdata data2 = {.score = 2000, .hashed_password =
"hash0987654321"};
user_map_insert(&map, user1, data1);
user_map_insert(&map, user2, data2);
struct userdata const* found_data1 = user_map_read(&map, user1);
struct userdata const* found_data2 = user_map_read(&map, user2);
struct user_id invalid_user = {.username =
"charlie", .uuid = 9999};
struct userdata const* not_found = user_map_try_read(&map, invalid_user);
fprintf(stdout, "\n");
}
};
fprintf(
stream,
"largedata@%p {\n", (
void*)self);
}
#define KEY uint64_t
#define KEY_HASH DC_DEFAULT_HASH
#define VALUE uint16_t
#define NAME uuid_to_index_map
DC_SCOPED(uuid_to_index_map) map = uuid_to_index_map_new(stdalloc_get_ref());
uuid_to_index_map_insert(&map, 1001, 0);
uuid_to_index_map_insert(&map, 1002, 1);
uuid_to_index_map_insert(&map, 1003, 2);
fprintf(stdout, "\n");
{.data = "First large data"}, {.data = "Second large data"}, {.data = "Third large data"}};
uint16_t index = *entry.value;
fprintf(stdout, "\n");
}
}
#define KEY uint64_t
#define KEY_HASH DC_DEFAULT_HASH
#define VALUE struct largedata
#define VALUE_DEBUG largedata_debug
#define NAME ankerl_largedata_map
DC_SCOPED(ankerl_largedata_map) map = ankerl_largedata_map_new(stdalloc_get_ref());
struct largedata data1 = {.data =
"Ankerl map entry number one"};
struct largedata data2 = {.data =
"Ankerl map entry number two"};
struct largedata data3 = {.data =
"Ankerl map entry number three"};
ankerl_largedata_map_insert(&map, 2001, data1);
ankerl_largedata_map_insert(&map, 2002, data2);
ankerl_largedata_map_insert(&map, 2003, data3);
size_t count = 0;
printf("Entry %zu: uuid=%" PRIu64 " data=", count, *entry.key);
fprintf(stdout, "\n");
count++;
}
fprintf(stdout, "\n");
}
return 0;
}
static void example_basic()
static DC_PUBLIC size_t dc_hash_combine(size_t seed, size_t h)
#define DC_DEFAULT_HASH(obj)
static DC_PUBLIC void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_new()
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
static DC_PUBLIC uint64_t dc_fnv1a_str_const(const char *const *s)
#define DC_FOR_CONST(TYPE, INSTANCE, ITER, ITEM)
static void largedata_debug(struct largedata const *self, dc_debug_fmt fmt, FILE *stream)
static void example_small()
static void example_iteration()
static size_t user_id_hash(struct user_id const *self)
static void userdata_debug(struct userdata const *self, dc_debug_fmt fmt, FILE *stream)
static void user_id_debug(struct user_id const *self, dc_debug_fmt fmt, FILE *stream)
static bool user_id_eq(struct user_id const *a, struct user_id const *b)
#define DC_ASSERT(expr,...)
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Debug format helpers for debug printin data structures.
static DC_PUBLIC FILE * stream(SELF *self)