2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,10 +1,17 @@
{{Data structure}}
[[File:Fifo.gif|frame|right|Illustration of FIFO behavior]]
Create a queue data structure and demonstrate its operations. (For implementations of queues, see the [[FIFO]] task.)
;Task:
Create a queue data structure and demonstrate its operations.
(For implementations of queues, see the [[FIFO]] task.)
Operations:
* push (aka ''enqueue'') - add element
* pop (aka ''dequeue'') - pop first element
* empty - return truth value when empty
::*   push       (aka ''enqueue'') - add element
::*   pop         (aka ''dequeue'') - pop first element
::*   empty     - return truth value when empty
<br>
{{Template:See also lists}}
<br><br>

View file

@ -0,0 +1,25 @@
#import system.
#import system'collections.
#import extensions.
#symbol program =
[
// Create a queue and "push" items into it
#var queue := Queue new.
queue push:1.
queue push:3.
queue push:5.
// "Pop" items from the queue in FIFO order
console writeLine:(queue pop). // 1
console writeLine:(queue pop). // 3
console writeLine:(queue pop). // 5
// To tell if the queue is empty, we check the count
console writeLine:"queue is ":((queue length == 0) iif:"empty":"nonempty").
// If we try to pop from an empty queue, an exception
// is thrown.
queue pop | if &Error: e [ console writeLine:"Queue empty.". ].
].

View file

@ -0,0 +1,45 @@
: cqueue: ( n -- <text>)
create \ compile time: build the data structure in memory
dup
dup 1- and abort" queue size must be power of 2"
0 , \ write pointer "HEAD"
0 , \ read pointer "TAIL"
0 , \ byte counter
dup 1- , \ mask value used for wrap around
allot ; \ run time: returns the address of this data structure
\ calculate offsets into the queue data structure
: ->head ( q -- adr ) ; \ syntactic sugar
: ->tail ( q -- adr ) cell+ ;
: ->cnt ( q -- adr ) 2 cells + ;
: ->msk ( q -- adr ) 3 cells + ;
: ->data ( q -- adr ) 4 cells + ;
: head++ ( q -- ) \ circular increment head pointer of a queue
dup >r ->head @ 1+ r@ ->msk @ and r> ->head ! ;
: tail++ ( q -- ) \ circular increment tail pointer of a queue
dup >r ->tail @ 1+ r@ ->msk @ and r> ->tail ! ;
: qempty ( q -- flag)
dup ->head off dup ->tail off dup ->cnt off \ reset all fields to "off" (zero)
->cnt @ 0= ; \ per the spec qempty returns a flag
: cnt=msk? ( q -- flag) dup >r ->cnt @ r> ->msk @ = ;
: ?empty ( q -- ) ->cnt @ 0= abort" queue is empty" ;
: ?full ( q -- ) cnt=msk? abort" queue is full" ;
: 1+! ( adr -- ) 1 swap +! ; \ increment contents of adr
: 1-! ( adr -- ) -1 swap +! ; \ decrement contents of adr
: qc@ ( queue -- char ) \ fetch next char in queue
dup >r ?empty \ abort if empty
r@ ->cnt 1-! \ decr. the counter
r@ tail++
r@ ->data r> ->tail @ + c@ ; \ calc. address and fetch the byte
: qc! ( char queue -- )
dup >r ?full \ abort if q full
r@ ->cnt 1+! \ incr. the counter
r@ head++
r@ ->data r> ->head @ + c! ; \ data+head = adr, and store the char

View file

@ -0,0 +1,16 @@
make-queue constant q1
make-queue constant q2
q1 empty? .
5 q1 enqueue
q1 empty? .
7 q1 enqueue
9 q1 enqueue
q2 empty? .
3 q2 enqueue
q2 empty? .
q1 dequeue .
q1 dequeue .
q1 dequeue .
q1 empty? .
q2 dequeue .
q2 empty? .

View file

@ -0,0 +1,3 @@
$queue = New-Object -TypeName System.Collections.Queue
#or
$queue = [System.Collections.Queue] @()

View file

@ -0,0 +1 @@
Get-Member -InputObject $queue

View file

@ -0,0 +1 @@
1,2,3 | ForEach-Object {$queue.Enqueue($_)}

View file

@ -0,0 +1 @@
$queue.Peek()

View file

@ -0,0 +1 @@
$queue.Dequeue()

View file

@ -0,0 +1 @@
$queue.Clear()

View file

@ -0,0 +1 @@
if (-not $queue.Count) {"Queue is empty"}