aboutsummaryrefslogtreecommitdiffstats
path: root/include/container.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/container.h')
-rw-r--r--include/container.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/include/container.h b/include/container.h
index 775ffc2..b987d35 100644
--- a/include/container.h
+++ b/include/container.h
@@ -29,6 +29,13 @@ typedef unsigned char byte;
unsigned long __bit_scan_32(int32_t number);
unsigned long __bit_scan_64(int64_t number);
#endif
+#if defined(__clang__) || defined(__GNUC__)
+#define FALLTHROUGH __attribute__((fallthrough))
+#elif defined(_MSC_VER)
+#define FALLTHROUGH __fallthrough()
+#else
+#define FALLTHROUGH ((void)0)
+#endif
typedef int(*qsort_cmp_t)(const void*, const void*);
#define get_qsort_cmp(type) __qsort_cmps[sizeof(type)]
@@ -115,6 +122,7 @@ extern const qsort_cmp_t __qsort_cmps[64];
int array_compare(const void *const a1, const void *const a2, qsort_cmp_t comp);
/// Exact copy of the array. you have to free the pointer as well.
void* array_copy(void* old);
+char array_binary_search(void* array, void* element, qsort_cmp_t cmp);
/* ----------------------------------------------------------------- */
/* ----------------------LINKED LIST HEADER------------------------- */
@@ -340,6 +348,20 @@ int array_compare(const void *const a1, const void *const a2, qsort_cmp_t comp)
/* //TODO!*/
/*}*/
+char array_binary_search(void* array, void* element, qsort_cmp_t cmp) {
+ ssize_t index1 = -1,
+ index2 = array_size(array);
+ if (index2 == 0) return 0;
+ while (1) {
+ ssize_t index3 = (index1+index2)/2;
+ if (index3 == index2 || index3 == index1 || index1 == index2) return 0;
+ int result = cmp(element, (byte*)array + array_element_size(array) * index3);
+ if (result == 0) return 1;
+ if (result < 0) index2 = index3;
+ else if (result > 0) index1 = index3;
+ }
+}
+
#endif // CONTAINER_IMPLEMENTATION