Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9e86dd4

Browse files
authored
Add minElement / maxElement (#473)
* Add minElement / maxElement * Fixup typo
1 parent b0d1e50 commit 9e86dd4

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

‎source/mir/algorithm/iteration.d

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ $(T2 find, Finds backward index.)
2020
$(T2 findIndex, Finds index.)
2121
$(T2 fold, Accumulates all elements (different parameter order than `reduce`).)
2222
$(T2 isSymmetric, Checks if the matrix is symmetric.)
23+
$(T2 maxElement, Returns the maximum.)
2324
$(T2 maxIndex, Finds index of the maximum.)
2425
$(T2 maxPos, Finds backward index of the maximum.)
26+
$(T2 minElement, Returns the minimum.)
2527
$(T2 minIndex, Finds index of the minimum.)
2628
$(T2 minmaxIndex, Finds indices of the minimum and the maximum.)
2729
$(T2 minmaxPos, Finds backward indices of the minimum and the maximum.)
@@ -4432,3 +4434,86 @@ unittest
44324434
auto z4 = y.minIndex;
44334435
auto z5 = y.maxIndex;
44344436
}
4437+
4438+
/++
4439+
Returns the minimal(maximal) element of a multidimensional slice.
4440+
4441+
Params:
4442+
pred = A predicate.
4443+
4444+
See_also:
4445+
$(LREF minIndex),
4446+
$(LREF maxElement),
4447+
$(LREF maxIndex),
4448+
$(LREF maxPos).
4449+
+/
4450+
template minElement(alias pred = "a < b")
4451+
{
4452+
import mir.functional: naryFun;
4453+
static if (__traits(isSame, naryFun!pred, pred))
4454+
/++
4455+
Params:
4456+
slice = ndslice.
4457+
Returns:
4458+
Minimal(maximal) element of a multidimensional slice
4459+
+/
4460+
@fmamath DeepElementType!(Slice!(Iterator, N, kind)) minElement(Iterator, size_t N, SliceKind kind)(Slice!(Iterator, N, kind) slice)
4461+
{
4462+
return slice[slice.minIndex!pred];
4463+
}
4464+
else
4465+
alias minElement = .minElement!(naryFun!pred);
4466+
}
4467+
4468+
/// ditto
4469+
template maxElement(alias pred = "a < b")
4470+
{
4471+
import mir.functional: naryFun, reverseArgs;
4472+
alias maxElement = minElement!(reverseArgs!(naryFun!pred));
4473+
}
4474+
4475+
///
4476+
@safe pure nothrow
4477+
version(mir_test)
4478+
unittest
4479+
{
4480+
import mir.ndslice.slice: sliced;
4481+
auto s = [
4482+
2, 6, 4, -3,
4483+
0, -4, -3, 3,
4484+
-3, -2, 7, 8,
4485+
].sliced(3, 4);
4486+
4487+
assert(s.minElement == -4);
4488+
assert(s.maxElement == 8);
4489+
}
4490+
4491+
///
4492+
@safe pure nothrow
4493+
version(mir_test)
4494+
unittest
4495+
{
4496+
import mir.ndslice.slice: sliced;
4497+
auto s = [
4498+
-8, 6, 4, -3,
4499+
0, -4, -3, 3,
4500+
-3, -2, 7, 8,
4501+
].sliced(3, 4);
4502+
4503+
assert(s.minElement == -8);
4504+
}
4505+
4506+
@safe pure nothrow
4507+
version(mir_test)
4508+
unittest
4509+
{
4510+
import mir.ndslice.slice: sliced;
4511+
auto s = [
4512+
0, 1, 2, 3,
4513+
4, 5, 6, 7,
4514+
8, 9, 10, 11
4515+
].sliced(3, 4);
4516+
4517+
assert(s.minElement == 0);
4518+
assert(s.maxElement == 11);
4519+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /