Priority queue: Difference between revisions

(Add CLU)
Line 5,445:
=={{header|Quackery}}==
 
For more examples uf usage, see [[Sorting algorithms/Heapsort#Quackery]] and [[Huffman coding#Quackery]]
The task [[Sorting algorithms/Heapsort#Quackery]] implements a priority queue. Here we reuse the words <code>comparison</code>, <code>toheap</code>, and <code>fromheap</code> from that task and adapt them to this task. <code>p</code> in the stack comments denotes a priority queue.
 
<lang Quackery>( ***** priotity queue ***** )
(I presume 1 to be the highest priority, and 5 the lowest.)
 
[ fromheapstack ] 1 peek ] is removeheap.pq ( p[ --> p[ $ )
<lang Quackery> [ ' [ 0 peek swap 0 peek > ]
comparison put [] ] is new-pq ( --> p )
 
[ nestedstack ] join toheap ] is insertcomp.pq ( p n $ [ --> p[ )
 
[ unpack
[ fromheap 1 peek ] is remove ( p --> p $ )
comp.pq put heap.pq put ] is pq->stacks ( [ --> )
 
[ heap.pq take comp.pq take
[ drop comparison release ] is end-pq ( p --> )
2 pack ] is stacks->pq ( --> [ )
 
[ dropheap.pq comparisonshare releaseswap peek ] is end-pqheapeek ( pn --> x )
 
[ heap.pq take
swap poke heap.pq put ] is heapoke ( n x --> )
 
[ 1+ 2 / 1 - ] is parent ( n --> n )
 
[ 0 > ] is has-parent ( n --> b )
 
[ 2 * 1+ ] is child ( n --> n )
 
[ child heap.pq share size < ] is has-child ( n --> b )
 
[ 1+ ] is sibling ( n --> n )
 
[ sibling
heap.pq comparisonshare putsize []< ] is newhas-pqsibling ( n --> pb )
 
[ comp.pq share do ] is compare ( x x --> b )
 
[ 0 peek size ] is pqsize ( p --> b )
 
[ swap pq->stacks
heap.pq take tuck size
rot 0 join heap.pq put
[ dup has-parent while
dup parent
rot over heapeek
2dup compare iff
[ 2swap unrot heapoke ]
again
rot 2drop swap ]
heapoke
stacks->pq ] is topq ( p x --> p )
 
[ dup heapeek swap
[ dup has-child while
dup child
dup has-sibling if
[ dup sibling heapeek
over heapeek
compare if sibling ]
dip over dup heapeek
rot dip dup compare iff
[ rot heapoke ]
again
2drop ]
heapoke ] is heapify.pq ( n --> )
 
[ dup pqsize 0 = if
[ $ "Unexpectedly empty priority queue."
fail ]
pq->stacks heap.pq share
behead
over [] = iff
[ swap heap.pq replace ]
else
[ swap -1 split
swap join heap.pq put
0 heapify.pq ]
stacks->pq swap ] is frompq ( p --> p x )
 
[ ]'[ 2 pack pq->stacks
heap.pq share
size 2 / times
[ i heapify.pq ]
stacks->pq ] is pqwith ( [ --> p )
 
( ***** task ***** )
 
[ nested join topq ] is insert ( p n $ --> p )
 
<lang Quackery> [] 'pqwith [ 0 peek swap 0 peek > ]
 
new-pq
3 $ "Clear drains" insert
4 $ "Feed cat" insert
Line 5,465 ⟶ 5,538:
1 $ "Solve RC tasks" insert
2 $ "Tax return" insert
 
5 times [ removefrompq echo$ cr ]unpack
swap echo say ": "
echo$ cr ]
end-pqdrop</lang>
 
{{out}}
 
<pre>1: Solve RC tasks
2: Tax return
3: Clear drains
4: Feed cat
5: Make tea
</pre>
 
1,496

edits