Skip to content

Commit

Permalink
Merge pull request #4630 from 9il/ndsave
Browse files Browse the repository at this point in the history
fix Issue 16308 - [ndslice] should always has save primitive
  • Loading branch information
dnadlinger committed Jul 31, 2016
2 parents 69c00bc + c58e3fa commit 73e3c70
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 78 deletions.
8 changes: 0 additions & 8 deletions std/experimental/ndslice/internal.d
Expand Up @@ -226,12 +226,4 @@ pure nothrow unittest
assert(lengthsProduct([3, 4, 5]) == 60);
}

enum canSave(T) = isPointer!T || isDynamicArray!T ||
__traits(compiles,
{
T r1 = T.init;
auto s1 = r1.save;
static assert (is(typeof(s1) == T));
});

struct _Slice { size_t i, j; }
13 changes: 2 additions & 11 deletions std/experimental/ndslice/selection.d
Expand Up @@ -933,10 +933,9 @@ auto byElement(size_t N, Range)(auto ref Slice!(N, Range) slice)
size_t _length;
size_t[N] _indexes;

static if (canSave!PureRange)
auto save() @property
{
return typeof(this)(_slice.save, _length, _indexes);
return this;
}

bool empty() const @property
Expand Down Expand Up @@ -1448,10 +1447,9 @@ auto byElementInStandardSimplex(size_t N, Range)(auto ref Slice!(N, Range) slice
size_t sum;
size_t[N] _indexes;

static if (canSave!PureRange)
auto save() @property
{
return typeof(this)(_slice.save, _length, maxHypercubeLength, sum, _indexes);
return this;
}

bool empty() const @property
Expand Down Expand Up @@ -1688,12 +1686,6 @@ template IndexSlice(size_t N)
{
private size_t[N-1] _lengths;

IndexMap save() @property const
{
pragma(inline, true);
return this;
}

size_t[N] opIndex(size_t index) const
{
pragma(inline, true);
Expand Down Expand Up @@ -1785,7 +1777,6 @@ template IotaSlice(size_t N)
struct IotaMap()
{
enum bool empty = false;
enum IotaMap save = IotaMap.init;

static size_t opIndex()(size_t index) @safe pure nothrow @nogc @property
{
Expand Down
62 changes: 3 additions & 59 deletions std/experimental/ndslice/slice.d
Expand Up @@ -296,30 +296,6 @@ pure nothrow unittest
assert(equal(alpha, beta));
}

/// Input range primitives for slices over user defined types
pure nothrow @nogc unittest
{
struct MyIota
{
//`[index]` operator overloading
auto opIndex(size_t index)
{
return index;
}
}

alias S = Slice!(3, MyIota);

import std.range.primitives;
static assert(hasLength!S);
static assert(isInputRange!S);
static assert(isForwardRange!S == false);

auto slice = MyIota().sliced(20, 10);
assert(slice[1, 2] == 12);

}

/// Random access range primitives for slices over user defined types
pure nothrow @nogc unittest
{
Expand All @@ -330,19 +306,12 @@ pure nothrow @nogc unittest
{
return index;
}
// `save` property to allow a slice to be a forward range
auto save() @property
{
return this;
}
}

alias S = Slice!(3, MyIota);
import std.range.primitives;
static assert(hasLength!S);
static assert(hasSlicing!S);
static assert(isForwardRange!S);
static assert(isBidirectionalRange!S);
static assert(isRandomAccessRange!S);

auto slice = MyIota().sliced(20, 10);
Expand Down Expand Up @@ -1358,16 +1327,11 @@ struct Slice(size_t _N, _Range)
}

/++
Range primitive.
Defined only if `Range` is a forward range or a pointer type.
Forward range primitive.
+/
static if (canSave!PureRange)
auto save() @property
{
static if (isPointer!PureRange)
return typeof(this)(_lengths, _strides, _ptr);
else
return typeof(this)(_lengths, _strides, _ptr.save);
return this;
}

static if (doUnittest)
Expand Down Expand Up @@ -2862,13 +2826,9 @@ private struct PtrShell(Range)
}
}

static if (canSave!Range)
auto save() @property
{
static if (isDynamicArray!Range)
return typeof(this)(_shift, _range);
else
return typeof(this)(_shift, _range.save);
return this;
}
}

Expand Down Expand Up @@ -2954,22 +2914,6 @@ private template PtrTuple(Names...)
{
Ptrs ptrs;

static if (allSatisfy!(canSave, Ptrs))
auto save() @property
{
static if (anySatisfy!(hasElaborateAssign, Ptrs))
PtrTuple p;
else
PtrTuple p = void;
foreach (i, ref ptr; ptrs)
static if (isPointer!(Ptrs[i]))
p.ptrs[i] = ptr;
else
p.ptrs[i] = ptr.save;

return p;
}

void opOpAssign(string op)(sizediff_t shift)
if (op == `+` || op == `-`)
{
Expand Down

0 comments on commit 73e3c70

Please sign in to comment.