mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +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_ordered_numeric :: proc($T: typeid) -> bool ---
|
||||||
type_is_indexable :: proc($T: typeid) -> bool ---
|
type_is_indexable :: proc($T: typeid) -> bool ---
|
||||||
type_is_sliceable :: 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_simple_compare :: proc($T: typeid) -> bool --- // easily compared using memcmp
|
||||||
type_is_dereferenceable :: proc($T: typeid) -> bool ---
|
type_is_dereferenceable :: proc($T: typeid) -> bool ---
|
||||||
type_is_valid_map_key :: 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)
|
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 {
|
||||||
|
|
||||||
|
|
||||||
n := len(array);
|
n := len(array);
|
||||||
switch n {
|
switch n {
|
||||||
case 0:
|
case 0:
|
||||||
@@ -327,3 +326,14 @@ binary_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
|
|||||||
}
|
}
|
||||||
return -1, false;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -3755,6 +3755,7 @@ BuiltinTypeIsProc *builtin_type_is_procs[BuiltinProc__type_simple_boolean_end -
|
|||||||
is_type_ordered_numeric,
|
is_type_ordered_numeric,
|
||||||
is_type_indexable,
|
is_type_indexable,
|
||||||
is_type_sliceable,
|
is_type_sliceable,
|
||||||
|
is_type_comparable,
|
||||||
is_type_simple_compare,
|
is_type_simple_compare,
|
||||||
is_type_dereferenceable,
|
is_type_dereferenceable,
|
||||||
is_type_valid_for_keys,
|
is_type_valid_for_keys,
|
||||||
@@ -5568,6 +5569,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
|
|||||||
case BuiltinProc_type_is_ordered_numeric:
|
case BuiltinProc_type_is_ordered_numeric:
|
||||||
case BuiltinProc_type_is_indexable:
|
case BuiltinProc_type_is_indexable:
|
||||||
case BuiltinProc_type_is_sliceable:
|
case BuiltinProc_type_is_sliceable:
|
||||||
|
case BuiltinProc_type_is_comparable:
|
||||||
case BuiltinProc_type_is_simple_compare:
|
case BuiltinProc_type_is_simple_compare:
|
||||||
case BuiltinProc_type_is_dereferenceable:
|
case BuiltinProc_type_is_dereferenceable:
|
||||||
case BuiltinProc_type_is_valid_map_key:
|
case BuiltinProc_type_is_valid_map_key:
|
||||||
|
|||||||
@@ -138,6 +138,7 @@ BuiltinProc__type_simple_boolean_begin,
|
|||||||
BuiltinProc_type_is_ordered_numeric,
|
BuiltinProc_type_is_ordered_numeric,
|
||||||
BuiltinProc_type_is_indexable,
|
BuiltinProc_type_is_indexable,
|
||||||
BuiltinProc_type_is_sliceable,
|
BuiltinProc_type_is_sliceable,
|
||||||
|
BuiltinProc_type_is_comparable,
|
||||||
BuiltinProc_type_is_simple_compare, // easily compared using memcmp
|
BuiltinProc_type_is_simple_compare, // easily compared using memcmp
|
||||||
BuiltinProc_type_is_dereferenceable,
|
BuiltinProc_type_is_dereferenceable,
|
||||||
BuiltinProc_type_is_valid_map_key,
|
BuiltinProc_type_is_valid_map_key,
|
||||||
@@ -315,6 +316,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = {
|
|||||||
{STR_LIT("type_is_ordered_numeric"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("type_is_ordered_numeric"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
{STR_LIT("type_is_indexable"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("type_is_indexable"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
{STR_LIT("type_is_sliceable"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("type_is_sliceable"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
|
{STR_LIT("type_is_comparable"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
{STR_LIT("type_is_simple_compare"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("type_is_simple_compare"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
{STR_LIT("type_is_dereferenceable"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("type_is_dereferenceable"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
{STR_LIT("type_is_valid_map_key"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("type_is_valid_map_key"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
|
|||||||
Reference in New Issue
Block a user