View mode: basic / threaded / horizontal-split · Log in · Help
July 01, 2012
Re: Fixed size multidimensional array at runtime
01.07.2012 13:46, Denis Shelomovskij пишет:
> So I deliberately disallow rule
>   "`matrix[x]` means `matrix[x, R[]...]`"
> and made `byTopDimension` property for such iteration:
> `matrix.byTopDimension[x].byTopDimension[y].byTopDimension[z]`
>
> See:
> http://deoma-cmd.ru/d/docs/src/my/rarray.html#byTopDimension
>

And yes, addition of `alias byTopDimension opIndex;` in my 
`RectangularArray` struct should allow your (inconsistent and 
error-prone, IMHO) syntax.

-- 
Денис В. Шеломовский
Denis V. Shelomovskij
July 01, 2012
Re: Fixed size multidimensional array at runtime
On Sunday, 1 July 2012 at 09:52:22 UTC, Denis Shelomovskij wrote:
> 01.07.2012 13:46, Denis Shelomovskij пишет:
>> So I deliberately disallow rule
>>  "`matrix[x]` means `matrix[x, R[]...]`"
>> and made `byTopDimension` property for such iteration:
>> `matrix.byTopDimension[x].byTopDimension[y].byTopDimension[z]`
>>
>> See:
>> http://deoma-cmd.ru/d/docs/src/my/rarray.html#byTopDimension
>>
>
> And yes, addition of `alias byTopDimension opIndex;` in my 
> `RectangularArray` struct should allow your (inconsistent and 
> error-prone, IMHO) syntax.

Just a remark: I like this approach more than the one I provided. 
Mine is simpler, but this one seems to be more robust and generic.
July 01, 2012
Re: Fixed size multidimensional array at runtime
On Sunday, 1 July 2012 at 09:46:52 UTC, Denis Shelomovskij wrote:
> I'm curious why do you need such syntax? `matrix[x][y][z]` 
> expression means that `matrix[x]` is also a valid expression 
> but it shouldn't.

It's not a syntax I need, it's a syntax I desire as that's the 
syntax I'm used to for multidimensional arrays.
July 01, 2012
Re: Fixed size multidimensional array at runtime
01.07.2012 16:02, Vidar Wahlberg пишет:
> On Sunday, 1 July 2012 at 09:46:52 UTC, Denis Shelomovskij wrote:
>> I'm curious why do you need such syntax? `matrix[x][y][z]` expression
>> means that `matrix[x]` is also a valid expression but it shouldn't.
>
> It's not a syntax I need, it's a syntax I desire as that's the syntax
> I'm used to for multidimensional arrays.

No, that is the syntax you used for arrays of arrays.

-- 
Денис В. Шеломовский
Denis V. Shelomovskij
July 01, 2012
Re: Fixed size multidimensional array at runtime
On Sunday, 1 July 2012 at 12:21:59 UTC, Denis Shelomovskij wrote:
> No, that is the syntax you used for arrays of arrays.

In D, yes. In other languages I'm familiar with, such as Java, 
that syntax is used for "rectangular" arrays. I've grown to like 
the syntax as used in Java and I wanted to know if it was 
possible to achieve it in D, Jonathan answered the question 
perfectly.
July 01, 2012
Re: Fixed size multidimensional array at runtime
On 06/30/12 22:06, Vidar Wahlberg wrote:
> Although it seems to me that you still end up with "matrix[x, y, z]" instead of "matrix[x][y][z]", so it will only solve one half of the problem :)
> For this particular case I'll just do the conversion from two-dimensional to one-dimensional array programmatically and use a "get(x, y)"-method,

"matrix[x,y,z]" is a problem, yet "matrix.get(x,y,z)" is fine?

Anyway, converting one syntax to the other is trivial - see example below.
You also get the "matrix[x,y][z]" and "matrix[x][y,z]" syntax as a bonus.

All of the neatIdx glue gets optimized away, so in this case there's zero
extra overhead - but this is not something I'd count on in general.
It actually surprised me how well gcc manages to handle it; even in the
variable-indexes case the compiler only emits a few adds and shifts.

artur

  import std.stdio;

  template IDS(A...) { alias A IDS; }

  static struct NeatIndex(A, uint N=0) {
     import std.traits;
     A* a;
     ParameterTypeTuple!(A.opIndex)[0..$-1] idxs;
     auto ref opIndex(IDXS...)(IDXS args) if (N+args.length==idxs.length+1) {
        return (*a)[idxs[0..$-args.length+1], args];
     }
     auto ref opIndex(IDXS...)(IDXS args) if (N+args.length<idxs.length+1) {
        idxs[N..N+args.length] = args;
        return *cast(NeatIndex!(A, N+args.length)*)&this;
     }
  }
  
  NeatIndex!A neatIdx(A)(ref A a) {
     NeatIndex!A na = {&a};
     return na;
  }

  void main() {
     static struct A {
        int[3][3][3] data;
        auto ref opIndex(size_t x, size_t y, size_t z) {
           return data[x][y][z];
        }
     }

     A a;

     foreach (z; 0..3) foreach(y; 0..3) foreach(x; 0..3)
        a[z,y,x] = 100*z+10*y+x;

     static assert(!__traits(compiles, a[2][1][0]));
     
     auto b = neatIdx(a);
     writeln(b[2,1,0]);
     writeln(b[2,1][0]);
     writeln(b[2][1,0]);
     writeln(b[2][1][0]);

     writeln(neatIdx(a)[2][1][0]);
  }
July 02, 2012
Re: Fixed size multidimensional array at runtime
On Sunday, 1 July 2012 at 12:21:59 UTC, Denis Shelomovskij wrote:
> No, that is the syntax you used for arrays of arrays.

In D, yes. In other languages I'm familiar with, such as Java, 
that syntax is used for "rectangular" arrays. I've grown to like 
the syntax as used in Java and I wanted to know if it was 
possible to achieve it in D.


On Sunday, 1 July 2012 at 13:31:12 UTC, Artur Skawina wrote:
> "matrix[x,y,z]" is a problem, yet "matrix.get(x,y,z)" is fine?

"[x, y, z]" is not a syntax I'm used to, whereas "get(x, y, z)" 
(or preferably "[x][y][z]") is. It really is just a matter of 
preference, maybe I can get used to it when I'm more experienced 
with D.

I appreciate all the answers and suggestions, my question was 
already answered very well by Jonathan, so I'm going to leave 
this discussion here.
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home