Skip to content

Commit 64cfcbf

Browse files
committed
Fix #363
1 parent e4d4717 commit 64cfcbf

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

source/mir/ndslice/slice.d

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2929,10 +2929,11 @@ public:
29292929
sl.opIndexOpAssignImplConcatenation!""(concatenation);
29302930
}
29312931

2932+
static if (!isNumeric!DeepElement)
29322933
/++
29332934
Assignment of a value (e.g. a number) to a $(B fully defined slice).
29342935
+/
2935-
void opIndexAssign(T, Slices...)(T value, Slices slices) scope return
2936+
void opIndexAssign(T, Slices...)(T value, Slices slices) scope
29362937
if ((isFullPureSlice!Slices || isIndexedSlice!Slices)
29372938
&& (!isDynamicArray!T || isDynamicArray!DeepElement)
29382939
&& DynamicArrayDimensionsCount!T == DynamicArrayDimensionsCount!DeepElement
@@ -2943,6 +2944,14 @@ public:
29432944
if(!sl.anyRUEmpty)
29442945
sl.opIndexOpAssignImplValue!""(value);
29452946
}
2947+
else
2948+
void opIndexAssign(Slices...)(DeepElement value, Slices slices) scope
2949+
if (isFullPureSlice!Slices || isIndexedSlice!Slices)
2950+
{
2951+
auto sl = this.lightScope.opIndex(slices);
2952+
if(!sl.anyRUEmpty)
2953+
sl.opIndexOpAssignImplValue!""(value);
2954+
}
29462955

29472956
static if (doUnittest)
29482957
///
@@ -2996,6 +3005,17 @@ public:
29963005
}
29973006
return _iterator[indexStride(_indices)] = value;
29983007
}
3008+
///ditto
3009+
auto ref opIndexAssign()(DeepElement value, size_t[N] _indices...) scope return @trusted
3010+
{
3011+
import mir.functional: forward;
3012+
// check assign safety
3013+
static auto ref fun(ref DeepElement t, ref DeepElement v) @safe
3014+
{
3015+
return t = v;
3016+
}
3017+
return _iterator[indexStride(_indices)] = forward!value;
3018+
}
29993019

30003020
static if (doUnittest)
30013021
///
@@ -4080,3 +4100,17 @@ unittest // check it can be compiled
40804100
alias S = Slice!(double*, 2);
40814101
alias D = Variant!S;
40824102
}
4103+
4104+
version(mir_test)
4105+
unittest
4106+
{
4107+
import mir.ndslice;
4108+
4109+
auto matrix = slice!short(3, 4);
4110+
matrix[] = 0;
4111+
matrix.diagonal[] = 1;
4112+
4113+
auto row = matrix[2];
4114+
row[3] = 6;
4115+
assert(matrix[2, 3] == 6); // D & C index order
4116+
}

0 commit comments

Comments
 (0)