Multidimensional slice
H. S. Teoh via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Aug 9 14:02:02 PDT 2014
On Sat, Aug 09, 2014 at 08:43:32PM +0000, Remi Thebault via Digitalmars-d-learn wrote:
> Hello D-community
>
> Sorry to dig an old post, but I have the exact same need.
> I have C++ background and I started to use D a few days ago only
> (a pity I didn't start sooner!)
>
> My needs are mostly around numerical calculations. I have a safe
> and efficient matrix type in C++ that I am porting to D.
>
> Implementation is really easier, no doubt.
> I have coded slicing following this page
> http://dlang.org/operatoroverloading.html#Slice
> but the code doesn't compile.
[...]
> slice opSlice(uint from, uint to)
> {
> return slice(from, to-from);
> }
You need opSlice to take a compile-time integer argument to determine
which dimension you're slicing. Something like this:
slice opSlice(size_t dim)(uint from, uint to)
{
// In this case it's not necessary to actually use dim, but
// the lowering does translate to opSlice!0(...). Defining
// opSlice without dim will only work for 1D arrays.
return slice(from, to-from);
}
[...]
> // m3 should refer to the same slice as m2
> auto m3 = m[2..5, 2..4]; // <- compiler syntax error is here
[...]
I think you need 2.066 or later to get this to work. After adding
(size_t dim) to opSlice, your code compiles fine with git HEAD.
T
--
Give a man a fish, and he eats once. Teach a man to fish, and he will
sit forever.
More information about the Digitalmars-d-learn
mailing list