Skip to content

Commit e34c684

Browse files
committed
@safe ndslice
1 parent cec8f1e commit e34c684

File tree

5 files changed

+295
-241
lines changed

5 files changed

+295
-241
lines changed

source/mir/ndslice/allocation.d

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ auto slice(SliceKind kind, size_t[] packs, Iterator)(Slice!(kind, packs, Iterato
7676
}
7777

7878
///
79-
pure nothrow unittest
79+
@safe pure nothrow unittest
8080
{
8181
auto tensor = slice!int(5, 6, 7);
8282
assert(tensor.length == 5);
@@ -89,7 +89,7 @@ pure nothrow unittest
8989
}
9090

9191
///
92-
pure nothrow unittest
92+
@safe pure nothrow unittest
9393
{
9494
auto tensor = slice([2, 3], 5);
9595
assert(tensor.elementsCount == 2 * 3);
@@ -109,7 +109,7 @@ auto slice(size_t dim, Slices...)(Concatenation!(dim, Slices) concatenation)
109109
return ret;
110110
}
111111

112-
pure nothrow unittest
112+
@safe pure nothrow unittest
113113
{
114114
import mir.ndslice.topology : iota;
115115
auto tensor = iota(2, 3).slice;
@@ -132,7 +132,7 @@ auto uninitializedSlice(T, size_t N)(size_t[N] lengths...)
132132
}
133133

134134
///
135-
pure nothrow unittest
135+
@safe pure nothrow unittest
136136
{
137137
auto tensor = uninitializedSlice!int(5, 6, 7);
138138
assert(tensor.length == 5);
@@ -259,7 +259,7 @@ makeUninitializedSlice(T, Allocator, size_t N)(auto ref Allocator alloc, size_t[
259259
}
260260

261261
///
262-
@nogc unittest
262+
@system @nogc unittest
263263
{
264264
import std.experimental.allocator;
265265
import std.experimental.allocator.mallocator;
@@ -306,7 +306,7 @@ auto ndarray(SliceKind kind, size_t[] packs, Iterator)(Slice!(kind, packs, Itera
306306
}
307307

308308
///
309-
pure nothrow unittest
309+
@safe pure nothrow unittest
310310
{
311311
import mir.ndslice.topology : iota;
312312
auto slice = iota(3, 4);

source/mir/ndslice/dynamic.d

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import std.meta;
8383
import mir.internal.utility;
8484
import mir.ndslice.internal;
8585
import mir.ndslice.slice;
86+
import mir.utility;
8687

8788
@fastmath:
8889

@@ -330,24 +331,15 @@ Returns:
330331
n-dimensional slice of the same type
331332
See_also: $(LREF swapped), $(LREF transposed)
332333
+/
333-
Slice!(Universal, packs, Iterator) everted(size_t[] packs, Iterator)(Slice!(Universal, packs, Iterator) slice)
334+
Slice!(kind, packs, Iterator) everted(size_t[] packs, SliceKind kind, Iterator)(Slice!(kind, packs, Iterator) slice)
335+
if (kind == Universal || kind == Canonical && packs.length > 1)
334336
{
335-
mixin _DefineRet;
336-
with (slice)
337+
with(slice) foreach (i; Iota!(packs[0] / 2))
337338
{
338-
foreach (i; Iota!(packs[0]))
339-
{
340-
ret._lengths[N - 1 - i] = _lengths[i];
341-
ret._strides[N - 1 - i] = _strides[i];
342-
}
343-
foreach (i; Iota!(packs[0], slice.N))
344-
{
345-
ret._lengths[i] = _lengths[i];
346-
ret._strides[i] = _strides[i];
347-
}
348-
ret._iterator = _iterator;
349-
return ret;
339+
swap(_lengths[i], _lengths[packs[0] - i - 1]);
340+
swap(_strides[i], _strides[packs[0] - i - 1]);
350341
}
342+
return slice;
351343
}
352344

353345
///
@@ -362,24 +354,21 @@ Slice!(Universal, packs, Iterator) everted(size_t[] packs, Iterator)(Slice!(Univ
362354
}
363355

364356
private enum _transposedCode = q{
365-
mixin _DefineRet;
366-
with (slice)
357+
size_t[typeof(return).N] lengths_;
358+
ptrdiff_t[max(typeof(return).S, size_t(1))] strides_;
359+
with(slice) foreach (i; Iota!(packs[0]))
367360
{
368-
foreach (i; Iota!(packs[0]))
369-
{
370-
ret._lengths[i] = _lengths[perm[i]];
371-
static if (i < ret.S)
372-
ret._strides[i] = _strides[perm[i]];
373-
}
374-
foreach (i; Iota!(packs[0], slice.N))
375-
{
376-
ret._lengths[i] = _lengths[i];
377-
static if (i < ret.S)
378-
ret._strides[i] = _strides[i];
379-
}
380-
ret._iterator = _iterator;
381-
return ret;
361+
lengths_[i] = _lengths[perm[i]];
362+
static if (i < typeof(return).S)
363+
strides_[i] = _strides[perm[i]];
364+
}
365+
with(slice) foreach (i; Iota!(packs[0], slice.N))
366+
{
367+
lengths_[i] = _lengths[i];
368+
static if (i < typeof(return).S)
369+
strides_[i] = _strides[i];
382370
}
371+
return typeof(return)(lengths_, strides_[0 .. typeof(return).S], slice._iterator);
383372
};
384373

385374
private size_t[N] completeTranspose(size_t N)(size_t[] dimensions)
@@ -536,6 +525,7 @@ Returns:
536525
n-dimensional slice of the same type
537526
+/
538527
Slice!(kind, packs, Iterator) allReversed(SliceKind kind, size_t[] packs, Iterator)(Slice!(kind, packs, Iterator) slice)
528+
@trusted
539529
if (kind == Universal || kind == Canonical && packs.length > 1)
540530
{
541531
foreach (dimension; Iota!(packs[0]))
@@ -571,7 +561,9 @@ template reversed(Dimensions...)
571561
alias reversed = .reversed!(staticMap!(toSize_t, Dimensions));
572562
else
573563
///
574-
@fastmath Slice!(kind, packs, Iterator) reversed(SliceKind kind, size_t[] packs, Iterator)(Slice!(kind, packs, Iterator) slice)
564+
@fastmath Slice!(kind, packs, Iterator)
565+
reversed(SliceKind kind, size_t[] packs, Iterator)(Slice!(kind, packs, Iterator) slice)
566+
@trusted
575567
if (kind == Universal || kind == Canonical)
576568
{
577569
foreach (i, dimension; Dimensions)
@@ -585,6 +577,7 @@ template reversed(Dimensions...)
585577

586578
///ditto
587579
Slice!(kind, packs, Iterator) reversed(SliceKind kind, size_t[] packs, Iterator, size_t M)(Slice!(kind, packs, Iterator) slice, size_t[M] dimensions...)
580+
@trusted
588581
if (kind == Universal || kind == Canonical)
589582
in
590583
{
@@ -602,7 +595,7 @@ body
602595
}
603596

604597
///
605-
pure nothrow unittest
598+
@safe pure nothrow unittest
606599
{
607600
import mir.ndslice.topology: iota, universal;
608601
auto slice = iota([2, 2], 1).universal;
@@ -626,7 +619,7 @@ pure nothrow unittest
626619
}
627620

628621
///
629-
pure nothrow unittest
622+
@safe pure nothrow unittest
630623
{
631624
import mir.ndslice.topology: iota, canonical;
632625
auto slice = iota([2, 2], 1).canonical;

source/mir/ndslice/package.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ pure nothrow unittest
628628
import std.range : iota;
629629
auto r = (10_000L * 2 * 3 * 4).iota;
630630

631-
auto t0 = r.sliced(10, 20, 30, 40);
631+
auto t0 = r.slicedField(10, 20, 30, 40);
632632
assert(t0.length == 10);
633633
assert(t0.length!0 == 10);
634634
assert(t0.length!1 == 20);

0 commit comments

Comments
 (0)