From 71917d501c8fb8c117766aef0277fdee6af6ce91 Mon Sep 17 00:00:00 2001 From: Cameron Ross Date: Wed, 16 Jan 2019 00:57:38 -0330 Subject: [PATCH] let Queue be a range (#220) let Queue be a range merged-on-behalf-of: BBasile --- source/dyaml/emitter.d | 8 +------- source/dyaml/queue.d | 44 +++++++++++++++++++----------------------- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/source/dyaml/emitter.d b/source/dyaml/emitter.d index 8eef1c3..cd74507 100644 --- a/source/dyaml/emitter.d +++ b/source/dyaml/emitter.d @@ -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;} diff --git a/source/dyaml/queue.d b/source/dyaml/queue.d index 710bd39..57b0d34 100644 --- a/source/dyaml/queue.d +++ b/source/dyaml/queue.d @@ -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 - { - assert(!empty); - assert(cursor_ !is null); - } - do - { - const previous = cursor_; - cursor_ = cursor_.next_; - return previous.payload_; - } - - /// Returns: true if itrating is not possible anymore, false otherwise. - bool iterationOver() @safe pure nothrow const @nogc - { - return cursor_ is null; + void popFront() @safe pure nothrow @nogc + { + cursor = cursor.next_; + } + ref T front() @safe pure nothrow @nogc + in(cursor !is null) + { + return cursor.payload_; + } + bool empty() @safe pure nothrow @nogc const + { + return cursor is null; + } + } + return Result(first_); } /// Push a new item to the queue.