C Container Collection (CCC)
Loading...
Searching...
No Matches
impl_flat_priority_queue.h
1
16#ifndef CCC_IMPL_FLAT_PRIORITY_QUEUE_H
17#define CCC_IMPL_FLAT_PRIORITY_QUEUE_H
18
20#include <assert.h>
21#include <stddef.h>
24#include "../buffer.h"
25#include "../types.h"
26
27/* NOLINTBEGIN(readability-identifier-naming) */
28
35struct ccc_fpq
36{
38 ccc_buffer buf;
40 ccc_threeway_cmp order;
43};
44
45/*======================== Private Interface =========================*/
46
48size_t ccc_impl_fpq_bubble_up(struct ccc_fpq *, void *, size_t);
50void ccc_impl_fpq_in_place_heapify(struct ccc_fpq *, size_t n, void *tmp);
52void *ccc_impl_fpq_update_fixup(struct ccc_fpq *, void *, void *tmp);
53
54/*====================== Macro Implementations ========================*/
55
57#define ccc_impl_fpq_init(impl_mem_ptr, impl_any_type_name, impl_cmp_order, \
58 impl_cmp_fn, impl_alloc_fn, impl_aux_data, \
59 impl_capacity) \
60 { \
61 .buf = ccc_buf_init(impl_mem_ptr, impl_any_type_name, impl_alloc_fn, \
62 impl_aux_data, impl_capacity), \
63 .cmp = (impl_cmp_fn), \
64 .order = (impl_cmp_order), \
65 }
66
68#define ccc_impl_fpq_heapify_init(impl_mem_ptr, impl_any_type_name, \
69 impl_cmp_order, impl_cmp_fn, impl_alloc_fn, \
70 impl_aux_data, impl_capacity, impl_size) \
71 (__extension__({ \
72 __auto_type impl_fpq_heapify_mem = (impl_mem_ptr); \
73 struct ccc_fpq impl_fpq_heapify_res = ccc_impl_fpq_init( \
74 impl_fpq_heapify_mem, impl_any_type_name, impl_cmp_order, \
75 impl_cmp_fn, impl_alloc_fn, impl_aux_data, impl_capacity); \
76 ccc_impl_fpq_in_place_heapify(&impl_fpq_heapify_res, (impl_size), \
77 &(impl_any_type_name){0}); \
78 impl_fpq_heapify_res; \
79 }))
80
84#define ccc_impl_fpq_emplace(fpq, type_initializer...) \
85 (__extension__({ \
86 struct ccc_fpq *impl_fpq = (fpq); \
87 typeof(type_initializer) *impl_fpq_res \
88 = ccc_buf_alloc_back(&impl_fpq->buf); \
89 if (impl_fpq_res) \
90 { \
91 *impl_fpq_res = type_initializer; \
92 if (impl_fpq->buf.count > 1) \
93 { \
94 impl_fpq_res \
95 = ccc_buf_at(&impl_fpq->buf, \
96 ccc_impl_fpq_bubble_up( \
97 impl_fpq, &(typeof(type_initializer)){0}, \
98 impl_fpq->buf.count - 1)); \
99 } \
100 else \
101 { \
102 impl_fpq_res = ccc_buf_at(&impl_fpq->buf, 0); \
103 } \
104 } \
105 impl_fpq_res; \
106 }))
107
110#define ccc_impl_fpq_update_w(fpq_ptr, T_ptr, update_closure_over_T...) \
111 (__extension__({ \
112 struct ccc_fpq *const impl_fpq = (fpq_ptr); \
113 typeof(*T_ptr) *T = (T_ptr); \
114 if (impl_fpq && !ccc_buf_is_empty(&impl_fpq->buf) && T) \
115 { \
116 {update_closure_over_T} T = ccc_impl_fpq_update_fixup( \
117 impl_fpq, T, &(typeof(*T_ptr)){0}); \
118 } \
119 T; \
120 }))
121
123#define ccc_impl_fpq_increase_w(fpq_ptr, T_ptr, increase_closure_over_T...) \
124 ccc_impl_fpq_update_w(fpq_ptr, T_ptr, increase_closure_over_T)
125
127#define ccc_impl_fpq_decrease_w(fpq_ptr, T_ptr, decrease_closure_over_T...) \
128 ccc_impl_fpq_update_w(fpq_ptr, T_ptr, decrease_closure_over_T)
129
130/* NOLINTEND(readability-identifier-naming) */
131
132#endif /* CCC_IMPL_FLAT_PRIORITY_QUEUE_H */
struct ccc_buffer ccc_buffer
A contiguous block of storage for elements of the same type.
Definition: buffer.h:74
ccc_threeway_cmp ccc_any_type_cmp_fn(ccc_any_type_cmp)
A callback function for comparing two elements in a container.
Definition: types.h:320
ccc_threeway_cmp
A three-way comparison for comparison functions.
Definition: types.h:153