Browse Source

Add `slice.min` and add `slice.max`

gingerBill 4 years ago
parent
commit
7acbf8b7b9
1 changed files with 24 additions and 0 deletions
  1. 24 0
      core/slice/slice.odin

+ 24 - 0
core/slice/slice.odin

@@ -1,10 +1,12 @@
 package slice
 
 import "intrinsics"
+import "builtin"
 import "core:math/bits"
 import "core:mem"
 
 _ :: intrinsics;
+_ :: builtin;
 _ :: bits;
 _ :: mem;
 
@@ -292,6 +294,28 @@ filter :: proc(s: $S/[]$U, f: proc(U) -> bool, allocator := context.allocator) -
 
 
 
+min :: proc(s: $S/[]$T) -> (res: T, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
+	if len(s) != 0 {
+		res = s[0];
+		ok = true;
+		for v in s[1:] {
+			res = min(res, v);
+		}
+	}
+	return;
+}
+max :: proc(s: $S/[]$T) -> (res: T, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
+	if len(s) != 0 {
+		res = s[0];
+		ok = true;
+		for v in s[1:] {
+			res = max(res, v);
+		}
+	}
+	return;
+}
+
+
 dot_product :: proc(a, b: $S/[]$T) -> T
 	where intrinsics.type_is_numeric(T) {
 	if len(a) != len(b) {