@@ -80,15 +80,24 @@ ContiguousSlice!(N, T)
8080// / ditto
8181auto slice (SliceKind kind, size_t [] packs, Iterator)(Slice! (kind, packs, Iterator) slice)
8282{
83- alias T = Unqual! (slice.DeepElemType);
84- static if (hasElaborateAssign! T)
85- alias fun = .slice;
83+ if (__ctfe)
84+ {
85+ import mir.ndslice.topology: flattened;
86+ import mir.array.allocation: array;
87+ return slice.flattened.array.sliced(slice.shape);
88+ }
8689 else
87- alias fun = .uninitSlice;
88- auto ret = (shape)@trusted { return fun! T(shape);}(slice.shape);
89- ret[] = slice;
90- auto retq = ()@trusted { return (cast (slice.DeepElemType* )ret._iterator).sliced(ret.shape); }();
91- return retq;
90+ {
91+ import std.backdoor : emplaceRef;
92+ alias E = slice.DeepElemType;
93+
94+ auto result = (() @trusted => slice.shape.uninitSlice! (Unqual! E))();
95+
96+ import mir.ndslice.algorithm: each;
97+ each! ((ref to, auto ref from) => emplaceRef! E(to, from))(result, slice);
98+
99+ return (() @trusted => cast (Slice! (Contiguous, [packs[0 ]], E* )) result)();
100+ }
92101}
93102
94103// /
@@ -197,53 +206,40 @@ makeSlice(T, Allocator, size_t N)(auto ref Allocator alloc, size_t[N] lengths, T
197206 return array.sliced(lengths);
198207}
199208
200- // /// ditto
201- // ContiguousSlice!(N, T)
202- // makeSlice(T,
203- // Flag!`replaceArrayWithPointer` ra = Yes.replaceArrayWithPointer,
204- // Allocator,
205- // SliceKind kind, size_t[] packs, Iterator)(auto ref Allocator alloc, Slice!(kind, packs, Iterator) slice)
206- // {
207- // import std.experimental.allocator : makeArray;
208- // import mir.ndslice.topology : flattened;
209- // auto array = alloc.makeArray!T(slice.flattened);
210- // auto _slice = array.sliced!ra(slice.shape);
211- // return typeof(return)(array, _slice);
212- // }
213-
214- // ///
215- version (mir_test)
216- // @nogc unittest
217- // {
218- // import std.experimental.allocator;
219- // import std.experimental.allocator.mallocator;
220-
221- // auto tup = makeSlice!int(Mallocator.instance, 2, 3, 4);
209+ // / ditto
210+ auto makeSlice (Allocator, SliceKind kind, size_t [] packs, Iterator)
211+ (auto ref Allocator allocator, Slice! (kind, packs, Iterator) slice)
212+ {
213+ import std.backdoor : emplaceRef;
214+ alias E = slice.DeepElemType;
222215
223- // assert(tup.array.length == 24);
224- // assert(tup.slice.elementsCount == 24);
225- // assert(tup.array.ptr == &tup.slice[0, 0, 0]);
216+ auto result = allocator.makeUninitSlice! (Unqual! E)(slice.shape);
226217
227- // //makes duplicate using `makeSlice`
228- // tup.slice[0, 0, 0] = 3;
229- // auto dup = makeSlice(Mallocator.instance, tup.slice);
230- // assert(dup.slice == tup.slice);
218+ import mir.ndslice.algorithm: each;
219+ each! ((ref to, auto ref from) => emplaceRef! E(to, from))(result, slice);
231220
232- // Mallocator.instance.dispose(tup.array);
233- // Mallocator.instance.dispose(dup.array);
234- // }
221+ return cast (Slice! (Contiguous, [packs[0 ]], E* )) result;
222+ }
235223
236224// / Initialization with default value
237225version (mir_test)
238226@nogc unittest
239227{
240228 import std.experimental.allocator ;
241229 import std.experimental.allocator.mallocator ;
230+ import mir.ndslice.algorithm: all;
231+ import mir.ndslice.topology: map;
242232
243- auto sl = makeSlice( Mallocator.instance, [2 , 3 , 4 ], 10 );
233+ auto sl = Mallocator.instance.makeSlice( [2 , 3 , 4 ], 10 );
244234 auto ar = sl.field;
245- assert (sl[1 , 1 , 1 ] == 10 );
235+ assert (sl.all! " a == 10" );
236+
237+ auto sl2 = Mallocator.instance.makeSlice(sl.map! " a * 2" );
238+ auto ar2 = sl2.field;
239+ assert (sl2.all! " a == 20" );
240+
246241 Mallocator.instance.dispose(ar);
242+ Mallocator.instance.dispose(ar2);
247243}
248244
249245version (mir_test)
@@ -270,6 +266,7 @@ Returns:
270266+/
271267ContiguousSlice! (N, T)
272268makeUninitSlice(T, Allocator, size_t N)(auto ref Allocator alloc, size_t [N] lengths... )
269+ if (N)
273270{
274271 if (immutable len = lengthsProduct(lengths))
275272 {
0 commit comments