diff options
| author | gingerBill <bill@gingerbill.org> | 2021-01-09 00:40:30 +0000 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2021-01-09 00:40:30 +0000 |
| commit | fba4bfb2d5568e84f82b3ceb48154427d75337b1 (patch) | |
| tree | ad18f499a0f1155f1b6c231de3a6fcb05db10e99 /core/slice | |
| parent | 79432be784e7d7b25275239ac81ffe94ebe53da9 (diff) | |
Minor cleanup of slice/slice.odin code
Diffstat (limited to 'core/slice')
| -rw-r--r-- | core/slice/slice.odin | 12 |
1 files changed, 11 insertions, 1 deletions
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; |