Skip to content

Commit

Permalink
add iotaSlice variant with step
Browse files Browse the repository at this point in the history
  • Loading branch information
9il committed Jul 30, 2016
1 parent 69c00bc commit 945feb7
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions std/experimental/ndslice/selection.d
Original file line number Diff line number Diff line change
Expand Up @@ -1724,7 +1724,8 @@ For a multidimensional index, see $(LREF indexSlice).
Params:
N = dimension count
lengths = list of dimension lengths
shift = value of the first element in a slice
shift = value of the first element in a slice (optional)
step = value of the step between elements (optional)
Returns:
`N`-dimensional slice composed of indexes
See_also: $(LREF IotaSlice), $(LREF indexSlice)
Expand All @@ -1742,6 +1743,15 @@ IotaSlice!N iotaSlice(size_t N)(auto ref size_t[N] lengths, size_t shift = 0)
with (typeof(return)) return Range.init.sliced(lengths, shift);
}

///ditto
IotaSlice!N iotaSlice(size_t N)(auto ref size_t[N] lengths, size_t shift, size_t step)
{
auto iota = iotaSlice(lengths, shift);
foreach (i; Iota!(0, N))
iota._strides[i] *= step;
return iota;
}

///
@safe pure nothrow @nogc unittest
{
Expand All @@ -1759,7 +1769,7 @@ IotaSlice!N iotaSlice(size_t N)(auto ref size_t[N] lengths, size_t shift = 0)
}

///
@safe pure nothrow unittest
@safe pure nothrow @nogc unittest
{
auto im = iotaSlice([10, 5], 100);

Expand All @@ -1770,6 +1780,15 @@ IotaSlice!N iotaSlice(size_t N)(auto ref size_t[N] lengths, size_t shift = 0)
assert(cm[2, 1] == 119); // 119 = 100 + (1 + 2) * 5 + (3 + 1)
}

/// `iotaSlice` with step
@safe pure nothrow unittest
{
auto sl = iotaSlice([2, 3], 10, 10);

assert(sl == [[10, 20, 30],
[40, 50, 60]]);
}

/++
Slice composed of flattened indexes.
See_also: $(LREF iotaSlice)
Expand Down

0 comments on commit 945feb7

Please sign in to comment.