Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions source/mir/ndslice/slice.d
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,58 @@ Slice!(Iterator, N, kind)
assert(e == i+6);
}

/// slicedExactly wrapper function wrt Issue 313 https://github.com/libmir/mir-algorithm/issues/313
/++
Creates an n-dimensional slice-shell over an iterator having the exact elemnt count as that of the iterator.
Params:
iterator = An iterator, a pointer, or an array.
lengths = A list of lengths for each dimension
Returns:
n-dimensional slice
+/
auto slicedExactly(size_t N, Iterator)(return scope Iterator iterator, size_t[N] lengths...)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return scope T[] array, keep it simple

if (!__traits(isStaticArray, Iterator) && N
&& !is(Iterator : Slice!(_Iterator, _N, kind), _Iterator, size_t _N, SliceKind kind))
{
static if (isDynamicArray!Iterator)
{
assert(lengthsProduct(lengths) == iterator.length, "array length should be exactly equal to the product of constructed ndslice dimensions");
}
return iterator.sliced(lengths);
}

@safe pure nothrow @nogc unittest
{
int[6] values= [1, 2, 3, 4, 5, 6];
auto arr = values[];
auto s = arr.slicedExactly(2, 3);
assert(s.elementCount == arr.length);
}

/++
Creates an n-dimensional slice-shell over the 1-dimensional input slice having exact same number of elements as the 1-dimensional input slice.
Params:
slice = slice
lengths = A list of lengths for each dimension.
Returns:
n-dimensional slice
+/
Slice!(Iterator, N, kind) slicedExactly (Iterator, size_t N, SliceKind kind)
(Slice!(Iterator, 1, kind) slice, size_t[N] lengths...)
if (N)
{
assert(lengthsProduct(lengths) == slice.length,"slice length should be exactly equal to the product of constructed ndslice lengths");
return slice.sliced(lengths);
}

@safe pure nothrow version(mir_ndslice_test) unittest
{
import mir.ndslice.topology: iota;
auto a = iota(8).slicedExactly(2, 2, 2);
assert(a.elementCount == 8);
assert(a == [[[0, 1], [2, 3]], [[4, 5], [6, 7]]]);
}

/++
Creates an n-dimensional slice-shell over a field.
Params:
Expand Down
Loading