Derive-C
Loading...
Searching...
No Matches
set.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <derive-c/container/set/swiss/template.h>

Go to the source code of this file.

Data Structures

struct  complex_data

Macros

#define ITEM   int
#define ITEM_HASH   DC_DEFAULT_HASH
#define NAME   int_set
#define ITEM   struct complex_data
#define ITEM_HASH   complex_data_hash
#define ITEM_EQ   complex_data_eq
#define ITEM_DELETE   complex_data_delete
#define ITEM_DEBUG   complex_data_debug
#define NAME   complex_set

Functions

static void example_integer_set ()
static void complex_data_delete (struct complex_data *self)
static bool complex_data_eq (struct complex_data const *a, struct complex_data const *b)
static size_t complex_data_hash (struct complex_data const *self)
static void complex_data_debug (struct complex_data const *self, dc_debug_fmt fmt, FILE *stream)
static void example_complex_set ()
int main ()

Macro Definition Documentation

◆ ITEM [1/2]

#define ITEM   struct complex_data

Definition at line 14 of file set.c.

◆ ITEM [2/2]

#define ITEM   int

Definition at line 14 of file set.c.

◆ ITEM_DEBUG

#define ITEM_DEBUG   complex_data_debug

Definition at line 68 of file set.c.

◆ ITEM_DELETE

#define ITEM_DELETE   complex_data_delete

Definition at line 67 of file set.c.

◆ ITEM_EQ

#define ITEM_EQ   complex_data_eq

Definition at line 66 of file set.c.

◆ ITEM_HASH [1/2]

#define ITEM_HASH   complex_data_hash

Definition at line 15 of file set.c.

◆ ITEM_HASH [2/2]

#define ITEM_HASH   DC_DEFAULT_HASH

Definition at line 15 of file set.c.

◆ NAME [1/2]

#define NAME   complex_set

Definition at line 16 of file set.c.

◆ NAME [2/2]

#define NAME   int_set

Definition at line 16 of file set.c.

Function Documentation

◆ complex_data_debug()

void complex_data_debug ( struct complex_data const * self,
dc_debug_fmt fmt,
FILE * stream )
static
Examples
container/set.c.

Definition at line 54 of file set.c.

54 {
55 fprintf(stream, "complex_data@%p {\n", (void*)self);
56 fmt = dc_debug_fmt_scope_begin(fmt);
57 dc_debug_fmt_print(fmt, stream, "flag: %s,\n", self->flag ? "true" : "false");
58 dc_debug_fmt_print(fmt, stream, "name: %s,\n", self->name);
59 dc_debug_fmt_print(fmt, stream, "value: %d,\n", self->value);
60 fmt = dc_debug_fmt_scope_end(fmt);
61 dc_debug_fmt_print(fmt, stream, "}");
62}
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

◆ complex_data_delete()

void complex_data_delete ( struct complex_data * self)
static
Examples
container/set.c.

Definition at line 39 of file set.c.

39{ free(self->name); }
char * name
Definition set.c:35

◆ complex_data_eq()

bool complex_data_eq ( struct complex_data const * a,
struct complex_data const * b )
static
Examples
container/set.c.

Definition at line 41 of file set.c.

41 {
42 return a->flag == b->flag && strcmp(a->name, b->name) == 0 && a->value == b->value;
43}

◆ complex_data_hash()

size_t complex_data_hash ( struct complex_data const * self)
static
Examples
container/set.c.

Definition at line 45 of file set.c.

45 {
46 const char* name_ptr = self->name;
47 size_t hash = dc_fnv1a_str_const(&name_ptr);
48 int32_t value = (int32_t)self->value;
49 hash = dc_hash_combine(hash, int32_t_hash_id(&value));
50 hash = dc_hash_combine(hash, (size_t)self->flag);
51 return hash;
52}
static DC_PUBLIC size_t dc_hash_combine(size_t seed, size_t h)
Definition combine.h:7
static DC_PUBLIC uint64_t dc_fnv1a_str_const(const char *const *s)
Definition fnv1a.h:26

◆ example_complex_set()

void example_complex_set ( )
static
Examples
container/set.c.

Definition at line 72 of file set.c.

72 {
74 DC_SCOPED(complex_set) set = complex_set_new(stdalloc_get_ref());
75
76 struct complex_data item1 = {.flag = true, .name = strdup("first"), .value = 42};
77 struct complex_data item2 = {.flag = false, .name = strdup("second"), .value = 100};
78
79 complex_set_add(&set, item1);
80 complex_set_add(&set, item2);
81
82 struct complex_data lookup = {.flag = true, .name = strdup("first"), .value = 42};
83 DC_ASSERT(complex_set_contains(&set, lookup));
84 free(lookup.name);
85
86 complex_set_debug(&set, dc_debug_fmt_new(), stdout);
87}
static DC_PUBLIC void set(SELF *self, INDEX_TYPE index, bool value)
Definition template.h:73
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
#define DC_DEBUG_TRACE
Definition debug.h:17

◆ example_integer_set()

void example_integer_set ( )
static
Examples
container/set.c.

Definition at line 19 of file set.c.

19 {
21 DC_SCOPED(int_set) set = int_set_new(stdalloc_get_ref());
22
23 for (int i = 0; i < 10; i++) {
24 int_set_add(&set, i);
25 }
26
27 DC_FOR_CONST(int_set, &set, iter, item) { printf("%d ", *item); }
28 printf("\n");
29
30 int_set_debug(&set, dc_debug_fmt_new(), stdout);
31}
IV_PAIR item
Definition template.h:281
#define DC_FOR_CONST(TYPE, INSTANCE, ITER, ITEM)
Definition for.h:14

◆ main()

int main ( )

Definition at line 89 of file set.c.

89 {
92 return 0;
93}
static void example_complex_set()
Definition set.c:72
static void example_integer_set()
Definition set.c:19