Queue: Leave only enqueue/dequeue/empty/opApply

This commit is contained in:
Eugen Wissner 2017-01-11 18:24:50 +01:00
parent 291920b479
commit ab930657b6
7 changed files with 70 additions and 107 deletions

View File

@ -159,7 +159,7 @@ class EpollLoop : SelectorLoop
} }
else if (io.output.length) else if (io.output.length)
{ {
swapPendings.insertBack(io); swapPendings.enqueue(io);
} }
} }
else if (events[i].events & EPOLLOUT) else if (events[i].events & EPOLLOUT)

View File

@ -218,11 +218,11 @@ class IOCPLoop : Loop
auto transport = MmapPool.instance.make!IOCPStreamTransport(socket); auto transport = MmapPool.instance.make!IOCPStreamTransport(socket);
auto io = MmapPool.instance.make!IOWatcher(transport, connection.protocol); auto io = MmapPool.instance.make!IOWatcher(transport, connection.protocol);
connection.incoming.insertBack(io); connection.incoming.enqueue(io);
reify(io, EventMask(Event.none), EventMask(Event.read, Event.write)); reify(io, EventMask(Event.none), EventMask(Event.read, Event.write));
swapPendings.insertBack(connection); swapPendings.enqueue(connection);
listener.beginAccept(overlapped); listener.beginAccept(overlapped);
break; break;
case OverlappedSocketEvent.read: case OverlappedSocketEvent.read:
@ -264,7 +264,7 @@ class IOCPLoop : Loop
{ {
transport.socket.beginReceive(io.output[], overlapped); transport.socket.beginReceive(io.output[], overlapped);
} }
swapPendings.insertBack(io); swapPendings.enqueue(io);
} }
break; break;
case OverlappedSocketEvent.write: case OverlappedSocketEvent.write:

View File

@ -271,7 +271,7 @@ class KqueueLoop : SelectorLoop
} }
else if (io.output.length) else if (io.output.length)
{ {
swapPendings.insertBack(io); swapPendings.enqueue(io);
} }
} }
else if (events[i].filter == EVFILT_WRITE) else if (events[i].filter == EVFILT_WRITE)

View File

@ -248,12 +248,12 @@ abstract class SelectorLoop : Loop
} }
reify(io, EventMask(Event.none), EventMask(Event.read, Event.write)); reify(io, EventMask(Event.none), EventMask(Event.read, Event.write));
connection.incoming.insertBack(io); connection.incoming.enqueue(io);
} }
if (!connection.incoming.empty) if (!connection.incoming.empty)
{ {
swapPendings.insertBack(connection); swapPendings.enqueue(connection);
} }
} }
} }

View File

