Priority queue: Difference between revisions
Content added Content deleted
Line 4,591: | Line 4,591: | ||
<syntaxhighlight lang="m2000 interpreter"> |
<syntaxhighlight lang="m2000 interpreter"> |
||
// class definitions are global |
|||
// if there aren't defintions in a class |
|||
global countmany=0& |
|||
⚫ | |||
⚫ | |||
property toString$ { |
|||
⚫ | |||
⚫ | |||
value$=format$("{0::-5}"+string$(" ", sp)+"{1:20}", x, s$) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
class: |
|||
⚫ | |||
} |
|||
Module PriorityQueueForGroups { |
Module PriorityQueueForGroups { |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Flush ' empty current stack |
Flush ' empty current stack |
||
Data obj(3, "Clear drains"), obj(4 ,"Feed cat"), obj( 5 , "Make tea |
Data obj(3, "Clear drains"), obj(4 ,"Feed cat"), obj( 5 , "Make tea") |
||
Data obj( 1 ,"Solve RC tasks"), obj( 2 , "Tax return") |
|||
ObjectCount() |
|||
b=stack |
b=stack |
||
while not empty |
|||
InsertPQ(b) // top of stack is b then objects follow |
|||
end while |
|||
ObjectCount() |
|||
Print "Using Peek to Examine Priority Queue" |
|||
⚫ | |||
n1=each(b) |
|||
⚫ | |||
Header() |
|||
⚫ | |||
⚫ | |||
end while |
|||
ObjectCount() |
|||
⚫ | |||
⚫ | |||
⚫ | |||
end while |
|||
ObjectCount() |
|||
// here are the subs/simple functions |
|||
// these are static parts of module |
|||
sub Header() |
|||
Print " Priority Task" |
|||
Print "========== ================" |
|||
⚫ | |||
sub ObjectCount() |
|||
Print "There are ";countmany;" objects of type obj" |
|||
end sub |
|||
sub InsertPQ(a, n) |
|||
Print "Insert:";n.tostring$(1) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
push n |
push n |
||
t=2 |
local t=2, b=len(a) |
||
m=b |
local m=b |
||
while t<=b |
|||
t1=m |
|||
m=(b+t) div 2 |
m=(b+t) div 2 |
||
if m=0 then m=t1 : exit |
if m=0 then m=t1 : exit |
||
If comp(stackitem(m),n) then t=m+1: continue |
If @comp(stackitem(m),n) then t=m+1: continue |
||
b=m-1 |
b=m-1 |
||
m=b |
m=b |
||
end while |
|||
if m>1 then shiftback m |
if m>1 then shiftback m |
||
} |
} |
||
end sub |
|||
b |
function comp(a, b) |
||
=a.x<b.x |
|||
end function |
|||
InsertPQ b, Group, &comp ' Group pop a group from current stack |
|||
function Peek$(a as stack) |
|||
⚫ | |||
=stackitem(a)=>toString$ |
|||
countmany++ |
|||
end function |
|||
⚫ | |||
⚫ | |||
=len(a)=0 |
|||
end function |
|||
Function Peek$(a) {m=stackitem(a) : =m.s$} |
|||
Function Pop(a) |
|||
// Group make a copy |
|||
⚫ | |||
stack a {=Group:countmany++} |
|||
end function |
|||
m=stackitem() |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
PriorityQueueForGroups |
PriorityQueueForGroups |