www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Multidimensional array operator overloading

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
I'm experimenting with implementing "true" multidimensional arrays in D.
It's actually very nice, compared with the hassle of memory management
in C/C++, and it even lets you give opIndex multiple parameters so that
you can write things like arr[1,2] instead of the uglier arr[1][2] (or
worse, arr[2][1]).

Two questions, though:

1) Is multidimensional slicing supported? I.e., does opSlice support
notation like arr[1..2, 2..3]?

2) Is opDollar supported for multidimensional arrays? I.e., when you
write arr[1..$, 2..$], the $ in each dimension can potentially be
different values (say you have a 2x3 array, so the first $ is 2, and the
second $ is 3)?

D will totally rock if these features are supported.


T

-- 
Today's society is one of specialization: as you grow, you learn more
and more about less and less. Eventually, you know everything about
nothing.
Nov 19 2012
next sibling parent reply Mike Wey <mike-wey example.com> writes:
On 11/19/2012 07:04 PM, H. S. Teoh wrote:
 I'm experimenting with implementing "true" multidimensional arrays in D.
 It's actually very nice, compared with the hassle of memory management
 in C/C++, and it even lets you give opIndex multiple parameters so that
 you can write things like arr[1,2] instead of the uglier arr[1][2] (or
 worse, arr[2][1]).

 Two questions, though:

 1) Is multidimensional slicing supported? I.e., does opSlice support
 notation like arr[1..2, 2..3]?
This is currently not supported. Would be nice to have tough.
 2) Is opDollar supported for multidimensional arrays? I.e., when you
 write arr[1..$, 2..$], the $ in each dimension can potentially be
 different values (say you have a 2x3 array, so the first $ is 2, and the
 second $ is 3)?
size_t opDollar(int dim)() { } Where dim is the dimension in witch the $ is being used.
 D will totally rock if these features are supported.


 T
-- Mike Wey
Nov 19 2012
next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
I think it would be better to make the slice syntax (a..b) a type of its
own. the foreach could use that type as an aggregate and opSlice (and
friends) would be replaced with an opIndex, which takes a slice. This would
also allow one to construct slices of user-defined types (e.g. integers of
specific ranges) and different type wrappers to allow overloading. To go
one step further, it would be nicer to have the double dot, (which is
currently magical syntax for slices) become a legitimate overloadable
binary operator. I think we could do much more interesting things, then
merely multidimensional slices with a slice operator.


On Mon, Nov 19, 2012 at 11:25 PM, Mike Wey <mike-wey example.com> wrote:

 On 11/19/2012 07:04 PM, H. S. Teoh wrote:

 I'm experimenting with implementing "true" multidimensional arrays in D.
 It's actually very nice, compared with the hassle of memory management
 in C/C++, and it even lets you give opIndex multiple parameters so that
 you can write things like arr[1,2] instead of the uglier arr[1][2] (or
 worse, arr[2][1]).

 Two questions, though:

 1) Is multidimensional slicing supported? I.e., does opSlice support
 notation like arr[1..2, 2..3]?
This is currently not supported. Would be nice to have tough. 2) Is opDollar supported for multidimensional arrays? I.e., when you
 write arr[1..$, 2..$], the $ in each dimension can potentially be
 different values (say you have a 2x3 array, so the first $ is 2, and the
 second $ is 3)?
size_t opDollar(int dim)() { } Where dim is the dimension in witch the $ is being used. D will totally rock if these features are supported.
 T
-- Mike Wey
-- Bye, Gor Gyolchanyan.
Nov 19 2012
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
 1) Is multidimensional slicing supported? I.e., does opSlice 
 support notation like arr[1..2, 2..3]?
I expect this to be eventually added. It seems a purely additive change, and even natural. ------------------- Jonathan M Davis:
 We could then do
 releases even though something as massive as 64-bit Windows 
 support was being
 worked on, because that work would be on a separate branch. But 
 it seems that
 Walter has never worked that way before, and he's having a very 
 hard time adjusting to it.
Walter is sometimes moving slowly, but unlike lot of other people he never stops moving forward. So I think similar branching will happen. Bye, bearophile
Nov 19 2012
prev sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Monday, 19 November 2012 at 18:02:32 UTC, H. S. Teoh wrote:

 2) Is opDollar supported for multidimensional arrays? I.e., 
 when you
 write arr[1..$, 2..$], the $ in each dimension can potentially 
 be
 different values (say you have a 2x3 array, so the first $ is 
 2, and the
 second $ is 3)?
What Mike said. However, complete support for $ is new, and only available in 2.061 alpha. In the current public 2.060, you may get a link error. Off topic: What is the release cycle of D? Seems like I've been on 2.060 forever.
Nov 19 2012
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, November 19, 2012 22:55:50 monarch_dodra wrote:
 Off topic: What is the release cycle of D? Seems like I've been
 on 2.060 forever.
Whenever Walter feels like doing a release. Previously, we were at something like every 2 or 3 months, but that's gotten longer over the last year or two (you can look at the changelog for the actual release dates), and this particular release is delayed big time due to the addition of 64-bit Windows support. Presumably, once Walter feels that that's ready enough for release, then we'll do a release, and further releases will be closer to the 2 to 3 month mark again. However, this _is_ a good example of why we should be using branches more ( the fiasco of custom attributes being recently added to master being another good example). We could then do releases even though something as massive as 64-bit Windows support was being worked on, because that work would be on a separate branch. But it seems that Walter has never worked that way before, and he's having a very hard time adjusting to it. - Jonathan M Davis
Nov 19 2012