Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
115
Task/Priority-queue/M2000-Interpreter/priority-queue-1.m2000
Normal file
115
Task/Priority-queue/M2000-Interpreter/priority-queue-1.m2000
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
Module UnOrderedArray {
|
||||
Class PriorityQueue {
|
||||
Private:
|
||||
Dim Item()
|
||||
many=0, level=0, first
|
||||
cmp = lambda->0
|
||||
Module Reduce {
|
||||
if .many<.first*2 then exit
|
||||
if .level<.many/2 then .many/=2 : Dim .Item(.many)
|
||||
}
|
||||
Public:
|
||||
Module Clear {
|
||||
Dim .Item() \\ erase all
|
||||
.many<=0 \\ default
|
||||
.Level<=0
|
||||
}
|
||||
Module Add {
|
||||
if .level=.many then
|
||||
if .many=0 then Error "Define Size First"
|
||||
Dim .Item(.many*2)
|
||||
.many*=2
|
||||
end if
|
||||
Read Item
|
||||
if .level=0 then
|
||||
.Item(0)=Item
|
||||
else.If .cmp(.Item(0), Item)=-1 then \\ Item is max
|
||||
.Item(.level)=Item
|
||||
swap .Item(0), .Item(.level)
|
||||
else
|
||||
.Item(.level)=Item
|
||||
end if
|
||||
.level++
|
||||
}
|
||||
Function Peek {
|
||||
if .level=0 then error "empty"
|
||||
=.Item(0)
|
||||
}
|
||||
Function Poll {
|
||||
if .level=0 then error "empty"
|
||||
=.Item(0)
|
||||
if .level=2 then
|
||||
swap .Item(0), .Item(1)
|
||||
.Item(1)=0
|
||||
.Level<=1
|
||||
else.If .level>2 then
|
||||
.Level--
|
||||
Swap .Item(.level), .Item(0)
|
||||
.Item(.level)=0
|
||||
for I=.level-1 to 1
|
||||
if .cmp(.Item(I), .Item(I-1))=1 then Swap .Item(I), .Item(I-1)
|
||||
next
|
||||
else
|
||||
.level<=0 : .Item(0)=0
|
||||
end if
|
||||
.Reduce
|
||||
}
|
||||
Module Remove {
|
||||
if .level=0 then error "empty"
|
||||
Read Item
|
||||
k=true
|
||||
if .cmp(.Item(0), Item)=0 then
|
||||
Item=.Poll()
|
||||
K~ \\ k=false
|
||||
else.If .Level>1 then
|
||||
I2=.Level-1
|
||||
for I=1 to I2
|
||||
if k then
|
||||
if .cmp(.Item(I), Item)=0 then
|
||||
if I<I2 then Swap .Item(I), .Item(I2)
|
||||
.Item(I2)=0
|
||||
k=false
|
||||
end if
|
||||
else
|
||||
exit
|
||||
end if
|
||||
next
|
||||
.Level--
|
||||
end if
|
||||
if k then Error "Not Found"
|
||||
.Reduce
|
||||
}
|
||||
Function Size {
|
||||
if .many=0 then Error "Define Size First"
|
||||
=.Level
|
||||
}
|
||||
Class:
|
||||
Module PriorityQueue {
|
||||
if .many>0 then Error "Clear List First"
|
||||
Read .many, .cmp
|
||||
.first<=.many
|
||||
Dim .Item(.many)
|
||||
}
|
||||
}
|
||||
|
||||
Class Item { X, S$
|
||||
Class: // constructor as temporary definition
|
||||
Module Item {Read .X, .S$}
|
||||
}
|
||||
Queue=PriorityQueue(100, Lambda -> {Read A,B : =Compare(A.X,B.X)})
|
||||
Queue.Add Item(3, "Clear drains") : Gosub PrintTop()
|
||||
Queue.Add Item(4 ,"Feed cat") : PrintTop()
|
||||
Queue.Add Item(5 ,"Make tea") : PrintTop()
|
||||
Queue.Add Item(1 ,"Solve RC tasks") : PrintTop()
|
||||
Queue.Add Item(2 ,"Tax return") : PrintTop()
|
||||
Print "remove items"
|
||||
While true
|
||||
MM=Queue.Poll() :Print MM.X, MM.S$,,"Size="; Queue.Size()
|
||||
if Queue.Size()=0 then exit
|
||||
PrintTop()
|
||||
End While
|
||||
Sub PrintTop()
|
||||
M=Queue.Peek() : Print "Item ";M.X, M.S$
|
||||
End Sub
|
||||
}
|
||||
UnOrderedArray
|
||||
57
Task/Priority-queue/M2000-Interpreter/priority-queue-2.m2000
Normal file
57
Task/Priority-queue/M2000-Interpreter/priority-queue-2.m2000
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
Module PriorityQueue {
|
||||
a= ((3, "Clear drains"), (4 ,"Feed cat"), ( 5 , "Make tea"))
|
||||
a=cons(a, ((1 ,"Solve RC tasks"), ( 2 , "Tax return")))
|
||||
b=stack
|
||||
comp=lambda (a, b) -> array(a, 0)<array(b, 0)
|
||||
module InsertPQ (a, n, &comp) {
|
||||
if len(a)=0 then stack a {data n} : exit
|
||||
if comp(n, stackitem(a)) then stack a {push n} : exit
|
||||
stack a {
|
||||
push n
|
||||
t=2: b=len(a)
|
||||
m=b
|
||||
while t<=b
|
||||
t1=m
|
||||
m=(b+t) div 2
|
||||
if m=0 then m=t1 : exit
|
||||
If comp(stackitem(m),n) then t=m+1: continue
|
||||
b=m-1
|
||||
m=b
|
||||
end while
|
||||
if m>1 then shiftback m
|
||||
}
|
||||
}
|
||||
|
||||
n=each(a)
|
||||
while n
|
||||
InsertPq b, array(n), &comp
|
||||
end while
|
||||
|
||||
n1=each(b)
|
||||
while n1
|
||||
m=stackitem(n1)
|
||||
print array(m, 0), array$(m, 1)
|
||||
end while
|
||||
|
||||
\\ Peek topitem (without popping)
|
||||
print Array$(stackitem(b), 1)
|
||||
\\ Pop item
|
||||
Stack b {
|
||||
Read old
|
||||
}
|
||||
print Array$(old, 1)
|
||||
def Peek$(a)=Array$(stackitem(a), 1)
|
||||
Function Pop$(a) {
|
||||
stack a {
|
||||
=Array$(stackitem(), 1)
|
||||
drop
|
||||
}
|
||||
}
|
||||
print Peek$(b)
|
||||
print Pop$(b)
|
||||
def IsEmpty(a)=len(a)=0
|
||||
while not IsEmpty(b)
|
||||
print pop$(b)
|
||||
end while
|
||||
}
|
||||
PriorityQueue
|
||||
83
Task/Priority-queue/M2000-Interpreter/priority-queue-3.m2000
Normal file
83
Task/Priority-queue/M2000-Interpreter/priority-queue-3.m2000
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
// class definitions are global
|
||||
// if there aren't defintions in a class
|
||||
global countmany=0&
|
||||
class obj {
|
||||
x, s$
|
||||
property toString$ {
|
||||
value (sp=8) {
|
||||
link parent x, s$ to x, s$
|
||||
value$=format$("{0::-5}"+string$(" ", sp)+"{1:20}", x, s$)
|
||||
}
|
||||
}
|
||||
remove {
|
||||
countmany--
|
||||
}
|
||||
class:
|
||||
module obj (.x, .s$) {countmany++}
|
||||
}
|
||||
Module PriorityQueueForGroups {
|
||||
Flush ' empty current stack
|
||||
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
|
||||
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()
|
||||
while n1
|
||||
Print @Peek$(n1)
|
||||
end while
|
||||
ObjectCount()
|
||||
Header()
|
||||
while not @isEmpty(b)
|
||||
Print @Pop(b)=>tostring$
|
||||
end while
|
||||
ObjectCount()
|
||||
// here are the subs/simple functions
|
||||
// these are static parts of module
|
||||
sub Header()
|
||||
Print " Priority Task"
|
||||
Print "========== ================"
|
||||
end sub
|
||||
sub ObjectCount()
|
||||
Print "There are ";countmany;" objects of type obj"
|
||||
end sub
|
||||
sub InsertPQ(a, n)
|
||||
Print "Insert:";n.tostring$(1)
|
||||
if len(a)=0 then stack a {data n} : exit sub
|
||||
if @comp(n, stackitem(a)) then stack a {push n} : exit sub
|
||||
stack a {
|
||||
push n
|
||||
local t=2, b=len(a)
|
||||
local m=b
|
||||
while t<=b
|
||||
t1=m
|
||||
m=(b+t) div 2
|
||||
if m=0 then m=t1 : exit
|
||||
If @comp(stackitem(m),n) then t=m+1: continue
|
||||
b=m-1
|
||||
m=b
|
||||
end while
|
||||
if m>1 then shiftback m
|
||||
}
|
||||
end sub
|
||||
function comp(a, b)
|
||||
=a.x<b.x
|
||||
end function
|
||||
function Peek$(a as stack)
|
||||
=stackitem(a)=>toString$
|
||||
countmany++
|
||||
end function
|
||||
function IsEmpty(a)
|
||||
=len(a)=0
|
||||
end function
|
||||
Function Pop(a)
|
||||
// Group make a copy
|
||||
stack a {=Group:countmany++}
|
||||
end function
|
||||
}
|
||||
PriorityQueueForGroups
|
||||
123
Task/Priority-queue/M2000-Interpreter/priority-queue-4.m2000
Normal file
123
Task/Priority-queue/M2000-Interpreter/priority-queue-4.m2000
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
global countmany=0&
|
||||
class obj {
|
||||
x, s$
|
||||
property toString$ {
|
||||
value (sp=8) {
|
||||
link parent x, s$ to x, s$
|
||||
value$=format$("{0::-5}"+string$(" ", sp)+"{1:20}", x, s$)
|
||||
}
|
||||
}
|
||||
function Copy {
|
||||
countmany++
|
||||
z=this
|
||||
=pointer((z))
|
||||
}
|
||||
remove {
|
||||
countmany--
|
||||
}
|
||||
class:
|
||||
module obj (.x, .s$) {countmany++}
|
||||
}
|
||||
// obj() return object as value (using a special pointer)
|
||||
function global g(priority, task$) {
|
||||
// here we return an object using nonrmal pointer
|
||||
// try to change -> to = to see the error
|
||||
->obj(priority, task$)
|
||||
}
|
||||
Module PriorityQueueForGroups {
|
||||
Flush ' empty current stack
|
||||
Data g(3, "Clear drains"),g(4 ,"Feed cat"), g( 5 , "Make tea")
|
||||
Data g( 1 ,"Solve RC tasks")
|
||||
ObjectCount()
|
||||
pq=stack
|
||||
zz=stack
|
||||
while not empty
|
||||
InsertPQ(pq) // top of stack is pq then objects follow
|
||||
end while
|
||||
Pen 15 {
|
||||
data g(2 , "Tax return"), g(1 ,"Solve RC tasks#2")
|
||||
while not empty: InsertPq(zz): End While
|
||||
n1=each(zz,-1,1)
|
||||
Header()
|
||||
while n1
|
||||
Print @Peek$(stackitem(n1))
|
||||
end while
|
||||
}
|
||||
MergePq(pq, zz, false)
|
||||
InsertPq(pq, g(1 ,"Solve RC tasks#3"))
|
||||
ObjectCount()
|
||||
Print "Using Peek to Examine Priority Queue"
|
||||
n1=each(pq,-1, 1)
|
||||
Header()
|
||||
while n1
|
||||
Print @Peek$(stackitem(n1))
|
||||
end while
|
||||
ObjectCount()
|
||||
Header()
|
||||
while not @isEmpty(pq)
|
||||
Print @Pop(pq)=>tostring$
|
||||
end while
|
||||
ObjectCount()
|
||||
Header()
|
||||
while not @isEmpty(zz)
|
||||
Print @Pop(zz)=>tostring$
|
||||
end while
|
||||
ObjectCount()
|
||||
// here are the subs/simple functions
|
||||
// these are static parts of module
|
||||
sub Header()
|
||||
Print " Priority Task"
|
||||
Print "========== ================"
|
||||
end sub
|
||||
sub ObjectCount()
|
||||
Print "There are ";countmany;" objects of type obj"
|
||||
end sub
|
||||
sub MergePq(a, pq, emptyqueue)
|
||||
local n1=each(pq, -1, 1), z=pointer()
|
||||
while n1
|
||||
if emptyqueue then
|
||||
stack pq {
|
||||
shiftback len(pq)
|
||||
InsertPQ(a, Group)
|
||||
}
|
||||
else
|
||||
z=stackitem(n1)
|
||||
InsertPQ(a, z=>copy())
|
||||
end if
|
||||
end while
|
||||
end sub
|
||||
sub InsertPQ(a, n as *obj)
|
||||
Print "Insert:";n=>tostring$(1)
|
||||
if len(a)=0 then stack a {data n} : exit sub
|
||||
if @comp(n, stackitem(a)) then stack a {push n} : exit sub
|
||||
stack a {
|
||||
push n
|
||||
local t=2, pq=len(a), t1=0
|
||||
local m=pq
|
||||
while t<=pq
|
||||
t1=m
|
||||
m=(pq+t) div 2
|
||||
if m=0 then m=t1 : exit
|
||||
If @comp(stackitem(m),n) then t=m+1: continue
|
||||
pq=m-1
|
||||
m=pq
|
||||
end while
|
||||
if m>1 then shiftback m
|
||||
}
|
||||
end sub
|
||||
function comp(a as *obj, pq as *obj)
|
||||
=a=>x>pq=>x
|
||||
end function
|
||||
function Peek$(a as *obj)
|
||||
=a=>toString$
|
||||
end function
|
||||
function IsEmpty(a)
|
||||
=len(a)=0
|
||||
end function
|
||||
function Pop(a)
|
||||
// Group make a copy (but here is a pointer of group)
|
||||
stack a {shift stack.size
|
||||
=Group}
|
||||
end function
|
||||
}
|
||||
PriorityQueueForGroups
|
||||
100
Task/Priority-queue/M2000-Interpreter/priority-queue-5.m2000
Normal file
100
Task/Priority-queue/M2000-Interpreter/priority-queue-5.m2000
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
form 80, 42
|
||||
Module OrdrerQueue (filename$) {
|
||||
// f=-2 or use empty filename for screen
|
||||
open filename$ for output as #f
|
||||
zz=list
|
||||
pq=List
|
||||
flush
|
||||
// subs can read from module's stack
|
||||
println("Add items to pq queue")
|
||||
Data 4 ,"Feed cat",5 , "Make tea", 3, "Clear drains",1 , "Solve RC tasks"
|
||||
AddItems(pq)
|
||||
println("Add items to zz queue")
|
||||
AddItems(zz, 2 , "Tax return", 1 ,"Solve RC tasks#2")
|
||||
println("Peek top from zz queue")
|
||||
PeekTop(zz) // Solve RC tasks#2
|
||||
println("Merge two priority lists")
|
||||
merge(pq, zz, false)
|
||||
println("Peek top from pq queue")
|
||||
PeekTop(pq) // Solve RC tasks
|
||||
println("Add items to pq queue")
|
||||
AddItems(pq, 1 ,"Solve RC tasks#3")
|
||||
println("Peek top from pq queue")
|
||||
PeekTop(pq) // Solve RC tasks
|
||||
println("Pop one from pq until empty queue")
|
||||
while len(pq)>0
|
||||
PopOne(pq)
|
||||
end while
|
||||
println("Pop one from zz until empty queue")
|
||||
while len(zz)>0
|
||||
PopOne(zz)
|
||||
end while
|
||||
close #f
|
||||
sub AddItems(pq)
|
||||
local s, z
|
||||
while not empty
|
||||
read z
|
||||
if not exist(pq, z) then s=stack:append pq, z:=s else s=eval(pq)
|
||||
read what$: stack s {data what$}
|
||||
stack new {println( "add item",z,what$)}
|
||||
end while
|
||||
sort descending pq as number
|
||||
Println()
|
||||
end sub
|
||||
sub merge(pq, qp, emptyqueue)
|
||||
local needsort=false
|
||||
local kqp=each(qp, -1, 1), k$, t, p
|
||||
while kqp
|
||||
t=eval(kqp)
|
||||
k$= eval$(kqp!)
|
||||
if not exist(pq, eval$(kqp!)) then
|
||||
p=stack
|
||||
append pq, val(eval$(kqp!)):=p
|
||||
needsort=true
|
||||
else
|
||||
p=eval(pq)
|
||||
end if
|
||||
stack p {
|
||||
if emptyqueue then
|
||||
data !t
|
||||
delete qp,eval$(kqp!)
|
||||
else
|
||||
data !stack(t)
|
||||
end if
|
||||
}
|
||||
end while
|
||||
if needsort then sort descending pq as number
|
||||
end sub
|
||||
sub PeekTop(pq)
|
||||
Local k=len(pq)
|
||||
if k=0 then exit sub
|
||||
k=val(eval$(pq, k-1))
|
||||
if exist(pq, k) then local s=eval(pq): println( k,stackitem$(s, 1))
|
||||
End sub
|
||||
Sub PopOne(pq)
|
||||
Local k=len(pq)
|
||||
if k<0 then exit sub
|
||||
k=val(eval$(pq, k-1))
|
||||
if exist(pq, k) then
|
||||
local s=eval(pq)
|
||||
println( k,stackitem$(s, 1))
|
||||
if len(s)=1 then
|
||||
delete pq, k
|
||||
else
|
||||
stack s {drop}
|
||||
end if
|
||||
end if
|
||||
end sub
|
||||
Sub println()
|
||||
if empty then print #f, "": exit sub
|
||||
while not empty
|
||||
if islet then print #f, letter$;
|
||||
if empty else print #f, " ";
|
||||
if isnum then print #f, number;
|
||||
if empty else print #f, " ";
|
||||
end while
|
||||
if f=-2 and pos=0 then exit sub
|
||||
print #f, ""
|
||||
end sub
|
||||
}
|
||||
OrdrerQueue ""
|
||||
Loading…
Add table
Add a link
Reference in a new issue