DMD 0.177 release [Length in slice expressions]

Oskar Linde oskar.lindeREM at OVEgmail.com
Tue Dec 26 07:03:35 PST 2006


== Quote from Bill Baxter (dnewsgroup at billbaxter.com)'s article
> Oskar Linde wrote:
>> Bill Baxter wrote:
>>> After trying to write a multi-dimensional array class, my opinion is
>>> that D slice support could use some upgrades overall.
>>
>> I'd be very interested in looking at what you've come up with. With my
>> own implementation of a multi-dimensional array type a couple of months
>> ago, I came to the same conclusion. I posted about it in:
>>
>> news://news.digitalmars.com:119/edrv0n$hth$1@digitaldaemon.com
>> http://www.digitalmars.com/d/archives/digitalmars/D/announce/4717.html
>>
>>> What I'd like to see:
>>>
>> --MultiRange Slice--
>>> * A way to have multiple ranges in a slice, and a mix slice of and
>>> non-slice indices:
>>>     A[i..j, k..m]
>>>     A[i..j, p, k..m]
>> (snip)
>>>     A[0..$,3..$]
>>
>> Yes, I would too. It is quite frustrating having the syntax in the
>> language but not being allowed to utilize it... :)
>>
>> I work around this by instead using a custom slice syntax instead:
>>
>> A[range(i,j), range(k,m)]
>> A[range(i,j), p, range(k,m)]
>> A[range(0,end), range(3..end)]
>> A[end-1, p % end]
> Yeh, that's similar to what I'm doing too.  But it's pretty ugly.  So I
> guess that means you're using opIndex for everything and leaving opSlice
> alone.

Yes. Apart from the no-argument opSlice and opSliceAssign, that I overload
with the same meaning as for the built in arrays. opSlice is not designed
to support more than one dimension.

> Are you able to have ranges return arrays and specific indexes
> return scalar values that way?

Yes.

> That seems to me a big reason for having
> opSlice exist in the first place.  The .. in the brackets not only means
> you're slicing, it also means the function should return another array,
> versus returning an element.  That seems like a nice distinction to have
> to me.

Yes, definitely. My implementation mimics the behavior of the D native
array/slice type (T[]), but extended for multiple dimensions and strides.

So given Array!(2,int) A,  A[all,5] and A[5,all] are both Array!(1,int)
of the 6-th row and column of A. A[5,5] is an int. Some other neat
features made possible by strided arrays are for instance A.diag, that
returns the 1-dimensional diagonal of the array, so eg:

A[] = 0;
A.diag[] = 1;

would construct the unit matrix.

>> Basicly, the transformation is:
>>
>> $ => end
>> a..b => range(a,b)
>>
>> I briefly described this in:
>> news://news.digitalmars.com:119/eft9id$2aq3$1@digitaldaemon.com
> Thanks for the link.  The 'end' thing isn't so bad, at least for a
> former Matlab user. :-)

I wish I too could call myself a _former_ such. :)

/Oskar



More information about the Digitalmars-d-announce mailing list