@@ -1461,6 +1461,105 @@ struct Slice(SliceKind kind, size_t[] packs, Iterator)
14611461 assert (p.evertPack.elementsCount == 56 );
14621462 }
14631463
1464+ /+ +
1465+ Slice selected dimension.
1466+ Params:
1467+ begin = initial index of the sub-slice (inclusive)
1468+ end = final index of the sub-slice (noninclusive)
1469+ Returns: ndslice with `length!dimension` equal to `end - begin`.
1470+ +/
1471+ auto select (size_t dimension)(size_t begin, size_t end)
1472+ {
1473+ static if (kind == Contiguous && dimension)
1474+ {
1475+ import mir.ndslice.topology: canonical;
1476+ auto ret = this .canonical;
1477+ }
1478+ else
1479+ {
1480+ auto ret = this ;
1481+ }
1482+ auto len = end - begin;
1483+ assert (len <= ret._lengths[dimension]);
1484+ ret._lengths[dimension] = len;
1485+ ret._iterator += ret._stride! dimension * begin;
1486+ return ret;
1487+ }
1488+
1489+ static if (doUnittest)
1490+ // /
1491+ @safe @nogc pure nothrow unittest
1492+ {
1493+ import mir.ndslice.topology : iota;
1494+ auto sl = iota(3 , 4 );
1495+ assert (sl.select! 1 (1 , 3 ) == sl[0 .. $, 1 .. 3 ]);
1496+ }
1497+
1498+ /+ +
1499+ Select the first n elements for the dimension.
1500+ Params:
1501+ dimension = Dimension to slice.
1502+ n = count of elements for the dimension
1503+ Returns: ndslice with `length!dimension` equal to `n`.
1504+ +/
1505+ auto selectFront (size_t dimension)(size_t n)
1506+ {
1507+ static if (kind == Contiguous && dimension)
1508+ {
1509+ import mir.ndslice.topology: canonical;
1510+ auto ret = this .canonical;
1511+ }
1512+ else
1513+ {
1514+ auto ret = this ;
1515+ }
1516+ assert (n <= ret._lengths[dimension]);
1517+ ret._lengths[dimension] = n;
1518+ return ret;
1519+ }
1520+
1521+ static if (doUnittest)
1522+ // /
1523+ @safe @nogc pure nothrow unittest
1524+ {
1525+ import mir.ndslice.topology : iota;
1526+ auto sl = iota(3 , 4 );
1527+ assert (sl.selectFront! 1 (2 ) == sl[0 .. $, 0 .. 2 ]);
1528+ }
1529+
1530+ /+ +
1531+ Select the last n elements for the dimension.
1532+ Params:
1533+ dimension = Dimension to slice.
1534+ n = count of elements for the dimension
1535+ Returns: ndslice with `length!dimension` equal to `n`.
1536+ +/
1537+ auto selectBack (size_t dimension)(size_t n)
1538+ {
1539+ static if (kind == Contiguous && dimension)
1540+ {
1541+ import mir.ndslice.topology: canonical;
1542+ auto ret = this .canonical;
1543+ }
1544+ else
1545+ {
1546+ auto ret = this ;
1547+ }
1548+ assert (n <= ret._lengths[dimension]);
1549+ ret._iterator += ret._stride! dimension * (ret._lengths[dimension] - n);
1550+ ret._lengths[dimension] = n;
1551+ return ret;
1552+ }
1553+
1554+ static if (doUnittest)
1555+ // /
1556+ @safe @nogc pure nothrow unittest
1557+ {
1558+ import mir.ndslice.topology : iota;
1559+ auto sl = iota(3 , 4 );
1560+ assert (sl.selectBack! 1 (2 ) == sl[0 .. $, $ - 2 .. $]);
1561+ }
1562+
14641563 /+ +
14651564 Overloading `==` and `!=`
14661565 +/
@@ -1539,9 +1638,8 @@ struct Slice(SliceKind kind, size_t[] packs, Iterator)
15391638 {
15401639 assert (i <= j,
15411640 " Slice.opSlice!" ~ dimension.stringof ~ " : the left bound must be less than or equal to the right bound." );
1542- enum errorMsg = " : difference between the right and the left bounds"
1543- ~ " must be less than or equal to the length of the given dimension." ;
1544- assert (j - i <= _lengths[dimension],
1641+ enum errorMsg = " : the right must be less than or equal to the length of the given dimension." ;
1642+ assert (j <= _lengths[dimension],
15451643 " Slice.opSlice!" ~ dimension.stringof ~ errorMsg);
15461644 }
15471645 body
@@ -1586,7 +1684,6 @@ struct Slice(SliceKind kind, size_t[] packs, Iterator)
15861684 return Ret (_lengths[I .. N], _strides, _iterator + indexStride(_indexes));
15871685 }
15881686 }
1589-
15901687 }
15911688
15921689 /+ +
0 commit comments