intoGenerator

A generator is a range which owns its state (typically a non-reference counted container).

alias intoGenerator = intoUniqueRange

Examples

basics

import std.experimental.allocator.mallocator : Mallocator;
import nxt.container.dynamic_array : DA = DynamicArray;
import std.traits : isIterable;
import std.range.primitives : isInputRange;
alias C = DA!(int, Mallocator);

auto cs = C([11, 13, 15, 17].s).intoUniqueRange;
auto cs2 = C([11, 13, 15, 17].s).intoUniqueRange;
// TODO: instead use auto cs2 = cs.dupShallow;

assert(cs !is cs2);

assert(cs == cs2);
cs2.popFront();
assert(cs2.length == 3);
assert(cs != cs2);

static assert(isInputRange!(typeof(cs)));
static assert(isIterable!(typeof(cs)));

assert(!cs.empty);
assert(cs.length == 4);
assert(cs.front == 11);
assert(cs.back == 17);

cs.popFront();
assert(cs.length == 3);
assert(cs.front == 13);
assert(cs.back == 17);

cs.popBack();
assert(cs.length == 2);
assert(cs.front == 13);
assert(cs.back == 15);

assert(cs.takeFront() == 13);
assert(cs.length == 1);
assert(cs.front == 15);
assert(cs.back == 15);

assert(cs.takeBack() == 15);
assert(cs.length == 0);
assert(cs.empty);

combined with Phobos ranges

import std.experimental.allocator.mallocator : Mallocator;
import nxt.container.dynamic_array : DA = DynamicArray;
alias C = DA!(int, Mallocator);
assert(C([11, 13, 15, 17].s)
	   .intoUniqueRange()
	   .filterUnique!(_ => _ != 11)
	   .mapUnique!(_ => 2*_)
	   .equal([2*13, 2*15, 2*17]));

Meta