opDollar and infinite slices on infinite ranges

Philippe Sigaud philippe.sigaud at gmail.com
Sat Jul 17 13:01:44 PDT 2010


Hi,

I'm trying to find a way to have infinite slices for some infinite ranges.
Like this:

MyInfiniteRange ir;
auto slice1 = ir[2..5]; // standard slice, just define opSlice(i,j) in
MyInfiniteRange. It's a finite range.
auto slice2 = ir[2..$];
'$' means: up to the end. So, it's a semi-infinite slice: I want slice2 to be
an infinite range.
In fact, it should be equivalent to drop(ir, 2) where drop(range, n) drops the
first n elements of range and returns the shortened range.

And also:
auto slice3 = ir[2..$/100-1_000_000]; // the very same, a semi-infinite slice.

My google-fu is weak, I cannot find any indication on opDollar and how to
define it, for D2. So, maybe someone here can help me? Can someone points me
to some documentation?

Right now, here is where I am. Let say I defined opDollar for MyInfiniteRange.
What I want is this:

- the returned value must be some kind of 'integral infinity': ie, it must be
bigger than any other possible index: opCmp should return a positive amount,
whatever integral I throw at it.
- the returned value must allow simple arithmetic operations with integral
types, to allow for '$/2-100'-like expressions. So operations like +,-,*,/ and
% (maybe even ^^) must work. But the result must be also 'infinite' most of
the time. So it's a value that will gobble most operations and most operands
and returns itself unchanged.
- adding or removing any (finite) integral amount must give back this integral
infinity.
- multiplying or dividing by most amounts also.
- multiplying it by 0 should give... an Exception, I suppose? Also, I'm not
clear on ir[0 .. $-$]. Is '$-$' even defined? What about $/$? My gut feeling
is not to authorize such operations, as they are mathematically dodgy. So $+$
is OK, $*$ also, but not the negative operations.
- multiplying it by a negative amount should make it a negative infinity, I
guess... Though I'm not interested there, for indexing.
- in MyIntegralRange, opSlice should be an overload: opSlice(size_t i1, size_t
i2) and opSlice(size_t i1, IntegralInfinity i2), the latter just returns
drop(this, i1).

So, I'm about to define a struct called IntegralInfinity to extend operations
on integral types, with just a boolean flag isPositive, and then lots of
operator overloading. Is that the right way to do it?

As an afterthought, this does not allow slices like ir[$/2..$] or
ir[$-100..$], but I think these should not exist: if ir is truly infinite, I
don't know what ir[$/2..$] is...


Philippe


More information about the Digitalmars-d mailing list