Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Queue-Definition/00-META.yaml
Normal file
3
Task/Queue-Definition/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Queue/Definition
|
||||
note: Data Structures
|
||||
26
Task/Queue-Definition/00-TASK.txt
Normal file
26
Task/Queue-Definition/00-TASK.txt
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
{{Data structure}}
|
||||
[[File:Fifo.gif|frame|right|Illustration of FIFO behavior]]
|
||||
|
||||
;Task:
|
||||
Implement a FIFO queue.
|
||||
|
||||
Elements are added at one side and popped from the other in the order of insertion.
|
||||
|
||||
|
||||
Operations:
|
||||
:* push (aka ''enqueue'') - add element
|
||||
:* pop (aka ''dequeue'') - pop first element
|
||||
:* empty - return truth value when empty
|
||||
|
||||
|
||||
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}}
|
||||
<br><br>
|
||||
|
||||
16
Task/Queue-Definition/11l/queue-definition.11l
Normal file
16
Task/Queue-Definition/11l/queue-definition.11l
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
T FIFO
|
||||
[Int] contents
|
||||
|
||||
F push(item)
|
||||
.contents.append(item)
|
||||
F pop()
|
||||
R .contents.pop(0)
|
||||
F empty()
|
||||
R .contents.empty
|
||||
|
||||
V f = FIFO()
|
||||
f.push(3)
|
||||
f.push(2)
|
||||
f.push(1)
|
||||
L !f.empty()
|
||||
print(f.pop())
|
||||
235
Task/Queue-Definition/AArch64-Assembly/queue-definition.aarch64
Normal file
235
Task/Queue-Definition/AArch64-Assembly/queue-definition.aarch64
Normal file
|
|
@ -0,0 +1,235 @@
|
|||
/* ARM assembly AARCH64 Raspberry PI 3B */
|
||||
/* program defqueue64.s */
|
||||
|
||||
/*******************************************/
|
||||
/* Constantes file */
|
||||
/*******************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly*/
|
||||
.include "../includeConstantesARM64.inc"
|
||||
|
||||
.equ NBMAXIELEMENTS, 100
|
||||
|
||||
/*******************************************/
|
||||
/* Structures */
|
||||
/********************************************/
|
||||
/* example structure for value of item */
|
||||
.struct 0
|
||||
value_ident: // ident
|
||||
.struct value_ident + 8
|
||||
value_value1: // value 1
|
||||
.struct value_value1 + 8
|
||||
value_value2: // value 2
|
||||
.struct value_value2 + 8
|
||||
value_fin:
|
||||
/* example structure for queue */
|
||||
.struct 0
|
||||
queue_ptdeb: // begin pointer of item
|
||||
.struct queue_ptdeb + 8
|
||||
queue_ptfin: // end pointer of item
|
||||
.struct queue_ptfin + 8
|
||||
queue_stvalue: // structure of value item
|
||||
.struct queue_stvalue + (value_fin * NBMAXIELEMENTS)
|
||||
queue_fin:
|
||||
|
||||
|
||||
/*********************************/
|
||||
/* Initialized data */
|
||||
/*********************************/
|
||||
.data
|
||||
szMessEmpty: .asciz "Empty queue. \n"
|
||||
szMessNotEmpty: .asciz "Not empty queue. \n"
|
||||
szMessError: .asciz "Error detected !!!!. \n"
|
||||
szMessResult: .asciz "Ident : @ value 1 : @ value 2 : @ \n" // message result
|
||||
|
||||
szCarriageReturn: .asciz "\n"
|
||||
/*********************************/
|
||||
/* UnInitialized data */
|
||||
/*********************************/
|
||||
.bss
|
||||
.align 4
|
||||
Queue1: .skip queue_fin // queue memory place
|
||||
stItem: .skip value_fin // value item memory place
|
||||
sZoneConv: .skip 100
|
||||
/*********************************/
|
||||
/* code section */
|
||||
/*********************************/
|
||||
.text
|
||||
.global main
|
||||
main: // entry of program
|
||||
ldr x0,qAdrQueue1 // queue structure address
|
||||
bl isEmpty
|
||||
cmp x0,#0
|
||||
beq 1f
|
||||
ldr x0,qAdrszMessEmpty
|
||||
bl affichageMess // display message empty
|
||||
b 2f
|
||||
1:
|
||||
ldr x0,qAdrszMessNotEmpty
|
||||
bl affichageMess // display message not empty
|
||||
2:
|
||||
// init item 1
|
||||
ldr x0,qAdrstItem
|
||||
mov x1,#1
|
||||
str x1,[x0,#value_ident]
|
||||
mov x1,#11
|
||||
str x1,[x0,#value_value1]
|
||||
mov x1,#12
|
||||
str x1,[x0,#value_value2]
|
||||
|
||||
ldr x0,qAdrQueue1 // queue structure address
|
||||
ldr x1,qAdrstItem
|
||||
bl pushQueue // add item in queue
|
||||
cmp x0,#-1 // error ?
|
||||
beq 99f
|
||||
// init item 2
|
||||
ldr x0,qAdrstItem
|
||||
mov x1,#2
|
||||
str x1,[x0,#value_ident]
|
||||
mov x1,#21
|
||||
str x1,[x0,#value_value1]
|
||||
mov x1,#22
|
||||
str x1,[x0,#value_value2]
|
||||
|
||||
ldr x0,qAdrQueue1 // queue structure address
|
||||
ldr x1,qAdrstItem
|
||||
bl pushQueue // add item in queue
|
||||
cmp x0,#-1
|
||||
beq 99f
|
||||
ldr x0,qAdrQueue1 // queue structure address
|
||||
bl isEmpty
|
||||
cmp x0,#0 // not empty
|
||||
beq 3f
|
||||
ldr x0,qAdrszMessEmpty
|
||||
bl affichageMess // display message empty
|
||||
b 4f
|
||||
3:
|
||||
ldr x0,qAdrszMessNotEmpty
|
||||
bl affichageMess // display message not empty
|
||||
|
||||
4:
|
||||
ldr x0,qAdrQueue1 // queue structure address
|
||||
bl popQueue // return address item
|
||||
cmp x0,#-1 // error ?
|
||||
beq 99f
|
||||
mov x2,x0 // save item pointer
|
||||
ldr x0,[x2,#value_ident]
|
||||
ldr x1,qAdrsZoneConv // conversion ident
|
||||
bl conversion10S // decimal conversion
|
||||
ldr x0,qAdrszMessResult
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl strInsertAtCharInc // insert result at first @ character
|
||||
mov x5,x0
|
||||
ldr x0,[x2,#value_value1]
|
||||
ldr x1,qAdrsZoneConv // conversion value 1
|
||||
bl conversion10S // decimal conversion
|
||||
mov x0,x5
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl strInsertAtCharInc // insert result at Second @ character
|
||||
mov x5,x0
|
||||
ldr x0,[x2,#value_value2]
|
||||
ldr x1,qAdrsZoneConv // conversion value 2
|
||||
bl conversion10S // decimal conversion
|
||||
mov x0,x5
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl strInsertAtCharInc // insert result at third @ character
|
||||
bl affichageMess // display message final
|
||||
b 4b // loop
|
||||
|
||||
99: // error
|
||||
ldr x0,qAdrszMessError
|
||||
bl affichageMess
|
||||
100: // standard end of the program
|
||||
mov x0,0 // return code
|
||||
mov x8,EXIT // request to exit program
|
||||
svc 0 // perform the system call
|
||||
|
||||
qAdrQueue1: .quad Queue1
|
||||
qAdrstItem: .quad stItem
|
||||
qAdrszMessError: .quad szMessError
|
||||
qAdrszMessEmpty: .quad szMessEmpty
|
||||
qAdrszMessNotEmpty: .quad szMessNotEmpty
|
||||
qAdrszMessResult: .quad szMessResult
|
||||
qAdrszCarriageReturn: .quad szCarriageReturn
|
||||
qAdrsZoneConv: .quad sZoneConv
|
||||
|
||||
/******************************************************************/
|
||||
/* test if queue empty */
|
||||
/******************************************************************/
|
||||
/* x0 contains the address of queue structure */
|
||||
/* x0 returns 0 if not empty, 1 if empty */
|
||||
isEmpty:
|
||||
stp x1,lr,[sp,-16]! // save registers
|
||||
stp x2,x3,[sp,-16]! // save registers
|
||||
ldr x1,[x0,#queue_ptdeb] // begin pointer
|
||||
ldr x2,[x0,#queue_ptfin] // begin pointer
|
||||
cmp x1,x2
|
||||
bne 1f
|
||||
mov x0,#1 // empty queue
|
||||
b 2f
|
||||
1:
|
||||
mov x0,#0 // not empty
|
||||
2:
|
||||
ldp x2,x3,[sp],16 // restaur 2 registers
|
||||
ldp x1,lr,[sp],16 // restaur 2 registers
|
||||
ret // return to address lr x30
|
||||
|
||||
/******************************************************************/
|
||||
/* add item in queue */
|
||||
/******************************************************************/
|
||||
/* x0 contains the address of queue structure */
|
||||
/* x1 contains the address of item */
|
||||
pushQueue:
|
||||
stp x1,lr,[sp,-16]! // save registers
|
||||
stp x2,x3,[sp,-16]! // save registers
|
||||
add x2,x0,#queue_stvalue // address of values structure
|
||||
ldr x3,[x0,#queue_ptfin] // end pointer
|
||||
add x2,x2,x3 // free address of queue
|
||||
ldr x4,[x1,#value_ident] // load ident item
|
||||
str x4,[x2,#value_ident] // and store in queue
|
||||
ldr x4,[x1,#value_value1] // idem
|
||||
str x4,[x2,#value_value1]
|
||||
ldr x4,[x1,#value_value2]
|
||||
str x4,[x2,#value_value2]
|
||||
add x3,x3,#value_fin
|
||||
cmp x3,#value_fin * NBMAXIELEMENTS
|
||||
beq 99f
|
||||
str x3,[x0,#queue_ptfin] // store new end pointer
|
||||
b 100f
|
||||
99:
|
||||
mov x0,#-1 // error
|
||||
100:
|
||||
ldp x2,x3,[sp],16 // restaur 2 registers
|
||||
ldp x1,lr,[sp],16 // restaur 2 registers
|
||||
ret // return to address lr x30
|
||||
|
||||
/******************************************************************/
|
||||
/* pop queue */
|
||||
/******************************************************************/
|
||||
/* x0 contains the address of queue structure */
|
||||
popQueue:
|
||||
stp x1,lr,[sp,-16]! // save registers
|
||||
stp x2,x3,[sp,-16]! // save registers
|
||||
mov x1,x0 // control if empty queue
|
||||
bl isEmpty
|
||||
cmp x0,#1 // yes -> error
|
||||
beq 99f
|
||||
mov x0,x1
|
||||
ldr x1,[x0,#queue_ptdeb] // begin pointer
|
||||
add x2,x0,#queue_stvalue // address of begin values item
|
||||
add x2,x2,x1 // address of item
|
||||
add x1,x1,#value_fin
|
||||
str x1,[x0,#queue_ptdeb] // store nex begin pointer
|
||||
mov x0,x2 // return pointer item
|
||||
b 100f
|
||||
99:
|
||||
mov x0,#-1 // error
|
||||
100:
|
||||
ldp x2,x3,[sp],16 // restaur 2 registers
|
||||
ldp x1,lr,[sp],16 // restaur 2 registers
|
||||
ret // return to address lr x30
|
||||
|
||||
/********************************************************/
|
||||
/* File Include fonctions */
|
||||
/********************************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly */
|
||||
.include "../includeARM64.inc"
|
||||
14
Task/Queue-Definition/ACL2/queue-definition.acl2
Normal file
14
Task/Queue-Definition/ACL2/queue-definition.acl2
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(defun enqueue (x xs)
|
||||
(cons x xs))
|
||||
|
||||
(defun dequeue (xs)
|
||||
(declare (xargs :guard (and (consp xs)
|
||||
(true-listp xs))))
|
||||
(if (or (endp xs) (endp (rest xs)))
|
||||
(mv (first xs) nil)
|
||||
(mv-let (x ys)
|
||||
(dequeue (rest xs))
|
||||
(mv x (cons (first xs) ys)))))
|
||||
|
||||
(defun empty (xs)
|
||||
(endp xs))
|
||||
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
|
||||
51
Task/Queue-Definition/ALGOL-W/queue-definition.alg
Normal file
51
Task/Queue-Definition/ALGOL-W/queue-definition.alg
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
begin
|
||||
% define a Queue type that will hold StringQueueElements %
|
||||
record StringQueue ( reference(StringQueueElement) front, back );
|
||||
% define the StringQueueElement type %
|
||||
record StringQueueElement ( string(8) element
|
||||
; reference(StringQueueElement) next
|
||||
);
|
||||
% we would need separate types for other element types %
|
||||
% adds s to the end of the StringQueue q %
|
||||
procedure pushString ( reference(StringQueue) value q
|
||||
; string(8) value e
|
||||
) ;
|
||||
begin
|
||||
reference(StringQueueElement) newElement;
|
||||
newElement := StringQueueElement( e, null );
|
||||
if front(q) = null then begin
|
||||
% adding to an empty queue %
|
||||
front(q) := newElement;
|
||||
back(q) := newElement
|
||||
end
|
||||
else begin
|
||||
% the queue is not empty %
|
||||
next(back(q)) := newElement;
|
||||
back(q) := newElement
|
||||
end
|
||||
end pushString ;
|
||||
% removes an element from the front of the StringQueue q %
|
||||
% asserts the queue is not empty, which will stop the %
|
||||
% program if it is %
|
||||
string(8) procedure popString ( reference(StringQueue) value q ) ;
|
||||
begin
|
||||
string(8) v;
|
||||
assert( not isEmptyStringQueue( q ) );
|
||||
v := element(front(q));
|
||||
front(q) := next(front(q));
|
||||
if front(q) = null then % just popped the last element % back(q) := null;
|
||||
v
|
||||
end popStringQueue ;
|
||||
% returns true if the StringQueue q is empty, false otherwise %
|
||||
logical procedure isEmptyStringQueue ( reference(StringQueue) value q ) ; front(q) = null;
|
||||
|
||||
begin % test the StringQueue operations %
|
||||
reference(StringQueue) q;
|
||||
q := StringQueue( null, null );
|
||||
pushString( q, "fred" );
|
||||
pushString( q, "whilma" );
|
||||
pushString( q, "betty" );
|
||||
pushString( q, "barney" );
|
||||
while not isEmptyStringQueue( q ) do write( popString( q ) )
|
||||
end
|
||||
end.
|
||||
283
Task/Queue-Definition/ARM-Assembly/queue-definition.arm
Normal file
283
Task/Queue-Definition/ARM-Assembly/queue-definition.arm
Normal file
|
|
@ -0,0 +1,283 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program defqueue.s */
|
||||
|
||||
/* Constantes */
|
||||
.equ STDOUT, 1 @ Linux output console
|
||||
.equ EXIT, 1 @ Linux syscall
|
||||
.equ WRITE, 4 @ Linux syscall
|
||||
|
||||
.equ NBMAXIELEMENTS, 100
|
||||
|
||||
/*******************************************/
|
||||
/* Structures */
|
||||
/********************************************/
|
||||
/* example structure for value of item */
|
||||
.struct 0
|
||||
value_ident: @ ident
|
||||
.struct value_ident + 4
|
||||
value_value1: @ value 1
|
||||
.struct value_value1 + 4
|
||||
value_value2: @ value 2
|
||||
.struct value_value2 + 4
|
||||
value_fin:
|
||||
/* example structure for queue */
|
||||
.struct 0
|
||||
queue_ptdeb: @ begin pointer of item
|
||||
.struct queue_ptdeb + 4
|
||||
queue_ptfin: @ end pointer of item
|
||||
.struct queue_ptfin + 4
|
||||
queue_stvalue: @ structure of value item
|
||||
.struct queue_stvalue + (value_fin * NBMAXIELEMENTS)
|
||||
queue_fin:
|
||||
|
||||
|
||||
/*********************************/
|
||||
/* Initialized data */
|
||||
/*********************************/
|
||||
.data
|
||||
szMessEmpty: .asciz "Empty queue. \n"
|
||||
szMessNotEmpty: .asciz "Not empty queue. \n"
|
||||
szMessError: .asciz "Error detected !!!!. \n"
|
||||
szMessResult: .ascii "Ident :" @ message result
|
||||
sMessIdent: .fill 11, 1, ' '
|
||||
.ascii " value 1 :"
|
||||
sMessValue1: .fill 11, 1, ' '
|
||||
.ascii " value 2 :"
|
||||
sMessValue2: .fill 11, 1, ' '
|
||||
.asciz "\n"
|
||||
|
||||
szCarriageReturn: .asciz "\n"
|
||||
/*********************************/
|
||||
/* UnInitialized data */
|
||||
/*********************************/
|
||||
.bss
|
||||
.align 4
|
||||
Queue1: .skip queue_fin @ queue memory place
|
||||
stItem: .skip value_fin @ value item memory place
|
||||
/*********************************/
|
||||
/* code section */
|
||||
/*********************************/
|
||||
.text
|
||||
.global main
|
||||
main: @ entry of program
|
||||
ldr r0,iAdrQueue1 @ queue structure address
|
||||
bl isEmpty
|
||||
cmp r0,#0
|
||||
beq 1f
|
||||
ldr r0,iAdrszMessEmpty
|
||||
bl affichageMess @ display message empty
|
||||
b 2f
|
||||
1:
|
||||
ldr r0,iAdrszMessNotEmpty
|
||||
bl affichageMess @ display message not empty
|
||||
2:
|
||||
@ init item 1
|
||||
ldr r0,iAdrstItem
|
||||
mov r1,#1
|
||||
str r1,[r0,#value_ident]
|
||||
mov r1,#11
|
||||
str r1,[r0,#value_value1]
|
||||
mov r1,#12
|
||||
str r1,[r0,#value_value2]
|
||||
|
||||
ldr r0,iAdrQueue1 @ queue structure address
|
||||
ldr r1,iAdrstItem
|
||||
bl pushQueue @ add item in queue
|
||||
cmp r0,#-1 @ error ?
|
||||
beq 99f
|
||||
@ init item 2
|
||||
ldr r0,iAdrstItem
|
||||
mov r1,#2
|
||||
str r1,[r0,#value_ident]
|
||||
mov r1,#21
|
||||
str r1,[r0,#value_value1]
|
||||
mov r1,#22
|
||||
str r1,[r0,#value_value2]
|
||||
|
||||
ldr r0,iAdrQueue1 @ queue structure address
|
||||
ldr r1,iAdrstItem
|
||||
bl pushQueue @ add item in queue
|
||||
cmp r0,#-1
|
||||
beq 99f
|
||||
ldr r0,iAdrQueue1 @ queue structure address
|
||||
bl isEmpty
|
||||
cmp r0,#0 @ not empty
|
||||
beq 3f
|
||||
ldr r0,iAdrszMessEmpty
|
||||
bl affichageMess @ display message empty
|
||||
b 4f
|
||||
3:
|
||||
ldr r0,iAdrszMessNotEmpty
|
||||
bl affichageMess @ display message not empty
|
||||
|
||||
4:
|
||||
ldr r0,iAdrQueue1 @ queue structure address
|
||||
bl popQueue @ return address item
|
||||
cmp r0,#-1 @ error ?
|
||||
beq 99f
|
||||
mov r2,r0 @ save item pointer
|
||||
ldr r0,[r2,#value_ident]
|
||||
ldr r1,iAdrsMessIdent @ display ident
|
||||
bl conversion10 @ decimal conversion
|
||||
ldr r0,[r2,#value_value1]
|
||||
ldr r1,iAdrsMessValue1 @ display value 1
|
||||
bl conversion10 @ decimal conversion
|
||||
ldr r0,[r2,#value_value2]
|
||||
ldr r1,iAdrsMessValue2 @ display value 2
|
||||
bl conversion10 @ decimal conversion
|
||||
ldr r0,iAdrszMessResult
|
||||
bl affichageMess @ display message
|
||||
b 4b @ loop
|
||||
|
||||
99:
|
||||
@ error
|
||||
ldr r0,iAdrszMessError
|
||||
bl affichageMess
|
||||
100: @ standard end of the program
|
||||
mov r0, #0 @ return code
|
||||
mov r7, #EXIT @ request to exit program
|
||||
svc #0 @ perform the system call
|
||||
|
||||
iAdrQueue1: .int Queue1
|
||||
iAdrstItem: .int stItem
|
||||
iAdrszMessError: .int szMessError
|
||||
iAdrszMessEmpty: .int szMessEmpty
|
||||
iAdrszMessNotEmpty: .int szMessNotEmpty
|
||||
iAdrszMessResult: .int szMessResult
|
||||
iAdrszCarriageReturn: .int szCarriageReturn
|
||||
iAdrsMessIdent: .int sMessIdent
|
||||
iAdrsMessValue1: .int sMessValue1
|
||||
iAdrsMessValue2: .int sMessValue2
|
||||
/******************************************************************/
|
||||
/* test if queue empty */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of queue structure */
|
||||
isEmpty:
|
||||
push {r1,r2,lr} @ save registres
|
||||
ldr r1,[r0,#queue_ptdeb] @ begin pointer
|
||||
ldr r2,[r0,#queue_ptfin] @ begin pointer
|
||||
cmp r1,r2
|
||||
moveq r0,#1 @ empty queue
|
||||
movne r0,#0 @ not empty
|
||||
pop {r1,r2,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
/******************************************************************/
|
||||
/* add item in queue */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of queue structure */
|
||||
/* r1 contains the address of item */
|
||||
pushQueue:
|
||||
push {r1-r4,lr} @ save registres
|
||||
add r2,r0,#queue_stvalue @ address of values structure
|
||||
ldr r3,[r0,#queue_ptfin] @ end pointer
|
||||
add r2,r3 @ free address of queue
|
||||
ldr r4,[r1,#value_ident] @ load ident item
|
||||
str r4,[r2,#value_ident] @ and store in queue
|
||||
ldr r4,[r1,#value_value1] @ idem
|
||||
str r4,[r2,#value_value1]
|
||||
ldr r4,[r1,#value_value2]
|
||||
str r4,[r2,#value_value2]
|
||||
add r3,#value_fin
|
||||
cmp r3,#value_fin * NBMAXIELEMENTS
|
||||
moveq r0,#-1 @ error
|
||||
beq 100f
|
||||
str r3,[r0,#queue_ptfin] @ store new end pointer
|
||||
100:
|
||||
pop {r1-r4,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
/******************************************************************/
|
||||
/* pop queue */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of queue structure */
|
||||
popQueue:
|
||||
push {r1,r2,lr} @ save registres
|
||||
mov r1,r0 @ control if empty queue
|
||||
bl isEmpty
|
||||
cmp r0,#1 @ yes -> error
|
||||
moveq r0,#-1
|
||||
beq 100f
|
||||
mov r0,r1
|
||||
ldr r1,[r0,#queue_ptdeb] @ begin pointer
|
||||
add r2,r0,#queue_stvalue @ address of begin values item
|
||||
add r2,r1 @ address of item
|
||||
add r1,#value_fin
|
||||
str r1,[r0,#queue_ptdeb] @ store nex begin pointer
|
||||
mov r0,r2 @ return pointer item
|
||||
100:
|
||||
pop {r1,r2,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
/******************************************************************/
|
||||
/* display text with size calculation */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the message */
|
||||
affichageMess:
|
||||
push {r0,r1,r2,r7,lr} @ save registres
|
||||
mov r2,#0 @ counter length
|
||||
1: @ loop length calculation
|
||||
ldrb r1,[r0,r2] @ read octet start position + index
|
||||
cmp r1,#0 @ if 0 its over
|
||||
addne r2,r2,#1 @ else add 1 in the length
|
||||
bne 1b @ and loop
|
||||
@ so here r2 contains the length of the message
|
||||
mov r1,r0 @ address message in r1
|
||||
mov r0,#STDOUT @ code to write to the standard output Linux
|
||||
mov r7, #WRITE @ code call system "write"
|
||||
svc #0 @ call systeme
|
||||
pop {r0,r1,r2,r7,lr} @ restaur registers */
|
||||
bx lr @ return
|
||||
/******************************************************************/
|
||||
/* Converting a register to a decimal */
|
||||
/******************************************************************/
|
||||
/* r0 contains value and r1 address area */
|
||||
.equ LGZONECAL, 10
|
||||
conversion10:
|
||||
push {r1-r4,lr} @ save registers
|
||||
mov r3,r1
|
||||
mov r2,#LGZONECAL
|
||||
1: @ start loop
|
||||
bl divisionpar10 @ r0 <- dividende. quotient ->r0 reste -> r1
|
||||
add r1,#48 @ digit
|
||||
strb r1,[r3,r2] @ store digit on area
|
||||
cmp r0,#0 @ stop if quotient = 0
|
||||
subne r2,#1 @ previous position
|
||||
bne 1b @ else loop
|
||||
@ end replaces digit in front of area
|
||||
mov r4,#0
|
||||
2:
|
||||
ldrb r1,[r3,r2]
|
||||
strb r1,[r3,r4] @ store in area begin
|
||||
add r4,#1
|
||||
add r2,#1 @ previous position
|
||||
cmp r2,#LGZONECAL @ end
|
||||
ble 2b @ loop
|
||||
mov r1,#' '
|
||||
3:
|
||||
strb r1,[r3,r4]
|
||||
add r4,#1
|
||||
cmp r4,#LGZONECAL @ end
|
||||
ble 3b
|
||||
100:
|
||||
pop {r1-r4,lr} @ restaur registres
|
||||
bx lr @return
|
||||
/***************************************************/
|
||||
/* division par 10 signé */
|
||||
/* Thanks to http://thinkingeek.com/arm-assembler-raspberry-pi/*
|
||||
/* and http://www.hackersdelight.org/ */
|
||||
/***************************************************/
|
||||
/* r0 dividende */
|
||||
/* r0 quotient */
|
||||
/* r1 remainder */
|
||||
divisionpar10:
|
||||
/* r0 contains the argument to be divided by 10 */
|
||||
push {r2-r4} @ save registers */
|
||||
mov r4,r0
|
||||
mov r3,#0x6667 @ r3 <- magic_number lower
|
||||
movt r3,#0x6666 @ r3 <- magic_number upper
|
||||
smull r1, r2, r3, r0 @ r1 <- Lower32Bits(r1*r0). r2 <- Upper32Bits(r1*r0)
|
||||
mov r2, r2, ASR #2 @ r2 <- r2 >> 2
|
||||
mov r1, r0, LSR #31 @ r1 <- r0 >> 31
|
||||
add r0, r2, r1 @ r0 <- r2 + r1
|
||||
add r2,r0,r0, lsl #2 @ r2 <- r0 * 5
|
||||
sub r1,r4,r2, lsl #1 @ r1 <- r4 - (r2 * 2) = r4 - (r0 * 10)
|
||||
pop {r2-r4}
|
||||
bx lr @ return
|
||||
181
Task/Queue-Definition/ATS/queue-definition-1.ats
Normal file
181
Task/Queue-Definition/ATS/queue-definition-1.ats
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
(*------------------------------------------------------------------*)
|
||||
|
||||
#define ATS_DYNLOADFLAG 0
|
||||
|
||||
#include "share/atspre_staload.hats"
|
||||
|
||||
staload UN = "prelude/SATS/unsafe.sats"
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
|
||||
vtypedef queue_vt (vt : vt@ype+, n : int) =
|
||||
(* A list that forms the queue, and a pointer to its last node. *)
|
||||
@(list_vt (vt, n), ptr)
|
||||
|
||||
#define NIL list_vt_nil ()
|
||||
#define :: list_vt_cons
|
||||
|
||||
fn {}
|
||||
queue_vt_nil {vt : vt@ype}
|
||||
() :
|
||||
queue_vt (vt, 0) =
|
||||
@(NIL, the_null_ptr)
|
||||
|
||||
fn {}
|
||||
queue_vt_is_empty
|
||||
{n : int}
|
||||
{vt : vt@ype}
|
||||
(q : !queue_vt (vt, n)) :
|
||||
[is_empty : bool | is_empty == (n == 0)]
|
||||
bool is_empty =
|
||||
case+ q.0 of
|
||||
| NIL => true
|
||||
| _ :: _ => false
|
||||
|
||||
fn {vt : vt@ype}
|
||||
queue_vt_enqueue
|
||||
{n : int}
|
||||
(q : queue_vt (vt, n),
|
||||
x : vt) :
|
||||
(* Returns the new queue. *)
|
||||
[m : int | 1 <= m; m == n + 1]
|
||||
queue_vt (vt, m) =
|
||||
let
|
||||
val @(lst, tail_ptr) = q
|
||||
prval _ = lemma_list_vt_param lst
|
||||
in
|
||||
case+ lst of
|
||||
| ~ NIL =>
|
||||
let
|
||||
val lst = x :: NIL
|
||||
val tail_ptr = $UN.castvwtp1{ptr} lst
|
||||
in
|
||||
@(lst, tail_ptr)
|
||||
end
|
||||
| _ :: _ =>
|
||||
let
|
||||
val old_tail = $UN.castvwtp0{list_vt (vt, 1)} tail_ptr
|
||||
val+ @ (hd :: tl) = old_tail
|
||||
|
||||
(* Extend the list by one node, at its tail end. *)
|
||||
val new_tail : list_vt (vt, 1) = x :: NIL
|
||||
val tail_ptr = $UN.castvwtp1{ptr} new_tail
|
||||
prval _ = $UN.castvwtp0{void} tl
|
||||
val _ = tl := new_tail
|
||||
|
||||
prval _ = fold@ old_tail
|
||||
prval _ = $UN.castvwtp0{void} old_tail
|
||||
|
||||
(* Let us cheat and simply *assert* (rather than prove) that
|
||||
the list has grown by one node. *)
|
||||
val lst = $UN.castvwtp0{list_vt (vt, n + 1)} lst
|
||||
in
|
||||
@(lst, tail_ptr)
|
||||
end
|
||||
end
|
||||
|
||||
(* The dequeue routine simply CANNOT BE CALLED with an empty queue.
|
||||
It requires a queue of type queue_vt (vt, n) where n is positive. *)
|
||||
fn {vt : vt@ype}
|
||||
queue_vt_dequeue
|
||||
{n : int | 1 <= n}
|
||||
(q : queue_vt (vt, n)) :
|
||||
(* Returns a tuple: the dequeued element and the new queue. *)
|
||||
[m : int | 0 <= m; m == n - 1]
|
||||
@(vt, queue_vt (vt, m)) =
|
||||
case+ q.0 of
|
||||
| ~ (x :: lst) => @(x, @(lst, q.1))
|
||||
|
||||
(* queue_vt is a linear type that must be freed. *)
|
||||
extern fun {vt : vt@ype}
|
||||
queue_vt$element_free : vt -> void
|
||||
fn {vt : vt@ype}
|
||||
queue_vt_free {n : int}
|
||||
(q : queue_vt (vt, n)) :
|
||||
void =
|
||||
let
|
||||
fun
|
||||
loop {n : nat} .<n>. (lst : list_vt (vt, n)) : void =
|
||||
case+ lst of
|
||||
| ~ NIL => begin end
|
||||
| ~ (hd :: tl) =>
|
||||
begin
|
||||
queue_vt$element_free<vt> hd;
|
||||
loop tl
|
||||
end
|
||||
prval _ = lemma_list_vt_param (q.0)
|
||||
in
|
||||
loop (q.0)
|
||||
end
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
(* An example: a queue of nonlinear strings. *)
|
||||
|
||||
vtypedef strq_vt (n : int) = queue_vt (string, n)
|
||||
|
||||
fn {} (* A parameterless template, for efficiency. *)
|
||||
strq_vt_nil () : strq_vt 0 =
|
||||
queue_vt_nil ()
|
||||
|
||||
fn {} (* A parameterless template, for efficiency. *)
|
||||
strq_vt_is_empty {n : int} (q : !strq_vt n) :
|
||||
[is_empty : bool | is_empty == (n == 0)] bool is_empty =
|
||||
queue_vt_is_empty<> q
|
||||
|
||||
fn
|
||||
strq_vt_enqueue {n : int} (q : strq_vt n, x : string) :
|
||||
[m : int | 1 <= m; m == n + 1] strq_vt m =
|
||||
queue_vt_enqueue<string> (q, x)
|
||||
|
||||
fn (* Impossible to... VVVVVV ...call with an empty queue. *)
|
||||
strq_vt_dequeue {n : int | 1 <= n} (q : strq_vt n) :
|
||||
[m : int | 0 <= m; m == n - 1] @(string, strq_vt m) =
|
||||
queue_vt_dequeue<string> q
|
||||
|
||||
fn
|
||||
strq_vt_free {n : int} (q : strq_vt n) : void =
|
||||
let
|
||||
implement
|
||||
queue_vt$element_free<string> x =
|
||||
(* A nonlinear string will be allowed to leak. (It might be
|
||||
collected as garbage, however.) *)
|
||||
begin end
|
||||
in
|
||||
queue_vt_free<string> q
|
||||
end
|
||||
|
||||
macdef qnil = strq_vt_nil ()
|
||||
overload iseqz with strq_vt_is_empty
|
||||
overload << with strq_vt_enqueue
|
||||
overload pop with strq_vt_dequeue
|
||||
overload free with strq_vt_free
|
||||
|
||||
implement
|
||||
main0 () =
|
||||
{
|
||||
val q = qnil
|
||||
val _ = println! ("val q = qnil")
|
||||
val _ = println! ("iseqz q = ", iseqz q)
|
||||
val _ = println! ("val q = q << \"one\" << \"two\" << \"three\"")
|
||||
val q = q << "one" << "two" << "three"
|
||||
val _ = println! ("iseqz q = ", iseqz q)
|
||||
val _ = println! ("val (x, q) = pop q")
|
||||
val (x, q) = pop q
|
||||
val _ = println! ("x = ", x)
|
||||
val _ = println! ("val (x, q) = pop q")
|
||||
val (x, q) = pop q
|
||||
val _ = println! ("x = ", x)
|
||||
val _ = println! ("val q = q << \"four\"")
|
||||
val q = q << "four"
|
||||
val _ = println! ("val (x, q) = pop q")
|
||||
val (x, q) = pop q
|
||||
val _ = println! ("x = ", x)
|
||||
val _ = println! ("val (x, q) = pop q")
|
||||
val (x, q) = pop q
|
||||
val _ = println! ("x = ", x)
|
||||
val _ = println! ("iseqz q = ", iseqz q)
|
||||
//val (x, q) = pop q // If you uncomment this you cannot compile!
|
||||
val _ = free q
|
||||
}
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
208
Task/Queue-Definition/ATS/queue-definition-2.ats
Normal file
208
Task/Queue-Definition/ATS/queue-definition-2.ats
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
(*------------------------------------------------------------------*)
|
||||
(*
|
||||
|
||||
The following implementation prevents us from trying to dequeue
|
||||
from an empty queue. A program that tries to do so cannot be
|
||||
compiled.
|
||||
|
||||
However, it does not prove there are no buffer overruns.
|
||||
|
||||
It contains much embedded C code, for which I used the quick and
|
||||
dirty "$extfcall" method.
|
||||
|
||||
*)
|
||||
(*------------------------------------------------------------------*)
|
||||
|
||||
#define ATS_DYNLOADFLAG 0
|
||||
|
||||
#include "share/atspre_staload.hats"
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
|
||||
(* For the demonstration, let us set BUFSIZE_INITIAL to the minimum
|
||||
possible. If you try setting it any lower, though, you cannot
|
||||
compile the program. *)
|
||||
#define BUFSIZE_INITIAL 2
|
||||
|
||||
prval _ = prop_verify {2 <= BUFSIZE_INITIAL} ()
|
||||
|
||||
datatype queue_t (t : t@ype+,
|
||||
n : int) =
|
||||
| queue_t_empty (t, 0) of (size_t, ptr)
|
||||
| {1 <= n}
|
||||
queue_t_nonempty (t, n) of
|
||||
(size_t, ptr, size_t n, size_t, size_t)
|
||||
|
||||
fn
|
||||
queue_t_new {t : t@ype}
|
||||
() : queue_t (t, 0) =
|
||||
queue_t_empty (i2sz 0, the_null_ptr)
|
||||
|
||||
fn
|
||||
queue_t_is_empty
|
||||
{n : int}
|
||||
{t : t@ype}
|
||||
(q : queue_t (t, n)) :
|
||||
[b : bool | b == (n == 0)]
|
||||
bool b =
|
||||
case+ q of
|
||||
| queue_t_empty _ => true
|
||||
| queue_t_nonempty _ => false
|
||||
|
||||
fn {t : t@ype}
|
||||
queue_t_enqueue
|
||||
{n : int}
|
||||
(q : queue_t (t, n),
|
||||
x : t) :
|
||||
[m : int | 1 <= m; m == n + 1]
|
||||
queue_t (t, m) =
|
||||
let
|
||||
macdef tsz = sizeof<t>
|
||||
macdef zero = i2sz 0
|
||||
macdef one = i2sz 1
|
||||
var xvar = x
|
||||
val px = addr@ xvar
|
||||
in
|
||||
case+ q of
|
||||
| queue_t_empty (bufsize, pbuf) =>
|
||||
if bufsize = zero then
|
||||
let
|
||||
val bufsize = i2sz BUFSIZE_INITIAL
|
||||
val pbuf =
|
||||
$extfcall (ptr, "ATS_MALLOC", bufsize * tsz)
|
||||
val _ = $extfcall (ptr, "memcpy", pbuf, px, tsz)
|
||||
in
|
||||
queue_t_nonempty (bufsize, pbuf, one, zero, one)
|
||||
end
|
||||
else
|
||||
let
|
||||
val _ = $extfcall (ptr, "memcpy", pbuf, px, tsz)
|
||||
in
|
||||
queue_t_nonempty (bufsize, pbuf, one, zero, one)
|
||||
end
|
||||
| queue_t_nonempty (bufsize, pbuf, n, ihead, itail) =>
|
||||
if n = bufsize then
|
||||
let
|
||||
(* Resize the buffer. *)
|
||||
val bsize = i2sz 2 * bufsize
|
||||
val _ = assertloc (itail = ihead) (* Sanity check. *)
|
||||
val _ = assertloc (bufsize < bsize) (* Overflow? *)
|
||||
val p = $extfcall (ptr, "ATS_MALLOC", bsize * tsz)
|
||||
val _ = $extfcall (ptr, "memcpy", p,
|
||||
ptr_add<t> (pbuf, ihead),
|
||||
(bufsize - ihead) * tsz)
|
||||
val _ = $extfcall (ptr, "memcpy",
|
||||
ptr_add<t> (p, bufsize - ihead),
|
||||
pbuf, ihead * tsz)
|
||||
val _ = $extfcall (ptr, "memcpy", ptr_add<t> (p, n),
|
||||
px, tsz)
|
||||
in
|
||||
queue_t_nonempty (bsize, p, succ n, zero, succ n)
|
||||
end
|
||||
else
|
||||
let
|
||||
val _ = $extfcall (ptr, "memcpy", ptr_add<t> (pbuf, itail),
|
||||
px, tsz)
|
||||
val itail = (succ itail) mod bufsize
|
||||
in
|
||||
queue_t_nonempty (bufsize, pbuf, succ n, ihead, itail)
|
||||
end
|
||||
end
|
||||
|
||||
fn {t : t@ype}
|
||||
queue_t_dequeue
|
||||
{n : int | 1 <= n}
|
||||
(q : queue_t (t, n)) :
|
||||
[m : int | m == n - 1]
|
||||
@(t, queue_t (t, m)) =
|
||||
let
|
||||
macdef tsz = sizeof<t>
|
||||
macdef zero = i2sz 0
|
||||
macdef one = i2sz 1
|
||||
var xvar : t
|
||||
val px = addr@ xvar
|
||||
val queue_t_nonempty (bufsize, pbuf, n, ihead, itail) = q
|
||||
val _ = $extfcall (ptr, "memcpy", px, ptr_add<t> (pbuf, ihead),
|
||||
tsz)
|
||||
val ihead = (succ ihead) mod bufsize
|
||||
val x = $UNSAFE.cast{t} xvar
|
||||
in
|
||||
if n = one then
|
||||
@(x, queue_t_empty (bufsize, pbuf))
|
||||
else
|
||||
@(x, queue_t_nonempty (bufsize, pbuf, pred n, ihead, itail))
|
||||
end
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
(* An example: a queue of strings. *)
|
||||
|
||||
vtypedef strq_t (n : int) = queue_t (string, n)
|
||||
|
||||
fn
|
||||
strq_t_new () : strq_t 0 =
|
||||
queue_t_new ()
|
||||
|
||||
fn {} (* A parameterless template, for efficiency. *)
|
||||
strq_t_is_empty {n : int} (q : strq_t n) :
|
||||
[is_empty : bool | is_empty == (n == 0)] bool is_empty =
|
||||
queue_t_is_empty q
|
||||
|
||||
fn
|
||||
strq_t_enqueue {n : int} (q : strq_t n, x : string) :
|
||||
[m : int | 1 <= m; m == n + 1] strq_t m =
|
||||
queue_t_enqueue<string> (q, x)
|
||||
|
||||
fn (* Impossible to... VVVVVV ...call with an empty queue. *)
|
||||
strq_t_dequeue {n : int | 1 <= n} (q : strq_t n) :
|
||||
[m : int | 0 <= m; m == n - 1] @(string, strq_t m) =
|
||||
queue_t_dequeue<string> q
|
||||
|
||||
overload strq with strq_t_new
|
||||
overload iseqz with strq_t_is_empty
|
||||
overload << with strq_t_enqueue
|
||||
overload pop with strq_t_dequeue
|
||||
|
||||
implement
|
||||
main0 () =
|
||||
{
|
||||
val q = strq ()
|
||||
val _ = println! ("val q = strq ()")
|
||||
val _ = println! ("iseqz q = ", iseqz q)
|
||||
val _ = println! ("val q = q << \"one\" << \"two\" << \"three\"")
|
||||
val q = q << "one" << "two" << "three"
|
||||
val _ = println! ("val q = q << \"ett\" << \"två\" << \"tre\"")
|
||||
val q = q << "ett" << "två" << "tre"
|
||||
val _ = println! ("iseqz q = ", iseqz q)
|
||||
val _ = println! ("val (x, q) = pop q")
|
||||
val (x, q) = pop q
|
||||
val _ = println! ("x = ", x)
|
||||
val _ = println! ("val (x, q) = pop q")
|
||||
val (x, q) = pop q
|
||||
val _ = println! ("x = ", x)
|
||||
val _ = println! ("val q = q << \"four\"")
|
||||
val q = q << "four"
|
||||
val _ = println! ("val (x, q) = pop q")
|
||||
val (x, q) = pop q
|
||||
val _ = println! ("x = ", x)
|
||||
val _ = println! ("val (x, q) = pop q")
|
||||
val (x, q) = pop q
|
||||
val _ = println! ("x = ", x)
|
||||
val _ = println! ("val q = q << \"fyra\"")
|
||||
val q = q << "fyra"
|
||||
val _ = println! ("val (x, q) = pop q")
|
||||
val (x, q) = pop q
|
||||
val _ = println! ("x = ", x)
|
||||
val _ = println! ("val (x, q) = pop q")
|
||||
val (x, q) = pop q
|
||||
val _ = println! ("x = ", x)
|
||||
val _ = println! ("val (x, q) = pop q")
|
||||
val (x, q) = pop q
|
||||
val _ = println! ("x = ", x)
|
||||
val _ = println! ("val (x, q) = pop q")
|
||||
val (x, q) = pop q
|
||||
val _ = println! ("x = ", x)
|
||||
val _ = println! ("iseqz q = ", iseqz q)
|
||||
//val (x, q) = pop q // If you uncomment this you cannot compile!
|
||||
}
|
||||
|
||||
(*------------------------------------------------------------------*)
|
||||
32
Task/Queue-Definition/AWK/queue-definition.awk
Normal file
32
Task/Queue-Definition/AWK/queue-definition.awk
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#!/usr/bin/awk -f
|
||||
|
||||
BEGIN {
|
||||
delete q
|
||||
print "empty? " emptyP()
|
||||
print "push " push("a")
|
||||
print "push " push("b")
|
||||
print "empty? " emptyP()
|
||||
print "pop " pop()
|
||||
print "pop " pop()
|
||||
print "empty? " emptyP()
|
||||
print "pop " pop()
|
||||
}
|
||||
|
||||
function push(n) {
|
||||
q[length(q)+1] = n
|
||||
return n
|
||||
}
|
||||
|
||||
function pop() {
|
||||
if (emptyP()) {
|
||||
print "Popping from empty queue."
|
||||
exit
|
||||
}
|
||||
r = q[length(q)]
|
||||
delete q[length(q)]
|
||||
return r
|
||||
}
|
||||
|
||||
function emptyP() {
|
||||
return length(q) == 0
|
||||
}
|
||||
72
Task/Queue-Definition/Action-/queue-definition-1.action
Normal file
72
Task/Queue-Definition/Action-/queue-definition-1.action
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
DEFINE MAXSIZE="200"
|
||||
BYTE ARRAY queue(MAXSIZE)
|
||||
BYTE queueFront=[0],queueRear=[0]
|
||||
|
||||
BYTE FUNC IsEmpty()
|
||||
IF queueFront=queueRear THEN
|
||||
RETURN (1)
|
||||
FI
|
||||
RETURN (0)
|
||||
|
||||
PROC Push(BYTE v)
|
||||
BYTE rear
|
||||
|
||||
rear=queueRear+1
|
||||
IF rear=MAXSIZE THEN
|
||||
rear=0
|
||||
FI
|
||||
IF rear=queueFront THEN
|
||||
PrintE("Error: queue is full!")
|
||||
Break()
|
||||
FI
|
||||
queue(queueRear)=v
|
||||
queueRear=rear
|
||||
RETURN
|
||||
|
||||
BYTE FUNC Pop()
|
||||
BYTE v
|
||||
|
||||
IF IsEmpty() THEN
|
||||
PrintE("Error: queue is empty!")
|
||||
Break()
|
||||
FI
|
||||
v=queue(queueFront)
|
||||
queueFront==+1
|
||||
IF queueFront=MAXSIZE THEN
|
||||
queueFront=0
|
||||
FI
|
||||
RETURN (v)
|
||||
|
||||
PROC TestIsEmpty()
|
||||
IF IsEmpty() THEN
|
||||
PrintE("Queue is empty")
|
||||
ELSE
|
||||
PrintE("Queue is not empty")
|
||||
FI
|
||||
RETURN
|
||||
|
||||
PROC TestPush(BYTE v)
|
||||
PrintF("Push: %B%E",v)
|
||||
Push(v)
|
||||
RETURN
|
||||
|
||||
PROC TestPop()
|
||||
BYTE v
|
||||
|
||||
Print("Pop: ")
|
||||
v=Pop()
|
||||
PrintBE(v)
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
TestIsEmpty()
|
||||
TestPush(10)
|
||||
TestIsEmpty()
|
||||
TestPush(31)
|
||||
TestPop()
|
||||
TestIsEmpty()
|
||||
TestPush(5)
|
||||
TestPop()
|
||||
TestPop()
|
||||
TestPop()
|
||||
RETURN
|
||||
84
Task/Queue-Definition/Action-/queue-definition-2.action
Normal file
84
Task/Queue-Definition/Action-/queue-definition-2.action
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
CARD EndProg ;required for ALLOCATE.ACT
|
||||
|
||||
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!
|
||||
|
||||
DEFINE PTR="CARD"
|
||||
DEFINE NODE_SIZE="3"
|
||||
TYPE QueueNode=[BYTE data PTR nxt]
|
||||
|
||||
QueueNode POINTER queueFront,queueRear
|
||||
|
||||
BYTE FUNC IsEmpty()
|
||||
IF queueFront=0 THEN
|
||||
RETURN (1)
|
||||
FI
|
||||
RETURN (0)
|
||||
|
||||
PROC Push(BYTE v)
|
||||
QueueNode POINTER node
|
||||
|
||||
node=Alloc(NODE_SIZE)
|
||||
node.data=v
|
||||
node.nxt=0
|
||||
IF IsEmpty() THEN
|
||||
queueFront=node
|
||||
ELSE
|
||||
queueRear.nxt=node
|
||||
FI
|
||||
queueRear=node
|
||||
RETURN
|
||||
|
||||
BYTE FUNC Pop()
|
||||
QueueNode POINTER node
|
||||
BYTE v
|
||||
|
||||
IF IsEmpty() THEN
|
||||
PrintE("Error: queue is empty!")
|
||||
Break()
|
||||
FI
|
||||
|
||||
node=queueFront
|
||||
v=node.data
|
||||
queueFront=node.nxt
|
||||
Free(node,NODE_SIZE)
|
||||
RETURN (v)
|
||||
|
||||
PROC TestIsEmpty()
|
||||
IF IsEmpty() THEN
|
||||
PrintE("Queue is empty")
|
||||
ELSE
|
||||
PrintE("Queue is not empty")
|
||||
FI
|
||||
RETURN
|
||||
|
||||
PROC TestPush(BYTE v)
|
||||
PrintF("Push: %B%E",v)
|
||||
Push(v)
|
||||
RETURN
|
||||
|
||||
PROC TestPop()
|
||||
BYTE v
|
||||
|
||||
Print("Pop: ")
|
||||
v=Pop()
|
||||
PrintBE(v)
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
AllocInit(0)
|
||||
queueFront=0
|
||||
queueRear=0
|
||||
|
||||
Put(125) PutE() ;clear screen
|
||||
|
||||
TestIsEmpty()
|
||||
TestPush(10)
|
||||
TestIsEmpty()
|
||||
TestPush(31)
|
||||
TestPop()
|
||||
TestIsEmpty()
|
||||
TestPush(5)
|
||||
TestPop()
|
||||
TestPop()
|
||||
TestPop()
|
||||
RETURN
|
||||
20
Task/Queue-Definition/Ada/queue-definition-1.ada
Normal file
20
Task/Queue-Definition/Ada/queue-definition-1.ada
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
generic
|
||||
type Element_Type is private;
|
||||
package Fifo is
|
||||
type Fifo_Type is private;
|
||||
procedure Push(List : in out Fifo_Type; Item : in Element_Type);
|
||||
procedure Pop(List : in out Fifo_Type; Item : out Element_Type);
|
||||
function Is_Empty(List : Fifo_Type) return Boolean;
|
||||
Empty_Error : exception;
|
||||
private
|
||||
type Fifo_Element;
|
||||
type Fifo_Ptr is access Fifo_Element;
|
||||
type Fifo_Type is record
|
||||
Head : Fifo_Ptr := null;
|
||||
Tail : Fifo_Ptr := null;
|
||||
end record;
|
||||
type Fifo_Element is record
|
||||
Value : Element_Type;
|
||||
Next : Fifo_Ptr := null;
|
||||
end record;
|
||||
end Fifo;
|
||||
11
Task/Queue-Definition/Ada/queue-definition-10.ada
Normal file
11
Task/Queue-Definition/Ada/queue-definition-10.ada
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
generic
|
||||
type Element_Type is private;
|
||||
package Asynchronous_Fifo is
|
||||
protected type Fifo is
|
||||
procedure Push(Item : Element_Type);
|
||||
entry Pop(Item : out Element_Type);
|
||||
private
|
||||
Value : Element_Type;
|
||||
Valid : Boolean := False;
|
||||
end Fifo;
|
||||
end Asynchronous_Fifo;
|
||||
30
Task/Queue-Definition/Ada/queue-definition-11.ada
Normal file
30
Task/Queue-Definition/Ada/queue-definition-11.ada
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package body Asynchronous_Fifo is
|
||||
|
||||
----------
|
||||
-- Fifo --
|
||||
----------
|
||||
|
||||
protected body Fifo is
|
||||
|
||||
----------
|
||||
-- Push --
|
||||
----------
|
||||
|
||||
procedure Push (Item : Element_Type) is
|
||||
begin
|
||||
Value := Item;
|
||||
Valid := True;
|
||||
end Push;
|
||||
|
||||
---------
|
||||
-- Pop --
|
||||
---------
|
||||
|
||||
entry Pop (Item : out Element_Type) when Valid is
|
||||
begin
|
||||
Item := Value;
|
||||
end Pop;
|
||||
|
||||
end Fifo;
|
||||
|
||||
end Asynchronous_Fifo;
|
||||
48
Task/Queue-Definition/Ada/queue-definition-12.ada
Normal file
48
Task/Queue-Definition/Ada/queue-definition-12.ada
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
with Asynchronous_Fifo;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Asynchronous_Fifo_Test is
|
||||
package Int_Fifo is new Asynchronous_Fifo(Integer);
|
||||
use Int_Fifo;
|
||||
Buffer : Fifo;
|
||||
|
||||
task Writer is
|
||||
entry Stop;
|
||||
end Writer;
|
||||
|
||||
task body Writer is
|
||||
Val : Positive := 1;
|
||||
begin
|
||||
loop
|
||||
select
|
||||
accept Stop;
|
||||
exit;
|
||||
else
|
||||
Buffer.Push(Val);
|
||||
Val := Val + 1;
|
||||
end select;
|
||||
end loop;
|
||||
end Writer;
|
||||
|
||||
task Reader is
|
||||
entry Stop;
|
||||
end Reader;
|
||||
|
||||
task body Reader is
|
||||
Val : Positive;
|
||||
begin
|
||||
loop
|
||||
select
|
||||
accept Stop;
|
||||
exit;
|
||||
else
|
||||
Buffer.Pop(Val);
|
||||
Put_Line(Integer'Image(Val));
|
||||
end select;
|
||||
end loop;<syntaxhighlight lang="ada">
|
||||
end Reader;
|
||||
begin
|
||||
delay 0.1;
|
||||
Writer.Stop;
|
||||
Reader.Stop;
|
||||
end Asynchronous_Fifo_Test;
|
||||
49
Task/Queue-Definition/Ada/queue-definition-2.ada
Normal file
49
Task/Queue-Definition/Ada/queue-definition-2.ada
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
with Ada.Unchecked_Deallocation;
|
||||
|
||||
package body Fifo is
|
||||
|
||||
----------
|
||||
-- Push --
|
||||
----------
|
||||
|
||||
procedure Push (List : in out Fifo_Type; Item : in Element_Type) is
|
||||
Temp : Fifo_Ptr := new Fifo_Element'(Item, null);
|
||||
begin
|
||||
if List.Tail = null then
|
||||
List.Tail := Temp;
|
||||
end if;
|
||||
if List.Head /= null then
|
||||
List.Head.Next := Temp;
|
||||
end if;
|
||||
List.Head := Temp;
|
||||
end Push;
|
||||
|
||||
---------
|
||||
-- Pop --
|
||||
---------
|
||||
|
||||
procedure Pop (List : in out Fifo_Type; Item : out Element_Type) is
|
||||
procedure Free is new Ada.Unchecked_Deallocation(Fifo_Element, Fifo_Ptr);
|
||||
Temp : Fifo_Ptr := List.Tail;
|
||||
begin
|
||||
if List.Head = null then
|
||||
raise Empty_Error;
|
||||
end if;
|
||||
Item := List.Tail.Value;
|
||||
List.Tail := List.Tail.Next;
|
||||
if List.Tail = null then
|
||||
List.Head := null;
|
||||
end if;
|
||||
Free(Temp);
|
||||
end Pop;
|
||||
|
||||
--------------
|
||||
-- Is_Empty --
|
||||
--------------
|
||||
|
||||
function Is_Empty (List : Fifo_Type) return Boolean is
|
||||
begin
|
||||
return List.Head = null;
|
||||
end Is_Empty;
|
||||
|
||||
end Fifo;
|
||||
17
Task/Queue-Definition/Ada/queue-definition-3.ada
Normal file
17
Task/Queue-Definition/Ada/queue-definition-3.ada
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
with Fifo;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Fifo_Test is
|
||||
package Int_Fifo is new Fifo(Integer);
|
||||
use Int_Fifo;
|
||||
My_Fifo : Fifo_Type;
|
||||
Val : Integer;
|
||||
begin
|
||||
for I in 1..10 loop
|
||||
Push(My_Fifo, I);
|
||||
end loop;
|
||||
while not Is_Empty(My_Fifo) loop
|
||||
Pop(My_Fifo, Val);
|
||||
Put_Line(Integer'Image(Val));
|
||||
end loop;
|
||||
end Fifo_Test;
|
||||
13
Task/Queue-Definition/Ada/queue-definition-4.ada
Normal file
13
Task/Queue-Definition/Ada/queue-definition-4.ada
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
with Ada.Containers.Doubly_Linked_Lists;
|
||||
generic
|
||||
type Element_Type is private;
|
||||
package Generic_Fifo is
|
||||
type Fifo_Type is tagged private;
|
||||
procedure Push(The_Fifo : in out Fifo_Type; Item : in Element_Type);
|
||||
procedure Pop(The_Fifo : in out Fifo_Type; Item : out Element_Type);
|
||||
Empty_Error : Exception;
|
||||
private
|
||||
package List_Pkg is new Ada.Containers.Doubly_Linked_Lists(Element_Type);
|
||||
use List_Pkg;
|
||||
Type Fifo_Type is new List with null record;
|
||||
end Generic_Fifo;
|
||||
25
Task/Queue-Definition/Ada/queue-definition-5.ada
Normal file
25
Task/Queue-Definition/Ada/queue-definition-5.ada
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package body Generic_Fifo is
|
||||
|
||||
----------
|
||||
-- Push --
|
||||
----------
|
||||
|
||||
procedure Push (The_Fifo : in out Fifo_Type; Item : in Element_Type) is
|
||||
begin
|
||||
The_Fifo.Prepend(Item);
|
||||
end Push;
|
||||
|
||||
---------
|
||||
-- Pop --
|
||||
---------
|
||||
|
||||
procedure Pop (The_Fifo : in out Fifo_Type; Item : out Element_Type) is
|
||||
begin
|
||||
if Is_Empty(The_Fifo) then
|
||||
raise Empty_Error;
|
||||
end if;
|
||||
Item := The_Fifo.Last_Element;
|
||||
The_Fifo.Delete_Last;
|
||||
end Pop;
|
||||
|
||||
end Generic_Fifo;
|
||||
17
Task/Queue-Definition/Ada/queue-definition-6.ada
Normal file
17
Task/Queue-Definition/Ada/queue-definition-6.ada
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
with Generic_Fifo;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Generic_Fifo_Test is
|
||||
package Int_Fifo is new Generic_Fifo(Integer);
|
||||
use Int_Fifo;
|
||||
My_Fifo : Fifo_Type;
|
||||
Val : Integer;
|
||||
begin
|
||||
for I in 1..10 loop
|
||||
My_Fifo.Push(I);
|
||||
end loop;
|
||||
while not My_Fifo.Is_Empty loop
|
||||
My_Fifo.Pop(Val);
|
||||
Put_Line(Integer'Image(Val));
|
||||
end loop;
|
||||
end Generic_Fifo_Test;
|
||||
11
Task/Queue-Definition/Ada/queue-definition-7.ada
Normal file
11
Task/Queue-Definition/Ada/queue-definition-7.ada
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
generic
|
||||
type Element_Type is private;
|
||||
package Synchronous_Fifo is
|
||||
protected type Fifo is
|
||||
entry Push(Item : Element_Type);
|
||||
entry Pop(Item : out Element_Type);
|
||||
private
|
||||
Value : Element_Type;
|
||||
Is_New : Boolean := False;
|
||||
end Fifo;
|
||||
end Synchronous_Fifo;
|
||||
31
Task/Queue-Definition/Ada/queue-definition-8.ada
Normal file
31
Task/Queue-Definition/Ada/queue-definition-8.ada
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package body Synchronous_Fifo is
|
||||
|
||||
----------
|
||||
-- Fifo --
|
||||
----------
|
||||
|
||||
protected body Fifo is
|
||||
|
||||
---------
|
||||
-- Push --
|
||||
---------
|
||||
|
||||
entry Push (Item : Element_Type) when not Is_New is
|
||||
begin
|
||||
Value := Item;
|
||||
Is_New := True;
|
||||
end Push;
|
||||
|
||||
---------
|
||||
-- Pop --
|
||||
---------
|
||||
|
||||
entry Pop (Item : out Element_Type) when Is_New is
|
||||
begin
|
||||
Item := Value;
|
||||
Is_New := False;
|
||||
end Pop;
|
||||
|
||||
end Fifo;
|
||||
|
||||
end Synchronous_Fifo;
|
||||
56
Task/Queue-Definition/Ada/queue-definition-9.ada
Normal file
56
Task/Queue-Definition/Ada/queue-definition-9.ada
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
with Synchronous_Fifo;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Synchronous_Fifo_Test is
|
||||
package Int_Fifo is new Synchronous_Fifo(Integer);
|
||||
use Int_Fifo;
|
||||
Buffer : Fifo;
|
||||
|
||||
task Writer is
|
||||
entry Stop;
|
||||
end Writer;
|
||||
|
||||
task body Writer is
|
||||
Val : Positive := 1;
|
||||
begin
|
||||
loop
|
||||
select
|
||||
accept Stop;
|
||||
exit;
|
||||
else
|
||||
select
|
||||
Buffer.Push(Val);
|
||||
Val := Val + 1;
|
||||
or
|
||||
delay 1.0;
|
||||
end select;
|
||||
end select;
|
||||
end loop;
|
||||
end Writer;
|
||||
|
||||
task Reader is
|
||||
entry Stop;
|
||||
end Reader;
|
||||
|
||||
task body Reader is
|
||||
Val : Positive;
|
||||
begin
|
||||
loop
|
||||
select
|
||||
accept Stop;
|
||||
exit;
|
||||
else
|
||||
select
|
||||
Buffer.Pop(Val);
|
||||
Put_Line(Integer'Image(Val));
|
||||
or
|
||||
delay 1.0;
|
||||
end select;
|
||||
end select;
|
||||
end loop;
|
||||
end Reader;
|
||||
begin
|
||||
delay 0.1;
|
||||
Writer.Stop;
|
||||
Reader.Stop;
|
||||
end Synchronous_Fifo_Test;
|
||||
16
Task/Queue-Definition/Applesoft-BASIC/queue-definition.basic
Normal file
16
Task/Queue-Definition/Applesoft-BASIC/queue-definition.basic
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
0 DEF FN E(MPTY) = SP = FIRST
|
||||
10 GOSUB 150EMPTY
|
||||
20 LET A$ = "A": GOSUB 100PUSH
|
||||
30 LET A$ = "B": GOSUB 100PUSH
|
||||
40 GOSUB 150EMPTY
|
||||
50 GOSUB 120PULL FIRST
|
||||
60 GOSUB 120PULL FIRST
|
||||
70 GOSUB 150EMPTY
|
||||
80 GOSUB 120PULL FIRST
|
||||
90 END
|
||||
100 PRINT "PUSH "A$
|
||||
110 LET S$(SP) = A$:SP = SP + 1: RETURN
|
||||
120 GOSUB 130: PRINT "POP "A$: RETURN
|
||||
130 IF FN E(0) THEN PRINT "POPPING FROM EMPTY QUEUE";: STOP
|
||||
140 A$ = S$(FI): FI = FI + 1 : RETURN
|
||||
150 PRINT "EMPTY? " MID$ ("YESNO",4 ^ FN E(0),3): RETURN
|
||||
21
Task/Queue-Definition/Arturo/queue-definition.arturo
Normal file
21
Task/Queue-Definition/Arturo/queue-definition.arturo
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
define :queue [][
|
||||
init: [
|
||||
this\items: []
|
||||
]
|
||||
]
|
||||
|
||||
empty?: function [this :queue][
|
||||
zero? this\items
|
||||
]
|
||||
|
||||
push: function [this :queue, item][
|
||||
this\items: this\items ++ item
|
||||
]
|
||||
|
||||
pop: function [this :queue][
|
||||
ensure -> not? empty? this
|
||||
|
||||
result: this\items\0
|
||||
this\items: remove.index this\items 0
|
||||
return result
|
||||
]
|
||||
31
Task/Queue-Definition/AutoHotkey/queue-definition.ahk
Normal file
31
Task/Queue-Definition/AutoHotkey/queue-definition.ahk
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
push("qu", 2), push("qu", 44), push("qu", "xyz") ; TEST
|
||||
|
||||
MsgBox % "Len = " len("qu") ; Number of entries
|
||||
While !empty("qu") ; Repeat until queue is not empty
|
||||
MsgBox % pop("qu") ; Print popped values (2, 44, xyz)
|
||||
MsgBox Error = %ErrorLevel% ; ErrorLevel = 0: OK
|
||||
MsgBox % pop("qu") ; Empty
|
||||
MsgBox Error = %ErrorLevel% ; ErrorLevel = -1: popped too much
|
||||
MsgBox % "Len = " len("qu") ; Number of entries
|
||||
|
||||
push(queue,_) { ; push _ onto queue named "queue" (!=_), _ string not containing |
|
||||
Global
|
||||
%queue% .= %queue% = "" ? _ : "|" _
|
||||
}
|
||||
|
||||
pop(queue) { ; pop value from queue named "queue" (!=_,_1,_2)
|
||||
Global
|
||||
RegExMatch(%queue%, "([^\|]*)\|?(.*)", _)
|
||||
Return _1, ErrorLevel := -(%queue%=""), %queue% := _2
|
||||
}
|
||||
|
||||
empty(queue) { ; check if queue named "queue" is empty
|
||||
Global
|
||||
Return %queue% = ""
|
||||
}
|
||||
|
||||
len(queue) { ; number of entries in "queue"
|
||||
Global
|
||||
StringReplace %queue%, %queue%, |, |, UseErrorLevel
|
||||
Return %queue% = "" ? 0 : ErrorLevel+1
|
||||
}
|
||||
31
Task/Queue-Definition/BBC-BASIC/queue-definition.basic
Normal file
31
Task/Queue-Definition/BBC-BASIC/queue-definition.basic
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
FIFOSIZE = 1000
|
||||
|
||||
FOR n = 3 TO 5
|
||||
PRINT "Push ";n : PROCenqueue(n)
|
||||
NEXT
|
||||
PRINT "Pop " ; FNdequeue
|
||||
PRINT "Push 6" : PROCenqueue(6)
|
||||
REPEAT
|
||||
PRINT "Pop " ; FNdequeue
|
||||
UNTIL FNisempty
|
||||
PRINT "Pop " ; FNdequeue
|
||||
END
|
||||
|
||||
DEF PROCenqueue(n) : LOCAL f%
|
||||
DEF FNdequeue : LOCAL f% : f% = 1
|
||||
DEF FNisempty : LOCAL f% : f% = 2
|
||||
PRIVATE fifo(), rptr%, wptr%
|
||||
DIM fifo(FIFOSIZE-1)
|
||||
CASE f% OF
|
||||
WHEN 0:
|
||||
wptr% = (wptr% + 1) MOD FIFOSIZE
|
||||
IF rptr% = wptr% ERROR 100, "Error: queue overflowed"
|
||||
fifo(wptr%) = n
|
||||
WHEN 1:
|
||||
IF rptr% = wptr% ERROR 101, "Error: queue empty"
|
||||
rptr% = (rptr% + 1) MOD FIFOSIZE
|
||||
= fifo(rptr%)
|
||||
WHEN 2:
|
||||
= (rptr% = wptr%)
|
||||
ENDCASE
|
||||
ENDPROC
|
||||
20
Task/Queue-Definition/BQN/queue-definition-1.bqn
Normal file
20
Task/Queue-Definition/BQN/queue-definition-1.bqn
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
queue ← {
|
||||
data ← ⟨⟩
|
||||
Push ⇐ {data∾˜↩𝕩}
|
||||
Pop ⇐ {
|
||||
𝕊𝕩:
|
||||
0=≠data ? •Show "Cannot pop from empty queue";
|
||||
(data↓˜↩¯1)⊢⊑⌽data
|
||||
}
|
||||
Empty ⇐ {𝕊𝕩: 0=≠data}
|
||||
Display ⇐ {𝕊𝕩: •Show data}
|
||||
}
|
||||
|
||||
q1 ← queue
|
||||
|
||||
•Show q1.Empty@
|
||||
q1.Push 3
|
||||
q1.Push 4
|
||||
q1.Display@
|
||||
•Show q1.Pop@
|
||||
q1.Display@
|
||||
4
Task/Queue-Definition/BQN/queue-definition-2.bqn
Normal file
4
Task/Queue-Definition/BQN/queue-definition-2.bqn
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
1
|
||||
⟨ 4 3 ⟩
|
||||
3
|
||||
⟨ 4 ⟩
|
||||
69
Task/Queue-Definition/Batch-File/queue-definition.bat
Normal file
69
Task/Queue-Definition/Batch-File/queue-definition.bat
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
@echo off
|
||||
setlocal enableDelayedExpansion
|
||||
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
:: FIFO queue usage
|
||||
|
||||
:: Define the queue
|
||||
call :newQueue myQ
|
||||
|
||||
:: Populate the queue
|
||||
for %%A in (value1 value2 value3) do call :enqueue myQ %%A
|
||||
|
||||
:: Test if queue is empty by examining the tail "attribute"
|
||||
if myQ.tail==0 (echo myQ is empty) else (echo myQ is NOT empty)
|
||||
|
||||
:: Peek at the head of the queue
|
||||
call:peekQueue myQ val && echo a peek at the head of myQueue shows !val!
|
||||
|
||||
:: Process the first queue value
|
||||
call :dequeue myQ val && echo dequeued myQ value=!val!
|
||||
|
||||
:: Add some more values to the queue
|
||||
for %%A in (value4 value5 value6) do call :enqueue myQ %%A
|
||||
|
||||
:: Process the remainder of the queue
|
||||
:processQueue
|
||||
call :dequeue myQ val || goto :queueEmpty
|
||||
echo dequeued myQ value=!val!
|
||||
goto :processQueue
|
||||
:queueEmpty
|
||||
|
||||
:: Test if queue is empty using the empty "method"/"macro". Use of the
|
||||
:: second IF statement serves to demonstrate the negation of the empty
|
||||
:: "method". A single IF could have been used with an ELSE clause instead.
|
||||
if %myQ.empty% echo myQ is empty
|
||||
if not %myQ.empty% echo myQ is NOT empty
|
||||
exit /b
|
||||
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
:: FIFO queue definition
|
||||
|
||||
:newQueue qName
|
||||
set /a %~1.head=1, %~1.tail=0
|
||||
:: Define an empty "method" for this queue as a sort of macro
|
||||
set "%~1.empty=^!%~1.tail^! == 0"
|
||||
exit /b
|
||||
|
||||
:enqueue qName value
|
||||
set /a %~1.tail+=1
|
||||
set %~1.!%~1.tail!=%2
|
||||
exit /b
|
||||
|
||||
:dequeue qName returnVar
|
||||
:: Sets errorlevel to 0 if success
|
||||
:: Sets errorlevel to 1 if failure because queue was empty
|
||||
if !%~1.tail! equ 0 exit /b 1
|
||||
for %%N in (!%~1.head!) do (
|
||||
set %~2=!%~1.%%N!
|
||||
set %~1.%%N=
|
||||
)
|
||||
if !%~1.head! == !%~1.tail! (set /a "%~1.head=1, %~1.tail=0") else set /a %~1.head+=1
|
||||
exit /b 0
|
||||
|
||||
:peekQueue qName returnVar
|
||||
:: Sets errorlevel to 0 if success
|
||||
:: Sets errorlevel to 1 if failure because queue was empty
|
||||
if !%~1.tail! equ 0 exit /b 1
|
||||
for %%N in (!%~1.head!) do set %~2=!%~1.%%N!
|
||||
exit /b 0
|
||||
10
Task/Queue-Definition/Bracmat/queue-definition.bracmat
Normal file
10
Task/Queue-Definition/Bracmat/queue-definition.bracmat
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
( queue
|
||||
= (list=)
|
||||
(enqueue=.(.!arg) !(its.list):?(its.list))
|
||||
( dequeue
|
||||
= x
|
||||
. !(its.list):?(its.list) (.?x)
|
||||
& !x
|
||||
)
|
||||
(empty=.!(its.list):)
|
||||
)
|
||||
67
Task/Queue-Definition/C++/queue-definition.cpp
Normal file
67
Task/Queue-Definition/C++/queue-definition.cpp
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
namespace rosettacode
|
||||
{
|
||||
template<typename T> class queue
|
||||
{
|
||||
public:
|
||||
queue();
|
||||
~queue();
|
||||
void push(T const& t);
|
||||
T pop();
|
||||
bool empty();
|
||||
private:
|
||||
void drop();
|
||||
struct node;
|
||||
node* head;
|
||||
node* tail;
|
||||
};
|
||||
|
||||
template<typename T> struct queue<T>::node
|
||||
{
|
||||
T data;
|
||||
node* next;
|
||||
node(T const& t): data(t), next(0) {}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
queue<T>::queue():
|
||||
head(0)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void queue<T>::drop()
|
||||
{
|
||||
node* n = head;
|
||||
head = head->next;
|
||||
delete n;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
queue<T>::~queue()
|
||||
{
|
||||
while (!empty())
|
||||
drop();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void queue<T>::push(T const& t)
|
||||
{
|
||||
node*& next = head? tail->next : head;
|
||||
next = new node(t);
|
||||
tail = next;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T queue<T>::pop()
|
||||
{
|
||||
T tmp = head->data;
|
||||
drop();
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool queue<T>::empty()
|
||||
{
|
||||
return head == 0;
|
||||
}
|
||||
}
|
||||
38
Task/Queue-Definition/C-sharp/queue-definition.cs
Normal file
38
Task/Queue-Definition/C-sharp/queue-definition.cs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
public class FIFO<T>
|
||||
{
|
||||
class Node
|
||||
{
|
||||
public T Item { get; set; }
|
||||
public Node Next { get; set; }
|
||||
}
|
||||
Node first = null;
|
||||
Node last = null;
|
||||
public void push(T item)
|
||||
{
|
||||
if (empty())
|
||||
{
|
||||
//Uses object initializers to set fields of new node
|
||||
first = new Node() { Item = item, Next = null };
|
||||
last = first;
|
||||
}
|
||||
else
|
||||
{
|
||||
last.Next = new Node() { Item = item, Next = null };
|
||||
last = last.Next;
|
||||
}
|
||||
}
|
||||
public T pop()
|
||||
{
|
||||
if (first == null)
|
||||
throw new System.Exception("No elements");
|
||||
if (last == first)
|
||||
last = null;
|
||||
T temp = first.Item;
|
||||
first = first.Next;
|
||||
return temp;
|
||||
}
|
||||
public bool empty()
|
||||
{
|
||||
return first == null;
|
||||
}
|
||||
}
|
||||
52
Task/Queue-Definition/C/queue-definition-1.c
Normal file
52
Task/Queue-Definition/C/queue-definition-1.c
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef int DATA; /* type of data to store in queue */
|
||||
typedef struct {
|
||||
DATA *buf;
|
||||
size_t head, tail, alloc;
|
||||
} queue_t, *queue;
|
||||
|
||||
queue q_new()
|
||||
{
|
||||
queue q = malloc(sizeof(queue_t));
|
||||
q->buf = malloc(sizeof(DATA) * (q->alloc = 4));
|
||||
q->head = q->tail = 0;
|
||||
return q;
|
||||
}
|
||||
|
||||
int empty(queue q)
|
||||
{
|
||||
return q->tail == q->head;
|
||||
}
|
||||
|
||||
void enqueue(queue q, DATA n)
|
||||
{
|
||||
if (q->tail >= q->alloc) q->tail = 0;
|
||||
q->buf[q->tail++] = n;
|
||||
|
||||
// Fixed bug where it failed to resizes
|
||||
if (q->tail == q->alloc) { /* needs more room */
|
||||
q->buf = realloc(q->buf, sizeof(DATA) * q->alloc * 2);
|
||||
if (q->head) {
|
||||
memcpy(q->buf + q->head + q->alloc, q->buf + q->head,
|
||||
sizeof(DATA) * (q->alloc - q->head));
|
||||
q->head += q->alloc;
|
||||
} else
|
||||
q->tail = q->alloc;
|
||||
q->alloc *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
int dequeue(queue q, DATA *n)
|
||||
{
|
||||
if (q->head == q->tail) return 0;
|
||||
*n = q->buf[q->head++];
|
||||
if (q->head >= q->alloc) { /* reduce allocated storage no longer needed */
|
||||
q->head = 0;
|
||||
if (q->alloc >= 512 && q->tail < q->alloc / 2)
|
||||
q->buf = realloc(q->buf, sizeof(DATA) * (q->alloc/=2));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
43
Task/Queue-Definition/C/queue-definition-2.c
Normal file
43
Task/Queue-Definition/C/queue-definition-2.c
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct node_t node_t, *node, *queue;
|
||||
struct node_t { int val; node prev, next; };
|
||||
|
||||
#define HEAD(q) q->prev
|
||||
#define TAIL(q) q->next
|
||||
queue q_new()
|
||||
{
|
||||
node q = malloc(sizeof(node_t));
|
||||
q->next = q->prev = 0;
|
||||
return q;
|
||||
}
|
||||
|
||||
int empty(queue q)
|
||||
{
|
||||
return !HEAD(q);
|
||||
}
|
||||
|
||||
void enqueue(queue q, int n)
|
||||
{
|
||||
node nd = malloc(sizeof(node_t));
|
||||
nd->val = n;
|
||||
if (!HEAD(q)) HEAD(q) = nd;
|
||||
nd->prev = TAIL(q);
|
||||
if (nd->prev) nd->prev->next = nd;
|
||||
TAIL(q) = nd;
|
||||
nd->next = 0;
|
||||
}
|
||||
|
||||
int dequeue(queue q, int *val)
|
||||
{
|
||||
node tmp = HEAD(q);
|
||||
if (!tmp) return 0;
|
||||
*val = tmp->val;
|
||||
|
||||
HEAD(q) = tmp->next;
|
||||
if (TAIL(q) == tmp) TAIL(q) = 0;
|
||||
free(tmp);
|
||||
|
||||
return 1;
|
||||
}
|
||||
22
Task/Queue-Definition/C/queue-definition-3.c
Normal file
22
Task/Queue-Definition/C/queue-definition-3.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
int main()
|
||||
{
|
||||
int i, n;
|
||||
queue q = q_new();
|
||||
|
||||
for (i = 0; i < 100000000; i++) {
|
||||
n = rand();
|
||||
if (n > RAND_MAX / 2) {
|
||||
// printf("+ %d\n", n);
|
||||
enqueue(q, n);
|
||||
} else {
|
||||
if (!dequeue(q, &n)) {
|
||||
// printf("empty\n");
|
||||
continue;
|
||||
}
|
||||
// printf("- %d\n", n);
|
||||
}
|
||||
}
|
||||
while (dequeue(q, &n));// printf("- %d\n", n);
|
||||
|
||||
return 0;
|
||||
}
|
||||
47
Task/Queue-Definition/C/queue-definition-4.c
Normal file
47
Task/Queue-Definition/C/queue-definition-4.c
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <sys/queue.h>
|
||||
|
||||
struct entry {
|
||||
int value;
|
||||
TAILQ_ENTRY(entry) entries;
|
||||
};
|
||||
|
||||
typedef struct entry entry_t;
|
||||
|
||||
TAILQ_HEAD(FIFOList_s, entry);
|
||||
|
||||
typedef struct FIFOList_s FIFOList;
|
||||
|
||||
|
||||
bool m_enqueue(int v, FIFOList *l)
|
||||
{
|
||||
entry_t *val;
|
||||
val = malloc(sizeof(entry_t));
|
||||
if ( val != NULL ) {
|
||||
val->value = v;
|
||||
TAILQ_INSERT_TAIL(l, val, entries);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool m_dequeue(int *v, FIFOList *l)
|
||||
{
|
||||
entry_t *e = l->tqh_first;
|
||||
if ( e != NULL ) {
|
||||
*v = e->value;
|
||||
TAILQ_REMOVE(l, e, entries);
|
||||
free(e);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isQueueEmpty(FIFOList *l)
|
||||
{
|
||||
if ( l->tqh_first == NULL ) return true;
|
||||
return false;
|
||||
}
|
||||
30
Task/Queue-Definition/Clojure/queue-definition.clj
Normal file
30
Task/Queue-Definition/Clojure/queue-definition.clj
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
user=> (def empty-queue clojure.lang.PersistentQueue/EMPTY)
|
||||
#'user/empty-queue
|
||||
user=> (def aqueue (atom empty-queue))
|
||||
#'user/aqueue
|
||||
; Check if queue is empty
|
||||
user=> (empty? @aqueue)
|
||||
true
|
||||
; As with other Clojure data structures, you can add items using conj and into
|
||||
user=> (swap! aqueue conj 1)
|
||||
user=> (swap! aqueue into [2 3 4])
|
||||
user=> (pprint @aqueue)
|
||||
<-(1 2 3 4)-<
|
||||
; You can read the head of the queue with peek
|
||||
user=> (peek @aqueue)
|
||||
1
|
||||
; You can remove the head producing a new queue using pop
|
||||
user=> (pprint (pop @aqueue))
|
||||
<-(2 3 4)-<
|
||||
; pop returns a new queue, the original is still intact
|
||||
user=> (pprint @aqueue)
|
||||
<-(1 2 3 4)-<
|
||||
; you can treat a queue as a sequence
|
||||
user=> (into [] @aqueue)
|
||||
[1 2 3 4]
|
||||
; but remember that using rest or next converts the queue to a seq. Compare:
|
||||
user=> (-> @aqueue rest (conj 5) pprint)
|
||||
(5 2 3 4)
|
||||
; with:
|
||||
user=> (-> @aqueue pop (conj 5) pprint)
|
||||
<-(2 3 4 5)-<
|
||||
38
Task/Queue-Definition/CoffeeScript/queue-definition.coffee
Normal file
38
Task/Queue-Definition/CoffeeScript/queue-definition.coffee
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
# Implement a fifo as an array of arrays, to
|
||||
# greatly amortize dequeue costs, at some expense of
|
||||
# memory overhead and insertion time. The speedup
|
||||
# depends on the underlying JS implementation, but
|
||||
# it's significant on node.js.
|
||||
Fifo = ->
|
||||
max_chunk = 512
|
||||
arr = [] # array of arrays
|
||||
count = 0
|
||||
|
||||
self =
|
||||
enqueue: (elem) ->
|
||||
if count == 0 or arr[arr.length-1].length >= max_chunk
|
||||
arr.push []
|
||||
count += 1
|
||||
arr[arr.length-1].push elem
|
||||
dequeue: (elem) ->
|
||||
throw Error("queue is empty") if count == 0
|
||||
val = arr[0].shift()
|
||||
count -= 1
|
||||
if arr[0].length == 0
|
||||
arr.shift()
|
||||
val
|
||||
is_empty: (elem) ->
|
||||
count == 0
|
||||
|
||||
# test
|
||||
do ->
|
||||
max = 5000000
|
||||
q = Fifo()
|
||||
for i in [1..max]
|
||||
q.enqueue
|
||||
number: i
|
||||
|
||||
console.log q.dequeue()
|
||||
while !q.is_empty()
|
||||
v = q.dequeue()
|
||||
console.log v
|
||||
26
Task/Queue-Definition/Common-Lisp/queue-definition.lisp
Normal file
26
Task/Queue-Definition/Common-Lisp/queue-definition.lisp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
(defstruct (queue (:constructor %make-queue))
|
||||
(items '() :type list)
|
||||
(tail '() :type list))
|
||||
|
||||
(defun make-queue ()
|
||||
"Returns an empty queue."
|
||||
(%make-queue))
|
||||
|
||||
(defun queue-empty-p (queue)
|
||||
"Returns true if the queue is empty."
|
||||
(endp (queue-items queue)))
|
||||
|
||||
(defun enqueue (item queue)
|
||||
"Enqueue item in queue. Returns the queue."
|
||||
(prog1 queue
|
||||
(if (queue-empty-p queue)
|
||||
(setf (queue-items queue) (list item)
|
||||
(queue-tail queue) (queue-items queue))
|
||||
(setf (cdr (queue-tail queue)) (list item)
|
||||
(queue-tail queue) (cdr (queue-tail queue))))))
|
||||
|
||||
(defun dequeue (queue)
|
||||
"Dequeues an item from queue. Signals an error if queue is empty."
|
||||
(if (queue-empty-p queue)
|
||||
(error "Cannot dequeue from empty queue.")
|
||||
(pop (queue-items queue))))
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
MODULE Queue;
|
||||
IMPORT
|
||||
Boxes;
|
||||
TYPE
|
||||
Instance* = POINTER TO LIMITED RECORD
|
||||
size: LONGINT;
|
||||
first,last: LONGINT;
|
||||
_queue: POINTER TO ARRAY OF Boxes.Box;
|
||||
END;
|
||||
|
||||
PROCEDURE (self: Instance) Initialize(capacity: LONGINT),NEW;
|
||||
BEGIN
|
||||
self.size := 0;
|
||||
self.first := 0;
|
||||
self.last := 0;
|
||||
NEW(self._queue,capacity)
|
||||
END Initialize;
|
||||
|
||||
PROCEDURE New*(capacity: LONGINT): Instance;
|
||||
VAR
|
||||
aQueue: Instance;
|
||||
BEGIN
|
||||
NEW(aQueue);aQueue.Initialize(capacity);RETURN aQueue
|
||||
END New;
|
||||
|
||||
PROCEDURE (self: Instance) IsEmpty*(): BOOLEAN, NEW;
|
||||
BEGIN
|
||||
RETURN self.size = 0;
|
||||
END IsEmpty;
|
||||
|
||||
PROCEDURE (self: Instance) Capacity*(): LONGINT, NEW;
|
||||
BEGIN
|
||||
RETURN LEN(self._queue)
|
||||
END Capacity;
|
||||
|
||||
PROCEDURE (self: Instance) Size*(): LONGINT, NEW;
|
||||
BEGIN
|
||||
RETURN self.size
|
||||
END Size;
|
||||
|
||||
PROCEDURE (self: Instance) IsFull*(): BOOLEAN, NEW;
|
||||
BEGIN
|
||||
RETURN self.size = self.Capacity()
|
||||
END IsFull;
|
||||
|
||||
PROCEDURE (self: Instance) Push*(b: Boxes.Box), NEW;
|
||||
VAR
|
||||
i, j, newCapacity, oldSize: LONGINT;
|
||||
queue: POINTER TO ARRAY OF Boxes.Box;
|
||||
BEGIN
|
||||
INC(self.size);
|
||||
self._queue[self.last] := b;
|
||||
self.last := (self.last + 1) MOD self.Capacity();
|
||||
IF self.IsFull() THEN
|
||||
(* grow queue *)
|
||||
newCapacity := self.Capacity() + (self.Capacity() DIV 2);
|
||||
(* new queue *)
|
||||
NEW(queue,newCapacity);
|
||||
(* move data from old to new queue *)
|
||||
i := self.first; j := 0; oldSize := self.Capacity() - self.first + self.last;
|
||||
WHILE (j < oldSize) & (j < newCapacity - 1) DO
|
||||
queue[j] := self._queue[i];
|
||||
i := (i + 1) MOD newCapacity;INC(j)
|
||||
END;
|
||||
self._queue := queue;self.first := 0;self.last := j
|
||||
END
|
||||
END Push;
|
||||
|
||||
PROCEDURE (self: Instance) Pop*(): Boxes.Box, NEW;
|
||||
VAR
|
||||
b: Boxes.Box;
|
||||
BEGIN
|
||||
ASSERT(~self.IsEmpty());
|
||||
DEC(self.size);
|
||||
b := self._queue[self.first];
|
||||
self._queue[self.first] := NIL;
|
||||
self.first := (self.first + 1) MOD self.Capacity();
|
||||
RETURN b
|
||||
END Pop;
|
||||
|
||||
END Queue.
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
DEFINITION Queue;
|
||||
|
||||
IMPORT Boxes;
|
||||
|
||||
TYPE
|
||||
Instance = POINTER TO LIMITED RECORD
|
||||
(self: Instance) Capacity (): LONGINT, NEW;
|
||||
(self: Instance) IsEmpty (): BOOLEAN, NEW;
|
||||
(self: Instance) IsFull (): BOOLEAN, NEW;
|
||||
(self: Instance) Pop (): Boxes.Box, NEW;
|
||||
(self: Instance) Push (b: Boxes.Box), NEW;
|
||||
(self: Instance) Size (): LONGINT, NEW
|
||||
END;
|
||||
|
||||
PROCEDURE New (capacity: LONGINT): Instance;
|
||||
|
||||
END Queue.
|
||||
65
Task/Queue-Definition/Cowgol/queue-definition.cowgol
Normal file
65
Task/Queue-Definition/Cowgol/queue-definition.cowgol
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
include "strings.coh";
|
||||
include "malloc.coh";
|
||||
|
||||
# Define types. The calling code is expected to provide a QueueData type.
|
||||
record QueueItem is
|
||||
data: QueueData;
|
||||
next: [QueueItem];
|
||||
end record;
|
||||
|
||||
record QueueMeta is
|
||||
head: [QueueItem];
|
||||
tail: [QueueItem];
|
||||
end record;
|
||||
|
||||
typedef Queue is [QueueMeta];
|
||||
const Q_NONE := 0 as [QueueItem];
|
||||
|
||||
# Allocate and free the queue datastructure.
|
||||
sub MakeQueue(): (q: Queue) is
|
||||
q := Alloc(@bytesof QueueMeta) as Queue;
|
||||
q.head := Q_NONE;
|
||||
q.tail := Q_NONE;
|
||||
end sub;
|
||||
|
||||
sub FreeQueue(q: Queue) is
|
||||
var cur := q.head;
|
||||
while cur != Q_NONE loop
|
||||
var next := cur.next;
|
||||
Free(cur as [uint8]);
|
||||
cur := next;
|
||||
end loop;
|
||||
Free(q as [uint8]);
|
||||
end sub;
|
||||
|
||||
# Check if queue is empty.
|
||||
sub QueueEmpty(q: Queue): (r: uint8) is
|
||||
r := 0;
|
||||
if q.head == Q_NONE then
|
||||
r := 1;
|
||||
end if;
|
||||
end sub;
|
||||
|
||||
# Enqueue and dequeue data. Cowgol has no exceptions, so the calling code
|
||||
# should check QueueEmpty first.
|
||||
sub Enqueue(q: Queue, d: QueueData) is
|
||||
var item := Alloc(@bytesof QueueItem) as [QueueItem];
|
||||
item.data := d;
|
||||
item.next := Q_NONE;
|
||||
if q.head == Q_NONE then
|
||||
q.head := item;
|
||||
else
|
||||
q.tail.next := item;
|
||||
end if;
|
||||
q.tail := item;
|
||||
end sub;
|
||||
|
||||
sub Dequeue(q: Queue): (d: QueueData) is
|
||||
d := q.head.data;
|
||||
var cur := q.head;
|
||||
q.head := q.head.next;
|
||||
Free(cur as [uint8]);
|
||||
if q.head == Q_NONE then
|
||||
q.tail := Q_NONE;
|
||||
end if;
|
||||
end sub;
|
||||
50
Task/Queue-Definition/Delphi/queue-definition.delphi
Normal file
50
Task/Queue-Definition/Delphi/queue-definition.delphi
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
program QueueDefinition;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
System.Generics.Collections;
|
||||
|
||||
type
|
||||
TQueue = System.Generics.Collections.TQueue<Integer>;
|
||||
|
||||
TQueueHelper = class helper for TQueue
|
||||
function Empty: Boolean;
|
||||
function Pop: Integer;
|
||||
procedure Push(const NewItem: Integer);
|
||||
end;
|
||||
|
||||
{ TQueueHelper }
|
||||
|
||||
function TQueueHelper.Empty: Boolean;
|
||||
begin
|
||||
Result := count = 0;
|
||||
end;
|
||||
|
||||
function TQueueHelper.Pop: Integer;
|
||||
begin
|
||||
Result := Dequeue;
|
||||
end;
|
||||
|
||||
procedure TQueueHelper.Push(const NewItem: Integer);
|
||||
begin
|
||||
Enqueue(NewItem);
|
||||
end;
|
||||
|
||||
var
|
||||
Queue: TQueue;
|
||||
i: Integer;
|
||||
|
||||
begin
|
||||
Queue := TQueue.Create;
|
||||
|
||||
for i := 1 to 1000 do
|
||||
Queue.push(i);
|
||||
|
||||
while not Queue.Empty do
|
||||
Write(Queue.pop, ' ');
|
||||
Writeln;
|
||||
|
||||
Queue.Free;
|
||||
Readln;
|
||||
end.
|
||||
27
Task/Queue-Definition/E/queue-definition.e
Normal file
27
Task/Queue-Definition/E/queue-definition.e
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
def makeQueue() {
|
||||
def [var head, var tail] := Ref.promise()
|
||||
|
||||
def writer {
|
||||
to enqueue(value) {
|
||||
def [nh, nt] := Ref.promise()
|
||||
tail.resolve([value, nh])
|
||||
tail := nt
|
||||
}
|
||||
}
|
||||
|
||||
def reader {
|
||||
to empty() { return !Ref.isResolved(head) }
|
||||
|
||||
to dequeue(whenEmpty) {
|
||||
if (Ref.isResolved(head)) {
|
||||
def [value, next] := head
|
||||
head := next
|
||||
return value
|
||||
} else {
|
||||
throw.eject(whenEmpty, "pop() of empty queue")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [reader, writer]
|
||||
}
|
||||
45
Task/Queue-Definition/ERRE/queue-definition.erre
Normal file
45
Task/Queue-Definition/ERRE/queue-definition.erre
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
PROGRAM CLASS_DEMO
|
||||
|
||||
CLASS QUEUE
|
||||
|
||||
LOCAL SP
|
||||
LOCAL DIM STACK[100]
|
||||
|
||||
FUNCTION ISEMPTY()
|
||||
ISEMPTY=(SP=0)
|
||||
END FUNCTION
|
||||
|
||||
PROCEDURE INIT
|
||||
SP=0
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE POP(->XX)
|
||||
XX=STACK[SP]
|
||||
SP=SP-1
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE PUSH(XX)
|
||||
SP=SP+1
|
||||
STACK[SP]=XX
|
||||
END PROCEDURE
|
||||
|
||||
END CLASS
|
||||
|
||||
NEW PILA:QUEUE
|
||||
|
||||
BEGIN
|
||||
PILA_INIT ! constructor
|
||||
FOR N=1 TO 4 DO ! push 4 numbers
|
||||
PRINT("Push";N)
|
||||
PILA_PUSH(N)
|
||||
END FOR
|
||||
FOR I=1 TO 5 DO ! pop 5 numbers
|
||||
IF NOT PILA_ISEMPTY() THEN
|
||||
PILA_POP(->N)
|
||||
PRINT("Pop";N)
|
||||
ELSE
|
||||
PRINT("Queue is empty!")
|
||||
END IF
|
||||
END FOR
|
||||
PRINT("* End *")
|
||||
END PROGRAM
|
||||
35
Task/Queue-Definition/EchoLisp/queue-definition.l
Normal file
35
Task/Queue-Definition/EchoLisp/queue-definition.l
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
;; put info string in permanent storage for later use
|
||||
(info 'make-Q
|
||||
"usage: (define q (make-Q)) ; (q '[top | empty? | pop | push value | to-list | from-list list])")
|
||||
|
||||
;; make-Q
|
||||
(define (make-Q)
|
||||
(let ((q (make-vector 0)))
|
||||
(lambda (message . args)
|
||||
(case message
|
||||
((empty?) (vector-empty? q))
|
||||
((top) (if (vector-empty? q) (error 'Q:top:empty q) (vector-ref q 0)))
|
||||
((push) (vector-push q (car args)))
|
||||
((pop) (if (vector-empty? q) (error 'Q:pop:empty q) (vector-shift q)))
|
||||
((to-list) (vector->list q))
|
||||
((from-list) (set! q (list->vector (car args))) q )
|
||||
(else (info 'make-Q) (error "Q:bad message:" message )))))) ; display info if unknown message
|
||||
|
||||
;;
|
||||
(define q (make-Q))
|
||||
(q 'empty?) → #t
|
||||
(q 'push 'first) → first
|
||||
(q 'push 'second) → second
|
||||
(q 'pop) → first
|
||||
(q 'pop) → second
|
||||
(q 'top)
|
||||
"💬 error: Q:top:empty #()"
|
||||
(q 'from-list '( 6 7 8)) → #( 6 7 8)
|
||||
(q 'top) → 6
|
||||
(q 'pop) → 6
|
||||
(q 'to-list)→ (7 8)
|
||||
(q 'delete)
|
||||
"💭 error: Q:bad message: delete"
|
||||
|
||||
;; save make-Q
|
||||
(local-put 'make-Q)
|
||||
63
Task/Queue-Definition/Elena/queue-definition.elena
Normal file
63
Task/Queue-Definition/Elena/queue-definition.elena
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
import extensions;
|
||||
|
||||
template queue<T>
|
||||
{
|
||||
T[] theArray;
|
||||
int theTop;
|
||||
int theTale;
|
||||
|
||||
constructor()
|
||||
{
|
||||
theArray := new T[](8);
|
||||
theTop := 0;
|
||||
theTale := 0;
|
||||
}
|
||||
|
||||
bool empty()
|
||||
= theTop == theTale;
|
||||
|
||||
push(T object)
|
||||
{
|
||||
if (theTale > theArray.Length)
|
||||
{
|
||||
theArray := theArray.reallocate(theTale)
|
||||
};
|
||||
|
||||
theArray[theTale] := object;
|
||||
|
||||
theTale += 1
|
||||
}
|
||||
|
||||
T pop()
|
||||
{
|
||||
if (theTale == theTop)
|
||||
{ InvalidOperationException.new:"Queue is empty".raise() };
|
||||
|
||||
T item := theArray[theTop];
|
||||
|
||||
theTop += 1;
|
||||
|
||||
^ item
|
||||
}
|
||||
}
|
||||
|
||||
public program()
|
||||
{
|
||||
queue<int> q := new queue<int>();
|
||||
q.push(1);
|
||||
q.push(2);
|
||||
q.push(3);
|
||||
console.printLine(q.pop());
|
||||
console.printLine(q.pop());
|
||||
console.printLine(q.pop());
|
||||
console.printLine("a queue is ", q.empty().iif("empty","not empty"));
|
||||
console.print("Trying to pop:");
|
||||
try
|
||||
{
|
||||
q.pop()
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
console.printLine(e.Message)
|
||||
}
|
||||
}
|
||||
23
Task/Queue-Definition/Elisa/queue-definition-1.elisa
Normal file
23
Task/Queue-Definition/Elisa/queue-definition-1.elisa
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
component GenericQueue ( Queue, Element );
|
||||
type Queue;
|
||||
Queue (MaxLength = integer) -> Queue;
|
||||
Length( Queue ) -> integer;
|
||||
Empty ( Queue ) -> boolean;
|
||||
Full ( Queue ) -> boolean;
|
||||
Push ( Queue, Element) -> nothing;
|
||||
Pull ( Queue ) -> Element;
|
||||
begin
|
||||
Queue (MaxLength) = Queue:[ MaxLength; length:=0; list=alist(Element) ];
|
||||
Length ( queue ) = queue.length;
|
||||
Empty ( queue ) = (queue.length <= 0);
|
||||
Full ( queue ) = (queue.length >= queue.MaxLength);
|
||||
|
||||
Push ( queue, element ) =
|
||||
[ exception (Full(queue), "Queue Overflow");
|
||||
queue.length:= queue.length + 1;
|
||||
add (queue.list, element)];
|
||||
Pull ( queue ) =
|
||||
[ exception (Empty(queue), "Queue Underflow");
|
||||
queue.length:= queue.length - 1;
|
||||
remove(first(queue.list))];
|
||||
end component GenericQueue;
|
||||
29
Task/Queue-Definition/Elisa/queue-definition-2.elisa
Normal file
29
Task/Queue-Definition/Elisa/queue-definition-2.elisa
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
use GenericQueue (QueueofPersons, Person);
|
||||
type Person = text;
|
||||
Q = QueueofPersons(25);
|
||||
|
||||
Push (Q, "Peter");
|
||||
Push (Q, "Alice");
|
||||
Push (Q, "Edward");
|
||||
Q?
|
||||
QueueofPersons:[MaxLength = 25;
|
||||
length = 3;
|
||||
list = { "Peter",
|
||||
"Alice",
|
||||
"Edward"}]
|
||||
Pull (Q)?
|
||||
"Peter"
|
||||
|
||||
Pull (Q)?
|
||||
"Alice"
|
||||
|
||||
Pull (Q)?
|
||||
"Edward"
|
||||
|
||||
Q?
|
||||
QueueofPersons:[MaxLength = 25;
|
||||
length = 0;
|
||||
list = { }]
|
||||
|
||||
Pull (Q)?
|
||||
***** Exception: Queue Underflow
|
||||
12
Task/Queue-Definition/Elixir/queue-definition.elixir
Normal file
12
Task/Queue-Definition/Elixir/queue-definition.elixir
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
defmodule Queue do
|
||||
def new, do: {Queue, [], []}
|
||||
|
||||
def push({Queue, input, output}, x), do: {Queue, [x|input], output}
|
||||
|
||||
def pop({Queue, [], []}), do: (raise RuntimeError, message: "empty Queue")
|
||||
def pop({Queue, input, []}), do: pop({Queue, [], Enum.reverse(input)})
|
||||
def pop({Queue, input, [h|t]}), do: {h, {Queue, input, t}}
|
||||
|
||||
def empty?({Queue, [], []}), do: true
|
||||
def empty?({Queue, _, _}), do: false
|
||||
end
|
||||
13
Task/Queue-Definition/Erlang/queue-definition.erl
Normal file
13
Task/Queue-Definition/Erlang/queue-definition.erl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
-module(fifo).
|
||||
-export([new/0, push/2, pop/1, empty/1]).
|
||||
|
||||
new() -> {fifo, [], []}.
|
||||
|
||||
push({fifo, In, Out}, X) -> {fifo, [X|In], Out}.
|
||||
|
||||
pop({fifo, [], []}) -> erlang:error('empty fifo');
|
||||
pop({fifo, In, []}) -> pop({fifo, [], lists:reverse(In)});
|
||||
pop({fifo, In, [H|T]}) -> {H, {fifo, In, T}}.
|
||||
|
||||
empty({fifo, [], []}) -> true;
|
||||
empty({fifo, _, _}) -> false.
|
||||
18
Task/Queue-Definition/Factor/queue-definition.factor
Normal file
18
Task/Queue-Definition/Factor/queue-definition.factor
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
USING: accessors kernel ;
|
||||
IN: rosetta-code.queue-definition
|
||||
|
||||
TUPLE: queue head tail ;
|
||||
TUPLE: node value next ;
|
||||
|
||||
: <queue> ( -- queue ) queue new ;
|
||||
: <node> ( obj -- node ) node new swap >>value ;
|
||||
|
||||
: empty? ( queue -- ? ) head>> >boolean not ;
|
||||
|
||||
: enqueue ( obj queue -- )
|
||||
[ <node> ] dip 2dup dup empty?
|
||||
[ head<< ] [ tail>> next<< ] if tail<< ;
|
||||
|
||||
: dequeue ( queue -- obj )
|
||||
dup empty? [ "Cannot dequeue empty queue." throw ] when
|
||||
[ head>> value>> ] [ head>> next>> ] [ head<< ] tri ;
|
||||
24
Task/Queue-Definition/Fantom/queue-definition.fantom
Normal file
24
Task/Queue-Definition/Fantom/queue-definition.fantom
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
class Queue
|
||||
{
|
||||
List queue := [,]
|
||||
|
||||
public Void push (Obj obj)
|
||||
{
|
||||
queue.add (obj) // add to right of list
|
||||
}
|
||||
|
||||
public Obj pop ()
|
||||
{
|
||||
if (queue.isEmpty)
|
||||
throw (Err("queue is empty"))
|
||||
else
|
||||
{
|
||||
return queue.removeAt(0) // removes left-most item
|
||||
}
|
||||
}
|
||||
|
||||
public Bool isEmpty ()
|
||||
{
|
||||
queue.isEmpty
|
||||
}
|
||||
}
|
||||
22
Task/Queue-Definition/Forth/queue-definition-1.fth
Normal file
22
Task/Queue-Definition/Forth/queue-definition-1.fth
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
1024 constant size
|
||||
create buffer size cells allot
|
||||
here constant end
|
||||
variable head buffer head !
|
||||
variable tail buffer tail !
|
||||
variable used 0 used !
|
||||
|
||||
: empty? used @ 0= ;
|
||||
: full? used @ size = ;
|
||||
|
||||
: next ( ptr -- ptr )
|
||||
cell+ dup end = if drop buffer then ;
|
||||
|
||||
: put ( n -- )
|
||||
full? abort" buffer full"
|
||||
\ begin full? while pause repeat
|
||||
tail @ ! tail @ next tail ! 1 used +! ;
|
||||
|
||||
: get ( -- n )
|
||||
empty? abort" buffer empty"
|
||||
\ begin empty? while pause repeat
|
||||
head @ @ head @ next head ! -1 used +! ;
|
||||
42
Task/Queue-Definition/Forth/queue-definition-2.fth
Normal file
42
Task/Queue-Definition/Forth/queue-definition-2.fth
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
0
|
||||
field: list-next
|
||||
field: list-val
|
||||
constant list-struct
|
||||
|
||||
: insert ( x list-addr -- )
|
||||
list-struct allocate throw >r
|
||||
swap r@ list-val !
|
||||
dup @ r@ list-next !
|
||||
r> swap ! ;
|
||||
|
||||
: remove ( list-addr -- x )
|
||||
>r r@ @ ( list-node )
|
||||
r@ @ dup list-val @ ( list-node x )
|
||||
swap list-next @ r> !
|
||||
swap free throw ;
|
||||
|
||||
0
|
||||
field: queue-last \ points to the last entry (head of the list)
|
||||
field: queue-nextaddr \ points to the pointer to the next-inserted entry
|
||||
constant queue-struct
|
||||
|
||||
: init-queue ( queue -- )
|
||||
>r 0 r@ queue-last !
|
||||
r@ queue-last r> queue-nextaddr ! ;
|
||||
|
||||
: make-queue ( -- queue )
|
||||
queue-struct allocate throw dup init-queue ;
|
||||
|
||||
: empty? ( queue -- f )
|
||||
queue-last @ 0= ;
|
||||
|
||||
: enqueue ( x queue -- )
|
||||
dup >r queue-nextaddr @ insert
|
||||
r@ queue-nextaddr @ @ list-next r> queue-nextaddr ! ;
|
||||
|
||||
: dequeue ( queue -- x )
|
||||
dup empty? abort" dequeue applied to an empty queue"
|
||||
dup queue-last remove ( queue x )
|
||||
over empty? if
|
||||
over init-queue then
|
||||
nip ;
|
||||
69
Task/Queue-Definition/Fortran/queue-definition.f
Normal file
69
Task/Queue-Definition/Fortran/queue-definition.f
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
module FIFO
|
||||
use fifo_nodes
|
||||
! fifo_nodes must define the type fifo_node, with the two field
|
||||
! next and valid, for queue handling, while the field datum depends
|
||||
! on the usage (see [[FIFO (usage)]] for an example)
|
||||
! type fifo_node
|
||||
! integer :: datum
|
||||
! ! the next part is not variable and must be present
|
||||
! type(fifo_node), pointer :: next
|
||||
! logical :: valid
|
||||
! end type fifo_node
|
||||
|
||||
type fifo_head
|
||||
type(fifo_node), pointer :: head, tail
|
||||
end type fifo_head
|
||||
|
||||
contains
|
||||
|
||||
subroutine new_fifo(h)
|
||||
type(fifo_head), intent(out) :: h
|
||||
nullify(h%head)
|
||||
nullify(h%tail)
|
||||
end subroutine new_fifo
|
||||
|
||||
subroutine fifo_enqueue(h, n)
|
||||
type(fifo_head), intent(inout) :: h
|
||||
type(fifo_node), intent(inout), target :: n
|
||||
|
||||
if ( associated(h%tail) ) then
|
||||
h%tail%next => n
|
||||
h%tail => n
|
||||
else
|
||||
h%tail => n
|
||||
h%head => n
|
||||
end if
|
||||
|
||||
nullify(n%next)
|
||||
end subroutine fifo_enqueue
|
||||
|
||||
subroutine fifo_dequeue(h, n)
|
||||
type(fifo_head), intent(inout) :: h
|
||||
type(fifo_node), intent(out), target :: n
|
||||
|
||||
if ( associated(h%head) ) then
|
||||
n = h%head
|
||||
if ( associated(n%next) ) then
|
||||
h%head => n%next
|
||||
else
|
||||
nullify(h%head)
|
||||
nullify(h%tail)
|
||||
end if
|
||||
n%valid = .true.
|
||||
else
|
||||
n%valid = .false.
|
||||
end if
|
||||
nullify(n%next)
|
||||
end subroutine fifo_dequeue
|
||||
|
||||
function fifo_isempty(h) result(r)
|
||||
logical :: r
|
||||
type(fifo_head), intent(in) :: h
|
||||
if ( associated(h%head) ) then
|
||||
r = .false.
|
||||
else
|
||||
r = .true.
|
||||
end if
|
||||
end function fifo_isempty
|
||||
|
||||
end module FIFO
|
||||
21
Task/Queue-Definition/Free-Pascal/queue-definition.pas
Normal file
21
Task/Queue-Definition/Free-Pascal/queue-definition.pas
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
program queue;
|
||||
{$IFDEF FPC}{$MODE DELPHI}{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}{$ENDIF}
|
||||
{$ASSERTIONS ON}
|
||||
uses Generics.Collections;
|
||||
|
||||
var
|
||||
lQueue: TQueue<Integer>;
|
||||
begin
|
||||
lQueue := TQueue<Integer>.Create;
|
||||
try
|
||||
lQueue.EnQueue(1);
|
||||
lQueue.EnQueue(2);
|
||||
lQueue.EnQueue(3);
|
||||
Write(lQueue.DeQueue:2); // 1
|
||||
Write(lQueue.DeQueue:2); // 2
|
||||
Writeln(lQueue.DeQueue:2); // 3
|
||||
Assert(lQueue.Count = 0, 'Queue is not empty'); // should be empty
|
||||
finally
|
||||
lQueue.Free;
|
||||
end;
|
||||
end.
|
||||
89
Task/Queue-Definition/FreeBASIC/queue-definition-1.basic
Normal file
89
Task/Queue-Definition/FreeBASIC/queue-definition-1.basic
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
' queue_rosetta.bi
|
||||
' simple generic Queue type
|
||||
|
||||
#Define Queue(T) Queue_##T
|
||||
|
||||
#Macro Declare_Queue(T)
|
||||
Type Queue(T)
|
||||
Public:
|
||||
Declare Constructor()
|
||||
Declare Destructor()
|
||||
Declare Property capacity As Integer
|
||||
Declare Property count As Integer
|
||||
Declare Property empty As Boolean
|
||||
Declare Property front As T
|
||||
Declare Function pop() As T
|
||||
Declare Sub push(item As T)
|
||||
Private:
|
||||
a(any) As T
|
||||
count_ As Integer = 0
|
||||
Declare Function resize(size As Integer) As Integer
|
||||
End Type
|
||||
|
||||
Constructor Queue(T)()
|
||||
Redim a(0 To 0) '' create a default T instance for various purposes
|
||||
End Constructor
|
||||
|
||||
Destructor Queue(T)()
|
||||
Erase a
|
||||
End Destructor
|
||||
|
||||
Property Queue(T).capacity As Integer
|
||||
Return UBound(a)
|
||||
End Property
|
||||
|
||||
Property Queue(T).count As Integer
|
||||
Return count_
|
||||
End Property
|
||||
|
||||
Property Queue(T).empty As Boolean
|
||||
Return count_ = 0
|
||||
End Property
|
||||
|
||||
Property Queue(T).front As T
|
||||
If count_ > 0 Then
|
||||
Return a(1)
|
||||
End If
|
||||
Print "Error: Attempted to access 'front' element of an empty queue"
|
||||
Return a(0) '' return default element
|
||||
End Property
|
||||
|
||||
Function Queue(T).pop() As T
|
||||
If count_ > 0 Then
|
||||
Dim value As T = a(1)
|
||||
If count_ > 1 Then '' move remaining elements to fill space vacated
|
||||
For i As Integer = 2 To count_
|
||||
a(i - 1) = a(i)
|
||||
Next
|
||||
End If
|
||||
a(count_) = a(0) '' zero last element
|
||||
count_ -= 1
|
||||
Return value
|
||||
End If
|
||||
Print "Error: Attempted to remove 'front' element of an empty queue"
|
||||
Return a(0) '' return default element
|
||||
End Function
|
||||
|
||||
Sub Queue(T).push(item As T)
|
||||
Dim size As Integer = UBound(a)
|
||||
count_ += 1
|
||||
If count_ > size Then
|
||||
size = resize(size)
|
||||
Redim Preserve a(0 to size)
|
||||
End If
|
||||
a(count_) = item
|
||||
End Sub
|
||||
|
||||
Function Queue(T).resize(size As Integer) As Integer
|
||||
If size = 0 Then
|
||||
size = 4
|
||||
ElseIf size <= 32 Then
|
||||
size = 2 * size
|
||||
Else
|
||||
size += 32
|
||||
End If
|
||||
Return size
|
||||
End Function
|
||||
#EndMacro
|
||||
53
Task/Queue-Definition/FreeBASIC/queue-definition-2.basic
Normal file
53
Task/Queue-Definition/FreeBASIC/queue-definition-2.basic
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
#Include "queue_rosetta.bi"
|
||||
|
||||
Type Cat
|
||||
name As String
|
||||
age As Integer
|
||||
Declare Constructor
|
||||
Declare Constructor(name_ As string, age_ As integer)
|
||||
Declare Operator Cast() As String
|
||||
end type
|
||||
|
||||
Constructor Cat '' default constructor
|
||||
End Constructor
|
||||
|
||||
Constructor Cat(name_ As String, age_ As Integer)
|
||||
name = name_
|
||||
age = age_
|
||||
End Constructor
|
||||
|
||||
Operator Cat.Cast() As String
|
||||
Return "[" + name + ", " + Str(age) + "]"
|
||||
End Operator
|
||||
|
||||
Declare_Queue(Cat) '' expand Queue type for Cat instances
|
||||
|
||||
Dim CatQueue As Queue(Cat)
|
||||
|
||||
Var felix = Cat("Felix", 8)
|
||||
Var sheba = Cat("Sheba", 4)
|
||||
Var fluffy = Cat("Fluffy", 2)
|
||||
With CatQueue '' push these Cat instances into the Queue
|
||||
.push(felix)
|
||||
.push(sheba)
|
||||
.push(fluffy)
|
||||
End With
|
||||
Print "Number of Cats in the Queue :" ; CatQueue.count
|
||||
Print "Capacity of Cat Queue :" ; CatQueue.capacity
|
||||
Print "Front Cat : "; CatQueue.front
|
||||
CatQueue.pop()
|
||||
Print "Front Cat now : "; CatQueue.front
|
||||
Print "Number of Cats in the Queue :" ; CatQueue.count
|
||||
CatQueue.pop()
|
||||
Print "Front Cat now : "; CatQueue.front
|
||||
Print "Number of Cats in the Queue :" ; CatQueue.count
|
||||
Print "Is Queue empty now : "; CatQueue.empty
|
||||
catQueue.pop()
|
||||
Print "Number of Cats in the Queue :" ; CatQueue.count
|
||||
Print "Is Queue empty now : "; CatQueue.empty
|
||||
catQueue.pop()
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
34
Task/Queue-Definition/GAP/queue-definition.gap
Normal file
34
Task/Queue-Definition/GAP/queue-definition.gap
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
Enqueue := function(v, x)
|
||||
Add(v[1], x);
|
||||
end;
|
||||
|
||||
Dequeue := function(v)
|
||||
if IsEmpty(v[2]) then
|
||||
if IsEmpty(v[1]) then
|
||||
return fail;
|
||||
else
|
||||
v[2] := Reversed(v[1]);
|
||||
v[1] := [];
|
||||
fi;
|
||||
fi;
|
||||
return Remove(v[2]);
|
||||
end;
|
||||
|
||||
|
||||
# a new queue
|
||||
v := [[], []];
|
||||
|
||||
Enqueue(v, 3);
|
||||
Enqueue(v, 4);
|
||||
Enqueue(v, 5);
|
||||
Dequeue(v);
|
||||
# 3
|
||||
Enqueue(v, 6);
|
||||
Dequeue(v);
|
||||
# 4
|
||||
Dequeue(v);
|
||||
# 5
|
||||
Dequeue(v);
|
||||
# 6
|
||||
Dequeue(v);
|
||||
# fail
|
||||
55
Task/Queue-Definition/Go/queue-definition.go
Normal file
55
Task/Queue-Definition/Go/queue-definition.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package queue
|
||||
|
||||
// int queue
|
||||
// the zero object is a valid queue ready to be used.
|
||||
// items are pushed at tail, popped at head.
|
||||
// tail = -1 means queue is full
|
||||
type Queue struct {
|
||||
b []string
|
||||
head, tail int
|
||||
}
|
||||
|
||||
func (q *Queue) Push(x string) {
|
||||
switch {
|
||||
// buffer full. reallocate.
|
||||
case q.tail < 0:
|
||||
next := len(q.b)
|
||||
bigger := make([]string, 2*next)
|
||||
copy(bigger[copy(bigger, q.b[q.head:]):], q.b[:q.head])
|
||||
bigger[next] = x
|
||||
q.b, q.head, q.tail = bigger, 0, next+1
|
||||
// zero object. make initial allocation.
|
||||
case len(q.b) == 0:
|
||||
q.b, q.head, q.tail = make([]string, 4), 0 ,1
|
||||
q.b[0] = x
|
||||
// normal case
|
||||
default:
|
||||
q.b[q.tail] = x
|
||||
q.tail++
|
||||
if q.tail == len(q.b) {
|
||||
q.tail = 0
|
||||
}
|
||||
if q.tail == q.head {
|
||||
q.tail = -1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Queue) Pop() (string, bool) {
|
||||
if q.head == q.tail {
|
||||
return "", false
|
||||
}
|
||||
r := q.b[q.head]
|
||||
if q.tail == -1 {
|
||||
q.tail = q.head
|
||||
}
|
||||
q.head++
|
||||
if q.head == len(q.b) {
|
||||
q.head = 0
|
||||
}
|
||||
return r, true
|
||||
}
|
||||
|
||||
func (q *Queue) Empty() bool {
|
||||
return q.head == q.tail
|
||||
}
|
||||
22
Task/Queue-Definition/Groovy/queue-definition-1.groovy
Normal file
22
Task/Queue-Definition/Groovy/queue-definition-1.groovy
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
class Queue {
|
||||
private List buffer
|
||||
|
||||
public Queue(List buffer = new LinkedList()) {
|
||||
assert buffer != null
|
||||
assert buffer.empty
|
||||
this.buffer = buffer
|
||||
}
|
||||
|
||||
def push (def item) { buffer << item }
|
||||
final enqueue = this.&push
|
||||
|
||||
def pop() {
|
||||
if (this.empty) throw new NoSuchElementException('Empty Queue')
|
||||
buffer.remove(0)
|
||||
}
|
||||
final dequeue = this.&pop
|
||||
|
||||
def getEmpty() { buffer.empty }
|
||||
|
||||
String toString() { "Queue:${buffer}" }
|
||||
}
|
||||
27
Task/Queue-Definition/Groovy/queue-definition-2.groovy
Normal file
27
Task/Queue-Definition/Groovy/queue-definition-2.groovy
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
def q = new Queue()
|
||||
assert q.empty
|
||||
|
||||
['Crosby', 'Stills'].each { q.push(it) }
|
||||
assert !q.empty
|
||||
['Nash', 'Young'].each { q.enqueue(it) }
|
||||
println q
|
||||
assert !q.empty
|
||||
assert q.pop() == 'Crosby'
|
||||
println q
|
||||
assert !q.empty
|
||||
assert q.dequeue() == 'Stills'
|
||||
println q
|
||||
assert !q.empty
|
||||
assert q.pop() == 'Nash'
|
||||
println q
|
||||
assert !q.empty
|
||||
q.push('Crazy Horse')
|
||||
println q
|
||||
assert q.dequeue() == 'Young'
|
||||
println q
|
||||
assert !q.empty
|
||||
assert q.pop() == 'Crazy Horse'
|
||||
println q
|
||||
assert q.empty
|
||||
try { q.pop() } catch (NoSuchElementException e) { println e }
|
||||
try { q.dequeue() } catch (NoSuchElementException e) { println e }
|
||||
16
Task/Queue-Definition/Haskell/queue-definition.hs
Normal file
16
Task/Queue-Definition/Haskell/queue-definition.hs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
data Fifo a = F [a] [a]
|
||||
|
||||
emptyFifo :: Fifo a
|
||||
emptyFifo = F [] []
|
||||
|
||||
push :: Fifo a -> a -> Fifo a
|
||||
push (F input output) item = F (item:input) output
|
||||
|
||||
pop :: Fifo a -> (Maybe a, Fifo a)
|
||||
pop (F input (item:output)) = (Just item, F input output)
|
||||
pop (F [] [] ) = (Nothing, F [] [])
|
||||
pop (F input [] ) = pop (F [] (reverse input))
|
||||
|
||||
isEmpty :: Fifo a -> Bool
|
||||
isEmpty (F [] []) = True
|
||||
isEmpty _ = False
|
||||
34
Task/Queue-Definition/Icon/queue-definition.icon
Normal file
34
Task/Queue-Definition/Icon/queue-definition.icon
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
# Use a record to hold a Queue, using a list as the concrete implementation
|
||||
record Queue(items)
|
||||
|
||||
procedure make_queue ()
|
||||
return Queue ([])
|
||||
end
|
||||
|
||||
procedure queue_push (queue, item)
|
||||
put (queue.items, item)
|
||||
end
|
||||
|
||||
# if the queue is empty, this will 'fail' and return nothing
|
||||
procedure queue_pop (queue)
|
||||
return pop (queue.items)
|
||||
end
|
||||
|
||||
procedure queue_empty (queue)
|
||||
return *queue.items = 0
|
||||
end
|
||||
|
||||
# procedure to test class
|
||||
procedure main ()
|
||||
queue := make_queue()
|
||||
|
||||
# add the numbers 1 to 5
|
||||
every (item := 1 to 5) do
|
||||
queue_push (queue, item)
|
||||
|
||||
# pop them in the added order, and show a message when queue is empty
|
||||
every (1 to 6) do {
|
||||
write ("Popped value: " || queue_pop (queue))
|
||||
if (queue_empty (queue)) then write ("empty queue")
|
||||
}
|
||||
end
|
||||
16
Task/Queue-Definition/J/queue-definition-1.j
Normal file
16
Task/Queue-Definition/J/queue-definition-1.j
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
queue_fifo_=: ''
|
||||
|
||||
pop_fifo_=: verb define
|
||||
r=. {. ::] queue
|
||||
queue=: }.queue
|
||||
r
|
||||
)
|
||||
|
||||
push_fifo_=: verb define
|
||||
queue=: queue,y
|
||||
y
|
||||
)
|
||||
|
||||
isEmpty_fifo_=: verb define
|
||||
0=#queue
|
||||
)
|
||||
10
Task/Queue-Definition/J/queue-definition-2.j
Normal file
10
Task/Queue-Definition/J/queue-definition-2.j
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
pop =: ( {.^:notnull ; }. )@: > @: ] /
|
||||
push =: ( '' ; ,~ )& > /
|
||||
tell_atom =: >& {.
|
||||
tell_queue =: >& {:
|
||||
is_empty =: '' -: 1 tell_queue
|
||||
|
||||
make_empty =: a: , a: [ ]
|
||||
onto =: [ ; }.@]
|
||||
|
||||
notnull =: 0 ~: #
|
||||
40
Task/Queue-Definition/Java/queue-definition.java
Normal file
40
Task/Queue-Definition/Java/queue-definition.java
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
public class Queue<E>{
|
||||
Node<E> head = null, tail = null;
|
||||
|
||||
static class Node<E>{
|
||||
E value;
|
||||
Node<E> next;
|
||||
|
||||
Node(E value, Node<E> next){
|
||||
this.value= value;
|
||||
this.next= next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Queue(){
|
||||
}
|
||||
|
||||
public void enqueue(E value){ //standard queue name for "push"
|
||||
Node<E> newNode= new Node<E>(value, null);
|
||||
if(empty()){
|
||||
head= newNode;
|
||||
}else{
|
||||
tail.next = newNode;
|
||||
}
|
||||
tail= newNode;
|
||||
}
|
||||
|
||||
public E dequeue() throws java.util.NoSuchElementException{//standard queue name for "pop"
|
||||
if(empty()){
|
||||
throw new java.util.NoSuchElementException("No more elements.");
|
||||
}
|
||||
E retVal= head.value;
|
||||
head= head.next;
|
||||
return retVal;
|
||||
}
|
||||
|
||||
public boolean empty(){
|
||||
return head == null;
|
||||
}
|
||||
}
|
||||
5
Task/Queue-Definition/JavaScript/queue-definition-1.js
Normal file
5
Task/Queue-Definition/JavaScript/queue-definition-1.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
var fifo = [];
|
||||
fifo.push(42); // Enqueue.
|
||||
fifo.push(43);
|
||||
var x = fifo.shift(); // Dequeue.
|
||||
alert(x); // 42
|
||||
10
Task/Queue-Definition/JavaScript/queue-definition-2.js
Normal file
10
Task/Queue-Definition/JavaScript/queue-definition-2.js
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
function FIFO() {
|
||||
this.data = new Array();
|
||||
|
||||
this.push = function(element) {this.data.push(element)}
|
||||
this.pop = function() {return this.data.shift()}
|
||||
this.empty = function() {return this.data.length == 0}
|
||||
|
||||
this.enqueue = this.push;
|
||||
this.dequeue = this.pop;
|
||||
}
|
||||
35
Task/Queue-Definition/Jq/queue-definition.jq
Normal file
35
Task/Queue-Definition/Jq/queue-definition.jq
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
# Input: an object
|
||||
# Output: the updated object with .emit filled in from `update|emit`.
|
||||
# `emit` may produce a stream of values, which need not be strings.
|
||||
def observe(update; emit):
|
||||
def s(stream): reduce stream as $_ (null;
|
||||
if $_ == null then .
|
||||
elif . == null then "\($_)"
|
||||
else . + "\n\($_)"
|
||||
end);
|
||||
.emit as $x
|
||||
| update
|
||||
| .emit = s($x // null, emit);
|
||||
|
||||
|
||||
def fifo: {queue: []};
|
||||
|
||||
# Is the input an object that represents the empty queue?
|
||||
def isempty:
|
||||
type == "object"
|
||||
and (.queue | length == 0); # so .queue == null and .queue == [] are equivalent
|
||||
|
||||
def push(e): .queue += [e];
|
||||
|
||||
def pop: if isempty then empty else .item = .queue[0] | .queue |= .[1:] end;
|
||||
|
||||
def pop_or_error: if isempty then error("pop_or_error") else pop end;
|
||||
|
||||
# Examples
|
||||
# fifo | pop // "nothing" # produces the string "nothing"
|
||||
|
||||
fifo
|
||||
| observe(push(42); "length after pushing: \(.queue | length)" )
|
||||
| observe(push(43); "length after pushing: \(.queue | length)" )
|
||||
| pop # dequeue
|
||||
| .emit, .item
|
||||
24
Task/Queue-Definition/Julia/queue-definition.julia
Normal file
24
Task/Queue-Definition/Julia/queue-definition.julia
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
struct Queue{T}
|
||||
a::Array{T,1}
|
||||
end
|
||||
|
||||
Queue() = Queue(Any[])
|
||||
Queue(a::DataType) = Queue(a[])
|
||||
Queue(a) = Queue(typeof(a)[])
|
||||
|
||||
Base.isempty(q::Queue) = isempty(q.a)
|
||||
|
||||
function Base.pop!(q::Queue{T}) where {T}
|
||||
!isempty(q) || error("queue must be non-empty")
|
||||
pop!(q.a)
|
||||
end
|
||||
|
||||
function Base.push!(q::Queue{T}, x::T) where {T}
|
||||
pushfirst!(q.a, x)
|
||||
return q
|
||||
end
|
||||
|
||||
function Base.push!(q::Queue{Any}, x::T) where {T}
|
||||
pushfirst!(q.a, x)
|
||||
return q
|
||||
end
|
||||
26
Task/Queue-Definition/Klingphix/queue-definition.klingphix
Normal file
26
Task/Queue-Definition/Klingphix/queue-definition.klingphix
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
{ include ..\Utilitys.tlhy }
|
||||
"..\Utilitys.tlhy" load
|
||||
|
||||
|
||||
:push! { l i -- l&i }
|
||||
0 put
|
||||
;
|
||||
|
||||
:empty? { l -- flag }
|
||||
len not { len 0 equal }
|
||||
;
|
||||
|
||||
:pop! { l -- l-1 }
|
||||
empty? (
|
||||
["Empty"]
|
||||
[pop swap]
|
||||
) if
|
||||
;
|
||||
|
||||
|
||||
( ) { empty queue }
|
||||
|
||||
1 push! 2 push! 3 push!
|
||||
pop! ? pop! ? pop! ? pop! ?
|
||||
|
||||
"End " input
|
||||
47
Task/Queue-Definition/Kotlin/queue-definition.kotlin
Normal file
47
Task/Queue-Definition/Kotlin/queue-definition.kotlin
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// version 1.1.2
|
||||
|
||||
import java.util.LinkedList
|
||||
|
||||
class Queue<E> {
|
||||
private val data = LinkedList<E>()
|
||||
|
||||
val size get() = data.size
|
||||
|
||||
val empty get() = size == 0
|
||||
|
||||
fun push(element: E) = data.add(element)
|
||||
|
||||
fun pop(): E {
|
||||
if (empty) throw RuntimeException("Can't pop elements from an empty queue")
|
||||
return data.removeFirst()
|
||||
}
|
||||
|
||||
val top: E
|
||||
get() {
|
||||
if (empty) throw RuntimeException("Empty queue can't have a top element")
|
||||
return data.first()
|
||||
}
|
||||
|
||||
fun clear() = data.clear()
|
||||
|
||||
override fun toString() = data.toString()
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val q = Queue<Int>()
|
||||
(1..5).forEach { q.push(it) }
|
||||
println(q)
|
||||
println("Size of queue = ${q.size}")
|
||||
print("Popping: ")
|
||||
(1..3).forEach { print("${q.pop()} ") }
|
||||
println("\nRemaining in queue: $q")
|
||||
println("Top element is now ${q.top}")
|
||||
q.clear()
|
||||
println("After clearing, queue is ${if(q.empty) "empty" else "not empty"}")
|
||||
try {
|
||||
q.pop()
|
||||
}
|
||||
catch (e: Exception) {
|
||||
println(e.message)
|
||||
}
|
||||
}
|
||||
30
Task/Queue-Definition/Lambdatalk/queue-definition.lambdatalk
Normal file
30
Task/Queue-Definition/Lambdatalk/queue-definition.lambdatalk
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
{def queue.add
|
||||
{lambda {:v :q}
|
||||
{let { {_ {A.addlast! :v :q}}}
|
||||
} ok}}
|
||||
-> queue.add
|
||||
|
||||
{def queue.get
|
||||
{lambda {:q}
|
||||
{let { {:v {A.first :q}}
|
||||
{_ {A.subfirst! :q}}
|
||||
} :v}}}
|
||||
-> queue.get
|
||||
|
||||
{def queue.empty?
|
||||
{lambda {:q}
|
||||
{A.empty? :q}}}
|
||||
-> queue.empty?
|
||||
|
||||
{def Q {A.new}} -> Q []
|
||||
{queue.add 1 {Q}} -> ok [1]
|
||||
{queue.add 2 {Q}} -> ok [1,2]
|
||||
{queue.add 3 {Q}} -> ok [1,2,3]
|
||||
{queue.get {Q}} -> 1 [2,3]
|
||||
{queue.add 4 {Q}} -> ok [2,3,4]
|
||||
{queue.empty? {Q}} -> false
|
||||
{queue.get {Q}} -> 2 [3,4]
|
||||
{queue.get {Q}} -> 3 [4]
|
||||
{queue.get {Q}} -> 4 []
|
||||
{queue.get {Q}} -> undefined
|
||||
{queue.empty? {Q}} -> true
|
||||
21
Task/Queue-Definition/Lasso/queue-definition-1.lasso
Normal file
21
Task/Queue-Definition/Lasso/queue-definition-1.lasso
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
define myqueue => type {
|
||||
data store = list
|
||||
|
||||
public onCreate(...) => {
|
||||
if(void != #rest) => {
|
||||
with item in #rest do .`store`->insert(#item)
|
||||
}
|
||||
}
|
||||
|
||||
public push(value) => .`store`->insertLast(#value)
|
||||
|
||||
public pop => {
|
||||
handle => {
|
||||
.`store`->removefirst
|
||||
}
|
||||
|
||||
return .`store`->first
|
||||
}
|
||||
|
||||
public isEmpty => (.`store`->size == 0)
|
||||
}
|
||||
14
Task/Queue-Definition/Lasso/queue-definition-2.lasso
Normal file
14
Task/Queue-Definition/Lasso/queue-definition-2.lasso
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
local(q) = myqueue('a')
|
||||
#q->isEmpty
|
||||
// => false
|
||||
|
||||
#q->push('b')
|
||||
#q->pop
|
||||
// => a
|
||||
#q->pop
|
||||
// => b
|
||||
|
||||
#q->isEmpty
|
||||
// => true
|
||||
#q->pop
|
||||
// => void
|
||||
25
Task/Queue-Definition/Lua/queue-definition.lua
Normal file
25
Task/Queue-Definition/Lua/queue-definition.lua
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
Queue = {}
|
||||
|
||||
function Queue.new()
|
||||
return { first = 0, last = -1 }
|
||||
end
|
||||
|
||||
function Queue.push( queue, value )
|
||||
queue.last = queue.last + 1
|
||||
queue[queue.last] = value
|
||||
end
|
||||
|
||||
function Queue.pop( queue )
|
||||
if queue.first > queue.last then
|
||||
return nil
|
||||
end
|
||||
|
||||
local val = queue[queue.first]
|
||||
queue[queue.first] = nil
|
||||
queue.first = queue.first + 1
|
||||
return val
|
||||
end
|
||||
|
||||
function Queue.empty( queue )
|
||||
return queue.first > queue.last
|
||||
end
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
Module Checkit {
|
||||
a=Stack
|
||||
Stack a {
|
||||
Data 100,200, 300
|
||||
}
|
||||
Stack a {
|
||||
While not empty {
|
||||
Read N
|
||||
Print N
|
||||
}
|
||||
}
|
||||
}
|
||||
Checkit
|
||||
10
Task/Queue-Definition/MATLAB/queue-definition-1.m
Normal file
10
Task/Queue-Definition/MATLAB/queue-definition-1.m
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
myfifo = {};
|
||||
|
||||
% push
|
||||
myfifo{end+1} = x;
|
||||
|
||||
% pop
|
||||
x = myfifo{1}; myfifo{1} = [];
|
||||
|
||||
% empty
|
||||
isempty(myfifo)
|
||||
82
Task/Queue-Definition/MATLAB/queue-definition-2.m
Normal file
82
Task/Queue-Definition/MATLAB/queue-definition-2.m
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
%This class impliments a standard FIFO queue.
|
||||
classdef FIFOQueue
|
||||
|
||||
properties
|
||||
queue
|
||||
end
|
||||
|
||||
methods
|
||||
|
||||
%Class constructor
|
||||
function theQueue = FIFOQueue(varargin)
|
||||
|
||||
if isempty(varargin) %No input arguments
|
||||
|
||||
%Initialize the queue state as empty
|
||||
theQueue.queue = {};
|
||||
elseif (numel(varargin) > 1) %More than 1 input arg
|
||||
|
||||
%Make the queue the list of input args
|
||||
theQueue.queue = varargin;
|
||||
elseif iscell(varargin{:}) %If the only input is a cell array
|
||||
|
||||
%Make the contents of the cell array the elements in the queue
|
||||
theQueue.queue = varargin{:};
|
||||
else %There is one input argument that is not a cell
|
||||
|
||||
%Make that one arg the only element in the queue
|
||||
theQueue.queue = varargin;
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
%push() - pushes a new element to the end of the queue
|
||||
function push(theQueue,varargin)
|
||||
|
||||
if isempty(varargin)
|
||||
theQueue.queue(end+1) = {[]};
|
||||
elseif (numel(varargin) > 1) %More than 1 input arg
|
||||
|
||||
%Make the queue the list of input args
|
||||
theQueue.queue( end+1:end+numel(varargin) ) = varargin;
|
||||
elseif iscell(varargin{:}) %If the only input is a cell array
|
||||
|
||||
%Make the contents of the cell array the elements in the queue
|
||||
theQueue.queue( end+1:end+numel(varargin{:}) ) = varargin{:};
|
||||
else %There is one input argument that is not a cell
|
||||
|
||||
%Make that one arg the only element in the queue
|
||||
theQueue.queue{end+1} = varargin{:};
|
||||
end
|
||||
|
||||
%Makes changes to the queue permanent
|
||||
assignin('caller',inputname(1),theQueue);
|
||||
|
||||
end
|
||||
|
||||
%pop() - pops the first element off the queue
|
||||
function element = pop(theQueue)
|
||||
|
||||
if empty(theQueue)
|
||||
error 'The queue is empty'
|
||||
else
|
||||
%Returns the first element in the queue
|
||||
element = theQueue.queue{1};
|
||||
|
||||
%Removes the first element from the queue
|
||||
theQueue.queue(1) = [];
|
||||
|
||||
%Makes changes to the queue permanent
|
||||
assignin('caller',inputname(1),theQueue);
|
||||
end
|
||||
end
|
||||
|
||||
%empty() - Returns true if the queue is empty
|
||||
function trueFalse = empty(theQueue)
|
||||
|
||||
trueFalse = isempty(theQueue.queue);
|
||||
|
||||
end
|
||||
|
||||
end %methods
|
||||
end
|
||||
22
Task/Queue-Definition/MATLAB/queue-definition-3.m
Normal file
22
Task/Queue-Definition/MATLAB/queue-definition-3.m
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
>> myQueue = FIFOQueue({'hello'})
|
||||
|
||||
myQueue =
|
||||
|
||||
FIFOQueue
|
||||
|
||||
>> push(myQueue,'jello')
|
||||
>> pop(myQueue)
|
||||
|
||||
ans =
|
||||
|
||||
hello
|
||||
|
||||
>> pop(myQueue)
|
||||
|
||||
ans =
|
||||
|
||||
jello
|
||||
|
||||
>> pop(myQueue)
|
||||
??? Error using ==> FIFOQueue.FIFOQueue>FIFOQueue.pop at 61
|
||||
The queue is empty
|
||||
3
Task/Queue-Definition/Mathematica/queue-definition.math
Normal file
3
Task/Queue-Definition/Mathematica/queue-definition.math
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
EmptyQ[a_] := Length[a] == 0
|
||||
SetAttributes[Push, HoldAll]; Push[a_, elem_] := AppendTo[a, elem]
|
||||
SetAttributes[Pop, HoldAllComplete]; Pop[a_] := If[EmptyQ[a], False, b = First[a]; Set[a, Most[a]]; b]
|
||||
18
Task/Queue-Definition/Maxima/queue-definition.maxima
Normal file
18
Task/Queue-Definition/Maxima/queue-definition.maxima
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
defstruct(queue(in=[], out=[]))$
|
||||
|
||||
enqueue(x, q) := (q@in: cons(x, q@in), done)$
|
||||
|
||||
dequeue(q) := (if not emptyp(q@out) then first([first(q@out), q@out: rest(q@out)])
|
||||
elseif emptyp(q@in) then 'fail
|
||||
else (q@out: reverse(q@in), q@in: [], first([first(q@out), q@out: rest(q@out)])))$
|
||||
|
||||
q:new(queue); /* queue([], []) */
|
||||
enqueue(1, q)$
|
||||
enqueue(2, q)$
|
||||
enqueue(3, q)$
|
||||
dequeue(q); /* 1 */
|
||||
enqueue(4, q)$
|
||||
dequeue(q); /* 2 */
|
||||
dequeue(q); /* 3 */
|
||||
dequeue(q); /* 4 */
|
||||
dequeue(q); /* fail */
|
||||
50
Task/Queue-Definition/Nanoquery/queue-definition.nanoquery
Normal file
50
Task/Queue-Definition/Nanoquery/queue-definition.nanoquery
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
class FIFO
|
||||
declare contents
|
||||
|
||||
// define constructors for FIFO objects
|
||||
def FIFO()
|
||||
this.contents = {}
|
||||
end
|
||||
def FIFO(contents)
|
||||
this.contents = contents
|
||||
end
|
||||
|
||||
// define methods for this class
|
||||
def push(value)
|
||||
contents.append(value)
|
||||
end
|
||||
def pop()
|
||||
if !this.empty()
|
||||
value = contents[len(contents) - 1]
|
||||
contents.remove(len(contents) - 1)
|
||||
return value
|
||||
else
|
||||
// we could throw our own exception here but
|
||||
// we'll return null instead
|
||||
return null
|
||||
end
|
||||
end
|
||||
def length()
|
||||
return len(contents)
|
||||
end
|
||||
def extend(itemlist)
|
||||
contents += itemlist
|
||||
end
|
||||
def empty()
|
||||
return len(contents) = 0
|
||||
end
|
||||
|
||||
// define operators for this class
|
||||
def toString()
|
||||
return str(contents)
|
||||
end
|
||||
def operator+(other)
|
||||
return this.contents + other.contents
|
||||
end
|
||||
def operator*(n)
|
||||
return this.contents * n
|
||||
end
|
||||
def operator=(other)
|
||||
return this.contents = other.contents
|
||||
end
|
||||
end
|
||||
35
Task/Queue-Definition/NetRexx/queue-definition.netrexx
Normal file
35
Task/Queue-Definition/NetRexx/queue-definition.netrexx
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols nobinary
|
||||
|
||||
mqueue = ArrayDeque()
|
||||
|
||||
viewQueue(mqueue)
|
||||
|
||||
a = "Fred"
|
||||
mqueue.push('') /* Puts an empty line onto the queue */
|
||||
mqueue.push(a 2) /* Puts "Fred 2" onto the queue */
|
||||
viewQueue(mqueue)
|
||||
|
||||
a = "Toft"
|
||||
mqueue.add(a 2) /* Enqueues "Toft 2" */
|
||||
mqueue.add('') /* Enqueues an empty line behind the last */
|
||||
viewQueue(mqueue)
|
||||
|
||||
loop q_ = 1 while mqueue.size > 0
|
||||
parse mqueue.pop.toString line
|
||||
say q_.right(3)':' line
|
||||
end q_
|
||||
viewQueue(mqueue)
|
||||
|
||||
return
|
||||
|
||||
method viewQueue(mqueue = Deque) private static
|
||||
|
||||
If mqueue.size = 0 then do
|
||||
Say 'Queue is empty'
|
||||
end
|
||||
else do
|
||||
Say 'There are' mqueue.size 'elements in the queue'
|
||||
end
|
||||
|
||||
return
|
||||
49
Task/Queue-Definition/Nim/queue-definition.nim
Normal file
49
Task/Queue-Definition/Nim/queue-definition.nim
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
type
|
||||
|
||||
Node[T] = ref object
|
||||
value: T
|
||||
next: Node[T]
|
||||
|
||||
Queue*[T] = object
|
||||
head, tail: Node[T]
|
||||
length: Natural
|
||||
|
||||
func initQueue*[T](): Queue[T] = Queue[T]()
|
||||
|
||||
func len*(queue: Queue): Natural =
|
||||
queue.length
|
||||
|
||||
func isEmpty*(queue: Queue): bool {.inline.} =
|
||||
queue.len == 0
|
||||
|
||||
func push*[T](queue: var Queue[T]; value: T) =
|
||||
let node = Node[T](value: value, next: nil)
|
||||
if queue.isEmpty: queue.head = node
|
||||
else: queue.tail.next = node
|
||||
queue.tail = node
|
||||
inc queue.length
|
||||
|
||||
func pop*[T](queue: var Queue[T]): T =
|
||||
if queue.isEmpty:
|
||||
raise newException(ValueError, "popping from empty queue.")
|
||||
result = queue.head.value
|
||||
queue.head = queue.head.next
|
||||
dec queue.length
|
||||
if queue.isEmpty: queue.tail = nil
|
||||
|
||||
|
||||
when isMainModule:
|
||||
|
||||
var fifo = initQueue[int]()
|
||||
|
||||
fifo.push(26)
|
||||
fifo.push(99)
|
||||
fifo.push(2)
|
||||
echo "Fifo size: ", fifo.len()
|
||||
try:
|
||||
echo "Popping: ", fifo.pop()
|
||||
echo "Popping: ", fifo.pop()
|
||||
echo "Popping: ", fifo.pop()
|
||||
echo "Popping: ", fifo.pop()
|
||||
except ValueError:
|
||||
echo "Exception catched: ", getCurrentExceptionMsg()
|
||||
20
Task/Queue-Definition/OCaml/queue-definition-1.ocaml
Normal file
20
Task/Queue-Definition/OCaml/queue-definition-1.ocaml
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
module FIFO : sig
|
||||
type 'a fifo
|
||||
val empty: 'a fifo
|
||||
val push: fifo:'a fifo -> item:'a -> 'a fifo
|
||||
val pop: fifo:'a fifo -> 'a * 'a fifo
|
||||
val is_empty: fifo:'a fifo -> bool
|
||||
end = struct
|
||||
type 'a fifo = 'a list * 'a list
|
||||
let empty = [], []
|
||||
let push ~fifo:(input,output) ~item = (item::input,output)
|
||||
let is_empty ~fifo =
|
||||
match fifo with
|
||||
| [], [] -> true
|
||||
| _ -> false
|
||||
let rec pop ~fifo =
|
||||
match fifo with
|
||||
| input, item :: output -> item, (input,output)
|
||||
| [], [] -> failwith "empty fifo"
|
||||
| input, [] -> pop ([], List.rev input)
|
||||
end
|
||||
28
Task/Queue-Definition/OCaml/queue-definition-2.ocaml
Normal file
28
Task/Queue-Definition/OCaml/queue-definition-2.ocaml
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
# open FIFO;;
|
||||
# let q = empty ;;
|
||||
val q : '_a FIFO.fifo = <abstr>
|
||||
# is_empty q ;;
|
||||
- : bool = true
|
||||
# let q = push q 1 ;;
|
||||
val q : int FIFO.fifo = <abstr>
|
||||
# is_empty q ;;
|
||||
- : bool = false
|
||||
|
||||
# let q =
|
||||
List.fold_left push q [2;3;4] ;;
|
||||
val q : int FIFO.fifo = <abstr>
|
||||
|
||||
# let v, q = pop q ;;
|
||||
val v : int = 1
|
||||
val q : int FIFO.fifo = <abstr>
|
||||
# let v, q = pop q ;;
|
||||
val v : int = 2
|
||||
val q : int FIFO.fifo = <abstr>
|
||||
# let v, q = pop q ;;
|
||||
val v : int = 3
|
||||
val q : int FIFO.fifo = <abstr>
|
||||
# let v, q = pop q ;;
|
||||
val v : int = 4
|
||||
val q : int FIFO.fifo = <abstr>
|
||||
# let v, q = pop q ;;
|
||||
Exception: Failure "empty fifo".
|
||||
6
Task/Queue-Definition/Oforth/queue-definition.fth
Normal file
6
Task/Queue-Definition/Oforth/queue-definition.fth
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
Object Class new: Queue(mutable l)
|
||||
|
||||
Queue method: initialize ListBuffer new := l ;
|
||||
Queue method: empty @l isEmpty ;
|
||||
Queue method: push @l add ;
|
||||
Queue method: pop @l removeFirst ;
|
||||
98
Task/Queue-Definition/OxygenBasic/queue-definition.basic
Normal file
98
Task/Queue-Definition/OxygenBasic/queue-definition.basic
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
'==========
|
||||
Class Queue
|
||||
'==========
|
||||
|
||||
'FIRST IN FIRST OUT
|
||||
|
||||
bstring buf 'buffer to hold queue content
|
||||
int bg 'buffer base offset
|
||||
int i 'indexer
|
||||
int le 'length of buffer
|
||||
|
||||
method constructor()
|
||||
====================
|
||||
buf=""
|
||||
le=0
|
||||
bg=0
|
||||
i=0
|
||||
end method
|
||||
|
||||
method destructor()
|
||||
===================
|
||||
del buf
|
||||
le=0
|
||||
bg=0
|
||||
i=0
|
||||
end method
|
||||
|
||||
method Encodelength(int ls)
|
||||
===========================
|
||||
int p at (i+strptr buf)
|
||||
p=ls
|
||||
i+=sizeof int
|
||||
end method
|
||||
|
||||
method push(string s)
|
||||
=====================
|
||||
int ls=len s
|
||||
if i+ls+8>le then
|
||||
buf+=nuls 8000+ls*2 'extend buf
|
||||
le=len buf
|
||||
end if
|
||||
EncodeLength ls 'length of input s
|
||||
mid buf,i+1,s 'append input s
|
||||
i+=ls
|
||||
end method
|
||||
|
||||
method popLength() as int
|
||||
=========================
|
||||
if bg>=i then return -1 'buffer empty
|
||||
int p at (bg+strptr buf)
|
||||
bg+=sizeof int
|
||||
return p
|
||||
end method
|
||||
|
||||
method pop(string *s) as int
|
||||
============================
|
||||
int ls=popLength
|
||||
if ls<0 then s="" : return ls 'empty buffer
|
||||
s=mid buf,bg+1,ls
|
||||
bg+=ls
|
||||
'cleanup buffer
|
||||
if bg>1e6 then
|
||||
buf=mid buf,bg+1
|
||||
le=len buf
|
||||
i-=bg 'shrink buf
|
||||
bg=0
|
||||
end if
|
||||
end method
|
||||
|
||||
method clear()
|
||||
==============
|
||||
buf=""
|
||||
le=0
|
||||
bg=0
|
||||
i=0
|
||||
end method
|
||||
|
||||
end class 'Queue
|
||||
|
||||
|
||||
'====
|
||||
'DEMO
|
||||
'====
|
||||
|
||||
new Queue fifo
|
||||
string s
|
||||
'
|
||||
fifo.push "HumptyDumpty"
|
||||
fifo.push "Sat on a wall"
|
||||
'
|
||||
int er
|
||||
do
|
||||
er=fifo.pop s
|
||||
if er then print "(buffer empty)" : exit do
|
||||
print s
|
||||
loop
|
||||
|
||||
del fifo
|
||||
34
Task/Queue-Definition/Oz/queue-definition.oz
Normal file
34
Task/Queue-Definition/Oz/queue-definition.oz
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
declare
|
||||
fun {NewQueue}
|
||||
Stream
|
||||
WritePort = {Port.new Stream}
|
||||
ReadPos = {NewCell Stream}
|
||||
in
|
||||
WritePort#ReadPos
|
||||
end
|
||||
|
||||
proc {Push WritePort#_ Value}
|
||||
{Port.send WritePort Value}
|
||||
end
|
||||
|
||||
fun {Empty _#ReadPos}
|
||||
%% the queue is empty if the value at the current
|
||||
%% read position is not determined
|
||||
{Not {IsDet @ReadPos}}
|
||||
end
|
||||
|
||||
fun {Pop _#ReadPos}
|
||||
%% blocks if empty
|
||||
case @ReadPos of X|Xr then
|
||||
ReadPos := Xr
|
||||
X
|
||||
end
|
||||
end
|
||||
|
||||
Q = {NewQueue}
|
||||
in
|
||||
{Show {Empty Q}}
|
||||
{Push Q 42}
|
||||
{Show {Empty Q}}
|
||||
{Show {Pop Q}}
|
||||
{Show {Empty Q}}
|
||||
21
Task/Queue-Definition/PHP/queue-definition-1.php
Normal file
21
Task/Queue-Definition/PHP/queue-definition-1.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
class Fifo {
|
||||
private $data = array();
|
||||
public function push($element){
|
||||
array_push($this->data, $element);
|
||||
}
|
||||
public function pop(){
|
||||
if ($this->isEmpty()){
|
||||
throw new Exception('Attempt to pop from an empty queue');
|
||||
}
|
||||
return array_shift($this->data);
|
||||
}
|
||||
|
||||
//Alias functions
|
||||
public function enqueue($element) { $this->push($element); }
|
||||
public function dequeue() { return $this->pop(); }
|
||||
|
||||
//Note: PHP prevents a method name of 'empty'
|
||||
public function isEmpty(){
|
||||
return empty($this->data);
|
||||
}
|
||||
}
|
||||
9
Task/Queue-Definition/PHP/queue-definition-2.php
Normal file
9
Task/Queue-Definition/PHP/queue-definition-2.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
$foo = new Fifo();
|
||||
$foo->push('One');
|
||||
$foo->enqueue('Two');
|
||||
$foo->push('Three');
|
||||
|
||||
echo $foo->pop(); //Prints 'One'
|
||||
echo $foo->dequeue(); //Prints 'Two'
|
||||
echo $foo->pop(); //Prints 'Three'
|
||||
echo $foo->pop(); //Throws an exception
|
||||
28
Task/Queue-Definition/PL-I/queue-definition.pli
Normal file
28
Task/Queue-Definition/PL-I/queue-definition.pli
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* To push a node onto the end of the queue. */
|
||||
push: procedure (tail);
|
||||
declare tail handle (node), t handle (node);
|
||||
t = new(:node:);
|
||||
get (t => value);
|
||||
if tail ^= bind(:null, node:) then
|
||||
tail => link = t;
|
||||
/* If the queue was non-empty, points the tail of the queue */
|
||||
/* to the new node. */
|
||||
tail = t; /* Point "tail" at the end of the queue. */
|
||||
tail => link = bind(:node, null:);
|
||||
end push;
|
||||
|
||||
/* To pop a node from the head of the queue. */
|
||||
pop: procedure (head, val);
|
||||
declare head handle (node), val fixed binary;
|
||||
if head = bind(:node, null:) then signal error;
|
||||
val = head => value;
|
||||
head = head => pointer; /* pops the top node. */
|
||||
if head = bind(:node, null:) then tail = head;
|
||||
/* (If the queue is now empty, make tail null also.) */
|
||||
end pop;
|
||||
|
||||
/* Queue status: the EMPTY function, returns true for empty queue. */
|
||||
empty: procedure (h) returns (bit(1));
|
||||
declare h handle (Node);
|
||||
return (h = bind(:Node, null:) );
|
||||
end empty;
|
||||
101
Task/Queue-Definition/Pascal/queue-definition.pas
Normal file
101
Task/Queue-Definition/Pascal/queue-definition.pas
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
program fifo(input, output);
|
||||
|
||||
type
|
||||
pNode = ^tNode;
|
||||
tNode = record
|
||||
value: integer;
|
||||
next: pNode;
|
||||
end;
|
||||
|
||||
tFifo = record
|
||||
first, last: pNode;
|
||||
end;
|
||||
|
||||
procedure initFifo(var fifo: tFifo);
|
||||
begin
|
||||
fifo.first := nil;
|
||||
fifo.last := nil
|
||||
end;
|
||||
|
||||
procedure pushFifo(var fifo: tFifo; value: integer);
|
||||
var
|
||||
node: pNode;
|
||||
begin
|
||||
new(node);
|
||||
node^.value := value;
|
||||
node^.next := nil;
|
||||
if fifo.first = nil
|
||||
then
|
||||
fifo.first := node
|
||||
else
|
||||
fifo.last^.next := node;
|
||||
fifo.last := node
|
||||
end;
|
||||
|
||||
function popFifo(var fifo: tFifo; var value: integer): boolean;
|
||||
var
|
||||
node: pNode;
|
||||
begin
|
||||
if fifo.first = nil
|
||||
then
|
||||
popFifo := false
|
||||
else
|
||||
begin
|
||||
node := fifo.first;
|
||||
fifo.first := fifo.first^.next;
|
||||
value := node^.value;
|
||||
dispose(node);
|
||||
popFifo := true
|
||||
end
|
||||
end;
|
||||
|
||||
procedure testFifo;
|
||||
var
|
||||
fifo: tFifo;
|
||||
procedure testpop(expectEmpty: boolean; expectedValue: integer);
|
||||
var
|
||||
i: integer;
|
||||
begin
|
||||
if popFifo(fifo, i)
|
||||
then
|
||||
if expectEmpty
|
||||
then
|
||||
writeln('Error! Expected empty, got ', i, '.')
|
||||
else
|
||||
if i = expectedValue
|
||||
then
|
||||
writeln('Ok, got ', i, '.')
|
||||
else
|
||||
writeln('Error! Expected ', expectedValue, ', got ', i, '.')
|
||||
else
|
||||
if expectEmpty
|
||||
then
|
||||
writeln('Ok, fifo is empty.')
|
||||
else
|
||||
writeln('Error! Expected ', expectedValue, ', found fifo empty.')
|
||||
end;
|
||||
begin
|
||||
initFifo(fifo);
|
||||
pushFifo(fifo, 2);
|
||||
pushFifo(fifo, 3);
|
||||
pushFifo(fifo, 5);
|
||||
testpop(false, 2);
|
||||
pushFifo(fifo, 7);
|
||||
testpop(false, 3);
|
||||
testpop(false, 5);
|
||||
pushFifo(fifo, 11);
|
||||
testpop(false, 7);
|
||||
testpop(false, 11);
|
||||
pushFifo(fifo, 13);
|
||||
testpop(false, 13);
|
||||
testpop(true, 0);
|
||||
pushFifo(fifo, 17);
|
||||
testpop(false, 17);
|
||||
testpop(true, 0)
|
||||
end;
|
||||
|
||||
begin
|
||||
writeln('Testing fifo implementation ...');
|
||||
testFifo;
|
||||
writeln('Testing finished.')
|
||||
end.
|
||||
4
Task/Queue-Definition/Perl/queue-definition-1.pl
Normal file
4
Task/Queue-Definition/Perl/queue-definition-1.pl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
use Carp;
|
||||
sub my push :prototype(\@@) {my($list,@things)=@_; push @$list, @things}
|
||||
sub maypop :prototype(\@) {my($list)=@_; @$list or croak "Empty"; shift @$list }
|
||||
sub empty :prototype(@) {not @_}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue