Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
38
Task/Queue-Usage/Bracmat/queue-usage.bracmat
Normal file
38
Task/Queue-Usage/Bracmat/queue-usage.bracmat
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
( queue
|
||||
= (list=)
|
||||
(enqueue=.(.!arg) !(its.list):?(its.list))
|
||||
( dequeue
|
||||
= x
|
||||
. !(its.list):?(its.list) (.?x)
|
||||
& !x
|
||||
)
|
||||
(empty=.!(its.list):)
|
||||
)
|
||||
& new$queue:?Q
|
||||
& ( (Q..enqueue)$1
|
||||
& (Q..enqueue)$2
|
||||
& (Q..enqueue)$3
|
||||
& out$((Q..dequeue)$)
|
||||
& (Q..enqueue)$4
|
||||
& out$((Q..dequeue)$)
|
||||
& out$((Q..dequeue)$)
|
||||
& out
|
||||
$ ( The
|
||||
queue
|
||||
is
|
||||
((Q..empty)$&|not)
|
||||
empty
|
||||
)
|
||||
& out$((Q..dequeue)$)
|
||||
& out
|
||||
$ ( The
|
||||
queue
|
||||
is
|
||||
((Q..empty)$&|not)
|
||||
empty
|
||||
)
|
||||
& out$((Q..dequeue)$)
|
||||
& out$Success!
|
||||
| out$"Attempt to dequeue failed"
|
||||
)
|
||||
;
|
||||
|
|
@ -1,62 +1,75 @@
|
|||
module queue_usage2;
|
||||
|
||||
import std.traits: hasIndirections;
|
||||
|
||||
struct GrowableCircularQueue(T) {
|
||||
public size_t length;
|
||||
private size_t head, tail;
|
||||
private size_t first, last;
|
||||
private T[] A = [T.init];
|
||||
|
||||
bool empty() const pure nothrow {
|
||||
this(T[] items...) pure nothrow @safe {
|
||||
foreach (x; items)
|
||||
push(x);
|
||||
}
|
||||
|
||||
@property bool empty() const pure nothrow @safe @nogc {
|
||||
return length == 0;
|
||||
}
|
||||
|
||||
void push(immutable T item) pure nothrow {
|
||||
@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.
|
||||
const old = A;
|
||||
A = new T[A.length * 2];
|
||||
A[0 .. (old.length - head)] = old[head .. $];
|
||||
if (head)
|
||||
A[(old.length - head) .. old.length] = old[0 .. head];
|
||||
head = 0;
|
||||
tail = length;
|
||||
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;
|
||||
}
|
||||
}
|
||||
A[tail] = item;
|
||||
tail = (tail + 1) & (A.length - 1);
|
||||
last = (last + 1) & (A.length - 1);
|
||||
A[last] = item;
|
||||
length++;
|
||||
}
|
||||
|
||||
T pop() pure {
|
||||
import std.traits: hasIndirections;
|
||||
|
||||
if (length == 0)
|
||||
throw new Exception("GrowableCircularQueue is empty.");
|
||||
auto saved = A[head];
|
||||
@property T pop() pure nothrow @safe @nogc {
|
||||
assert(length != 0);
|
||||
auto saved = A[first];
|
||||
static if (hasIndirections!T)
|
||||
A[head] = T.init; // Help for the GC.
|
||||
head = (head + 1) & (A.length - 1);
|
||||
A[first] = T.init; // Help for the GC.
|
||||
first = (first + 1) & (A.length - 1);
|
||||
length--;
|
||||
return saved;
|
||||
}
|
||||
}
|
||||
|
||||
version (queue_usage2_main) {
|
||||
unittest {
|
||||
auto q = new GrowableCircularQueue!int();
|
||||
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());
|
||||
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();
|
||||
q.pop;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {}
|
||||
}
|
||||
|
|
|
|||
16
Task/Queue-Usage/Lua/queue-usage-2.lua
Normal file
16
Task/Queue-Usage/Lua/queue-usage-2.lua
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
> -- create queue:
|
||||
> q = {}
|
||||
> -- push:
|
||||
> q[#q+1] = "first"
|
||||
> q[#q+1] = "second"
|
||||
> q[#q+1] = "third"
|
||||
> -- pop:
|
||||
> =table.remove(q, 1)
|
||||
first
|
||||
> =table.remove(q, 1)
|
||||
second
|
||||
> =table.remove(q, 1)
|
||||
third
|
||||
> -- empty?
|
||||
> =#q == 0
|
||||
true
|
||||
|
|
@ -1,18 +1,18 @@
|
|||
/*REXX program demonstrates three queue operations: push, pop, queued. */
|
||||
say '══════════════════════════════════Pushing five values to the stack.'
|
||||
/*REXX program demonstrates three queue operations: push, pop, empty. */
|
||||
say '══════════════════════════════════ Pushing five values to the stack.'
|
||||
do j=1 for 4 /*loop to PUSH four values. */
|
||||
call push j*10 /*PUSH (aka enqueue to stack). */
|
||||
say 'pushed value:' j*10 /*echo the pushed value. */
|
||||
if j\==3 then iterate /*also, insert a "null" entry. */
|
||||
call push /*PUSH (aka enqueue to stack). */
|
||||
if j\==3 then iterate /*also, insert a "null" entry. */
|
||||
call push /*PUSH (aka enqueue to stack). */
|
||||
say 'pushed a "null" value.' /*echo what was pushed. */
|
||||
end /*j*/
|
||||
say '══════════════════════════════════Popping all values from the stack.'
|
||||
say '══════════════════════════════════ Popping all values from the stack.'
|
||||
do m=1 while \empty() /*EMPTY (returns TRUE if empty). */
|
||||
call pop /*POP (aka dequeue from stack).*/
|
||||
say m': popped value=' result /*echo the popped value. */
|
||||
end /*m*/
|
||||
say '══════════════════════════════════The stack is now empty.'
|
||||
say '══════════════════════════════════ The stack is now empty.'
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────subroutines/functions/operators. */
|
||||
push: queue arg(1); return /*REXX QUEUE is FIFO. */
|
||||
|
|
|
|||
14
Task/Queue-Usage/UNIX-Shell/queue-usage.sh
Normal file
14
Task/Queue-Usage/UNIX-Shell/queue-usage.sh
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# any valid variable name can be used as a queue without initialization
|
||||
|
||||
queue_empty foo && echo foo is empty || echo foo is not empty
|
||||
|
||||
queue_push foo bar
|
||||
queue_push foo baz
|
||||
queue_push foo "element with spaces"
|
||||
|
||||
queue_empty foo && echo foo is empty || echo foo is not empty
|
||||
|
||||
print "peek: $(queue_peek foo)"; queue_pop foo
|
||||
print "peek: $(queue_peek foo)"; queue_pop foo
|
||||
print "peek: $(queue_peek foo)"; queue_pop foo
|
||||
print "peek: $(queue_peek foo)"; queue_pop foo
|
||||
Loading…
Add table
Add a link
Reference in a new issue