GSoC: Containers and Ranges

Ishan Thilina ishanthilina at gmail.com
Sat Apr 2 19:42:17 PDT 2011


Hi,

D's Ranges are somewhat like iterators in C++.But only interface is compared to
find out what kind of a range this is ( I think this is called duck typing, not
sure though).There are mainly 4 kinds of ranges.

One passs range

Forward range

Double ended range

Random access range

If I show you the inheritance hierarchy( again, this is not true inheritance :) ),

One pass range<-- Forward range<-- Random access range


One pass range:
Can get a single element from the container. Has three methods.

bool empty() --> Checks to see whether the current element that it's pointing to
is empty( or has been already accessed).

void popFront()--> Points to the next element in the container

Ref(T) front()--> Returns a reference to the current element that the range points to.

Forward range:
Has the same methods as the one pass range but also has an additional method "
ForwardRange save()". This method returns an exact copy of the range.

Double ended range:

Similar to One pass range, but can access data from the back of the container
too.This has the same methods of One pass range and also has these additional methods.

Ref(T) back()
void popBack()

( you can guess what they do, can't you ? ;) )

Random access range:

As the name suggests provides random access to elements of the container( Should
be in constant time I guess). Has these additional methods with respect to Forward
ranges.

Ref(T) at(int i)--> Fetches an element at the given index i.
RandomAccessRange slice(int i, int j)--> Returns a random access range which
points to a sub range lying between indexes i and j.

Hope this helps :)


More information about the Digitalmars-d mailing list