Derive-C
Loading...
Searching...
No Matches
decomposed.c File Reference
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <derive-c/algorithm/hash/hashers.h>
#include <derive-c/alloc/std.h>
#include <derive-c/core/prelude.h>
#include <derive-c/utils/for.h>
#include <derive-c/container/map/decomposed/template.h>

Go to the source code of this file.

Data Structures

struct  report_id
struct  report
struct  fixed_string

Macros

#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
#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
#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

Functions

void print_map (id_to_name const *map)
void id_to_name_example ()
void report_id_debug (struct report_id const *self, dc_debug_fmt fmt, FILE *stream)
bool report_id_equality (struct report_id const *report_1, struct report_id const *report_2)
size_t report_id_hash (struct report_id const *report_id)
void report_id_delete (struct report_id *self)
void report_debug (struct report const *self, dc_debug_fmt fmt, FILE *stream)
void report_delete (struct report *self)
void report_map_example ()
void fixed_string_debug (struct fixed_string const *self, dc_debug_fmt fmt, FILE *stream)
bool fixed_string_eq (struct fixed_string const *str1, struct fixed_string const *str2)
size_t fixed_string_hash (struct fixed_string const *str)
void fixed_string_example ()
int main ()

Macro Definition Documentation

◆ KEY [1/3]

#define KEY   struct fixed_string

Definition at line 17 of file decomposed.c.

◆ KEY [2/3]

#define KEY   struct report_id

Definition at line 17 of file decomposed.c.

◆ KEY [3/3]

#define KEY   uint32_t

Definition at line 17 of file decomposed.c.

◆ KEY_DEBUG [1/2]

#define KEY_DEBUG   fixed_string_debug

Definition at line 93 of file decomposed.c.

◆ KEY_DEBUG [2/2]

#define KEY_DEBUG   report_id_debug

Definition at line 93 of file decomposed.c.

◆ KEY_DELETE

#define KEY_DELETE   report_id_delete

Definition at line 92 of file decomposed.c.

◆ KEY_EQ [1/3]

#define KEY_EQ   fixed_string_eq

Definition at line 18 of file decomposed.c.

◆ KEY_EQ [2/3]

#define KEY_EQ   report_id_equality

Definition at line 18 of file decomposed.c.

◆ KEY_EQ [3/3]

#define KEY_EQ   uint32_t_eq

Definition at line 18 of file decomposed.c.

◆ KEY_HASH [1/3]

#define KEY_HASH   fixed_string_hash

Definition at line 19 of file decomposed.c.

◆ KEY_HASH [2/3]

#define KEY_HASH   report_id_hash

Definition at line 19 of file decomposed.c.

◆ KEY_HASH [3/3]

#define KEY_HASH   hash_id_uint32_t

Definition at line 19 of file decomposed.c.

◆ NAME [1/3]

#define NAME   fixed_string_map

Definition at line 21 of file decomposed.c.

◆ NAME [2/3]

#define NAME   report_map

Definition at line 21 of file decomposed.c.

◆ NAME [3/3]

#define NAME   id_to_name

Definition at line 21 of file decomposed.c.

◆ VALUE [1/3]

#define VALUE   uint32_t

Definition at line 20 of file decomposed.c.

◆ VALUE [2/3]

#define VALUE   struct report

Definition at line 20 of file decomposed.c.

◆ VALUE [3/3]

#define VALUE   char const*

Definition at line 20 of file decomposed.c.

◆ VALUE_DEBUG

#define VALUE_DEBUG   report_debug

Definition at line 96 of file decomposed.c.

◆ VALUE_DELETE

#define VALUE_DELETE   report_delete

Definition at line 95 of file decomposed.c.

Function Documentation

◆ fixed_string_debug()

void fixed_string_debug ( struct fixed_string const * self,
dc_debug_fmt fmt,
FILE * stream )
Examples
container/map/decomposed.c.

Definition at line 137 of file decomposed.c.

137 {
138 (void)fmt;
139 fprintf(stream, "fixed_string@%p { value: \"%.*s\" }", self, 4, self->value);
140}
static FILE * stream(SELF *self)
Opens a file for.
Definition template.h:107

◆ fixed_string_eq()

bool fixed_string_eq ( struct fixed_string const * str1,
struct fixed_string const * str2 )
Examples
container/map/decomposed.c.

Definition at line 142 of file decomposed.c.

142 {
143 return memcmp(str1->value, str2->value, sizeof(str1->value)) == 0;
144}

◆ fixed_string_example()

void fixed_string_example ( )
Examples
container/map/decomposed.c.

Definition at line 158 of file decomposed.c.

158 {
159 printf("Fixed Strings Example:\n");
160 fixed_string_map map = fixed_string_map_new(stdalloc_get());
161
162 struct fixed_string key1 = {.value = "abc"};
163 struct fixed_string key2 = {.value = "def"};
164 struct fixed_string key3 = {.value = "ghi"};
165
166 fixed_string_map_insert(&map, key1, 123);
167 fixed_string_map_insert(&map, key2, 456);
168 fixed_string_map_insert(&map, key3, 789);
169
170 DC_ASSERT(*fixed_string_map_read(&map, key1) == 123);
171 DC_ASSERT(*fixed_string_map_read(&map, key2) == 456);
172 DC_ASSERT(*fixed_string_map_read(&map, key3) == 789);
173
174 size_t pos = 0;
175 FOR_CONST(fixed_string_map, &map, iter, entry) {
176 printf("Position: %zu Key: %.3s Value: %u\n", pos, entry.key->value, *entry.value);
177 pos++;
178 }
179
180 fixed_string_map_debug(&map, dc_debug_fmt_new(), stdout);
181
182 fixed_string_map_delete(&map);
183}
static dc_debug_fmt dc_debug_fmt_new()
Definition fmt.h:14
#define FOR_CONST(TYPE, INSTANCE, ITER, ITEM)
Definition for.h:10
#define DC_ASSERT(expr,...)
Definition panic.h:36

