@@ -109,7 +109,6 @@ T4=$(TR $(TDNW $(LREF $1)) $(TD $2) $(TD $3) $(TD $4))
109
109
+/
110
110
module mir.ndslice.topology ;
111
111
112
- import std.meta ;
113
112
114
113
import mir.internal.utility;
115
114
import mir.math.common: optmath;
@@ -121,6 +120,7 @@ import mir.ndslice.slice;
121
120
import mir.primitives;
122
121
import mir.qualifier;
123
122
import mir.utility: min;
123
+ import std.meta : AliasSeq, allSatisfy, staticMap, templateOr, Repeat;
124
124
125
125
private immutable choppedExceptionMsg = " bounds passed to chopped are out of sliceable bounds." ;
126
126
version (D_Exceptions) private immutable choppedException = new Exception (choppedExceptionMsg);
@@ -3928,7 +3928,7 @@ Returns a slice that can be iterated along dimension. Transposes other dimension
3928
3928
Combines $(LREF byDim) and $(LREF evertPack).
3929
3929
3930
3930
Params:
3931
- Dimensions = dimensions to iterate along, length of d, `1 <= d < n`
3931
+ SDimensions = dimensions to iterate along, length of d, `1 <= d < n`. Negative dimensions are supported.
3932
3932
Returns:
3933
3933
`(n-d)`-dimensional slice composed of d-dimensional slices
3934
3934
See_also:
@@ -3938,15 +3938,11 @@ See_also:
3938
3938
$(LREF ipack),
3939
3939
$(SUBREF dynamic, transposed).
3940
3940
+/
3941
- template alongDim (Dimensions ... )
3942
- if (Dimensions .length > 0 )
3941
+ template alongDim (SDimensions ... )
3942
+ if (SDimensions .length > 0 )
3943
3943
{
3944
- import mir.ndslice.internal : isSize_t;
3945
- import std.meta : allSatisfy;
3946
-
3947
- static if (allSatisfy! (isSize_t, Dimensions))
3944
+ static if (allSatisfy! (isSizediff_t, SDimensions))
3948
3945
{
3949
- import mir.ndslice.slice : Slice, SliceKind;
3950
3946
/+ +
3951
3947
Params:
3952
3948
slice = input n-dimensional slice, n > d
@@ -3955,30 +3951,15 @@ template alongDim(Dimensions...)
3955
3951
+/
3956
3952
@optmath auto alongDim(Iterator, size_t N, SliceKind kind)
3957
3953
(Slice! (Iterator, N, kind) slice)
3958
- if (N > Dimensions .length)
3954
+ if (N > SDimensions .length)
3959
3955
{
3960
- import mir.ndslice.topology : ipack;
3961
- import mir.ndslice.internal : DimensionsCountCTError;
3962
-
3963
- mixin DimensionsCountCTError;
3964
-
3965
- static if (N == 1 )
3966
- {
3967
- return slice;
3968
- }
3969
- else
3970
- {
3971
- import core.lifetime : move;
3972
- return slice.move.byDim! Dimensions.evertPack;
3973
- }
3956
+ import core.lifetime : move;
3957
+ return slice.move.byDim! SDimensions.evertPack;
3974
3958
}
3975
3959
}
3976
3960
else
3977
3961
{
3978
- import std.meta : staticMap;
3979
- import mir.ndslice.internal : toSize_t;
3980
-
3981
- alias alongDim = .alongDim! (staticMap! (toSize_t, Dimensions));
3962
+ alias alongDim = .alongDim! (staticMap! (toSizediff_t, SDimensions));
3982
3963
}
3983
3964
}
3984
3965
@@ -4006,7 +3987,7 @@ version(mir_test) unittest
4006
3987
// | 4 5 6 7 |
4007
3988
// | 8 9 10 11 |
4008
3989
// ------------
4009
- auto x = slice.alongDim! 1 ;
3990
+ auto x = slice.alongDim! ( - 1 ); // -1 is the last dimension index, the same as 1 for this case.
4010
3991
static assert (is (typeof (x) == Slice! (SliceIterator! (IotaIterator! sizediff_t ), 1 , Universal)));
4011
3992
4012
3993
assert (x.shape == shape3);
@@ -4021,7 +4002,7 @@ version(mir_test) unittest
4021
4002
// | 2 6 10 |
4022
4003
// | 3 7 11 |
4023
4004
// ---------
4024
- auto y = slice.alongDim! 0 ;
4005
+ auto y = slice.alongDim! 0 ; // alongDim!(-2) is the same for matrices.
4025
4006
static assert (is (typeof (y) == Slice! (SliceIterator! (IotaIterator! sizediff_t , 1 , Universal))));
4026
4007
4027
4008
assert (y.shape == shape4);
@@ -4188,7 +4169,7 @@ Returns a slice that can be iterated by dimension. Transposes dimensions on top
4188
4169
Combines $(SUBREF dynamic, transposed), $(LREF ipack), and SliceKind Selectors.
4189
4170
4190
4171
Params:
4191
- Dimensions = dimensions to perform iteration on, length of d, `1 <= d <= n`
4172
+ SDimensions = dimensions to perform iteration on, length of d, `1 <= d <= n`. Negative dimensions are supported.
4192
4173
Returns:
4193
4174
d-dimensional slice composed of `(n-d)`-dimensional slices
4194
4175
See_also:
@@ -4197,15 +4178,11 @@ See_also:
4197
4178
$(LREF ipack),
4198
4179
$(SUBREF dynamic, transposed).
4199
4180
+/
4200
- template byDim (Dimensions ... )
4201
- if (Dimensions .length > 0 )
4181
+ template byDim (SDimensions ... )
4182
+ if (SDimensions .length > 0 )
4202
4183
{
4203
- import mir.ndslice.internal : isSize_t;
4204
- import std.meta : allSatisfy;
4205
-
4206
- static if (allSatisfy! (isSize_t, Dimensions))
4184
+ static if (allSatisfy! (isSizediff_t, SDimensions))
4207
4185
{
4208
- import mir.ndslice.slice : Slice, SliceKind;
4209
4186
/+ +
4210
4187
Params:
4211
4188
slice = input n-dimensional slice, n >= d
@@ -4214,10 +4191,10 @@ template byDim(Dimensions...)
4214
4191
+/
4215
4192
@optmath auto byDim(Iterator, size_t N, SliceKind kind)
4216
4193
(Slice! (Iterator, N, kind) slice)
4217
- if (N >= Dimensions .length)
4194
+ if (N >= SDimensions .length)
4218
4195
{
4219
- import mir.ndslice.topology : ipack;
4220
- import mir.ndslice.internal : DimensionsCountCTError ;
4196
+
4197
+ alias Dimensions = staticMap ! (ShiftNegativeWith ! N, SDimensions) ;
4221
4198
4222
4199
mixin DimensionsCountCTError;
4223
4200
@@ -4278,10 +4255,7 @@ template byDim(Dimensions...)
4278
4255
}
4279
4256
else
4280
4257
{
4281
- import std.meta : staticMap;
4282
- import mir.ndslice.internal : toSize_t;
4283
-
4284
- alias byDim = .byDim! (staticMap! (toSize_t, Dimensions));
4258
+ alias byDim = .byDim! (staticMap! (toSizediff_t, SDimensions));
4285
4259
}
4286
4260
}
4287
4261
@@ -4309,7 +4283,7 @@ version(mir_test) unittest
4309
4283
// | 4 5 6 7 |
4310
4284
// | 8 9 10 11 |
4311
4285
// ------------
4312
- auto x = slice.byDim! 0 ;
4286
+ auto x = slice.byDim! 0 ; // byDim!(-2) is the same for matrices.
4313
4287
static assert (is (typeof (x) == Slice! (SliceIterator! (IotaIterator! sizediff_t ), 1 , Universal)));
4314
4288
4315
4289
assert (x.shape == shape3);
@@ -4324,7 +4298,7 @@ version(mir_test) unittest
4324
4298
// | 2 6 10 |
4325
4299
// | 3 7 11 |
4326
4300
// ---------
4327
- auto y = slice.byDim! 1 ;
4301
+ auto y = slice.byDim! ( - 1 ); // -1 is the last dimension index, the same as 1 for this case.
4328
4302
static assert (is (typeof (y) == Slice! (SliceIterator! (IotaIterator! sizediff_t , 1 , Universal))));
4329
4303
4330
4304
assert (y.shape == shape4);
0 commit comments