Skip to content

Commit f1e142a

Browse files
committed
wrapper for silcedExactly
1 parent 2da2123 commit f1e142a

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎source/mir/ndslice/slice.d‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,34 @@ auto sliced(size_t N, Iterator)(return scope Iterator iterator, size_t[N] length
299299
assert(slice[1, 2] == 12);
300300
}
301301

302+
/++
303+
Creates an n-dimensional slice-shell over an iterator having the exact elemnt count as that of the array.
304+
Params:
305+
iterator = An iterator, a pointer, or an array.
306+
lengths = A list of lengths for each dimension
307+
Returns:
308+
n-dimensional slice
309+
+/
310+
auto slicedExactly(size_t N, Iterator)(return scope Iterator iterator, size_t[N] lengths...)
311+
if (!__traits(isStaticArray, Iterator) && N
312+
&& !is(Iterator : Slice!(_Iterator, _N, kind), _Iterator, size_t _N, SliceKind kind))
313+
{
314+
static if (isDynamicArray!Iterator)
315+
{
316+
assert(lengthsProduct(lengths) == iterator.length, "array length should be exactly equal to the product of constructed ndslice dimensions");
317+
}
318+
return iterator.sliced(lengths);
319+
}
320+
321+
/// Test case for Issue 313 https://github.com/libmir/mir-algorithm/issues/313
322+
@safe pure nothrow @nogc unittest
323+
{
324+
int[6] values= [1, 2, 3, 4, 5, 6];
325+
auto arr = values[];
326+
auto s = arr.slicedExactly(2, 3);
327+
assert(s.elementCount == arr.length);
328+
}
329+
302330
/++
303331
Creates an 1-dimensional slice-shell over an array.
304332
Params:

0 commit comments

Comments
 (0)