From f1e142aa1c6697cc87fef77ece2db910c54f8ae4 Mon Sep 17 00:00:00 2001 From: Iskaban10 Date: Wed, 26 Aug 2026 00:40:14 +0530 Subject: [PATCH 1/3] wrapper for silcedExactly --- source/mir/ndslice/slice.d | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/source/mir/ndslice/slice.d b/source/mir/ndslice/slice.d index fa8f9e75..3b64bb7d 100644 --- a/source/mir/ndslice/slice.d +++ b/source/mir/ndslice/slice.d @@ -299,6 +299,34 @@ auto sliced(size_t N, Iterator)(return scope Iterator iterator, size_t[N] length assert(slice[1, 2] == 12); } +/++ +Creates an n-dimensional slice-shell over an iterator having the exact elemnt count as that of the array. +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...) + 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); +} + +/// Test case for Issue 313 https://github.com/libmir/mir-algorithm/issues/313 +@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 1-dimensional slice-shell over an array. Params: From c32a3c4624878e20b562fd53196272245d60c9c2 Mon Sep 17 00:00:00 2001 From: Iskaban10 Date: Thu, 27 Aug 2026 13:58:07 +0530 Subject: [PATCH 2/3] added second slicedExactly wrapper for slices --- source/mir/ndslice/slice.d | 80 +++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 28 deletions(-) diff --git a/source/mir/ndslice/slice.d b/source/mir/ndslice/slice.d index 3b64bb7d..b841c231 100644 --- a/source/mir/ndslice/slice.d +++ b/source/mir/ndslice/slice.d @@ -299,34 +299,6 @@ auto sliced(size_t N, Iterator)(return scope Iterator iterator, size_t[N] length assert(slice[1, 2] == 12); } -/++ -Creates an n-dimensional slice-shell over an iterator having the exact elemnt count as that of the array. -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...) - 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); -} - -/// Test case for Issue 313 https://github.com/libmir/mir-algorithm/issues/313 -@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 1-dimensional slice-shell over an array. Params: @@ -390,6 +362,58 @@ Slice!(Iterator, N, kind) assert(e == i+6); } +/++ +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...) + 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); +} + +/// Test case for Issue 313 https://github.com/libmir/mir-algorithm/issues/313 +@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: From 3ec7bb433cbaa282981e64b6fc0810812fc50d4e Mon Sep 17 00:00:00 2001 From: Iskaban10 Date: Thu, 27 Aug 2026 14:01:52 +0530 Subject: [PATCH 3/3] minor change --- source/mir/ndslice/slice.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/mir/ndslice/slice.d b/source/mir/ndslice/slice.d index b841c231..b533eda6 100644 --- a/source/mir/ndslice/slice.d +++ b/source/mir/ndslice/slice.d @@ -362,6 +362,7 @@ 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: @@ -381,7 +382,6 @@ auto slicedExactly(size_t N, Iterator)(return scope Iterator iterator, size_t[N] return iterator.sliced(lengths); } -/// Test case for Issue 313 https://github.com/libmir/mir-algorithm/issues/313 @safe pure nothrow @nogc unittest { int[6] values= [1, 2, 3, 4, 5, 6];