Derive-C
Loading...
Searching...
No Matches
map.c File Reference
#include <derive-c/alloc/std.h>
#include <derive-c/algorithm/hash/default.h>
#include <derive-c/algorithm/hash/combine.h>
#include <derive-c/algorithm/hash/fnv1a.h>
#include <derive-c/prelude.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <derive-c/container/map/decomposed/template.h>
#include <derive-c/container/map/ankerl/template.h>

Go to the source code of this file.

Data Structures

struct  user_id
struct  userdata
struct  largedata

Macros

#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
#define KEY   uint64_t
#define KEY_HASH   DC_DEFAULT_HASH
#define VALUE   uint16_t
#define NAME   uuid_to_index_map
#define KEY   uint64_t
#define KEY_HASH   DC_DEFAULT_HASH
#define VALUE   struct largedata
#define VALUE_DEBUG   largedata_debug
#define NAME   ankerl_largedata_map

Functions

static bool user_id_eq (struct user_id const *a, struct user_id const *b)
static size_t user_id_hash (struct user_id const *self)
static void user_id_debug (struct user_id const *self, dc_debug_fmt fmt, FILE *stream)
static void userdata_debug (struct userdata const *self, dc_debug_fmt fmt, FILE *stream)
static void example_basic ()
static void largedata_debug (struct largedata const *self, dc_debug_fmt fmt, FILE *stream)
static void example_small ()
static void example_iteration ()
int main ()

Macro Definition Documentation

◆ KEY [1/3]

#define KEY   uint64_t

Definition at line 55 of file map.c.

◆ KEY [2/3]

#define KEY   uint64_t

Definition at line 55 of file map.c.

◆ KEY [3/3]

#define KEY   struct user_id

Definition at line 55 of file map.c.

◆ KEY_DEBUG

#define KEY_DEBUG   user_id_debug

Definition at line 58 of file map.c.

◆ KEY_EQ

#define KEY_EQ   user_id_eq

Definition at line 56 of file map.c.

◆ KEY_HASH [1/3]

#define KEY_HASH   DC_DEFAULT_HASH

Definition at line 57 of file map.c.

◆ KEY_HASH [2/3]

#define KEY_HASH   DC_DEFAULT_HASH

Definition at line 57 of file map.c.

◆ KEY_HASH [3/3]

#define KEY_HASH   user_id_hash

Definition at line 57 of file map.c.

◆ NAME [1/3]

#define NAME   ankerl_largedata_map

Definition at line 61 of file map.c.

◆ NAME [2/3]

#define NAME   uuid_to_index_map

Definition at line 61 of file map.c.

◆ NAME [3/3]

#define NAME   user_map

Definition at line 61 of file map.c.

◆ VALUE [1/3]

#define VALUE   struct largedata

Definition at line 59 of file map.c.

◆ VALUE [2/3]

#define VALUE   uint16_t

Definition at line 59 of file map.c.

◆ VALUE [3/3]

#define VALUE   struct userdata

Definition at line 59 of file map.c.

◆ VALUE_DEBUG [1/2]

#define VALUE_DEBUG   largedata_debug

Definition at line 60 of file map.c.

◆ VALUE_DEBUG [2/2]

#define VALUE_DEBUG   userdata_debug

Definition at line 60 of file map.c.

Function Documentation

◆ example_basic()

void example_basic ( )
static

Definition at line 64 of file map.c.

64 {
66 DC_SCOPED(user_map) map = user_map_new(stdalloc_get_ref());
67
68 struct user_id user1 = {.username = "alice", .uuid = 1001};
69 struct user_id user2 = {.username = "bob", .uuid = 1002};
70
71 struct userdata data1 = {.score = 1500, .hashed_password = "hash1234567890"};
72 struct userdata data2 = {.score = 2000, .hashed_password = "hash0987654321"};
73
74 user_map_insert(&map, user1, data1);
75 user_map_insert(&map, user2, data2);
76
77 struct userdata const* found_data1 = user_map_read(&map, user1);
78 DC_ASSERT(found_data1 != NULL);
79 DC_ASSERT(found_data1->score == 1500);
80 DC_ASSERT(strncmp(found_data1->hashed_password, "hash1234567890", 16) == 0);
81
82 struct userdata const* found_data2 = user_map_read(&map, user2);
83 DC_ASSERT(found_data2 != NULL);
84 DC_ASSERT(found_data2->score == 2000);
85
86 struct user_id invalid_user = {.username = "charlie", .uuid = 9999};
87 struct userdata const* not_found = user_map_try_read(&map, invalid_user);
88 DC_ASSERT(not_found == NULL);
89
90 user_map_debug(&map, dc_debug_fmt_new(), stdout);
91 fprintf(stdout, "\n");
92}
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_new()
Definition fmt.h:15
#define DC_ASSERT(expr,...)
Definition panic.h:37
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Definition scope.h:5
Definition map.c:16
Definition map.c:41
int score
Definition map.c:42
char hashed_password[16]
Definition map.c:43
#define DC_DEBUG_TRACE
Definition debug.h:17

◆ example_iteration()

void example_iteration ( )
static
Examples
container/map.c.

Definition at line 141 of file map.c.

