Range objects and fancy indexing -- Re: Removing elements from dynamic arrays?

Bill Baxter wbaxter at gmail.com
Wed Nov 1 08:40:08 PST 2006


Fredrik Olsson wrote:
> Bill Baxter skrev:
> <snip>
> 
>> Note that if 3..5 returned a range object, then there would be no real 
>> need for the opSlice methods.  They would be replaced by opIndex 
>> overloaed for the range type.  E.g opIndex(range r)
>>
> 
> I cut most of it, as you know what I am referring to anyway :).
> 
> 
> Ranges and sets are related, in fact in my own sets implementation I 
> have four kinds of sets:
> - finite sets

Ok.  Is that just numbers or anything?

> - range sets

Ok.  That must be just numbers.  Does that include things like the set 
containing the semi-open real number intervals (1,3] and [5,7)?

> - computer sets

No idea what that is. It sounds like a set of computers, which would be 
hard to implement with just code. :-)

> - functions sets

A set of functions?  A collection of things like 'int function(int bar)'?

> This implantation is however an overkill, and way too slow, as it also 
> covers infinite sets. For a compiler feature finite sets is the only 
> realistic goal. But having a infinite set solution as well in the 
> standard lib is good too.

Haskell's lazy infinite lists are pretty nifty in that regard.


> I think you should be able to have sets of any type, and ranges of any 
> numeric type. For obvious reasons finite sets of integers can be 
> optimized greatly if used having a special case. And in worst case, I 
> can live with only them (As they make up for 99% of real world cases and 
> a templete implementation can fix the rest).

Ok, but I think sets of arbitrary objects are also about as common as 
sets of integers.

> I would suggest declaring a set as this:
> int<> foo = <1,2,3>;
> char<> bar = <'a'..'z', 'A'..'Z'>;

Y'know there's a reason the < and > are available.  It's because their 
use in templates caused annoying ambiguity in C++ when nesting.  Your 
sets notation will have the same problem.

    int<int<>> /*oops!*/ foo = <</*oops*/1,2>,<3,4>>/*oops!*/;

So instead you could follow the lead of templates and use parens with a 
sigil.  Like $!  Looks sort of like an 'S' for set. :-)

    $int foo = $(1,2,3);
    $char bar = $('a'..'z', 'A'..'Z');

Urm, nah.  People are going to start thinking they're reading Perl or 
BASIC code.  The great thing about sets is that 'set' is such a small word.
    auto foo = set([1,2,3]);
    auto bar = set(['a'..'z', 'A'..'Z']);

Why bother with introducing another syntax?

> How a range type can be declared, I am not sure. Writing the literals is 
> obvious with the .. operator. But for declaring?
> int.. foo = 1..42;
> Just does not look right :).

Yeh, that does look odd.  I don't really see a great need for a special 
range syntax.  Have a standard definition for a thing called a 'range', 
and make that be what 1..42 evaluates to.

   range!(int) foo = 1..42;

or

   auto foo = 1..42;

--bb





More information about the Digitalmars-d-learn mailing list