opSlice and $

Daniel Keep daniel.keep.lists at gmail.com
Wed Sep 12 07:27:41 PDT 2007



Witold Baryluk wrote:
> [snip]
> 
> Imho this should be a choice of user via some operator overloading.
> like
> char[] opDollar()
> 
> but there will be problem with (hypothetical) multidimensional slices.
> (and also multidimensional arrays).
> 
> c[1..$,2..$]   each of dollar means something different
> 
> c[$/2,$/3].   similary.
> 
> 
> 
> mayby
> 
> type1 opDollar_1()
> type2 opDollar_2()
> 
> 
> but not always second dollar is independend of first.
> 
> 
> for example
> 
> let c be kind od triangular array, then $ here will be like
> second index in first subslice.
> 
> c[1..4, 1..$]          // $ == 4
> c[1..5, 1..$]          // $ == 5
> 
> 
> 
> Creating temporary objects are very very genneral for such
> constructions, but anyway we will need opDollar.

Two ideas:

elemT[] opSlice(Repeat!(offset,2) xr, Repeat!(offset,2) yr);

c[1..$,2..$] --> c.opSlice(offset(1), offset.dollar(0),
                           offset(2), offset.dollar(0));
c[1..$,2..$-1] --> c.opSlice(offset(1), offset.dollar(0),
                             offset(2), offset.dollar(-1));

You could create such a type by sacrificing the upper-most bit of a
size_t type; if that bit is set, treat it as a one's complement signed
offset from whatever "dollar" happens to be.

Other idea:

Tuple!(size_t, size_t) opDollar();

Or, if we aren't allowed to have tuple return types:

void opDollar(out size_t, out size_t);

That gives us multidimensional dollars.  Come to think of it, the second
is probably the better syntax since it will allow for overloading based
on how many dimensions the user is indexing on.  This could be useful
for, say, matrices which could be accessed as columns of vectors, or
individual scalars.

Just something to chew on :)

	-- Daniel



More information about the Digitalmars-d mailing list