summaryrefslogtreecommitdiff
path: root/source/kit/lower_bound.h
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2023-12-29 06:21:33 +0100
committerMitya Selivanov <automainint@guattari.tech>2023-12-29 06:21:33 +0100
commit2d6c8fec45b23a8a28668ecf3ef281139ab778a7 (patch)
tree75d2a8538992129a83c0c2b83688289443d697e5 /source/kit/lower_bound.h
parent820b171245f2f14766f3accdb0246a4e2c0d596a (diff)
downloadsaw-2d6c8fec45b23a8a28668ecf3ef281139ab778a7.zip
refactor dependencies; include dependencies source code
Diffstat (limited to 'source/kit/lower_bound.h')
-rw-r--r--source/kit/lower_bound.h44
1 files changed, 44 insertions, 0 deletions
diff --git a/source/kit/lower_bound.h b/source/kit/lower_bound.h
new file mode 100644
index 0000000..2ee04b9
--- /dev/null
+++ b/source/kit/lower_bound.h
@@ -0,0 +1,44 @@
+#ifndef KIT_LOWER_BOUND_H
+#define KIT_LOWER_BOUND_H
+
+#include "types.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define KIT_LOWER_BOUND_INL(return_val, size, ...) \
+ do { \
+ i64 position_ = 0; \
+ i64 count_ = (size); \
+ while (count_ > 0) { \
+ i64 delta_ = count_ / 2; \
+ i64 index_ = position_ + delta_; \
+ if (__VA_ARGS__) { \
+ 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)))
+
+#ifdef __cplusplus
+}
+#endif
+
+#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
+
+#endif