summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <0x7fffff@guattari.ru>2022-08-14 23:45:41 +0400
committerMitya Selivanov <0x7fffff@guattari.ru>2022-08-14 23:45:41 +0400
commit400ea79a6b5aab8ff200bf65fddba88065623903 (patch)
tree22be20630480188c2f7ba36323015ba71a92cd4d
parent9d52013ba61944ae437078c163e05d504ae989dc (diff)
downloadkit-400ea79a6b5aab8ff200bf65fddba88065623903.zip
Short names for lower bound; Refactor
-rw-r--r--source/kit/async_function.h66
-rw-r--r--source/kit/dynamic_array.h16
-rw-r--r--source/kit/lower_bound.h13
-rw-r--r--source/test/unittests/lower_bound.test.c38
4 files changed, 72 insertions, 61 deletions
diff --git a/source/kit/async_function.h b/source/kit/async_function.h
index b54ab63..d23998a 100644
--- a/source/kit/async_function.h
+++ b/source/kit/async_function.h
@@ -84,35 +84,35 @@ typedef struct {
KIT_CORO(kit_af_void, name_, __VA_ARGS__)
#define KIT_AF_YIELD(...) \
- { \
+ do { \
self->_index = KIT_AF_LINE_(); \
self->return_value = __VA_ARGS__; \
return; \
case KIT_AF_LINE_():; \
- }
+ } while (0)
#define KIT_AF_YIELD_VOID \
- { \
+ do { \
self->_index = KIT_AF_LINE_(); \
return; \
case KIT_AF_LINE_():; \
- }
+ } while (0)
#define KIT_AF_RETURN(...) \
- { \
+ do { \
self->_index = -1; \
self->return_value = __VA_ARGS__; \
return; \
- }
+ } while (0)
#define KIT_AF_RETURN_VOID \
- { \
+ do { \
self->_index = -1; \
return; \
- }
+ } while (0)
#define KIT_AF_AWAIT(promise_) \
- { \
+ do { \
case KIT_AF_LINE_(): \
if ((promise_)._index != -1) { \
self->_index = KIT_AF_LINE_(); \
@@ -121,10 +121,10 @@ typedef struct {
} \
if ((promise_)._index != -1) \
return; \
- }
+ } while (0)
#define KIT_AF_YIELD_AWAIT(promise_) \
- { \
+ do { \
case KIT_AF_LINE_(): \
if ((promise_)._index != -1) { \
self->_index = KIT_AF_LINE_(); \
@@ -133,7 +133,7 @@ typedef struct {
self->return_value = (promise_).return_value; \
return; \
} \
- }
+ } while (0)
#define KIT_AF_TYPE(coro_) struct coro_##_coro_state_
@@ -146,34 +146,38 @@ typedef struct {
promise_ = { KIT_AF_INITIAL(coro_), __VA_ARGS__ }
#define KIT_AF_INIT(promise_, coro_, ...) \
- { \
+ do { \
KIT_AF_CREATE(kit_af_temp_, coro_, __VA_ARGS__); \
(promise_) = kit_af_temp_; \
- }
+ } while (0)
#define KIT_AF_EXECUTION_CONTEXT(promise_, ...) \
- { \
+ do { \
kit_af_execution_context kit_af_temp_ = { ._internal = 0, \
__VA_ARGS__ }; \
(promise_)._context = kit_af_temp_; \
- }
+ } while (0)
#define KIT_AF_RESUME(promise_) \
(promise_)._state_machine(&(promise_), kit_af_request_resume)
-#define KIT_AF_RESUME_N(promises_, size_) \
- for (int kit_af_index_ = 0; kit_af_index_ < (size_); \
- kit_af_index_++) \
- KIT_AF_RESUME((promises_)[kit_af_index_])
+#define KIT_AF_RESUME_N(promises_, size_) \
+ do { \
+ for (int kit_af_index_ = 0; kit_af_index_ < (size_); \
+ kit_af_index_++) \
+ KIT_AF_RESUME((promises_)[kit_af_index_]); \
+ } while (0)
#define KIT_AF_JOIN(promise_) \
((promise_)._state_machine(&(promise_), kit_af_request_join), \
(promise_).return_value)
-#define KIT_AF_JOIN_N(promises_, size_) \
- for (int kit_af_index_ = 0; kit_af_index_ < (size_); \
- kit_af_index_++) \
- KIT_AF_JOIN((promises_)[kit_af_index_])
+#define KIT_AF_JOIN_N(promises_, size_) \
+ do { \
+ for (int kit_af_index_ = 0; kit_af_index_ < (size_); \
+ kit_af_index_++) \
+ KIT_AF_JOIN((promises_)[kit_af_index_]); \
+ } while (0)
#define KIT_AF_RESUME_AND_JOIN(promise_) \
((promise_)._state_machine(&(promise_), \
@@ -181,8 +185,10 @@ typedef struct {
(promise_).return_value)
#define KIT_AF_RESUME_AND_JOIN_N(promises_, size_) \
- KIT_AF_RESUME_N((promises_), (size_)); \
- KIT_AF_JOIN_N((promises_), (size_))
+ do { \
+ KIT_AF_RESUME_N((promises_), (size_)); \
+ KIT_AF_JOIN_N((promises_), (size_)); \
+ } while (0)
#define KIT_AF_RESUME_ALL(promises_) \
KIT_AF_RESUME_N((promises_), \
@@ -199,7 +205,7 @@ typedef struct {
#define KIT_AF_FINISHED(promise_) ((promise_)._index == -1)
#define KIT_AF_FINISHED_N(return_, promises_, size_) \
- { \
+ do { \
(return_) = 1; \
for (int kit_af_index_ = 0; kit_af_index_ < (size_); \
kit_af_index_++) \
@@ -207,14 +213,14 @@ typedef struct {
(return_) = 0; \
break; \
} \
- }
+ } while (0)
#define KIT_AF_FINISHED_ALL(return_, promises_, size_) \
KIT_AF_FINISHED_N((return_), (promises_), \
sizeof(promises_) / sizeof((promises_)[0]))
#define KIT_AF_AWAIT_N(promises_, size_) \
- { \
+ do { \
case KIT_AF_LINE_(): \
self->_index = KIT_AF_LINE_(); \
KIT_AF_RESUME_AND_JOIN_N((promises_), (size_)); \
@@ -222,7 +228,7 @@ typedef struct {
KIT_AF_FINISHED_N(kit_af_done_, (promises_), (size_)); \
if (!kit_af_done_) \
return; \
- }
+ } while (0)
#define KIT_AF_AWAIT_ALL(promises_) \
KIT_AF_AWAIT_N((promises_), \
diff --git a/source/kit/dynamic_array.h b/source/kit/dynamic_array.h
index d9c3f87..409e7df 100644
--- a/source/kit/dynamic_array.h
+++ b/source/kit/dynamic_array.h
@@ -55,11 +55,11 @@ void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size,
/* Destroy dynamic array.
*/
#define KIT_DA_DESTROY(array_) \
- { \
+ do { \
if ((array_).values != NULL) \
(array_).alloc.deallocate((array_).alloc.state, \
(array_).values); \
- }
+ } while (0)
/* Resize dynamic array.
*/
@@ -70,17 +70,17 @@ void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size,
/* Append a value to dynamic array.
*/
#define KIT_DA_APPEND(array_, value_) \
- { \
+ do { \
ptrdiff_t const kit_index_back_ = (array_).size; \
KIT_DA_RESIZE((array_), kit_index_back_ + 1); \
if (kit_index_back_ < (array_).size) \
(array_).values[kit_index_back_] = (value_); \
- }
+ } while (0)
/* Insert a value into dynamic array.
*/
#define KIT_DA_INSERT(array_, index_, value_) \
- { \
+ do { \
ptrdiff_t const kit_index_back_ = (array_).size; \
ptrdiff_t const kit_indert_n_ = (index_); \
KIT_DA_RESIZE((array_), kit_index_back_ + 1); \
@@ -90,16 +90,16 @@ void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size,
(array_).values[kit_i_] = (array_).values[kit_i_ - 1]; \
(array_).values[kit_indert_n_] = (value_); \
} \
- }
+ } while (0)
/* Erase a value from dynamic array.
*/
#define KIT_DA_ERASE(array_, index_) \
- { \
+ do { \
for (ptrdiff_t i_ = (index_) + 1; i_ < (array_).size; i_++) \
(array_).values[i_ - 1] = (array_).values[i_]; \
KIT_DA_RESIZE((array_), (array_).size - 1); \
- }
+ } while (0)
#ifndef KIT_DISABLE_SHORT_NAMES
# define da_void_t kit_da_void_t
diff --git a/source/kit/lower_bound.h b/source/kit/lower_bound.h
index 3fd1b08..e87794f 100644
--- a/source/kit/lower_bound.h
+++ b/source/kit/lower_bound.h
@@ -6,7 +6,7 @@ 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) { \
@@ -18,10 +18,10 @@ extern "C" {
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) { \
@@ -33,7 +33,12 @@ extern "C" {
count_ = delta_; \
} \
(return_val) = position_; \
- }
+ } while (0)
+
+#ifndef KIT_DISABLE_SHORT_NAMES
+# define LOWER_BOUND KIT_LOWER_BOUND
+# define LOWER_BOUND_REF KIT_LOWER_BOUND_REF
+#endif
#ifdef __cplusplus
}
diff --git a/source/test/unittests/lower_bound.test.c b/source/test/unittests/lower_bound.test.c
index 44145f8..6729598 100644
--- a/source/test/unittests/lower_bound.test.c
+++ b/source/test/unittests/lower_bound.test.c
@@ -1,7 +1,7 @@
#include "../../kit/lower_bound.h"
#include "../../kit/array_ref.h"
-#define KIT_TEST_FILE lower_bound
+#define TEST_FILE lower_bound
#include "../../kit_test/test.h"
static int kit_less_int(int left, int right) {
@@ -16,7 +16,7 @@ TEST("lower bound empty") {
AR(ref, int) = { .size = 0, .values = NULL };
ptrdiff_t index;
- KIT_LOWER_BOUND(index, ref, 42, kit_less_int);
+ LOWER_BOUND(index, ref, 42, kit_less_int);
REQUIRE(index == 0);
}
@@ -25,7 +25,7 @@ TEST("lower bound single left") {
AR(ref, int) = { .size = 1, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND(index, ref, 42, kit_less_int);
+ LOWER_BOUND(index, ref, 42, kit_less_int);
REQUIRE(index == 0);
}
@@ -34,7 +34,7 @@ TEST("lower bound single right") {
AR(ref, int) = { .size = 1, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND(index, ref, 43, kit_less_int);
+ LOWER_BOUND(index, ref, 43, kit_less_int);
REQUIRE(index == 1);
}
@@ -43,7 +43,7 @@ TEST("lower bound first of four") {
AR(ref, int) = { .size = 4, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND(index, ref, 1, kit_less_int);
+ LOWER_BOUND(index, ref, 1, kit_less_int);
REQUIRE(index == 0);
}
@@ -52,7 +52,7 @@ TEST("lower bound second of four") {
AR(ref, int) = { .size = 4, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND(index, ref, 2, kit_less_int);
+ LOWER_BOUND(index, ref, 2, kit_less_int);
REQUIRE(index == 1);
}
@@ -61,7 +61,7 @@ TEST("lower bound third of four") {
AR(ref, int) = { .size = 4, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND(index, ref, 3, kit_less_int);
+ LOWER_BOUND(index, ref, 3, kit_less_int);
REQUIRE(index == 2);
}
@@ -70,7 +70,7 @@ TEST("lower bound forth of four") {
AR(ref, int) = { .size = 4, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND(index, ref, 4, kit_less_int);
+ LOWER_BOUND(index, ref, 4, kit_less_int);
REQUIRE(index == 3);
}
@@ -79,7 +79,7 @@ TEST("lower bound fifth of four") {
AR(ref, int) = { .size = 4, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND(index, ref, 5, kit_less_int);
+ LOWER_BOUND(index, ref, 5, kit_less_int);
REQUIRE(index == 4);
}
@@ -88,7 +88,7 @@ TEST("lower bound first of five") {
AR(ref, int) = { .size = 5, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND(index, ref, 1, kit_less_int);
+ LOWER_BOUND(index, ref, 1, kit_less_int);
REQUIRE(index == 0);
}
@@ -97,7 +97,7 @@ TEST("lower bound second of five") {
AR(ref, int) = { .size = 5, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND(index, ref, 2, kit_less_int);
+ LOWER_BOUND(index, ref, 2, kit_less_int);
REQUIRE(index == 1);
}
@@ -106,7 +106,7 @@ TEST("lower bound third of five") {
AR(ref, int) = { .size = 5, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND(index, ref, 3, kit_less_int);
+ LOWER_BOUND(index, ref, 3, kit_less_int);
REQUIRE(index == 2);
}
@@ -115,7 +115,7 @@ TEST("lower bound forth of five") {
AR(ref, int) = { .size = 5, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND(index, ref, 4, kit_less_int);
+ LOWER_BOUND(index, ref, 4, kit_less_int);
REQUIRE(index == 3);
}
@@ -124,7 +124,7 @@ TEST("lower bound fifth of five") {
AR(ref, int) = { .size = 5, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND(index, ref, 5, kit_less_int);
+ LOWER_BOUND(index, ref, 5, kit_less_int);
REQUIRE(index == 4);
}
@@ -133,7 +133,7 @@ TEST("lower bound sixth of five") {
AR(ref, int) = { .size = 5, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND(index, ref, 6, kit_less_int);
+ LOWER_BOUND(index, ref, 6, kit_less_int);
REQUIRE(index == 5);
}
@@ -143,7 +143,7 @@ TEST("lower bound ref first of four") {
AR(ref, int) = { .size = 4, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref);
+ LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref);
REQUIRE(index == 0);
}
@@ -153,7 +153,7 @@ TEST("lower bound ref second of four") {
AR(ref, int) = { .size = 4, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref);
+ LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref);
REQUIRE(index == 1);
}
@@ -163,7 +163,7 @@ TEST("lower bound ref fifth of five") {
AR(ref, int) = { .size = 5, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref);
+ LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref);
REQUIRE(index == 4);
}
@@ -173,6 +173,6 @@ TEST("lower bound ref sixth of five") {
AR(ref, int) = { .size = 5, .values = v };
ptrdiff_t index;
- KIT_LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref);
+ LOWER_BOUND_REF(index, ref, &value, kit_less_int_ref);
REQUIRE(index == 5);
}