diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-12-31 15:57:13 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-12-31 15:57:13 -0500 |
| commit | 8df0cc41c95e6c01d9c4c0fd1f57fbe96f40bbe7 (patch) | |
| tree | b185443288bc600f04ba88c58d686b4b1e3fcea3 /stdlib | |
| parent | 156d54a73e005eecbb9a4284b74994313a34e4aa (diff) | |
Array:heap_pop() now returns an optional value
Diffstat (limited to 'stdlib')
| -rw-r--r-- | stdlib/arrays.h | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/stdlib/arrays.h b/stdlib/arrays.h index f8921e0d..cf1f088b 100644 --- a/stdlib/arrays.h +++ b/stdlib/arrays.h @@ -107,9 +107,13 @@ void Array$heapify(Array_t *heap, Closure_t comparison, int64_t padded_item_size void Array$heap_push(Array_t *heap, const void *item, Closure_t comparison, int64_t padded_item_size); #define Array$heap_push_value(heap, _value, comparison, padded_item_size) ({ __typeof(_value) value = _value; Array$heap_push(heap, &value, comparison, padded_item_size); }) void Array$heap_pop(Array_t *heap, Closure_t comparison, int64_t padded_item_size); -#define Array$heap_pop_value(heap, comparison, padded_item_size, type) \ - ({ Array_t *_heap = heap; if (_heap->length == 0) fail("Attempt to pop from an empty array"); \ - type value = *(type*)_heap->data; Array$heap_pop(_heap, comparison, padded_item_size); value; }) +#define Array$heap_pop_value(heap, comparison, type, nonnone_var, nonnone_expr, none_expr, padded_item_size) \ + ({ Array_t *_heap = heap; \ + (_heap->length > 0) ? ({ \ + type nonnone_var = *(type*)_heap->data; \ + Array$heap_pop(_heap, comparison, padded_item_size); \ + nonnone_expr; \ + }) : none_expr; }) Int_t Array$binary_search(Array_t array, void *target, Closure_t comparison); #define Array$binary_search_value(array, target, comparison) \ ({ __typeof(target) _target = target; Array$binary_search(array, &_target, comparison); }) |