@ -184,8 +184,10 @@ abstract class Loop
poll(); poll();
// Invoke pendings // Invoke pendings
swapPendings.each!((ref p) @nogc => p.invoke()); foreach (ref w; *swapPendings)
{
w.invoke();
}
swap(pendings, swapPendings); swap(pendings, swapPendings);
} }
while (!done_); while (!done_);
@ -283,7 +285,7 @@ abstract class Loop
defaultAllocator.dispose(watcher.socket); defaultAllocator.dispose(watcher.socket);
MmapPool.instance.dispose(watcher.transport); MmapPool.instance.dispose(watcher.transport);
watcher.exception = exception; watcher.exception = exception;
swapPendings.insertBack(watcher); swapPendings.enqueue(watcher);
} }
/** /**

View File

@ -10,8 +10,9 @@
*/ */
module tanya.container.queue; module tanya.container.queue;
import tanya.container.entry;
import std.traits; import std.traits;
import std.algorithm.mutation;
import tanya.container.entry;
import tanya.memory; import tanya.memory;
/** /**
@ -27,32 +28,24 @@ struct Queue(T)
*/ */
~this() ~this()
{ {
clear(); while (!empty)
{
dequeue();
}
} }
/** /**
* Removes all elements from the queue. * Removes all elements from the queue.
*/ */
deprecated
void clear() void clear()
{ {
while (!empty) while (!empty)
{ {
popFront(); dequeue();
} }
} }
///
unittest
{
Queue!int q;
assert(q.empty);
q.insertBack(8);
q.insertBack(9);
q.clear();
assert(q.empty);
}
/** /**
* Returns how many elements are in the queue. It iterates through the queue * Returns how many elements are in the queue. It iterates through the queue
* to count the elements. * to count the elements.
@ -75,18 +68,18 @@ struct Queue(T)
Queue!int q; Queue!int q;
assert(q.length == 0); assert(q.length == 0);
q.insertBack(5); q.enqueue(5);
assert(q.length == 1); assert(q.length == 1);
q.insertBack(4); q.enqueue(4);
assert(q.length == 2); assert(q.length == 2);
q.insertBack(9); q.enqueue(9);
assert(q.length == 3); assert(q.length == 3);
q.popFront(); q.dequeue();
assert(q.length == 2); assert(q.length == 2);
q.popFront(); q.dequeue();
assert(q.length == 1); assert(q.length == 1);
q.popFront(); q.dequeue();
assert(q.length == 0); assert(q.length == 0);
} }
@ -97,14 +90,17 @@ struct Queue(T)
* *
* Returns: Whether $(D_KEYWORD this) and $(D_PARAM that) are equal. * Returns: Whether $(D_KEYWORD this) and $(D_PARAM that) are equal.
*/ */
deprecated
int opEquals(ref typeof(this) that); int opEquals(ref typeof(this) that);
/// Ditto. /// Ditto.
deprecated
int opEquals(typeof(this) that); int opEquals(typeof(this) that);
} }
else static if (!hasMember!(T, "opEquals") else static if (!hasMember!(T, "opEquals")
|| (functionAttributes!(T.opEquals) & FunctionAttribute.const_)) || (functionAttributes!(T.opEquals) & FunctionAttribute.const_))
{ {
deprecated
bool opEquals(in ref typeof(this) that) const bool opEquals(in ref typeof(this) that) const
{ {
const(Entry!T)* i = first.next; const(Entry!T)* i = first.next;
@ -121,7 +117,7 @@ struct Queue(T)
return i is null && j is null; return i is null && j is null;
} }
/// Ditto. deprecated
bool opEquals(in typeof(this) that) const bool opEquals(in typeof(this) that) const
{ {
return opEquals(that); return opEquals(that);
@ -129,11 +125,7 @@ struct Queue(T)
} }
else else
{ {
/** deprecated
* Compares two queues. Checks if all elements of the both queues are equal.
*
* Returns: How many elements are in the queue.
*/
bool opEquals(ref typeof(this) that) bool opEquals(ref typeof(this) that)
{ {
Entry!T* i = first.next; Entry!T* i = first.next;
@ -150,46 +142,17 @@ struct Queue(T)
return i is null && j is null; return i is null && j is null;
} }
/// Ditto. deprecated
bool opEquals(typeof(this) that) bool opEquals(typeof(this) that)
{ {
return opEquals(that); return opEquals(that);
} }
} }
///
unittest
{
Queue!int q1, q2;
q1.insertBack(5);
q1.insertBack(4);
q2.insertBack(5);
assert(q1 != q2);
q2.insertBack(4);
assert(q1 == q2);
q2.popFront();
assert(q1 != q2);
q1.popFront();
assert(q1 == q2);
q1.popFront();
q2.popFront();
assert(q1 == q2);
}
private unittest
{
static assert(is(Queue!ConstEqualsStruct));
static assert(is(Queue!MutableEqualsStruct));
static assert(is(Queue!NoEqualsStruct));
}
/** /**
* Returns: First element. * Returns: First element.
*/ */
deprecated("Use dequeue instead.")
@property ref inout(T) front() inout @property ref inout(T) front() inout
in in
{ {
@ -205,13 +168,12 @@ struct Queue(T)
* *
* Params: * Params:
* x = New element. * x = New element.
*
* Returns: $(D_KEYWORD this).
*/ */
void insertBack(ref T x) ref typeof(this) enqueue(ref T x)
{ {
auto temp = allocator.make!(Entry!T); auto temp = allocator.make!(Entry!T)(x);
temp.content = x;
if (empty) if (empty)
{ {
first.next = rear = temp; first.next = rear = temp;
@ -221,16 +183,20 @@ struct Queue(T)
rear.next = temp; rear.next = temp;
rear = rear.next; rear = rear.next;
} }
return this;
} }
/// Ditto. /// Ditto.
void insertBack(T x) ref typeof(this) enqueue(T x)
{ {
insertBack(x); return enqueue(x);
} }
/// Ditto. deprecated("Use enqueue instead.")
alias insert = insertBack; alias insert = enqueue;
deprecated("Use enqueue instead.")
alias insertBack = enqueue;
/// ///
unittest unittest
@ -238,10 +204,9 @@ struct Queue(T)
Queue!int q; Queue!int q;
assert(q.empty); assert(q.empty);
q.insertBack(8); q.enqueue(8).enqueue(9);
assert(q.front == 8); assert(q.dequeue() == 8);
q.insertBack(9); assert(q.dequeue() == 9);
assert(q.front == 8);
} }
/** /**
@ -259,14 +224,16 @@ struct Queue(T)
int value = 7; int value = 7;
assert(q.empty); assert(q.empty);
q.insertBack(value); q.enqueue(value);
assert(!q.empty); assert(!q.empty);
} }
/** /**
* Move the position to the next element. * Move the position to the next element.
*
* Returns: Dequeued element.
*/ */
void popFront() T dequeue()
in in
{ {
assert(!empty); assert(!empty);
@ -275,21 +242,24 @@ struct Queue(T)
body body
{ {
auto n = first.next.next; auto n = first.next.next;
T ret = move(first.next.content);
dispose(allocator, first.next); dispose(allocator, first.next);
first.next = n; first.next = n;
return ret;
} }
deprecated("Use dequeue instead.")
alias popFront = dequeue;
/// ///
unittest unittest
{ {
Queue!int q; Queue!int q;
q.insertBack(8); q.enqueue(8).enqueue(9);
q.insertBack(9); assert(q.dequeue() == 8);
assert(q.front == 8); assert(q.dequeue() == 9);
q.popFront();
assert(q.front == 9);
} }
/** /**
@ -305,11 +275,11 @@ struct Queue(T)
for (size_t i = 0; !empty; ++i) for (size_t i = 0; !empty; ++i)
{ {
if ((result = dg(i, front)) != 0) auto e = dequeue();
if ((result = dg(i, e)) != 0)
{ {
return result; return result;
} }
popFront();
} }
return result; return result;
} }
@ -321,11 +291,11 @@ struct Queue(T)
while (!empty) while (!empty)
{ {
if ((result = dg(front)) != 0) auto e = dequeue();
if ((result = dg(e)) != 0)
{ {
return result; return result;
} }
popFront();
} }
return result; return result;
} }
@ -336,9 +306,7 @@ struct Queue(T)
Queue!int q; Queue!int q;
size_t j; size_t j;
q.insertBack(5); q.enqueue(5).enqueue(4).enqueue(9);
q.insertBack(4);
q.insertBack(9);
foreach (i, e; q) foreach (i, e; q)
{ {
assert(i != 2 || e == 9); assert(i != 2 || e == 9);
@ -350,9 +318,7 @@ struct Queue(T)
assert(q.empty); assert(q.empty);
j = 0; j = 0;
q.insertBack(5); q.enqueue(5).enqueue(4).enqueue(9);
q.insertBack(4);
q.insertBack(9);
foreach (e; q) foreach (e; q)
{ {
assert(j != 2 || e == 9); assert(j != 2 || e == 9);
@ -378,17 +344,12 @@ unittest
{ {
Queue!int q; Queue!int q;
q.insertBack(5); q.enqueue(5);
assert(!q.empty); assert(!q.empty);
q.insertBack(4); q.enqueue(4).enqueue(9);
assert(q.front == 5);
q.insertBack(9); assert(q.dequeue() == 5);
assert(q.front == 5);
q.popFront();
assert(q.front == 4);
foreach (i, ref e; q) foreach (i, ref e; q)
{ {

View File

@ -1341,7 +1341,7 @@ struct Vector(T)
} }
body body
{ {
vector[i .. j] = value; vector[i .. j].fill(value);
return opSlice(i, j); return opSlice(i, j);
} }