RangeExtra

Denis Koroskin 2korden at gmail.com
Thu Apr 23 11:35:45 PDT 2009


On Thu, 23 Apr 2009 22:12:03 +0400, dsimcha <dsimcha at yahoo.com> wrote:

> I'm loving the new Phobos, and have been playing around with ranges,  
> alias
> this, etc.  I've compiled a list of a few range types that I have at  
> least
> partial implementations of for personal use that were overlooked by  
> Andrei, et
> al. in the initial release of the new Phobos.  Please tell me which of  
> these
> are generally useful and should be cleaned up and released, which ones  
> are too
> niche to belong in Phobos, which ones std.range can already do by some
> slightly non-obvious idiom, and which ones, if any, are already in the  
> works.
>
> CapacityArray:  A range that covers a builtin array and gives it a  
> capacity
> field.  All operations that don't have to do with appending are  
> forwarded to
> the underlying array via alias this (modulo a few compiler bugs),  
> meaning that
> a CapacityArray has an identical compile time interface to a builtin  
> array,
> and is implicitly convertible to one.
>
> Reindex:  Takes a zero-indexed random access range and converts it to a  
> range
> with an arbitrary base index.  Useful for one-indexed arrays to fit
> mathematical notation conventions, etc.
>
> Comb:  Given a random-access range, iterate over all unordered pairs,  
> triples,
> etc. of elements in the underlying range.  front(), opIndex(), etc. would
> return tuples or structs containing static arrays.  Note that to do this
> efficiently, the number of elements (pair, triplet, etc.) would have to  
> be
> known at compile time.
>
> Joint:  Kind of like std.range.Zip, but returns a tuple of values of the
> elements in the underlying ranges instead of a proxy of pointers to the
> elements of the underlying range.  Zip is great for what it's designed  
> for,
> but sometimes the pointer/reference semantics are problematic.
>
> Rotated:  Gives a rotated view of a random access range without  
> modifying the
> underlying range.  Also has a put() method.  Given an underlying range of
> length L, this provides an efficient way to remember the last L elements  
> seen
> by the range:
>
> auto underlying = [1,2,3];
> auto r = rotated(underlying, 1);
>
> foreach(elem; r) {  // Prints "2  3  1".
>     write(elem, "  ");
> }
>
> r.put(5);  // O(1).
> r.put(4);
> r.put(3);
> r.put(2);
> r.put(1);
> foreach(elem; r) {  // Prints "3  2  1".
>     write(elem, "  ");
> }

Great! Personally, I'd love to use CapacityArray, others are pretty cool, too.



More information about the Digitalmars-d mailing list