Skip to content

Commit f363759

Browse files
committed
fix issue #293
1 parent 1470461 commit f363759

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

source/mir/math/stat.d

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ T4=$(TR $(TDNW $(LREF $1)) $(TD $2) $(TD $3) $(TD $4))
1919
module mir.math.stat;
2020

2121
import core.lifetime: move;
22-
import mir.ndslice.slice: Slice, SliceKind;
22+
import mir.internal.utility: isFloatingPoint;
2323
import mir.math.common: fmamath;
2424
import mir.math.sum;
25+
import mir.ndslice.slice: Slice, SliceKind;
2526
import mir.primitives;
26-
import std.traits: isArray, isMutable, isIterable, isIntegral, CommonType;
27-
import mir.internal.utility: isFloatingPoint;
27+
import std.traits: Unqual, isArray, isMutable, isIterable, isIntegral, CommonType;
2828

2929
///
3030
package(mir)
@@ -1259,23 +1259,24 @@ template median(F, bool allowModify = false)
12591259
Params:
12601260
slice = slice
12611261
+/
1262-
meanType!F median(Iterator, size_t N, SliceKind kind)(Slice!(Iterator, N, kind) slice)
1262+
meanType!F median(Iterator, size_t N, SliceKind kind)(Slice!(Iterator, N, kind) slice) @nogc
12631263
{
12641264
static assert (!allowModify ||
1265-
isMutable!(DeepElementType!(Slice!(Iterator, N, kind))),
1265+
isMutable!(slice.DeepElement),
12661266
"allowModify must be false or the input must be mutable");
12671267
alias G = typeof(return);
12681268
size_t len = slice.elementCount;
12691269
assert(len > 0, "median: slice must have length greater than zero");
12701270

1271-
import mir.ndslice.topology: flattened;
1271+
import mir.ndslice.topology: as, flattened;
12721272

12731273
static if (!allowModify) {
12741274
import mir.ndslice.allocation: rcslice;
12751275

12761276
if (len > 2) {
1277-
auto val = slice.rcslice.flattened;
1278-
auto temp = val.lightScope;
1277+
auto view = slice.lightScope;
1278+
auto val = view.as!(Unqual!(slice.DeepElement)).rcslice;
1279+
auto temp = val.lightScope.flattened;
12791280
return .median!(G, true)(temp);
12801281
} else {
12811282
return mean!G(slice);
@@ -1383,6 +1384,15 @@ unittest
13831384
assert(x1.median!(float, true) == 5);
13841385
}
13851386

1387+
version(mir_test)
1388+
@safe pure nothrow @nogc
1389+
unittest
1390+
{
1391+
import mir.ndslice.slice: sliced;
1392+
static immutable x = [9.0, 1, 0, 2, 3];
1393+
assert(x.sliced.median == 2);
1394+
}
1395+
13861396
/++
13871397
For integral slices, can pass output type as template parameter to ensure output
13881398
type is correct

source/mir/ndslice/sorting.d

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ template partitionAt(alias less = "a < b")
10421042
function is finished.
10431043
+/
10441044
void partitionAt(Iterator, size_t N, SliceKind kind)
1045-
(Slice!(Iterator, N, kind) slice, size_t nth) @trusted nothrow
1045+
(Slice!(Iterator, N, kind) slice, size_t nth) @trusted nothrow @nogc
10461046
{
10471047
import mir.qualifier: lightScope;
10481048
import core.lifetime: move;
@@ -1160,7 +1160,7 @@ unittest {
11601160
assert(checkTopNAll([ 0, 2, 7, 16, 2, 20, 1, 11, 17, 5, 22, 17, 25, 13, 14, 5, 22, 21, 24, 14].sliced));
11611161
}
11621162

1163-
private @trusted pure nothrow
1163+
private @trusted pure nothrow @nogc
11641164
void partitionAtImpl(alias less, Iterator)(
11651165
Iterator loI,
11661166
Iterator hiI,
@@ -1372,7 +1372,7 @@ unittest {
13721372
assert(x[nth] == 10);
13731373
}
13741374

1375-
private @trusted pure nothrow
1375+
private @trusted pure nothrow @nogc
13761376
Iterator partitionAtPartition(alias less, Iterator)(
13771377
ref Iterator frontI,
13781378
ref Iterator lastI,
@@ -1439,7 +1439,7 @@ unittest {
14391439
assert(x[n - 1] == x_sort[n - 1]);
14401440
}
14411441

1442-
private @trusted pure nothrow
1442+
private @trusted pure nothrow @nogc
14431443
Iterator partitionAtPartitionOffMedian(alias less, bool leanRight, Iterator)(
14441444
ref Iterator frontI,
14451445
ref Iterator lastI,

0 commit comments

Comments
 (0)