141 {
143 DC_SCOPED(ankerl_largedata_map) map = ankerl_largedata_map_new(stdalloc_get_ref());
144
145 struct largedata data1 = {.data = "Ankerl map entry number one"};
146 struct largedata data2 = {.data = "Ankerl map entry number two"};
147 struct largedata data3 = {.data = "Ankerl map entry number three"};
148
149 ankerl_largedata_map_insert(&map, 2001, data1);
150 ankerl_largedata_map_insert(&map, 2002, data2);
151 ankerl_largedata_map_insert(&map, 2003, data3);
152
153 size_t count = 0;
154 DC_FOR_CONST(ankerl_largedata_map, &map, iter, entry) {
155 printf("Entry %zu: uuid=%" PRIu64 " data=", count, *entry.key);
156 largedata_debug(entry.value, dc_debug_fmt_new(), stdout);
157 fprintf(stdout, "\n");
158 count++;
159 }
160 DC_ASSERT(count == 3);
161
162 ankerl_largedata_map_debug(&map, dc_debug_fmt_new(), stdout);
163 fprintf(stdout, "\n");
164}
#define DC_FOR_CONST(TYPE, INSTANCE, ITER, ITEM)
Definition for.h:14
static void largedata_debug(struct largedata const *self, dc_debug_fmt fmt, FILE *stream)
Definition map.c:98

◆ example_small()

void example_small ( )
static
Examples
container/map.c.

Definition at line 112 of file map.c.

112 {
114 DC_SCOPED(uuid_to_index_map) map = uuid_to_index_map_new(stdalloc_get_ref());
115
116 uuid_to_index_map_insert(&map, 1001, 0);
117 uuid_to_index_map_insert(&map, 1002, 1);
118 uuid_to_index_map_insert(&map, 1003, 2);
119
120 uuid_to_index_map_debug(&map, dc_debug_fmt_new(), stdout);
121 fprintf(stdout, "\n");
122
123 struct largedata large_data_array[3] = {
124 {.data = "First large data"}, {.data = "Second large data"}, {.data = "Third large data"}};
125
126 DC_FOR_CONST(uuid_to_index_map, &map, iter, entry) {
127 uint16_t index = *entry.value;
128 DC_ASSERT(index < 3);
129 largedata_debug(&large_data_array[index], dc_debug_fmt_new(), stdout);
130 fprintf(stdout, "\n");
131 }
132}

◆ largedata_debug()

void largedata_debug ( struct largedata const * self,
dc_debug_fmt fmt,
FILE * stream )
static
Examples
container/map.c.

Definition at line 98 of file map.c.

98 {
99 fprintf(stream, "largedata@%p {\n", (void*)self);
100 fmt = dc_debug_fmt_scope_begin(fmt);
101 dc_debug_fmt_print(fmt, stream, "data: \"%.32s\",\n", self->data);
102 fmt = dc_debug_fmt_scope_end(fmt);
103 dc_debug_fmt_print(fmt, stream, "}");
104}
static DC_PUBLIC void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
Definition fmt.h:32
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
Definition fmt.h:57
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:50
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108

◆ main()

int main ( )

Definition at line 166 of file map.c.

166 {
170 return 0;
171}
static void example_small()
Definition map.c:112
static void example_iteration()
Definition map.c:141
static void example_basic()
Definition map.c:64

◆ user_id_debug()

void user_id_debug ( struct user_id const * self,
dc_debug_fmt fmt,
FILE * stream )
static
Examples
container/map.c.

Definition at line 32 of file map.c.

32 {
33 fprintf(stream, "user_id@%p {\n", (void*)self);
34 fmt = dc_debug_fmt_scope_begin(fmt);
35 dc_debug_fmt_print(fmt, stream, "username: %s,\n", self->username);
36 dc_debug_fmt_print(fmt, stream, "uuid: %" PRIu64 ",\n", self->uuid);
37 fmt = dc_debug_fmt_scope_end(fmt);
38 dc_debug_fmt_print(fmt, stream, "}");
39}

◆ user_id_eq()

bool user_id_eq ( struct user_id const * a,
struct user_id const * b )
static
Examples
container/map.c.

Definition at line 21 of file map.c.

21 {
22 return strcmp(a->username, b->username) == 0 && a->uuid == b->uuid;
23}

◆ user_id_hash()

size_t user_id_hash ( struct user_id const * self)
static
Examples
container/map.c.

Definition at line 25 of file map.c.

25 {
26 const char* username_ptr = self->username;
27 size_t hash = dc_fnv1a_str_const(&username_ptr);
28 hash = dc_hash_combine(hash, DC_DEFAULT_HASH(&self->uuid));
29 return hash;
30}
static DC_PUBLIC size_t dc_hash_combine(size_t seed, size_t h)
Definition combine.h:7
#define DC_DEFAULT_HASH(obj)
Definition default.h:15
static DC_PUBLIC uint64_t dc_fnv1a_str_const(const char *const *s)
Definition fnv1a.h:26

◆ userdata_debug()

void userdata_debug ( struct userdata const * self,
dc_debug_fmt fmt,
FILE * stream )
static
Examples
container/map.c.

Definition at line 46 of file map.c.

46 {
47 fprintf(stream, "userdata@%p {\n", (void*)self);
48 fmt = dc_debug_fmt_scope_begin(fmt);
49 dc_debug_fmt_print(fmt, stream, "score: %d,\n", self->score);
50 dc_debug_fmt_print(fmt, stream, "hashed_password: \"%.16s\",\n", self->hashed_password);
51 fmt = dc_debug_fmt_scope_end(fmt);
52 dc_debug_fmt_print(fmt, stream, "}");
53}