Minor cleanup of slice/slice.odin code

This commit is contained in:
gingerBill
2021-01-09 00:40:30 +00:00
parent 79432be784
commit fba4bfb2d5
+11 -1
View File
@@ -42,6 +42,15 @@ linear_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
return -1, false; 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) binary_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
where intrinsics.type_is_ordered(T) #no_bounds_check { 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 // NOTE(bill): This is technically interpolation search
m := lo + int((key - array[lo]) * T(hi - lo) / (array[hi] - array[lo])); m := lo + int((key - array[lo]) * T(hi - lo) / (array[hi] - array[lo]));
} else { } else {
m := (lo + hi)/2; m := lo + (hi - lo)/2;
} }
switch { switch {
case array[m] < key: case array[m] < key:
@@ -81,6 +90,7 @@ binary_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
return -1, false; return -1, false;
} }
equal :: proc(a, b: $T/[]$E) -> bool where intrinsics.type_is_comparable(E) { equal :: proc(a, b: $T/[]$E) -> bool where intrinsics.type_is_comparable(E) {
if len(a) != len(b) { if len(a) != len(b) {
return false; return false;