src/array_list.c

changeset 985
68754c7de906
parent 968
b5814aac3a76
--- a/src/array_list.c	Thu Nov 07 20:22:56 2024 +0100
+++ b/src/array_list.c	Thu Nov 07 22:46:58 2024 +0100
@@ -37,7 +37,7 @@
         void *array,
         size_t capacity,
         size_t elem_size,
-        __attribute__((__unused__)) CxArrayReallocator *alloc
+        cx_attr_unused CxArrayReallocator *alloc
 ) {
     return realloc(array, capacity * elem_size);
 }
@@ -54,7 +54,7 @@
         void *array,
         size_t capacity,
         size_t elem_size,
-        __attribute__((__unused__)) CxArrayReallocator *alloc
+        cx_attr_unused CxArrayReallocator *alloc
 ) {
     // retrieve the pointer to the actual allocator
     const CxAllocator *al = alloc->ptr1;
@@ -326,6 +326,44 @@
     return result < 0 ? (pivot_index - 1) : pivot_index;
 }
 
+size_t cx_array_binary_search(
+        const void *arr,
+        size_t size,
+        size_t elem_size,
+        const void *elem,
+        cx_compare_func cmp_func
+) {
+    size_t index = cx_array_binary_search_inf(
+            arr, size, elem_size, elem, cmp_func
+    );
+    if (index < size &&
+            cmp_func(((const char *) arr) + index * elem_size, elem) == 0) {
+        return index;
+    } else {
+        return size;
+    }
+}
+
+size_t cx_array_binary_search_sup(
+        const void *arr,
+        size_t size,
+        size_t elem_size,
+        const void *elem,
+        cx_compare_func cmp_func
+) {
+    size_t inf = cx_array_binary_search_inf(
+            arr, size, elem_size, elem, cmp_func
+    );
+    if (inf == size) {
+        // no infimum means, first element is supremum
+        return 0;
+    } else if (cmp_func(((const char *) arr) + inf * elem_size, elem) == 0) {
+        return inf;
+    } else {
+        return inf + 1;
+    }
+}
+
 #ifndef CX_ARRAY_SWAP_SBO_SIZE
 #define CX_ARRAY_SWAP_SBO_SIZE 128
 #endif

mercurial