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; int level;
//Rather ugly, but good enough for now. foreach(const event; events_.range)
//Couldn't be bothered writing a range as events_ should eventually
//become a Phobos queue/linked list.
events_.startIteration();
events_.next();
while(!events_.iterationOver())
{ {
const event = events_.next();
static starts = [EventID.documentStart, EventID.sequenceStart, EventID.mappingStart]; static starts = [EventID.documentStart, EventID.sequenceStart, EventID.mappingStart];
static ends = [EventID.documentEnd, EventID.sequenceEnd, EventID.mappingEnd]; static ends = [EventID.documentEnd, EventID.sequenceEnd, EventID.mappingEnd];
if(starts.canFind(event.id)) {++level;} if(starts.canFind(event.id)) {++level;}

View file

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