[Issue 6788] std.range.pairwise

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Dec 6 10:34:26 PST 2014


https://issues.dlang.org/show_bug.cgi?id=6788

--- Comment #7 from hsteoh at quickfur.ath.cx ---
If order doesn't matter, then a series of nested loops should do what you want.
However, we can't actually use nested loops for a generic algorithm, since (1)
the nesting level depends on how many elements you want in the subset, and (2)
we have to lazy-fy the loops to conform to the range API anyway. So I've to
think of some equivalent way of enumerating these subsets...

I'm thinking that a forward range should be sufficient for such an enumeration.
So if we have a stack of .save'd ranges, that should do the trick. First, to
generate k-element subsets, we prime the stack with .save'd copies of the range
up to the k'th element, then .front simply walks the stack and retrieves .front
from each item on the stack. In popFront(), we simply advance the range at the
top of the stack. If it's empty, we pop it off, and advance the previous range
on the stack, then fill up the stack again with the subsequent element. Each
time, if advancing a range makes it empty, we pop it off and advance the
previous range on the stack instead, then fill up the stack again. Yes, I think
this will work... PR time! :-)

--


More information about the Digitalmars-d-bugs mailing list