let Queue be a range (#220)

let Queue be a range
merged-on-behalf-of: BBasile <BBasile@users.noreply.github.com>
This commit is contained in:
Cameron Ross 2019-01-16 00:57:38 -03:30 committed by The Dlang Bot
parent d6300bc52e
commit 71917d501c
2 changed files with 21 additions and 31 deletions

View file

@ -273,14 +273,8 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType))
{
int level;
//Rather ugly, but good enough for now.
//Couldn't be bothered writing a range as events_ should eventually
//become a Phobos queue/linked list.
events_.startIteration();
events_.next();
while(!events_.iterationOver())
foreach(const event; events_.range)
{
const event = events_.next();
static starts = [EventID.documentStart, EventID.sequenceStart, EventID.mappingStart];
static ends = [EventID.documentEnd, EventID.sequenceEnd, EventID.mappingEnd];
if(starts.canFind(event.id)) {++level;}

View file

@ -36,8 +36,6 @@ private:
Node* first_;
// Last element of the linked list - last element added in time (start of the queue).
Node* last_;
// Cursor pointing to the current node in iteration.
Node* cursor_;
// free-list
Node* stock;
@ -119,30 +117,28 @@ public:
freeStock();
}
/// Start iterating over the queue.
void startIteration() @safe pure nothrow @nogc
/// Returns a forward range iterating over this queue.
auto range() @safe pure nothrow @nogc
{
cursor_ = first_;
}
static struct Result
{
private Node* cursor;
/// Returns: The next element in the queue.
ref const(T) next() @safe pure nothrow @nogc
in
void popFront() @safe pure nothrow @nogc
{
assert(!empty);
assert(cursor_ !is null);
cursor = cursor.next_;
}
do
ref T front() @safe pure nothrow @nogc
in(cursor !is null)
{
const previous = cursor_;
cursor_ = cursor_.next_;
return previous.payload_;
return cursor.payload_;
}
/// Returns: true if itrating is not possible anymore, false otherwise.
bool iterationOver() @safe pure nothrow const @nogc
bool empty() @safe pure nothrow @nogc const
{
return cursor_ is null;
return cursor is null;
}
}
return Result(first_);
}
/// Push a new item to the queue.