D Ranges in C#

Peter Alexander peter.alexander.au at gmail.com
Sat Jun 1 02:01:37 PDT 2013


On Saturday, 1 June 2013 at 08:12:00 UTC, bearophile wrote:
> David Piepgrass:
>>In fact, most STL algorithms require exactly two iterators--a 
>>range--and none require only a single iterator<
>
> I think there are some C++ data structures that store many 
> single iterators. If you instead store ranges you double the 
> data amount.

Hashmaps would be the most common example. Usually implemented as 
a linked list of key-value pairs along with a vector of list 
iterators.

Iterators are also useful for constructing sub-ranges, which 
proves useful in the implementation of some algorithms. Writing 
std::next_permutation in D with ranges is quiet frustrating 
compared to C++.

https://github.com/D-Programming-Language/phobos/blob/master/std/algorithm.d#L10901
http://gcc.gnu.org/onlinedocs/gcc-4.6.2/libstdc++/api/a01045_source.html#l03619

Notice that in D you need to maintain a count of the elements 
popped off the back and then use takeExactly to retrieve that 
portion again. In C++ you just move the iterators around and 
create "ranges" from pairs of iterators as you need them.

When I implemented nextPermutation in D, I constantly felt as if 
I was fighting with ranges instead of working with them - I knew 
exactly what I wanted to do, but ranges only provide a roundabout 
way of doing it.


More information about the Digitalmars-d mailing list