Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -11,3 +11,5 @@ Errors:
|
|||
* handle the error of trying to pop from an empty queue (behavior depends on the language and platform)
|
||||
|
||||
See [[Queue/Usage]] for the built-in FIFO or queue of your language or standard library.
|
||||
|
||||
{{Template:See also lists}}
|
||||
|
|
|
|||
73
Task/Queue-Definition/ALGOL-68/queue-definition.alg
Normal file
73
Task/Queue-Definition/ALGOL-68/queue-definition.alg
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
# -*- coding: utf-8 -*- #
|
||||
CO REQUIRES:
|
||||
MODE OBJLINK = STRUCT(
|
||||
REF OBJLINK next,
|
||||
REF OBJLINK prev,
|
||||
OBJVALUE value # ... etc. required #
|
||||
);
|
||||
PROC obj link new = REF OBJLINK: ~;
|
||||
PROC obj link free = (REF OBJLINK free)VOID: ~
|
||||
END CO
|
||||
|
||||
# actually a pointer to the last LINK, there ITEMs are ADDED/get #
|
||||
MODE OBJQUEUE = REF OBJLINK;
|
||||
|
||||
OBJQUEUE obj queue empty = NIL;
|
||||
|
||||
BOOL obj queue par = FALSE; # make code thread safe #
|
||||
SEMA obj queue sema = LEVEL ABS obj queue par;
|
||||
# Warning: 1 SEMA for all queues of type obj, i.e. not 1 SEMA per queue #
|
||||
|
||||
PROC obj queue init = (REF OBJQUEUE self)REF OBJQUEUE:
|
||||
self := obj queue empty;
|
||||
|
||||
PROC obj queue put = (REF OBJQUEUE self, OBJVALUE obj)REF OBJQUEUE: (
|
||||
REF OBJLINK out = obj link new;
|
||||
IF obj queue par THEN DOWN obj queue sema FI;
|
||||
IF self IS obj queue empty THEN
|
||||
out := (out, out, obj) # self referal #
|
||||
ELSE # join into list #
|
||||
out := (self, prev OF self, obj);
|
||||
next OF prev OF out := prev OF next OF out := out
|
||||
FI;
|
||||
IF obj queue par THEN UP obj queue sema FI;
|
||||
self := out
|
||||
);
|
||||
|
||||
# define a useful prepend/put/plusto (+=:) operator... #
|
||||
PROC obj queue plusto = (OBJVALUE obj, REF OBJQUEUE self)OBJQUEUE: obj queue put(self,obj);
|
||||
OP +=: = (OBJVALUE obj, REF OBJQUEUE self)REF OBJQUEUE: obj queue put(self,obj);
|
||||
# a potential append/plusab (+:=) operator...
|
||||
OP (REF OBJQUEUE, OBJVALUE)OBJQUEUE +:= = obj queue plusab;
|
||||
#
|
||||
|
||||
# see if the program/coder wants the OBJ problem mended... #
|
||||
PROC (REF OBJQUEUE #self#)BOOL obj queue index error mended
|
||||
:= (REF OBJQUEUE self)BOOL: (abend("obj queue index error"); stop);
|
||||
|
||||
PROC on obj queue index error = (REF OBJQUEUE self, PROC(REF OBJQUEUE #self#)BOOL mended)VOID:
|
||||
obj queue index error mended := mended;
|
||||
|
||||
PROC obj queue get = (REF OBJQUEUE self)OBJVALUE: (
|
||||
# DOWN obj queue sema; #
|
||||
IF self IS obj queue empty THEN
|
||||
IF NOT obj queue index error mended(self) THEN abend("obj stack index error") FI FI;
|
||||
OBJQUEUE old tail = prev OF self;
|
||||
IF old tail IS self THEN # free solo member #
|
||||
self := obj queue empty
|
||||
ELSE # free self/tail member #
|
||||
OBJQUEUE new tail = prev OF old tail;
|
||||
next OF new tail := self;
|
||||
prev OF self := new tail
|
||||
FI;
|
||||
#;UP obj queue sema #
|
||||
OBJVALUE out = value OF old tail;
|
||||
# give a recovery hint to the garbage collector #
|
||||
obj link free(old tail);
|
||||
out
|
||||
);
|
||||
|
||||
PROC obj queue is empty = (REF OBJQUEUE self)BOOL:
|
||||
self IS obj queue empty;
|
||||
|
||||
SKIP
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
MODULE Queue;
|
||||
IMPORT StdLog,Boxes;
|
||||
|
||||
TYPE
|
||||
Queue* = POINTER TO RECORD
|
||||
first,last: LONGINT;
|
||||
queue: POINTER TO ARRAY OF Boxes.Object;
|
||||
END;
|
||||
|
||||
PROCEDURE NewQueue*(cap: LONGINT): Queue;
|
||||
VAR
|
||||
q: Queue;
|
||||
BEGIN
|
||||
NEW(q);q.first := 0; q.last := 0;
|
||||
NEW(q.queue,cap);
|
||||
RETURN q
|
||||
END NewQueue;
|
||||
|
||||
PROCEDURE (q: Queue) IsEmpty*(): BOOLEAN, NEW;
|
||||
VAR
|
||||
BEGIN
|
||||
RETURN (q.first = q.last)
|
||||
END IsEmpty;
|
||||
|
||||
PROCEDURE (q: Queue) Push*(o: Boxes.Object),NEW;
|
||||
VAR
|
||||
i,j,oldSize,newSize: LONGINT;
|
||||
queue: POINTER TO ARRAY OF Boxes.Object;
|
||||
BEGIN
|
||||
IF q.IsEmpty() THEN
|
||||
q.queue[q.last] := o;
|
||||
q.last := (q.last + 1) MOD LEN(q.queue)
|
||||
ELSE
|
||||
q.queue[q.last] := o;
|
||||
q.last := (q.last + 1) MOD LEN(q.queue);
|
||||
IF q.first = q.last THEN
|
||||
(* grow queue *)
|
||||
newSize := LEN(q.queue) + (LEN(q.queue) DIV 2);
|
||||
(* new queue*)
|
||||
NEW(queue,newSize);
|
||||
(* move data from old queue to new queue *)
|
||||
i := q.first; j := 0;oldSize := LEN(q.queue) - q.first + q.last;
|
||||
WHILE (j < oldSize ) & (j < LEN(queue) - 1) DO
|
||||
queue[j] := q.queue[i];
|
||||
i := (i + 1) MOD LEN(q.queue);INC(j)
|
||||
END;
|
||||
q.queue := queue;q.first := 0;q.last := j
|
||||
END
|
||||
END;
|
||||
END Push;
|
||||
|
||||
PROCEDURE (q: Queue) Pop*(): Boxes.Object, NEW;
|
||||
VAR
|
||||
o: Boxes.Object;
|
||||
BEGIN
|
||||
ASSERT(~q.IsEmpty());
|
||||
o := q.queue[q.first];
|
||||
q.queue[q.first] := NIL;
|
||||
q.first := (q.first + 1) MOD LEN(q.queue);
|
||||
RETURN o;
|
||||
END Pop;
|
||||
|
||||
END Queue.
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
DEFINITION Queue;
|
||||
|
||||
IMPORT Boxes;
|
||||
|
||||
TYPE
|
||||
Queue = POINTER TO RECORD
|
||||
(q: Queue) IsEmpty (): BOOLEAN, NEW;
|
||||
(q: Queue) Pop (): Boxes.Object, NEW;
|
||||
(q: Queue) Push (o: Boxes.Object), NEW
|
||||
END;
|
||||
|
||||
PROCEDURE NewQueue (cap: LONGINT): Queue;
|
||||
|
||||
END Queue.
|
||||
18
Task/Queue-Definition/Nimrod/queue-definition.nimrod
Normal file
18
Task/Queue-Definition/Nimrod/queue-definition.nimrod
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import queues
|
||||
|
||||
# defining push & pop (obviously optional)
|
||||
proc push*[T](q: var TQueue[T]; item: T) =
|
||||
add(q,item)
|
||||
proc pop*[T](q: var TQueue[T]): T =
|
||||
result = dequeue(q)
|
||||
|
||||
var fifo: TQueue[int] = initQueue[int]()
|
||||
|
||||
fifo.push(26)
|
||||
fifo.push(99)
|
||||
fifo.push(2)
|
||||
echo("Fifo size: ", fifo.len())
|
||||
echo("Popping: ", fifo.pop())
|
||||
echo("Popping: ", fifo.pop())
|
||||
echo("Popping: ", fifo.pop())
|
||||
#echo("Popping: ", fifo.pop()) # popping an empty stack raises [EAssertionFailed]
|
||||
Loading…
Add table
Add a link
Reference in a new issue