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 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
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--e0cb4efe2f4efebedd04cedf72b7
Content-Type: text/plain; charset=UTF-8

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. --e0cb4efe2f4efebedd04cedf72b7 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable I think it would be better to make the slice syntax (a..b) a type of its ow= n. 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 a= llow one to construct slices of user-defined types (e.g. integers of specif= ic 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 mag= ical syntax for slices) become a legitimate overloadable binary operator. I= think we could do much more interesting things, then merely multidimension= al slices with a slice operator.<div class=3D"gmail_extra"> <br><br><div class=3D"gmail_quote">On Mon, Nov 19, 2012 at 11:25 PM, Mike W= ey <span dir=3D"ltr">&lt;<a href=3D"mailto:mike-wey example.com" target=3D"= _blank">mike-wey example.com</a>&gt;</span> wrote:<br><blockquote class=3D"= gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-= left:1ex"> <div class=3D"im">On 11/19/2012 07:04 PM, H. S. Teoh wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> I&#39;m experimenting with implementing &quot;true&quot; multidimensional a= rrays in D.<br> It&#39;s actually very nice, compared with the hassle of memory management<= br> in C/C++, and it even lets you give opIndex multiple parameters so that<br> you can write things like arr[1,2] instead of the uglier arr[1][2] (or<br> worse, arr[2][1]).<br> <br> Two questions, though:<br> <br> 1) Is multidimensional slicing supported? I.e., does opSlice support<br> notation like arr[1..2, 2..3]?<br> </blockquote> <br></div> This is currently not supported. Would be nice to have tough.<div class=3D"= im"><br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> 2) Is opDollar supported for multidimensional arrays? I.e., when you<br> write arr[1..$, 2..$], the $ in each dimension can potentially be<br> different values (say you have a 2x3 array, so the first $ is 2, and the<br=

</blockquote> <br></div> size_t opDollar(int dim)()<br> {<br> <br> }<br> <br> Where dim is the dimension in witch the $ is being used.<div class=3D"HOEnZ= b"><div class=3D"h5"><br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> D will totally rock if these features are supported.<br> <br> <br> T<br> <br> </blockquote> <br> -- <br></div></div><span class=3D"HOEnZb"><font color=3D"#888888"> Mike Wey<br> </font></span></blockquote></div><br><br clear=3D"all"><div><br></div>-- <b= r>Bye,<br>Gor Gyolchanyan.<br> </div> --e0cb4efe2f4efebedd04cedf72b7--
Nov 19 2012
prev sibling next sibling parent "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
prev sibling next sibling 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
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