aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2023-12-02 12:58:15 +0000
committerGitHub <noreply@github.com>2023-12-02 12:58:15 +0000
commitabe896a7beb5772e6a73697ec3add4f5afae4870 (patch)
tree65bd706aa7461b0f821454440b84bfe1cb01cca5
parentf00df0afe92ccae8d4776a8bc6f4829a87249043 (diff)
parent5a661dc67b04f066c50d4e55244e8bf9f4a7c7b8 (diff)
Merge pull request #2984 from mtarik34b/add-min-index-and-max-index-procs
Add min_index and max_index procedures
-rw-r--r--core/slice/slice.odin34
1 files changed, 34 insertions, 0 deletions
diff --git a/core/slice/slice.odin b/core/slice/slice.odin
index 64926ddfa..9722150a5 100644
--- a/core/slice/slice.odin
+++ b/core/slice/slice.odin
@@ -512,6 +512,40 @@ min_max :: proc(s: $S/[]$T) -> (min, max: T, ok: bool) where intrinsics.type_is_
return
}
+// Find the index of the (first) minimum element in a slice.
+@(require_results)
+min_index :: proc(s: $S/[]$T) -> (min_index: int, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
+ if len(s) == 0 {
+ return -1, false
+ }
+ min_index = 0
+ min_value := s[0]
+ for v, i in s[1:] {
+ if v < min_value {
+ min_value = v
+ min_index = i+1
+ }
+ }
+ return min_index, true
+}
+
+// Find the index of the (first) maximum element in a slice.
+@(require_results)
+max_index :: proc(s: $S/[]$T) -> (max_index: int, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
+ if len(s) == 0 {
+ return -1, false
+ }
+ max_index = 0
+ max_value := s[0]
+ for v, i in s[1:] {
+ if v > max_value {
+ max_value = v
+ max_index = i+1
+ }
+ }
+ return max_index, true
+}
+
@(require_results)
any_of :: proc(s: $S/[]$T, value: T) -> bool where intrinsics.type_is_comparable(T) {
for v in s {