-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprio.vbs
110 lines (92 loc) · 2.38 KB
/
prio.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
option explicit
'------------------------------------------------
'priority queue class in a self-dimensioning array
'AGV 2019
'a private out_of_order function must be adapted according to the kind of data in the queue and the order required
'--------------------------------------
Class prio_queue
private size
private q
'adapt this function to your data
private function out_of_order(father,son):out_of_order= (father>son): end function
function peek
peek=q(1)
end function
property get qty
qty=size
end property
property get isempty
isempty=(size=0)
end property
function remove
dim x
x=q(1)
q(1)=q(size)
size=size-1
sift_down
remove=x
end function
sub add (x)
size=size+1
if size>ubound(q) then redim preserve q(ubound(q)+100)
q(size)=x
sift_up
end sub
Private sub swap (i,j)
dim x
x=q(i):q(i)=q(j):q(j)=x
end sub
private sub sift_up
dim h,p
h=size:
p=h\2
while out_of_order(q(p),q(h)) and h>1
swap h,p
h=p
p=h\2
wend
end sub
private sub sift_down
dim p,h
p=1
do
if p>=size then exit do
h =p*2
if h >size then exit do
if h+1<=size then if out_of_order(q(h),q(h+1)) then h=h+1
if out_of_order(q(p),q(h)) then swap h,p
p=h
loop
end sub
'Al instanciar objeto con New
Private Sub Class_Initialize( )
redim q(100)
size=0
End Sub
'When Object is Set to Nothing
Private Sub Class_Terminate( )
erase q
End Sub
End Class
'-------------------------------------
'test program
'---------------------------------
dim queue,i,o,n,ercnt
set queue=new prio_queue
wscript.echo "Adding 2000 random inputs to the queue"
for i=1 to 2000
queue.add(cint(rnd*10000))
next
wscript.echo "Done. Using .qty and .peek methods: " & queue.qty() &" items in queue. Item "& queue.peek()& " is at the top." & vbcrlf
wscript.echo "Removing 2000 items from the queue and giving an error if one of them is smaller than the previous one"
o=-99999999:i=1:ercnt=0
while not queue.isempty()
n=queue.remove()
if o>n then
ercnt=ercnt+1
wscript.echo "error at item "& i & " Previous was " & o & " and present is " & n
end if
o= n:i=i+1
wend
wscript.echo "Finished. " & ercnt & " items out of order. " & i-1 & " items removed. "& n & " was the last one."
set queue= nothing