From fba4bfb2d5568e84f82b3ceb48154427d75337b1 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 9 Jan 2021 00:40:30 +0000 Subject: [PATCH] Minor cleanup of slice/slice.odin code --- core/slice/slice.odin | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/core/slice/slice.odin b/core/slice/slice.odin index d6172e27c..504f4f8a7 100644 --- a/core/slice/slice.odin +++ b/core/slice/slice.odin @@ -42,6 +42,15 @@ linear_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool) return -1, false; } +linear_search_proc :: proc(array: $A/[]$T, f: proc(T) -> bool) -> (index: int, found: bool) #no_bounds_check { + for x, i in array { + if f(x) { + return i, true; + } + } + return -1, false; +} + binary_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool) where intrinsics.type_is_ordered(T) #no_bounds_check { @@ -63,7 +72,7 @@ binary_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool) // NOTE(bill): This is technically interpolation search m := lo + int((key - array[lo]) * T(hi - lo) / (array[hi] - array[lo])); } else { - m := (lo + hi)/2; + m := lo + (hi - lo)/2; } switch { case array[m] < key: @@ -81,6 +90,7 @@ binary_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool) return -1, false; } + equal :: proc(a, b: $T/[]$E) -> bool where intrinsics.type_is_comparable(E) { if len(a) != len(b) { return false;