C Container Collection (CCC)
Loading...
Searching...
No Matches
impl_flat_priority_queue.h
1#ifndef CCC_IMPL_FLAT_PRIORITY_QUEUE_H
2#define CCC_IMPL_FLAT_PRIORITY_QUEUE_H
3
5#include <assert.h>
6#include <stddef.h>
9#include "../buffer.h"
10#include "../types.h"
11
12/* NOLINTBEGIN(readability-identifier-naming) */
13
15struct ccc_fpq_
16{
17 ccc_buffer buf_;
18 ccc_cmp_fn *cmp_;
19 ccc_threeway_cmp order_;
20};
21
22/*======================== Private Interface =========================*/
23
25size_t ccc_impl_fpq_bubble_up(struct ccc_fpq_ *, char[], size_t);
27void ccc_impl_fpq_in_place_heapify(struct ccc_fpq_ *, size_t n);
29void *ccc_impl_fpq_update_fixup(struct ccc_fpq_ *, void *);
30
31/*====================== Macro Implementations ========================*/
32
34#define ccc_impl_fpq_init(mem_ptr, cmp_order, cmp_fn, alloc_fn, aux_data, \
35 capacity) \
36 { \
37 .buf_ = ccc_buf_init(mem_ptr, alloc_fn, aux_data, capacity), \
38 .cmp_ = (cmp_fn), \
39 .order_ = (cmp_order), \
40 }
41
43#define ccc_impl_fpq_heapify_init(mem_ptr, cmp_order, cmp_fn, alloc_fn, \
44 aux_data, capacity, size) \
45 (__extension__({ \
46 __auto_type fpq_heapify_mem_ = (mem_ptr); \
47 struct ccc_fpq_ fpq_heapify_res_ \
48 = ccc_impl_fpq_init(fpq_heapify_mem_, cmp_order, cmp_fn, alloc_fn, \
49 aux_data, capacity); \
50 ccc_impl_fpq_in_place_heapify(&fpq_heapify_res_, (size)); \
51 fpq_heapify_res_; \
52 }))
53
57#define ccc_impl_fpq_emplace(fpq, type_initializer...) \
58 (__extension__({ \
59 typeof(type_initializer) *fpq_res_; \
60 struct ccc_fpq_ *fpq_ = (fpq); \
61 assert(sizeof(*fpq_res_) == ccc_buf_elem_size(&fpq_->buf_).count); \
62 fpq_res_ = ccc_buf_alloc_back(&fpq_->buf_); \
63 if (ccc_buf_size(&fpq_->buf_).count \
64 == ccc_buf_capacity(&fpq_->buf_).count) \
65 { \
66 fpq_res_ = NULL; \
67 ccc_result const extra_space_ = ccc_buf_alloc( \
68 &fpq_->buf_, ccc_buf_capacity(&fpq_->buf_).count * 2, \
69 fpq_->buf_.alloc_); \
70 if (extra_space_ == CCC_RESULT_OK) \
71 { \
72 fpq_res_ = ccc_buf_back(&fpq_->buf_); \
73 } \
74 } \
75 if (fpq_res_) \
76 { \
77 *fpq_res_ = type_initializer; \
78 if (ccc_buf_size(&fpq_->buf_).count > 1) \
79 { \
80 void *fpq_tmp_ = ccc_buf_at(&fpq_->buf_, \
81 ccc_buf_size(&fpq_->buf_).count); \
82 fpq_res_ = ccc_buf_at( \
83 &fpq_->buf_, \
84 ccc_impl_fpq_bubble_up( \
85 fpq_, fpq_tmp_, ccc_buf_size(&fpq_->buf_).count - 1)); \
86 } \
87 else \
88 { \
89 fpq_res_ = ccc_buf_at(&fpq_->buf_, 0); \
90 } \
91 } \
92 fpq_res_; \
93 }))
94
97#define ccc_impl_fpq_update_w(fpq_ptr, T_ptr, update_closure_over_T...) \
98 (__extension__({ \
99 struct ccc_fpq_ *const fpq_ = (fpq_ptr); \
100 void *fpq_update_res_ = NULL; \
101 void *const fpq_t_ptr_ = (T_ptr); \
102 if (fpq_ && fpq_t_ptr_ && !ccc_buf_is_empty(&fpq_->buf_)) \
103 { \
104 {update_closure_over_T} fpq_update_res_ \
105 = ccc_impl_fpq_update_fixup(fpq_, fpq_t_ptr_); \
106 } \
107 fpq_update_res_; \
108 }))
109
111#define ccc_impl_fpq_increase_w(fpq_ptr, T_ptr, increase_closure_over_T...) \
112 ccc_impl_fpq_update_w(fpq_ptr, T_ptr, increase_closure_over_T)
113
115#define ccc_impl_fpq_decrease_w(fpq_ptr, T_ptr, decrease_closure_over_T...) \
116 ccc_impl_fpq_update_w(fpq_ptr, T_ptr, decrease_closure_over_T)
117
118/* NOLINTEND(readability-identifier-naming) */
119
120#endif /* CCC_IMPL_FLAT_PRIORITY_QUEUE_H */
struct ccc_buf_ ccc_buffer
A contiguous block of storage for elements of the same type.
Definition: buffer.h:50
ccc_threeway_cmp ccc_cmp_fn(ccc_cmp)
A callback function for comparing two elements in a container.
Definition: types.h:291
ccc_threeway_cmp
A three-way comparison for comparison functions.
Definition: types.h:138