Creating a Sub-view of a non - RA (hasSlicing) range.

Monarch Dodra monarchdodra at gmail.com
Sat Jun 30 08:15:13 PDT 2012


On Saturday, 30 June 2012 at 14:22:06 UTC, Andrei Alexandrescu 
wrote:
>> 2) The new range is defined as a fixed length from the 
>> beginning of the
>> range, as opposed to start and finish points. If I were to 
>> insert new
>> items into my Slist, the new range would just bump the top 
>> items out of
>> its range.
>
> SList's range is not defined by start and finish points. It's 
> defined as the start point, and the finish point is implicit by 
> use of a sentinel (the null pointer).
Well, what about in the case of a BidirRange from a BDList? 
Surelly, it would be defined by a start and finish point?

Take, on the other hand, creates a range, which is defined by a 
start point, and a fixed length. Inserting elements into the 
middle of the original list would bump elements out of the "take 
range", which remains fixed sized.

On the other hand, had I manually shrunk my BDlistRange until it 
was 5 elements long, and then inserted elements into the midle of 
the list, it would cause my BDListRange to grow, and nothing to 
drop out of it.

On Saturday, 30 June 2012 at 14:22:06 UTC, Andrei Alexandrescu 
wrote:
> On 6/30/12 8:24 AM, monarch_dodra wrote:
>> 3) This doesn't work for BidirectionalRanges: The resulting 
>> range is
>> forward only. For the same reason, it is not possible to have 
>> a TakeLast
>> for bidirectional ranges.
>
> Not sure I understand this, but when we get into the realm of 
> bidir ranges, things get a fair amount better. How would 
> TakeLast work?
>
>
> Andrei

Well, if we forget Slist, and just focus on a standard 
BidirectionalRange, say comming from a DList. Things don't really 
get any better, because take still returns just a ForwardRange.

There is no way of doing, say:

BidirectionalRange aBD = ...;

//Create subrange...
//aBD = take(aBD, 5); //Error, wrong type
auto subrange = take(aBD, 5);

if(subrange.back == 5) //Error, subrange does not have a back 
method.
                        //...But that's strange, because aBD IS a 
bidiretinal range

Since the Take Range it is defined by a begining point and a 
length, there is no way it could give Bidir access.

----
I came to D after reading your talk on ranges, and I really liked 
(and am still enjoying) the concept. However, with C++, when you 
iterate over [First, Last), you are also creating a two new 
ranges: [First, Current) & [Current, Last). D doesn't provide 
that, it only provides a way to shrink the current range and 
create [Current, Last).

Using range, you don't get [First, Current). At best, you only get
a)A forward range only
b)A fixed size range
c)A range of another type


More information about the Digitalmars-d mailing list