mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-28 18:30:06 +00:00
Add intrinsics.type_is_comparable; Add sort.linear_search
This commit is contained in:
@@ -103,6 +103,7 @@ type_is_ordered :: proc($T: typeid) -> bool ---
|
||||
type_is_ordered_numeric :: proc($T: typeid) -> bool ---
|
||||
type_is_indexable :: proc($T: typeid) -> bool ---
|
||||
type_is_sliceable :: proc($T: typeid) -> bool ---
|
||||
type_is_comparable :: proc($T: typeid) -> bool ---
|
||||
type_is_simple_compare :: proc($T: typeid) -> bool --- // easily compared using memcmp
|
||||
type_is_dereferenceable :: proc($T: typeid) -> bool ---
|
||||
type_is_valid_map_key :: proc($T: typeid) -> bool ---
|
||||
|
||||
+11
-1
@@ -291,7 +291,6 @@ compare_strings :: proc(a, b: string) -> int {
|
||||
binary_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
|
||||
where intrinsics.type_is_ordered(T) #no_bounds_check {
|
||||
|
||||
|
||||
n := len(array);
|
||||
switch n {
|
||||
case 0:
|
||||
@@ -327,3 +326,14 @@ binary_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
|
||||
}
|
||||
return -1, false;
|
||||
}
|
||||
|
||||
|
||||
linear_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
|
||||
where intrinsics.type_is_comparable(T) #no_bounds_check {
|
||||
for x, i in array {
|
||||
if x == key {
|
||||
return i, true;
|
||||
}
|
||||
}
|
||||
return -1, false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user