◆ fixed_string_hash()

size_t fixed_string_hash ( struct fixed_string const * str)
Examples
container/map/decomposed.c.

Definition at line 146 of file decomposed.c.

146 {
147 return hash_murmurhash_string_4(str->value);
148}

◆ id_to_name_example()

void id_to_name_example ( )
Examples
container/map/decomposed.c.

Definition at line 33 of file decomposed.c.

33 {
34 printf("Id to Name Map Example:\n");
35 id_to_name map = id_to_name_new(stdalloc_get());
36
37 id_to_name_insert(&map, 23, "hello");
38 id_to_name_insert(&map, 10, "bob");
39 id_to_name_insert(&map, 42, "meaning");
40 DC_ASSERT(strcmp(*id_to_name_read(&map, 42), "meaning") == 0);
41
42 print_map(&map);
43
44 char const** entry = id_to_name_write(&map, 23);
45 DC_ASSERT(entry);
46 *entry = "a different string!";
47
48 print_map(&map);
49
50 id_to_name_debug(&map, dc_debug_fmt_new(), stdout);
51
52 id_to_name_delete(&map);
53}
void print_map(id_to_name const *map)
Definition decomposed.c:24

◆ main()

int main ( )

Definition at line 185 of file decomposed.c.

185 {
189}
void id_to_name_example()
Definition decomposed.c:33
void report_map_example()
Definition decomposed.c:100
void fixed_string_example()
Definition decomposed.c:158

◆ print_map()

void print_map ( id_to_name const * map)
Examples
container/map/decomposed.c.

Definition at line 24 of file decomposed.c.

24 {
25 printf("Map has items:\n");
26 size_t pos = 0;
27 FOR_CONST(id_to_name, map, iter, entry) {
28 printf("position: %zu key: %" PRIu32 " string: %s\n", pos, *entry.key, *entry.value);
29 pos++;
30 }
31}

◆ report_debug()

void report_debug ( struct report const * self,
dc_debug_fmt fmt,
FILE * stream )
Examples
container/map/decomposed.c.

Definition at line 81 of file decomposed.c.

81 {
82 (void)fmt;
83 fprintf(stream, " report@%p { description: \"%s\", value: %d}", self, self->description,
84 self->value);
85}

◆ report_delete()

void report_delete ( struct report * self)
Examples
container/map/decomposed.c.

Definition at line 87 of file decomposed.c.

87{ free(self->description); }
static void free(SELF *self, void *ptr)
Definition template.h:56
char * description
Definition decomposed.c:77

◆ report_id_debug()

void report_id_debug ( struct report_id const * self,
dc_debug_fmt fmt,
FILE * stream )
Examples
container/map/decomposed.c.

Definition at line 60 of file decomposed.c.

60 {
61 (void)fmt;
62 fprintf(stream, " report_id@%p { name: \"%s\", section: %d}", self, self->name, self->section);
63}

◆ report_id_delete()

void report_id_delete ( struct report_id * self)
Examples
container/map/decomposed.c.

Definition at line 74 of file decomposed.c.

74{ free(self->name); }
char * name
Definition decomposed.c:56

◆ report_id_equality()

bool report_id_equality ( struct report_id const * report_1,
struct report_id const * report_2 )
Examples
container/map/decomposed.c.

Definition at line 65 of file decomposed.c.

65 {
66 return strcmp(report_1->name, report_2->name) == 0 && report_1->section == report_2->section;
67}

◆ report_id_hash()

size_t report_id_hash ( struct report_id const * report_id)
Examples
container/map/decomposed.c.

Definition at line 69 of file decomposed.c.

69 {
71 hash_id_uint32_t(&report_id->section));
72}
size_t hash_murmurhash_string(const char *str)
Definition hashers.h:62
static size_t hash_combine(size_t seed, size_t h)
Definition hashers.h:88
uint32_t section
Definition decomposed.c:57

◆ report_map_example()

void report_map_example ( )
Examples
container/map/decomposed.c.

Definition at line 100 of file decomposed.c.

100 {
101 printf("Report Map Example:\n");
102 report_map map = report_map_new(stdalloc_get());
103
104 struct report_id id1 = {.name = strdup("Report A"), .section = 1};
105 struct report_id id2 = {.name = strdup("Report B"), .section = 2};
106
107 report_map_insert(&map, id1,
108 (struct report){.description = strdup("Description A"), .value = 100});
109 report_map_insert(&map, id2,
110 (struct report){.description = strdup("Description B"), .value = 200});
111
112 DC_ASSERT(strcmp(report_map_read(&map, id1)->description, "Description A") == 0);
113
114 {
115 size_t pos = 0;
116 FOR_CONST(report_map, &map, iter, entry) {
117 printf("Position: %zu Key: %s Section: %u Value: %d\n", pos, entry.key->name,
118 entry.key->section, entry.value->value);
119 pos++;
120 }
121 }
122
123 report_map_debug(&map, dc_debug_fmt_new(), stdout);
124
125 struct report entry = report_map_remove(&map, id1);
126 report_delete(&entry);
127
128 report_map_debug(&map, dc_debug_fmt_new(), stdout);
129
130 report_map_delete(&map);
131}
void report_delete(struct report *self)
Definition decomposed.c:87