Multidimensional slice
Ali Çehreli
acehreli at yahoo.com
Wed Dec 19 11:54:42 PST 2012
On 12/19/2012 11:35 AM, Ali Çehreli wrote:
> On 12/19/2012 01:58 AM, Rafael wrote:
>
> > my ... english.
>
> Thank you. Your English is very well.
Rather, "Your English is very good."
> > //Then I want to do something like
> > x = S[0..$, 1]; //get column
> > S[0..$, 2] = A[0..$, 2]; //get and set column
> > auto B = A[0..10, 0..10]; //get submatrix block of matrix A
>
> I don't know whether all of those are covered but what you need is
> opDollar, which is already implemented on git master:
>
> http://d.puremagic.com/issues/show_bug.cgi?id=3474
>
> Ali
Looks like I misunderstood the question. I don't think there is support
for number ranges when indexing.
Anyway, here is my initial experiment with opDollar():
import std.stdio;
struct S
{
enum width = 3;
enum height = 5;
int[width][height] numbers;
size_t opDollar(size_t dim)() const
{
static if (dim == 0) {
return height;
} else static if (dim == 1) {
return width;
} else {
static assert(false);
}
}
int opIndex(size_t h, size_t w)
{
return numbers[h][w];
}
int opIndexAssign(int value, size_t h, size_t w)
{
return numbers[h][w] = value;
}
}
void main()
{
auto s = S();
s[$-1, $-1] = 42;
assert(s[$-1, $-1] == 42);
writeln(s);
}
Why is opDollar() not documented?
Ali
More information about the Digitalmars-d-learn
mailing list