From 4386f1f005c73f9fd99f90f618b554be341f76f8 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov <0x7fffff@guattari.ru> Date: Tue, 30 Aug 2022 09:01:03 +0400 Subject: Refactor --- source/kit/lower_bound.h | 49 +++++++++++++++++--------------------- source/kit/move_back.h | 61 ++++++++++++++++++++---------------------------- 2 files changed, 47 insertions(+), 63 deletions(-) diff --git a/source/kit/lower_bound.h b/source/kit/lower_bound.h index e87794f..f40c918 100644 --- a/source/kit/lower_bound.h +++ b/source/kit/lower_bound.h @@ -5,37 +5,32 @@ extern "C" { #endif -#define KIT_LOWER_BOUND(return_val, array, value, op) \ - do { \ - ptrdiff_t position_ = 0; \ - ptrdiff_t count_ = (array).size; \ - while (count_ > 0) { \ - ptrdiff_t delta_ = count_ / 2; \ - if ((op) ((array).values[position_ + delta_], (value))) { \ - position_ += delta_ + 1; \ - count_ -= delta_ + 1; \ - } else \ - count_ = delta_; \ - } \ - (return_val) = position_; \ +#define KIT_LOWER_BOUND_INL(return_val, size, ...) \ + do { \ + ptrdiff_t position_ = 0; \ + ptrdiff_t count_ = (size); \ + while (count_ > 0) { \ + ptrdiff_t const delta_ = count_ / 2; \ + ptrdiff_t const index_ = position_ + delta_; \ + if (__VA_ARGS__) { \ + position_ += delta_ + 1; \ + count_ -= delta_ + 1; \ + } else \ + count_ = delta_; \ + } \ + (return_val) = position_; \ } while (0) -#define KIT_LOWER_BOUND_REF(return_val, array, value, op) \ - do { \ - ptrdiff_t position_ = 0; \ - ptrdiff_t count_ = (array).size; \ - while (count_ > 0) { \ - ptrdiff_t delta_ = count_ / 2; \ - if ((op) ((array).values + position_ + delta_, (value))) { \ - position_ += delta_ + 1; \ - count_ -= delta_ + 1; \ - } else \ - count_ = delta_; \ - } \ - (return_val) = position_; \ - } while (0) +#define KIT_LOWER_BOUND(return_val, array, value, op) \ + KIT_LOWER_BOUND_INL(return_val, (array).size, \ + (op) ((array).values[index_], (value))) + +#define KIT_LOWER_BOUND_REF(return_val, array, value, op) \ + KIT_LOWER_BOUND_INL(return_val, (array).size, \ + (op) ((array).values + index_, (value))) #ifndef KIT_DISABLE_SHORT_NAMES +# define LOWER_BOUND_INL KIT_LOWER_BOUND_INL # define LOWER_BOUND KIT_LOWER_BOUND # define LOWER_BOUND_REF KIT_LOWER_BOUND_REF #endif diff --git a/source/kit/move_back.h b/source/kit/move_back.h index cf9cb70..4bcc7ba 100644 --- a/source/kit/move_back.h +++ b/source/kit/move_back.h @@ -7,47 +7,36 @@ extern "C" { #endif -#define KIT_MOVE_BACK(new_size, array, value, cond) \ - do { \ - ptrdiff_t end_ = (array).size; \ - unsigned char temp_[sizeof *(array).values]; \ - for (ptrdiff_t i_ = 0; i_ < end_;) { \ - if ((cond) ((array).values[i_], (value))) { \ - end_--; \ - if (i_ != end_) { \ - memcpy(temp_, (array).values + end_, \ - sizeof *(array).values); \ - (array).values[end_] = (array).values[i_]; \ - memcpy((array).values + i_, temp_, \ - sizeof *(array).values); \ - } \ - } else \ - i_++; \ - } \ - (new_size) = end_; \ +#define KIT_MOVE_BACK_INL(new_size, array, ...) \ + do { \ + ptrdiff_t end_ = (array).size; \ + unsigned char temp_[sizeof *(array).values]; \ + for (ptrdiff_t index_ = 0; index_ < end_;) { \ + if (__VA_ARGS__) { \ + end_--; \ + if (index_ != end_) { \ + memcpy(temp_, (array).values + end_, \ + sizeof *(array).values); \ + (array).values[end_] = (array).values[index_]; \ + memcpy((array).values + index_, temp_, \ + sizeof *(array).values); \ + } \ + } else \ + index_++; \ + } \ + (new_size) = end_; \ } while (0) +#define KIT_MOVE_BACK(new_size, array, value, cond) \ + KIT_MOVE_BACK_INL(new_size, array, \ + (cond) ((array).values[index_], (value))) + #define KIT_MOVE_BACK_REF(new_size, array, value, cond) \ - do { \ - ptrdiff_t end_ = (array).size; \ - unsigned char temp_[sizeof *(array).values]; \ - for (ptrdiff_t i_ = 0; i_ < end_;) { \ - if ((cond) ((array).values + i_, (value))) { \ - end_--; \ - if (i_ != end_) { \ - memcpy(temp_, (array).values + end_, \ - sizeof *(array).values); \ - (array).values[end_] = (array).values[i_]; \ - memcpy((array).values + i_, temp_, \ - sizeof *(array).values); \ - } \ - } else \ - i_++; \ - } \ - (new_size) = end_; \ - } while (0) + KIT_MOVE_BACK_INL(new_size, array, \ + (cond) ((array).values + index_, (value))) #ifndef KIT_DISABLE_SHORT_NAMES +# define MOVE_BACK_INL KIT_MOVE_BACK_INL # define MOVE_BACK KIT_MOVE_BACK # define MOVE_BACK_REF KIT_MOVE_BACK_REF #endif -- cgit v1.2.3