@@ -22,7 +22,7 @@ import core.lifetime: move;
22
22
import mir.internal.utility: isFloatingPoint;
23
23
import mir.math.common: fmamath;
24
24
import mir.math.sum;
25
- import mir.ndslice.slice: Slice, SliceKind;
25
+ import mir.ndslice.slice: Slice, SliceKind, hasAsSlice ;
26
26
import mir.primitives;
27
27
import std.traits : Unqual, isArray, isMutable, isIterable, isIntegral, CommonType;
28
28
@@ -1318,6 +1318,28 @@ template median(bool allowModify = false)
1318
1318
}
1319
1319
}
1320
1320
1321
+ /+ +
1322
+ Params:
1323
+ ar = array
1324
+ +/
1325
+ meanType! (T[]) median (T)(scope const T[] ar... )
1326
+ {
1327
+ import mir.ndslice.slice: sliced;
1328
+
1329
+ alias F = typeof (return );
1330
+ return median! (F, false )(ar.sliced);
1331
+ }
1332
+
1333
+ /+ +
1334
+ Params:
1335
+ withAsSlice = input that satisfies hasAsSlice
1336
+ +/
1337
+ auto median (T)(T withAsSlice)
1338
+ if (hasAsSlice! T)
1339
+ {
1340
+ return median (withAsSlice.asSlice);
1341
+ }
1342
+
1321
1343
// / Median of vector
1322
1344
version (mir_test)
1323
1345
@safe pure nothrow
@@ -1332,6 +1354,18 @@ unittest
1332
1354
assert (x1.median == 5 );
1333
1355
}
1334
1356
1357
+ // / Median of dynamic array
1358
+ version (mir_test)
1359
+ @safe pure nothrow
1360
+ unittest
1361
+ {
1362
+ auto x0 = [9.0 , 1 , 0 , 2 , 3 , 4 , 6 , 8 , 7 , 10 , 5 ];
1363
+ assert (x0.median == 5 );
1364
+
1365
+ auto x1 = [9.0 , 1 , 0 , 2 , 3 , 4 , 6 , 8 , 7 , 10 ];
1366
+ assert (x1.median == 5 );
1367
+ }
1368
+
1335
1369
// / Median of matrix
1336
1370
version (mir_test)
1337
1371
@safe pure
@@ -1384,6 +1418,15 @@ unittest
1384
1418
assert (x1.median! (float , true ) == 5 );
1385
1419
}
1386
1420
1421
+ // / Arbitrary median
1422
+ version (mir_test)
1423
+ @safe pure nothrow
1424
+ unittest
1425
+ {
1426
+ assert (median(0 , 1 , 2 , 3 , 4 ) == 2 );
1427
+ }
1428
+
1429
+ // @nogc test
1387
1430
version (mir_test)
1388
1431
@safe pure nothrow @nogc
1389
1432
unittest
@@ -1411,6 +1454,7 @@ unittest
1411
1454
static assert (is (typeof (y) == double ));
1412
1455
}
1413
1456
1457
+ // additional logic tests
1414
1458
version (mir_test)
1415
1459
@safe pure nothrow
1416
1460
unittest
@@ -1461,6 +1505,7 @@ unittest
1461
1505
assert (x.median! float .approxEqual(2.5 ));
1462
1506
}
1463
1507
1508
+ // smallMedianImpl tests
1464
1509
version (mir_test)
1465
1510
@safe pure nothrow
1466
1511
unittest
@@ -1534,6 +1579,7 @@ F smallMedianImpl(F, Iterator)(Slice!Iterator slice)
1534
1579
}
1535
1580
}
1536
1581
1582
+ // smallMedianImpl tests
1537
1583
version (mir_test)
1538
1584
@safe pure nothrow
1539
1585
unittest
0 commit comments