Derive-C
Loading...
Searching...
No Matches
template.h
Go to the documentation of this file.
1
2
4#if !defined(SKIP_INCLUDES)
5 #include "includes.h"
6#endif
7
10
11#if !defined ITEM
12 #if !defined DC_PLACEHOLDERS
13TEMPLATE_ERROR("No ITEM")
14 #endif
15 #define ITEM set_item_t
16typedef size_t ITEM;
17#endif
18
19#if !defined ITEM_HASH
20 #if !defined DC_PLACEHOLDERS
21TEMPLATE_ERROR("No ITEM_HASH")
22 #endif
23
24 #define ITEM_HASH item_hash
25static size_t ITEM_HASH(ITEM const* item) { return *item; }
26#endif
27
28#if !defined ITEM_EQ
29 #define ITEM_EQ DC_MEM_EQ
30#endif
31
32#if !defined ITEM_DELETE
33 #define ITEM_DELETE DC_NO_DELETE
34#endif
35
36#if !defined ITEM_CLONE
37 #define ITEM_CLONE DC_COPY_CLONE
38#endif
39
40#if !defined ITEM_DEBUG
41 #define ITEM_DEBUG DC_DEFAULT_DEBUG
42#endif
43
44typedef ITEM NS(SELF, item_t);
45
46#define MAP PRIV(NS(NAME, inner_map))
47
48#pragma push_macro("ALLOC")
49
50#define KEY ITEM // [DERIVE-C] for template
51#define KEY_HASH ITEM_HASH // [DERIVE-C] for template
52#define KEY_EQ ITEM_EQ // [DERIVE-C] for template
53#define KEY_DELETE ITEM_DELETE // [DERIVE-C] for template
54#define KEY_CLONE ITEM_CLONE // [DERIVE-C] for template
55#define KEY_DEBUG ITEM_DEBUG // [DERIVE-C] for template
56#define VALUE dc_unit // [DERIVE-C] for template
57#define VALUE_DELETE dc_unit_delete // [DERIVE-C] for template
58#define VALUE_CLONE dc_unit_clone // [DERIVE-C] for template
59#define VALUE_DEBUG dc_unit_debug // [DERIVE-C] for template
60#define INTERNAL_NAME MAP // [DERIVE-C] for template
62
63#pragma pop_macro("ALLOC")
64
66
67typedef struct {
69} SELF;
70
71DC_PUBLIC static SELF NS(SELF, new_with_capacity_for)(size_t for_items, NS(ALLOC, ref) alloc_ref) {
72 return (SELF){
73 .map = NS(MAP, new_with_capacity_for)(for_items, alloc_ref),
74 };
75}
76
77DC_PUBLIC static SELF NS(SELF, new)(NS(ALLOC, ref) alloc_ref) {
78 return (SELF){
79 .map = NS(MAP, new)(alloc_ref),
80 };
81}
82
83DC_PUBLIC static SELF NS(SELF, clone)(SELF const* self) {
84 return (SELF){
85 .map = NS(MAP, clone)(&self->map),
86 };
87}
88
89DC_PUBLIC static void NS(SELF, extend_capacity_for)(SELF* self, size_t expected_items) {
90 NS(MAP, extend_capacity_for)(&self->map, expected_items);
91}
92
93DC_PUBLIC static bool NS(SELF, try_add)(SELF* self, ITEM item) {
94 return NS(MAP, try_insert)(&self->map, item, dc_unit_new()) != NULL;
95}
96
97DC_PUBLIC static void NS(SELF, add)(SELF* self, ITEM item) {
98 bool const inserted = NS(SELF, try_add)(self, item);
99 DC_ASSERT(inserted, "Failed to insert item");
100}
101
102DC_PUBLIC static bool NS(SELF, contains)(SELF const* self, ITEM item) {
103 return NS(MAP, try_read)(&self->map, item) != NULL;
104}
105
106DC_PUBLIC static bool NS(SELF, try_remove)(SELF* self, ITEM item) {
107 dc_unit dest;
108 return NS(MAP, try_remove)(&self->map, item, &dest);
109}
110
111DC_PUBLIC static void NS(SELF, remove)(SELF* self, ITEM item) {
112 bool const removed = NS(SELF, try_remove)(self, item);
113 DC_ASSERT(removed, "Failed to remove item {item=%s}", DC_DEBUG(ITEM_DEBUG, &item));
114}
115
116DC_PUBLIC static size_t NS(SELF, size)(SELF const* self) { return NS(MAP, size)(&self->map); }
117
118#define ITER_CONST NS(SELF, iter_const)
119
120typedef struct {
121 NS(MAP, iter_const) map_iter;
122} ITER_CONST;
123
124typedef ITEM const* NS(ITER_CONST, item);
125
126DC_PUBLIC static bool NS(ITER_CONST, empty_item)(ITEM const* const* item) { return *item == NULL; }
127
128DC_PUBLIC static ITEM const* NS(ITER_CONST, next)(ITER_CONST* iter) {
129 return NS(MAP, NS(iter_const, next))(&iter->map_iter).key;
130}
131
132DC_PUBLIC static bool NS(ITER_CONST, empty)(ITER_CONST const* iter) {
133 DC_ASSUME(iter);
134 return NS(MAP, NS(iter_const, empty))(&iter->map_iter);
135}
136
138 return (ITER_CONST){
139 .map_iter = NS(MAP, get_iter_const)(&self->map),
140 };
141}
142
143DC_PUBLIC static void NS(SELF, debug)(SELF const* self, dc_debug_fmt fmt, FILE* stream) {
144 fprintf(stream, DC_EXPAND_STRING(SELF) "@%p {\n", (void*)self);
145 fmt = dc_debug_fmt_scope_begin(fmt);
146
147 dc_debug_fmt_print(fmt, stream, "map: ");
148 NS(MAP, debug)(&self->map, fmt, stream);
149 fprintf(stream, ",\n");
150
151 fmt = dc_debug_fmt_scope_end(fmt);
152 dc_debug_fmt_print(fmt, stream, "}");
153}
154
155#undef ITER_CONST
156
157DC_PUBLIC static void NS(SELF, delete)(SELF* self) { NS(MAP, delete)(&self->map); }
158
159#undef MAP
160
161#undef ITEM_DEBUG
162#undef ITEM_CLONE
163#undef ITEM_DELETE
164#undef ITEM_EQ
165#undef ITEM_HASH
166#undef ITEM
167
169
static DC_PUBLIC void debug(SELF const *self, dc_debug_fmt fmt, FILE *stream)
Definition template.h:212
#define ITEM
Definition template.h:36
#define ITEM_DEBUG
Definition template.h:39
#define ALLOC
Definition template.h:31
#define DC_STATIC_CONSTANT
Definition attributes.h:23
static DC_PUBLIC VALUE const * try_read(SELF const *self, INDEX index)
Definition template.h:180
static DC_PUBLIC IV_PAIR next(ITER *iter)
Definition template.h:355
static DC_PUBLIC bool empty(ITER const *iter)
Definition template.h:349
static DC_PUBLIC bool try_remove(SELF *self, INDEX index, VALUE *destination)
Definition template.h:264
static DC_PUBLIC bool empty_item(IV_PAIR const *item)
Definition template.h:347
static DC_PUBLIC VALUE remove(SELF *self, INDEX index)
Definition template.h:292
#define ITER_CONST
Definition template.h:411
static DC_PUBLIC ITER_CONST get_iter_const(SELF const *self)
Definition template.h:464
static DC_PUBLIC size_t size(SELF const *self)
Definition template.h:252
static DC_PUBLIC SELF clone(SELF const *self)
Definition template.h:215
IV_PAIR item
Definition template.h:281
static DC_PUBLIC SELF new_with_capacity_for(INDEX_TYPE items, ref alloc_ref)
Definition template.h:96
static DC_PUBLIC void extend_capacity_for(SELF *self, size_t expected_items)
Definition template.h:307
DC_STATIC_CONSTANT size_t max_capacity
Definition template.h:130
static DC_PUBLIC VALUE * try_insert(SELF *self, KEY key, VALUE value)
Definition template.h:318
static DC_PUBLIC void add(SELF *self, ITEM item)
Definition template.h:97
static DC_PUBLIC bool try_add(SELF *self, ITEM item)
Definition template.h:93
#define MAP
Definition template.h:46
#define ITEM_HASH
Definition template.h:24
static DC_PUBLIC bool contains(SELF const *self, ITEM item)
Definition template.h:102
#define DC_TRAIT_SET(SELF)
Definition trait.h:5
#define TEMPLATE_ERROR(...)
With the user provided name, even in nested templates.
Definition def.h:56
#define SELF
Definition def.h:52
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
Definition dump.h:92
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
#define DC_PUBLIC
Definition namespace.h:25
#define NS(pre, post)
Definition namespace.h:14
#define DC_EXPAND_STRING(NAME)
Definition namespace.h:6
#define DC_ASSERT(expr,...)
Definition panic.h:37
#define DC_ASSUME(expr,...)
Definition panic.h:57
iter_const map_iter
Definition template.h:121
MAP map
Definition template.h:68
Debug format helpers for debug printin data structures.
Definition fmt.h:11
A queue comprised of an extendable circular buffer.
Definition template.h:16
static DC_PUBLIC dc_unit dc_unit_new()
Definition unit.h:9
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108