RFC on range design for D2

Fawzi Mohamed fmohamed at mac.com
Wed Sep 10 14:46:46 PDT 2008


I am sorry I hadn't the time t fully follow the discussion, but I took 
some time to actually define how I think a basic iterator should be, in 
this I think I am quite close to Bill and Steven from what I could see.

Again I am not against ranges, ranges are nice, but iterators are more 
general, and in my opinion they should be what foreach targets.
Then ranges can basically trivially be an iterator that has more 
structure (compareLevel= FullOrdering) and more

basic idea:
an iterator has a position in a sequence+ possibility to move into it


Basic Iterator

// return element and go to next
// (reasons: most common use, only one function call (good if not 
inlined), also o for iterators on data that is not stored)
// throw an exception if you iterate past end
T next();
void transferTo(ref R it2) // transfer this iterator to it2 
(un-copyable iterators)
void stop(); // stop the iteration (might release resources)
size_t nElNext(); // number of elements, constant time, 0 if empty
ComparePos comparePos(R it2); // comparison of position, has to be in 
constant time
static const CompareLevel compareLevel; // compare level (for compile 
time choices)
static const SizeLevel sizeLevel; // size level (for compile time choices)

Copyable Generator: Iterator
T value; // return the actual value
opAssign(R); // copies the iterator

A range is obviously also a Basic iterator, but has more structure

* Bidirectional Iterator
size_t nElPrev; // number of elements, constant time, 0 if empty
T prev; // goes to previous element

// constants
enum ComparePos:int {
    Uncomparable, // might be bigger smaller or incompatible (Same only 
if compareLevel==CompareLevel.None)
    Incompatible, // ranges of two different sequences
    Same, // at the same position
    Growing, // in growing order
    Descending // in decreasing order
}
enum CompareLevel:int {
    None, // no comparison
    Equal, // can decide if they are at the same position in constant time
    FullOrdering // can compare all iterators in constant time
}
enum SizeLevel:int{
    Bounded, // finite and known size
    Finite, // finite but possibly unknown size
    MaybeFinite, // maybe finite, maybe infinite
    Infinite // infinite
}
const INFINITE_SIZE=size_t.max;
const MAYBE_INFINITE=size_t.max-1;
const UNKNOWN_FINITE=size_t.max-2;



More information about the Digitalmars-d-announce mailing list