Skip to content

Commit

Permalink
Merge pull request #4325 from ZombineDev/patch-4
Browse files Browse the repository at this point in the history
Remove ref from the Slice.this parameters
  • Loading branch information
9il committed May 15, 2016
2 parents 21dcf42 + f4333fa commit 8671e88
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions std/experimental/ndslice/slice.d
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ struct Slice(size_t _N, _Range)
}

static if (!hasPtrBehavior!PureRange)
this(ref in size_t[PureN] lengths, ref in sizediff_t[PureN] strides, PtrShell!PureRange shell)
this(in size_t[PureN] lengths, in sizediff_t[PureN] strides, PtrShell!PureRange shell)
{
foreach (i; Iota!(0, PureN))
_lengths[i] = lengths[i];
Expand All @@ -1201,7 +1201,7 @@ struct Slice(size_t _N, _Range)
strides = strides
range = range or pointer to iterate on
+/
this(ref in size_t[PureN] lengths, ref in sizediff_t[PureN] strides, PureRange range)
this(in size_t[PureN] lengths, in sizediff_t[PureN] strides, PureRange range)
{
foreach (i; Iota!(0, PureN))
_lengths[i] = lengths[i];
Expand All @@ -1213,6 +1213,30 @@ struct Slice(size_t _N, _Range)
_ptr._range = range;
}

/// Creates a 2-dimentional slice with custom strides.
@nogc nothrow pure
unittest
{
import std.experimental.ndslice.selection: byElement;
import std.algorithm.comparison : equal;
import std.range : only;

uint[8] array = [1, 2, 3, 4, 5, 6, 7, 8];
auto slice = Slice!(2, uint*)([2, 2], [4, 1], array.ptr);

assert(&slice[0, 0] == &array[0]);
assert(&slice[0, 1] == &array[1]);
assert(&slice[1, 0] == &array[4]);
assert(&slice[1, 1] == &array[5]);
assert(slice.byElement.equal(only(1, 2, 5, 6)));

array[2] = 42;
assert(slice.byElement.equal(only(1, 2, 5, 6)));

array[1] = 99;
assert(slice.byElement.equal(only(1, 99, 5, 6)));
}

/++
Returns:
Pointer to the first element of a slice if slice is defined as `Slice!(N, T*)`
Expand Down

0 comments on commit 8671e88

Please sign in to comment.