Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
44
Task/Queue-Usage/D/queue-usage-1.d
Normal file
44
Task/Queue-Usage/D/queue-usage-1.d
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
class LinkedQueue(T) {
|
||||
private static struct Node {
|
||||
T data;
|
||||
Node* next;
|
||||
}
|
||||
|
||||
private Node* head, tail;
|
||||
|
||||
bool empty() { return head is null; }
|
||||
|
||||
void push(T item) {
|
||||
if (empty())
|
||||
head = tail = new Node(item);
|
||||
else {
|
||||
tail.next = new Node(item);
|
||||
tail = tail.next;
|
||||
}
|
||||
}
|
||||
|
||||
T pop() {
|
||||
if (empty())
|
||||
throw new Exception("Empty LinkedQueue.");
|
||||
auto item = head.data;
|
||||
head = head.next;
|
||||
if (head is tail) // Is last one?
|
||||
// Release tail reference so that GC can collect.
|
||||
tail = null;
|
||||
return item;
|
||||
}
|
||||
|
||||
alias push enqueue;
|
||||
alias pop dequeue;
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto q = new LinkedQueue!int();
|
||||
q.push(10);
|
||||
q.push(20);
|
||||
q.push(30);
|
||||
assert(q.pop() == 10);
|
||||
assert(q.pop() == 20);
|
||||
assert(q.pop() == 30);
|
||||
assert(q.empty());
|
||||
}
|
||||
75
Task/Queue-Usage/D/queue-usage-2.d
Normal file
75
Task/Queue-Usage/D/queue-usage-2.d
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
module queue_usage2;
|
||||
|
||||
import std.traits: hasIndirections;
|
||||
|
||||
struct GrowableCircularQueue(T) {
|
||||
public size_t length;
|
||||
private size_t first, last;
|
||||
private T[] A = [T.init];
|
||||
|
||||
this(T[] items...) pure nothrow @safe {
|
||||
foreach (x; items)
|
||||
push(x);
|
||||
}
|
||||
|
||||
@property bool empty() const pure nothrow @safe @nogc {
|
||||
return length == 0;
|
||||
}
|
||||
|
||||
@property T front() pure nothrow @safe @nogc {
|
||||
assert(length != 0);
|
||||
return A[first];
|
||||
}
|
||||
|
||||
T opIndex(in size_t i) pure nothrow @safe @nogc {
|
||||
assert(i < length);
|
||||
return A[(first + i) & (A.length - 1)];
|
||||
}
|
||||
|
||||
void push(T item) pure nothrow @safe {
|
||||
if (length >= A.length) { // Double the queue.
|
||||
immutable oldALen = A.length;
|
||||
A.length *= 2;
|
||||
if (last < first) {
|
||||
A[oldALen .. oldALen + last + 1] = A[0 .. last + 1];
|
||||
static if (hasIndirections!T)
|
||||
A[0 .. last + 1] = T.init; // Help for the GC.
|
||||
last += oldALen;
|
||||
}
|
||||
}
|
||||
last = (last + 1) & (A.length - 1);
|
||||
A[last] = item;
|
||||
length++;
|
||||
}
|
||||
|
||||
@property T pop() pure nothrow @safe @nogc {
|
||||
assert(length != 0);
|
||||
auto saved = A[first];
|
||||
static if (hasIndirections!T)
|
||||
A[first] = T.init; // Help for the GC.
|
||||
first = (first + 1) & (A.length - 1);
|
||||
length--;
|
||||
return saved;
|
||||
}
|
||||
}
|
||||
|
||||
version (queue_usage2_main) {
|
||||
void main() {
|
||||
GrowableCircularQueue!int q;
|
||||
q.push(10);
|
||||
q.push(20);
|
||||
q.push(30);
|
||||
assert(q.pop == 10);
|
||||
assert(q.pop == 20);
|
||||
assert(q.pop == 30);
|
||||
assert(q.empty);
|
||||
|
||||
uint count = 0;
|
||||
foreach (immutable i; 1 .. 1_000) {
|
||||
foreach (immutable j; 0 .. i)
|
||||
q.push(count++);
|
||||
foreach (immutable j; 0 .. i)
|
||||
q.pop;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue