1#ifndef IMPL_HANDLE_REALTIME_ORDERED_MAP_H
2#define IMPL_HANDLE_REALTIME_ORDERED_MAP_H
11#include "impl_types.h"
20struct ccc_hromap_elem_
42 size_t node_elem_offset_;
47struct ccc_hrtree_handle_
49 struct ccc_hromap_ *hrm_;
51 struct ccc_handl_ handle_;
55union ccc_hromap_handle_
57 struct ccc_hrtree_handle_ impl_;
63void *ccc_impl_hrm_key_at(
struct ccc_hromap_
const *hrm,
size_t slot);
65struct ccc_hromap_elem_ *ccc_impl_hrm_elem_at(
struct ccc_hromap_
const *hrm,
68struct ccc_hrtree_handle_ ccc_impl_hrm_handle(struct ccc_hromap_ const *hrm,
71void ccc_impl_hrm_insert(
struct ccc_hromap_ *hrm,
size_t parent_i,
74size_t ccc_impl_hrm_alloc_slot(
struct ccc_hromap_ *hrm);
79#define ccc_impl_hrm_init(memory_ptr, node_elem_field, key_elem_field, \
80 key_cmp_fn, alloc_fn, aux_data, capacity) \
82 .buf_ = ccc_buf_init(memory_ptr, alloc_fn, aux_data, capacity), \
85 .key_offset_ = offsetof(typeof(*(memory_ptr)), key_elem_field), \
86 .node_elem_offset_ = offsetof(typeof(*(memory_ptr)), node_elem_field), \
87 .cmp_ = (key_cmp_fn), \
91#define ccc_impl_hrm_as(handle_realtime_ordered_map_ptr, type_name, handle...) \
92 ((type_name *)ccc_buf_at(&(handle_realtime_ordered_map_ptr)->buf_, \
98#define ccc_impl_hrm_and_modify_w(handle_realtime_ordered_map_handle_ptr, \
99 type_name, closure_over_T...) \
101 __auto_type hrm_hndl_ptr_ = (handle_realtime_ordered_map_handle_ptr); \
102 struct ccc_hrtree_handle_ hrm_mod_hndl_ \
103 = {.handle_ = {.stats_ = CCC_ENTRY_ARG_ERROR}}; \
106 hrm_mod_hndl_ = hrm_hndl_ptr_->impl_; \
107 if (hrm_mod_hndl_.handle_.stats_ & CCC_ENTRY_OCCUPIED) \
109 type_name *const T = ccc_buf_at(&hrm_mod_hndl_.hrm_->buf_, \
110 hrm_mod_hndl_.handle_.i_); \
121#define ccc_impl_hrm_or_insert_w(handle_realtime_ordered_map_handle_ptr, \
124 __auto_type or_ins_handle_ptr_ \
125 = (handle_realtime_ordered_map_handle_ptr); \
126 ccc_handle_i hrm_or_ins_ret_ = 0; \
127 if (or_ins_handle_ptr_) \
129 struct ccc_hrtree_handle_ *hrm_or_ins_hndl_ \
130 = &or_ins_handle_ptr_->impl_; \
131 if (hrm_or_ins_hndl_->handle_.stats_ == CCC_ENTRY_OCCUPIED) \
133 hrm_or_ins_ret_ = hrm_or_ins_hndl_->handle_.i_; \
138 = ccc_impl_hrm_alloc_slot(hrm_or_ins_hndl_->hrm_); \
139 if (hrm_or_ins_ret_) \
141 *((typeof(lazy_key_value) *)ccc_buf_at( \
142 &hrm_or_ins_hndl_->hrm_->buf_, hrm_or_ins_ret_)) \
144 ccc_impl_hrm_insert( \
145 hrm_or_ins_hndl_->hrm_, hrm_or_ins_hndl_->handle_.i_, \
146 hrm_or_ins_hndl_->last_cmp_, hrm_or_ins_ret_); \
154#define ccc_impl_hrm_insert_handle_w(handle_realtime_ordered_map_handle_ptr, \
157 __auto_type ins_handle_ptr_ \
158 = (handle_realtime_ordered_map_handle_ptr); \
159 ccc_handle_i hrm_ins_hndl_ret_ = 0; \
160 if (ins_handle_ptr_) \
162 struct ccc_hrtree_handle_ *hrm_ins_hndl_ \
163 = &ins_handle_ptr_->impl_; \
164 if (!(hrm_ins_hndl_->handle_.stats_ & CCC_ENTRY_OCCUPIED)) \
167 = ccc_impl_hrm_alloc_slot(hrm_ins_hndl_->hrm_); \
168 if (hrm_ins_hndl_ret_) \
170 *((typeof(lazy_key_value) *)ccc_buf_at( \
171 &hrm_ins_hndl_->hrm_->buf_, hrm_ins_hndl_ret_)) \
173 ccc_impl_hrm_insert( \
174 hrm_ins_hndl_->hrm_, hrm_ins_hndl_->handle_.i_, \
175 hrm_ins_hndl_->last_cmp_, hrm_ins_hndl_ret_); \
178 else if (hrm_ins_hndl_->handle_.stats_ == CCC_ENTRY_OCCUPIED) \
180 hrm_ins_hndl_ret_ = hrm_ins_hndl_->handle_.i_; \
181 struct ccc_hromap_elem_ ins_hndl_saved_ \
182 = *ccc_impl_hrm_elem_at(hrm_ins_hndl_->hrm_, \
183 hrm_ins_hndl_ret_); \
184 *((typeof(lazy_key_value) *)ccc_buf_at( \
185 &hrm_ins_hndl_->hrm_->buf_, hrm_ins_hndl_ret_)) \
187 *ccc_impl_hrm_elem_at(hrm_ins_hndl_->hrm_, hrm_ins_hndl_ret_) \
195#define ccc_impl_hrm_try_insert_w(handle_realtime_ordered_map_ptr, key, \
198 __auto_type try_ins_map_ptr_ = (handle_realtime_ordered_map_ptr); \
199 struct ccc_handl_ hrm_try_ins_hndl_ret_ \
200 = {.stats_ = CCC_ENTRY_ARG_ERROR}; \
201 if (try_ins_map_ptr_) \
203 __auto_type hrm_key_ = (key); \
204 struct ccc_hrtree_handle_ hrm_try_ins_hndl_ \
205 = ccc_impl_hrm_handle(try_ins_map_ptr_, (void *)&hrm_key_); \
206 if (!(hrm_try_ins_hndl_.handle_.stats_ & CCC_ENTRY_OCCUPIED)) \
208 hrm_try_ins_hndl_ret_ = (struct ccc_handl_){ \
209 .i_ = ccc_impl_hrm_alloc_slot(hrm_try_ins_hndl_.hrm_), \
210 .stats_ = CCC_ENTRY_INSERT_ERROR}; \
211 if (hrm_try_ins_hndl_ret_.i_) \
213 *((typeof(lazy_value) *)ccc_buf_at( \
214 &try_ins_map_ptr_->buf_, hrm_try_ins_hndl_ret_.i_)) \
216 *((typeof(hrm_key_) *)ccc_impl_hrm_key_at( \
217 try_ins_map_ptr_, hrm_try_ins_hndl_ret_.i_)) \
219 ccc_impl_hrm_insert(hrm_try_ins_hndl_.hrm_, \
220 hrm_try_ins_hndl_.handle_.i_, \
221 hrm_try_ins_hndl_.last_cmp_, \
222 hrm_try_ins_hndl_ret_.i_); \
223 hrm_try_ins_hndl_ret_.stats_ = CCC_ENTRY_VACANT; \
226 else if (hrm_try_ins_hndl_.handle_.stats_ == CCC_ENTRY_OCCUPIED) \
228 hrm_try_ins_hndl_ret_ = (struct ccc_handl_){ \
229 .i_ = hrm_try_ins_hndl_.handle_.i_, \
230 .stats_ = hrm_try_ins_hndl_.handle_.stats_}; \
233 hrm_try_ins_hndl_ret_; \
237#define ccc_impl_hrm_insert_or_assign_w(handle_realtime_ordered_map_ptr, key, \
240 __auto_type ins_or_assign_map_ptr_ \
241 = (handle_realtime_ordered_map_ptr); \
242 struct ccc_handl_ hrm_ins_or_assign_hndl_ret_ \
243 = {.stats_ = CCC_ENTRY_ARG_ERROR}; \
244 if (ins_or_assign_map_ptr_) \
246 __auto_type hrm_key_ = (key); \
247 struct ccc_hrtree_handle_ hrm_ins_or_assign_hndl_ \
248 = ccc_impl_hrm_handle(ins_or_assign_map_ptr_, \
249 (void *)&hrm_key_); \
250 if (!(hrm_ins_or_assign_hndl_.handle_.stats_ \
251 & CCC_ENTRY_OCCUPIED)) \
253 hrm_ins_or_assign_hndl_ret_ \
254 = (struct ccc_handl_){.i_ = ccc_impl_hrm_alloc_slot( \
255 hrm_ins_or_assign_hndl_.hrm_), \
256 .stats_ = CCC_ENTRY_INSERT_ERROR}; \
257 if (hrm_ins_or_assign_hndl_ret_.i_) \
259 *((typeof(lazy_value) *)ccc_buf_at( \
260 &hrm_ins_or_assign_hndl_.hrm_->buf_, \
261 hrm_ins_or_assign_hndl_ret_.i_)) \
263 *((typeof(hrm_key_) *)ccc_impl_hrm_key_at( \
264 hrm_ins_or_assign_hndl_.hrm_, \
265 hrm_ins_or_assign_hndl_ret_.i_)) \
267 ccc_impl_hrm_insert(hrm_ins_or_assign_hndl_.hrm_, \
268 hrm_ins_or_assign_hndl_.handle_.i_, \
269 hrm_ins_or_assign_hndl_.last_cmp_, \
270 hrm_ins_or_assign_hndl_ret_.i_); \
271 hrm_ins_or_assign_hndl_ret_.stats_ = CCC_ENTRY_VACANT; \
274 else if (hrm_ins_or_assign_hndl_.handle_.stats_ \
275 == CCC_ENTRY_OCCUPIED) \
277 struct ccc_hromap_elem_ ins_hndl_saved_ \
278 = *ccc_impl_hrm_elem_at( \
279 hrm_ins_or_assign_hndl_.hrm_, \
280 hrm_ins_or_assign_hndl_.handle_.i_); \
281 *((typeof(lazy_value) *)ccc_buf_at( \
282 &hrm_ins_or_assign_hndl_.hrm_->buf_, \
283 hrm_ins_or_assign_hndl_.handle_.i_)) \
285 *ccc_impl_hrm_elem_at(hrm_ins_or_assign_hndl_.hrm_, \
286 hrm_ins_or_assign_hndl_.handle_.i_) \
288 hrm_ins_or_assign_hndl_ret_ = (struct ccc_handl_){ \
289 .i_ = hrm_ins_or_assign_hndl_.handle_.i_, \
290 .stats_ = hrm_ins_or_assign_hndl_.handle_.stats_}; \
291 *((typeof(hrm_key_) *)ccc_impl_hrm_key_at( \
292 hrm_ins_or_assign_hndl_.hrm_, \
293 hrm_ins_or_assign_hndl_.handle_.i_)) \
297 hrm_ins_or_assign_hndl_ret_; \
struct ccc_buf_ ccc_buffer
A contiguous block of storage for elements of the same type.
Definition: buffer.h:50
ccc_threeway_cmp ccc_key_cmp_fn(ccc_key_cmp)
A callback function for three-way comparing two stored keys.
Definition: types.h:333
ccc_threeway_cmp
A three-way comparison for comparison functions.
Definition: types.h:138