pqueue 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +11 -0
- data/LICENSE +167 -0
- data/MANIFEST +21 -0
- data/README +44 -0
- data/RELEASE +10 -0
- data/lib/pqueue.rb +269 -0
- data/meta/abstract +4 -0
- data/meta/authors +6 -0
- data/meta/created +1 -0
- data/meta/homepage +1 -0
- data/meta/license +1 -0
- data/meta/package +1 -0
- data/meta/project +1 -0
- data/meta/released +1 -0
- data/meta/repository +1 -0
- data/meta/summary +1 -0
- data/meta/title +1 -0
- data/meta/version +1 -0
- data/test/test_pqueue.rb +139 -0
- metadata +89 -0
data/HISTORY
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
|
2
|
+
= GNU LESSER GENERAL PUBLIC LICENSE
|
3
|
+
|
4
|
+
Version 3, 29 June 2007
|
5
|
+
|
6
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
7
|
+
Everyone is permitted to copy and distribute verbatim copies
|
8
|
+
of this license document, but changing it is not allowed.
|
9
|
+
|
10
|
+
This version of the GNU Lesser General Public License incorporates
|
11
|
+
the terms and conditions of version 3 of the GNU General Public
|
12
|
+
License, supplemented by the additional permissions listed below.
|
13
|
+
|
14
|
+
== 0. Additional Definitions.
|
15
|
+
|
16
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
17
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
18
|
+
General Public License.
|
19
|
+
|
20
|
+
"The Library" refers to a covered work governed by this License,
|
21
|
+
other than an Application or a Combined Work as defined below.
|
22
|
+
|
23
|
+
An "Application" is any work that makes use of an interface provided
|
24
|
+
by the Library, but which is not otherwise based on the Library.
|
25
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
26
|
+
of using an interface provided by the Library.
|
27
|
+
|
28
|
+
A "Combined Work" is a work produced by combining or linking an
|
29
|
+
Application with the Library. The particular version of the Library
|
30
|
+
with which the Combined Work was made is also called the "Linked
|
31
|
+
Version".
|
32
|
+
|
33
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
34
|
+
Corresponding Source for the Combined Work, excluding any source code
|
35
|
+
for portions of the Combined Work that, considered in isolation, are
|
36
|
+
based on the Application, and not on the Linked Version.
|
37
|
+
|
38
|
+
The "Corresponding Application Code" for a Combined Work means the
|
39
|
+
object code and/or source code for the Application, including any data
|
40
|
+
and utility programs needed for reproducing the Combined Work from the
|
41
|
+
Application, but excluding the System Libraries of the Combined Work.
|
42
|
+
|
43
|
+
== 1. Exception to Section 3 of the GNU GPL.
|
44
|
+
|
45
|
+
You may convey a covered work under sections 3 and 4 of this License
|
46
|
+
without being bound by section 3 of the GNU GPL.
|
47
|
+
|
48
|
+
== 2. Conveying Modified Versions.
|
49
|
+
|
50
|
+
If you modify a copy of the Library, and, in your modifications, a
|
51
|
+
facility refers to a function or data to be supplied by an Application
|
52
|
+
that uses the facility (other than as an argument passed when the
|
53
|
+
facility is invoked), then you may convey a copy of the modified
|
54
|
+
version:
|
55
|
+
|
56
|
+
a) under this License, provided that you make a good faith effort to
|
57
|
+
ensure that, in the event an Application does not supply the
|
58
|
+
function or data, the facility still operates, and performs
|
59
|
+
whatever part of its purpose remains meaningful, or
|
60
|
+
|
61
|
+
b) under the GNU GPL, with none of the additional permissions of
|
62
|
+
this License applicable to that copy.
|
63
|
+
|
64
|
+
== 3. Object Code Incorporating Material from Library Header Files.
|
65
|
+
|
66
|
+
The object code form of an Application may incorporate material from
|
67
|
+
a header file that is part of the Library. You may convey such object
|
68
|
+
code under terms of your choice, provided that, if the incorporated
|
69
|
+
material is not limited to numerical parameters, data structure
|
70
|
+
layouts and accessors, or small macros, inline functions and templates
|
71
|
+
(ten or fewer lines in length), you do both of the following:
|
72
|
+
|
73
|
+
a) Give prominent notice with each copy of the object code that the
|
74
|
+
Library is used in it and that the Library and its use are
|
75
|
+
covered by this License.
|
76
|
+
|
77
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
78
|
+
document.
|
79
|
+
|
80
|
+
== 4. Combined Works.
|
81
|
+
|
82
|
+
You may convey a Combined Work under terms of your choice that,
|
83
|
+
taken together, effectively do not restrict modification of the
|
84
|
+
portions of the Library contained in the Combined Work and reverse
|
85
|
+
engineering for debugging such modifications, if you also do each of
|
86
|
+
the following:
|
87
|
+
|
88
|
+
a) Give prominent notice with each copy of the Combined Work that
|
89
|
+
the Library is used in it and that the Library and its use are
|
90
|
+
covered by this License.
|
91
|
+
|
92
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
93
|
+
document.
|
94
|
+
|
95
|
+
c) For a Combined Work that displays copyright notices during
|
96
|
+
execution, include the copyright notice for the Library among
|
97
|
+
these notices, as well as a reference directing the user to the
|
98
|
+
copies of the GNU GPL and this license document.
|
99
|
+
|
100
|
+
d) Do one of the following:
|
101
|
+
|
102
|
+
d.0) Convey the Minimal Corresponding Source under the terms of this
|
103
|
+
License, and the Corresponding Application Code in a form
|
104
|
+
suitable for, and under terms that permit, the user to
|
105
|
+
recombine or relink the Application with a modified version of
|
106
|
+
the Linked Version to produce a modified Combined Work, in the
|
107
|
+
manner specified by section 6 of the GNU GPL for conveying
|
108
|
+
Corresponding Source.
|
109
|
+
|
110
|
+
d.1) Use a suitable shared library mechanism for linking with the
|
111
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
112
|
+
a copy of the Library already present on the user's computer
|
113
|
+
system, and (b) will operate properly with a modified version
|
114
|
+
of the Library that is interface-compatible with the Linked
|
115
|
+
Version.
|
116
|
+
|
117
|
+
e) Provide Installation Information, but only if you would otherwise
|
118
|
+
be required to provide such information under section 6 of the
|
119
|
+
GNU GPL, and only to the extent that such information is
|
120
|
+
necessary to install and execute a modified version of the
|
121
|
+
Combined Work produced by recombining or relinking the
|
122
|
+
Application with a modified version of the Linked Version. (If
|
123
|
+
you use option 4d0, the Installation Information must accompany
|
124
|
+
the Minimal Corresponding Source and Corresponding Application
|
125
|
+
Code. If you use option 4d1, you must provide the Installation
|
126
|
+
Information in the manner specified by section 6 of the GNU GPL
|
127
|
+
for conveying Corresponding Source.)
|
128
|
+
|
129
|
+
== 5. Combined Libraries.
|
130
|
+
|
131
|
+
You may place library facilities that are a work based on the
|
132
|
+
Library side by side in a single library together with other library
|
133
|
+
facilities that are not Applications and are not covered by this
|
134
|
+
License, and convey such a combined library under terms of your
|
135
|
+
choice, if you do both of the following:
|
136
|
+
|
137
|
+
a) Accompany the combined library with a copy of the same work based
|
138
|
+
on the Library, uncombined with any other library facilities,
|
139
|
+
conveyed under the terms of this License.
|
140
|
+
|
141
|
+
b) Give prominent notice with the combined library that part of it
|
142
|
+
is a work based on the Library, and explaining where to find the
|
143
|
+
accompanying uncombined form of the same work.
|
144
|
+
|
145
|
+
== 6. Revised Versions of the GNU Lesser General Public License.
|
146
|
+
|
147
|
+
The Free Software Foundation may publish revised and/or new versions
|
148
|
+
of the GNU Lesser General Public License from time to time. Such new
|
149
|
+
versions will be similar in spirit to the present version, but may
|
150
|
+
differ in detail to address new problems or concerns.
|
151
|
+
|
152
|
+
Each version is given a distinguishing version number. If the
|
153
|
+
Library as you received it specifies that a certain numbered version
|
154
|
+
of the GNU Lesser General Public License "or any later version"
|
155
|
+
applies to it, you have the option of following the terms and
|
156
|
+
conditions either of that published version or of any later version
|
157
|
+
published by the Free Software Foundation. If the Library as you
|
158
|
+
received it does not specify a version number of the GNU Lesser
|
159
|
+
General Public License, you may choose any version of the GNU Lesser
|
160
|
+
General Public License ever published by the Free Software Foundation.
|
161
|
+
|
162
|
+
If the Library as you received it specifies that a proxy can decide
|
163
|
+
whether future versions of the GNU Lesser General Public License shall
|
164
|
+
apply, that proxy's public statement of acceptance of any version is
|
165
|
+
permanent authorization for you to choose that version for the
|
166
|
+
Library.
|
167
|
+
|
data/MANIFEST
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
test
|
2
|
+
test/test_pqueue.rb
|
3
|
+
RELEASE
|
4
|
+
LICENSE
|
5
|
+
README
|
6
|
+
HISTORY
|
7
|
+
meta
|
8
|
+
meta/created
|
9
|
+
meta/repository
|
10
|
+
meta/homepage
|
11
|
+
meta/summary
|
12
|
+
meta/abstract
|
13
|
+
meta/package
|
14
|
+
meta/title
|
15
|
+
meta/released
|
16
|
+
meta/version
|
17
|
+
meta/license
|
18
|
+
meta/authors
|
19
|
+
meta/project
|
20
|
+
lib
|
21
|
+
lib/pqueue.rb
|
data/README
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
= PQueue
|
2
|
+
|
3
|
+
* http://death.rubyforge.org
|
4
|
+
* http://death.rubyforge.org/pqueue
|
5
|
+
|
6
|
+
|
7
|
+
== DESCRIPTION
|
8
|
+
|
9
|
+
Priority queue with array based heap.
|
10
|
+
|
11
|
+
A priority queue is like a standard queue, except that each inserted
|
12
|
+
elements is given a certain priority, based on the result of the
|
13
|
+
comparison block given at instantiation time. Also, retrieving an element
|
14
|
+
from the queue will always return the one with the highest priority
|
15
|
+
(see #pop and #top).
|
16
|
+
|
17
|
+
The default is to compare the elements in repect to their #> method.
|
18
|
+
For example, Numeric elements with higher values will have higher
|
19
|
+
priorities.
|
20
|
+
|
21
|
+
This library is based off the original PQueue.rb by K.Kodama and
|
22
|
+
Heap.rb by Ronald Butler. It was later improved by Olivier Renaud.
|
23
|
+
|
24
|
+
|
25
|
+
== SYNOPSIS
|
26
|
+
|
27
|
+
... to be done ...
|
28
|
+
|
29
|
+
|
30
|
+
== INSTALLATION
|
31
|
+
|
32
|
+
$ gem install pqueue
|
33
|
+
|
34
|
+
|
35
|
+
== COPYING
|
36
|
+
|
37
|
+
Copyright (c) 2009 The Coding Dead
|
38
|
+
Copyright (c) 2006 Olivier Renaud
|
39
|
+
Copyright (c) 2005 K.Kodama, Ronald Butler
|
40
|
+
|
41
|
+
This software is distributed under the terms of the LGPL v3 license.
|
42
|
+
|
43
|
+
See LICENSE file for details.
|
44
|
+
|
data/RELEASE
ADDED
data/lib/pqueue.rb
ADDED
@@ -0,0 +1,269 @@
|
|
1
|
+
# Priority queue with array based heap.
|
2
|
+
#
|
3
|
+
# A priority queue is like a standard queue, except that each inserted
|
4
|
+
# elements is given a certain priority, based on the result of the
|
5
|
+
# comparison block given at instantiation time. Also, retrieving an element
|
6
|
+
# from the queue will always return the one with the highest priority
|
7
|
+
# (see #pop and #top).
|
8
|
+
#
|
9
|
+
# The default is to compare the elements in repect to their #> method.
|
10
|
+
# For example, Numeric elements with higher values will have higher
|
11
|
+
# priorities.
|
12
|
+
#
|
13
|
+
class PQueue
|
14
|
+
|
15
|
+
# number of elements
|
16
|
+
attr_reader :size
|
17
|
+
# compare Proc
|
18
|
+
attr_reader :gt
|
19
|
+
attr_reader :qarray #:nodoc:
|
20
|
+
protected :qarray
|
21
|
+
|
22
|
+
# Returns a new priority queue.
|
23
|
+
#
|
24
|
+
# If elements are given, build the priority queue with these initial
|
25
|
+
# values. The elements object must respond to #to_a.
|
26
|
+
#
|
27
|
+
# If a block is given, it will be used to determine the priority between
|
28
|
+
# the elements.
|
29
|
+
#
|
30
|
+
# By default, the priority queue retrieves maximum elements first
|
31
|
+
# (using the #> method).
|
32
|
+
def initialize(elements=nil, &block) # :yields: a, b
|
33
|
+
@qarray = [nil]
|
34
|
+
@size = 0
|
35
|
+
@gt = block || lambda {|a,b| a > b}
|
36
|
+
replace(elements) if elements
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
# Assumes that the tree is a heap, for nodes < k.
|
42
|
+
#
|
43
|
+
# The element at index k will go up until it finds its place.
|
44
|
+
def upheap(k)
|
45
|
+
k2 = k.div(2)
|
46
|
+
v = @qarray[k]
|
47
|
+
while k2 > 0 && @gt[v, @qarray[k2]]
|
48
|
+
@qarray[k] = @qarray[k2]
|
49
|
+
k = k2
|
50
|
+
k2 = k2.div(2)
|
51
|
+
end
|
52
|
+
@qarray[k] = v
|
53
|
+
end
|
54
|
+
|
55
|
+
# Assumes the entire tree is a heap.
|
56
|
+
#
|
57
|
+
# The element at index k will go down until it finds its place.
|
58
|
+
def downheap(k)
|
59
|
+
v = @qarray[k]
|
60
|
+
q2 = @size.div(2)
|
61
|
+
loop {
|
62
|
+
break if k > q2
|
63
|
+
j = 2 * k
|
64
|
+
if j < @size && @gt[@qarray[j+1], @qarray[j]]
|
65
|
+
j += 1
|
66
|
+
end
|
67
|
+
break if @gt[v, @qarray[j]]
|
68
|
+
@qarray[k] = @qarray[j]
|
69
|
+
k = j
|
70
|
+
}
|
71
|
+
@qarray[k] = v;
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
# Recursive version of heapify. I kept the code, since it may be
|
76
|
+
# easier to understand than the non-recursive one.
|
77
|
+
# def heapify
|
78
|
+
# @size.div(2).downto(1) {|i| h(i)}
|
79
|
+
# end
|
80
|
+
# def h(t)
|
81
|
+
# l = 2 * t
|
82
|
+
# r = l + 1
|
83
|
+
# hi = if r > @size || @gt[@qarray[l],@qarray[r]] then l else r end
|
84
|
+
# if @gt[@qarray[hi],@qarray[t]]
|
85
|
+
# @qarray[hi], @qarray[t] = @qarray[t], @qarray[hi]
|
86
|
+
# h(hi) if hi <= @size.div(2)
|
87
|
+
# end
|
88
|
+
# end
|
89
|
+
|
90
|
+
# Make a heap out of an unordered array.
|
91
|
+
def heapify
|
92
|
+
@size.div(2).downto(1) do |t|
|
93
|
+
begin
|
94
|
+
l = 2 * t
|
95
|
+
r = l + 1
|
96
|
+
hi = if r > @size || @gt[@qarray[l],@qarray[r]] then l else r end
|
97
|
+
if @gt[@qarray[hi],@qarray[t]]
|
98
|
+
@qarray[hi], @qarray[t] = @qarray[t], @qarray[hi]
|
99
|
+
if hi <= @size.div(2)
|
100
|
+
t = hi
|
101
|
+
redo
|
102
|
+
end # if
|
103
|
+
end #if
|
104
|
+
end #begin
|
105
|
+
end # downto
|
106
|
+
end
|
107
|
+
|
108
|
+
public
|
109
|
+
|
110
|
+
# Add an element in the priority queue.
|
111
|
+
#
|
112
|
+
# The insertion time is O(log n), with n the size of the queue.
|
113
|
+
def push(v)
|
114
|
+
@size += 1
|
115
|
+
@qarray[@size] = v
|
116
|
+
upheap(@size)
|
117
|
+
return self
|
118
|
+
end
|
119
|
+
|
120
|
+
alias :<< :push
|
121
|
+
|
122
|
+
# Return the element with the highest priority and remove it from
|
123
|
+
# the queue.
|
124
|
+
#
|
125
|
+
# The highest priority is determined by the block given at instanciation
|
126
|
+
# time.
|
127
|
+
#
|
128
|
+
# The deletion time is O(log n), with n the size of the queue.
|
129
|
+
#
|
130
|
+
# Return nil if the queue is empty.
|
131
|
+
def pop
|
132
|
+
return nil if empty?
|
133
|
+
res = @qarray[1]
|
134
|
+
@qarray[1] = @qarray[@size]
|
135
|
+
@size -= 1
|
136
|
+
downheap(1)
|
137
|
+
return res
|
138
|
+
end
|
139
|
+
|
140
|
+
# Return the element with the highest priority.
|
141
|
+
def top
|
142
|
+
return nil if empty?
|
143
|
+
return @qarray[1]
|
144
|
+
end
|
145
|
+
|
146
|
+
# Add more than one element at the same time. See #push.
|
147
|
+
#
|
148
|
+
# The elements object must respond to #to_a, or to be a PQueue itself.
|
149
|
+
def push_all(elements)
|
150
|
+
if empty?
|
151
|
+
if elements.kind_of?(PQueue)
|
152
|
+
initialize_copy(elements)
|
153
|
+
else
|
154
|
+
replace(elements)
|
155
|
+
end
|
156
|
+
else
|
157
|
+
if elements.kind_of?(PQueue)
|
158
|
+
@qarray[@size + 1, elements.size] = elements.qarray[1..-1]
|
159
|
+
elements.size.times{ @size += 1; upheap(@size)}
|
160
|
+
else
|
161
|
+
ary = elements.to_a
|
162
|
+
@qarray[@size + 1, ary.size] = ary
|
163
|
+
ary.size.times{ @size += 1; upheap(@size)}
|
164
|
+
end
|
165
|
+
end
|
166
|
+
return self
|
167
|
+
end
|
168
|
+
|
169
|
+
alias :merge :push_all
|
170
|
+
|
171
|
+
|
172
|
+
# Return top n-element as a sorted array.
|
173
|
+
def pop_array(n=@size)
|
174
|
+
ary = []
|
175
|
+
n.times{ary.push(pop)}
|
176
|
+
return ary
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
# True if there is no more elements left in the priority queue.
|
181
|
+
def empty?
|
182
|
+
return @size.zero?
|
183
|
+
end
|
184
|
+
|
185
|
+
# Remove all elements from the priority queue.
|
186
|
+
def clear
|
187
|
+
@qarray.replace([nil])
|
188
|
+
@size = 0
|
189
|
+
return self
|
190
|
+
end
|
191
|
+
|
192
|
+
# Replace the content of the heap by the new elements.
|
193
|
+
#
|
194
|
+
# The elements object must respond to #to_a, or to be a PQueue itself.
|
195
|
+
def replace(elements)
|
196
|
+
if elements.kind_of?(PQueue)
|
197
|
+
initialize_copy(elements)
|
198
|
+
else
|
199
|
+
@qarray.replace([nil] + elements.to_a)
|
200
|
+
@size = @qarray.size - 1
|
201
|
+
heapify
|
202
|
+
end
|
203
|
+
return self
|
204
|
+
end
|
205
|
+
|
206
|
+
# Return a sorted array, with highest priority first.
|
207
|
+
def to_a
|
208
|
+
old_qarray = @qarray.dup
|
209
|
+
old_size = @size
|
210
|
+
res = pop_array
|
211
|
+
@qarray = old_qarray
|
212
|
+
@size = old_size
|
213
|
+
return res
|
214
|
+
end
|
215
|
+
|
216
|
+
alias :sort :to_a
|
217
|
+
|
218
|
+
# Replace the top element with the given one, and return this top element.
|
219
|
+
#
|
220
|
+
# Equivalent to successively calling #pop and #push(v).
|
221
|
+
def replace_top(v)
|
222
|
+
# replace top element
|
223
|
+
if empty?
|
224
|
+
@qarray[1] = v
|
225
|
+
@size += 1
|
226
|
+
return nil
|
227
|
+
else
|
228
|
+
res = @qarray[1]
|
229
|
+
@qarray[1] = v
|
230
|
+
downheap(1)
|
231
|
+
return res
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
# Return true if the given object is present in the queue.
|
236
|
+
def include?(element)
|
237
|
+
return @qarray.include?(element)
|
238
|
+
end
|
239
|
+
|
240
|
+
# Iterate over the ordered elements, destructively.
|
241
|
+
def each_pop #:yields: popped
|
242
|
+
until empty?
|
243
|
+
yield pop
|
244
|
+
end
|
245
|
+
return nil
|
246
|
+
end
|
247
|
+
|
248
|
+
# Pretty print
|
249
|
+
def inspect
|
250
|
+
"<#{self.class}: size=#{@size}, top=#{top || "nil"}>"
|
251
|
+
end
|
252
|
+
|
253
|
+
###########################
|
254
|
+
### Override Object methods
|
255
|
+
|
256
|
+
# Return true if the queues contain equal elements.
|
257
|
+
def ==(other)
|
258
|
+
return size == other.size && to_a == other.to_a
|
259
|
+
end
|
260
|
+
|
261
|
+
private
|
262
|
+
|
263
|
+
def initialize_copy(other)
|
264
|
+
@gt = other.gt
|
265
|
+
@qarray = other.qarray.dup
|
266
|
+
@size = other.size
|
267
|
+
end
|
268
|
+
end # class PQueue
|
269
|
+
|
data/meta/abstract
ADDED
@@ -0,0 +1,4 @@
|
|
1
|
+
A priority queue is like a standard queue, except that each inserted
|
2
|
+
elements is given a certain priority, based on the result of the
|
3
|
+
comparison block given at instantiation time. Retrieving an element
|
4
|
+
from the queue will always return the one with the highest priority.
|
data/meta/authors
ADDED
data/meta/created
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2005-01-01
|
data/meta/homepage
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
http://death.rubyforge.org/
|
data/meta/license
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
LGPLv3
|
data/meta/package
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pqueue
|
data/meta/project
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
death
|
data/meta/released
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2009-07-05
|
data/meta/repository
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
svn://rubyforge.org/var/svn/death/pqueue
|
data/meta/summary
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Queue with prioritized elements.
|
data/meta/title
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
PQueue
|
data/meta/version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/test/test_pqueue.rb
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
require 'pqueue'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TC_PQueue < Test::Unit::TestCase
|
5
|
+
ARY_TEST = [2,6,1,3,8,15,0,-4,7,8,10]
|
6
|
+
ARY_TEST_2 = [25,10,5,13,16,9,16,12]
|
7
|
+
|
8
|
+
def test_initialize
|
9
|
+
assert_nothing_raised { PQueue.new }
|
10
|
+
assert_nothing_raised { PQueue.new([3]) }
|
11
|
+
assert_nothing_raised { PQueue.new(ARY_TEST) }
|
12
|
+
assert_nothing_raised { PQueue.new {|a,b| a<b} }
|
13
|
+
assert_nothing_raised { PQueue.new([3]) {|a,b| a<b} }
|
14
|
+
assert_nothing_raised { PQueue.new(ARY_TEST) {|a,b| a<b} }
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_top
|
18
|
+
assert_equal(ARY_TEST.max, PQueue.new(ARY_TEST).top)
|
19
|
+
assert_nil(PQueue.new.top)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_pop
|
23
|
+
sorted_ary = ARY_TEST.sort
|
24
|
+
q = PQueue.new(ARY_TEST)
|
25
|
+
ARY_TEST.size.times do
|
26
|
+
assert_equal(sorted_ary.pop, q.pop)
|
27
|
+
end
|
28
|
+
assert_equal(0, q.size)
|
29
|
+
assert_nil(PQueue.new.pop)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_insertion
|
33
|
+
q = PQueue.new(ARY_TEST)
|
34
|
+
assert_equal(ARY_TEST.size, q.size)
|
35
|
+
|
36
|
+
ret = q.push(24)
|
37
|
+
assert_equal(q, ret)
|
38
|
+
assert_equal(ARY_TEST.size+1, q.size)
|
39
|
+
|
40
|
+
ret = q.push_all(ARY_TEST_2)
|
41
|
+
assert_equal(q, ret)
|
42
|
+
assert_equal(ARY_TEST.size+1+ARY_TEST_2.size, q.size)
|
43
|
+
|
44
|
+
q = PQueue.new(ARY_TEST)
|
45
|
+
r = PQueue.new(ARY_TEST_2)
|
46
|
+
q.push_all(r)
|
47
|
+
assert_equal(ARY_TEST.size + ARY_TEST_2.size, q.size)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_clear
|
51
|
+
q = PQueue.new(ARY_TEST).clear
|
52
|
+
assert_equal(q, q.clear)
|
53
|
+
assert_equal(0, q.size)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_replace
|
57
|
+
q = PQueue.new(ARY_TEST)
|
58
|
+
q.replace(ARY_TEST_2)
|
59
|
+
assert_equal(ARY_TEST_2.size, q.size)
|
60
|
+
|
61
|
+
q = PQueue.new(ARY_TEST)
|
62
|
+
q.replace(PQueue.new(ARY_TEST_2))
|
63
|
+
assert_equal(ARY_TEST_2.size, q.size)
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_inspect
|
67
|
+
assert_equal("<PQueue: size=#{ARY_TEST.size}, top=#{ARY_TEST.max}>",
|
68
|
+
PQueue.new(ARY_TEST).inspect)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_to_a
|
72
|
+
q = PQueue.new(ARY_TEST)
|
73
|
+
assert_equal(ARY_TEST.sort.reverse, q.sort)
|
74
|
+
q = PQueue.new(0..4)
|
75
|
+
assert_equal([4,3,2,1,0], q.sort)
|
76
|
+
end
|
77
|
+
|
78
|
+
def pop_array
|
79
|
+
q = PQueue.new(ARY_TEST)
|
80
|
+
assert_equal(ARY_TEST.sort.reverse[0..5], q.pop_array(5))
|
81
|
+
q = PQueue.new(ARY_TEST)
|
82
|
+
assert_equal(ARY_TEST.sort.reverse, q.pop_array)
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_include
|
86
|
+
q = PQueue.new(ARY_TEST + [21] + ARY_TEST_2)
|
87
|
+
assert_equal(true, q.include?(21))
|
88
|
+
|
89
|
+
q = PQueue.new(ARY_TEST - [15])
|
90
|
+
assert_equal(false, q.include?(15))
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_assert_equal
|
94
|
+
assert_equal(PQueue.new, PQueue.new)
|
95
|
+
assert_equal(PQueue.new(ARY_TEST), PQueue.new(ARY_TEST.sort_by{rand}))
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_replace_top
|
99
|
+
q = PQueue.new
|
100
|
+
assert_nil(q.replace_top(6))
|
101
|
+
assert_equal(6, q.top)
|
102
|
+
|
103
|
+
q = PQueue.new(ARY_TEST)
|
104
|
+
h = PQueue.new(ARY_TEST)
|
105
|
+
q.pop; q.push(11)
|
106
|
+
h.replace_top(11)
|
107
|
+
assert_equal(q, h)
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_dup
|
111
|
+
q = PQueue.new(ARY_TEST)
|
112
|
+
assert_equal(q, q.dup)
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_array_copied
|
116
|
+
ary = ARY_TEST.dup
|
117
|
+
q = PQueue.new(ary)
|
118
|
+
q.pop
|
119
|
+
assert_equal(ARY_TEST, ary)
|
120
|
+
|
121
|
+
ary = ARY_TEST.dup
|
122
|
+
q = PQueue.new
|
123
|
+
q.replace(ary)
|
124
|
+
q.pop
|
125
|
+
assert_equal(ARY_TEST, ary)
|
126
|
+
|
127
|
+
ary = ARY_TEST.dup
|
128
|
+
q = PQueue.new([1])
|
129
|
+
q.push_all(ary)
|
130
|
+
q.pop
|
131
|
+
assert_equal(ARY_TEST, ary)
|
132
|
+
|
133
|
+
q = PQueue.new(ARY_TEST)
|
134
|
+
r = q.dup
|
135
|
+
q.pop
|
136
|
+
assert_not_equal(q, r)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pqueue
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- K.Kodama
|
8
|
+
- Ronald Butler
|
9
|
+
- Olivier Renaud
|
10
|
+
- Rick Bradley
|
11
|
+
- Trans <transfire@gmail.com>
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
|
16
|
+
date: 2009-07-05 00:00:00 -04:00
|
17
|
+
default_executable:
|
18
|
+
dependencies: []
|
19
|
+
|
20
|
+
description: |-
|
21
|
+
A priority queue is like a standard queue, except that each inserted
|
22
|
+
elements is given a certain priority, based on the result of the
|
23
|
+
comparison block given at instantiation time. Retrieving an element
|
24
|
+
from the queue will always return the one with the highest priority.
|
25
|
+
email:
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README
|
32
|
+
- MANIFEST
|
33
|
+
- RELEASE
|
34
|
+
- LICENSE
|
35
|
+
- HISTORY
|
36
|
+
files:
|
37
|
+
- test/test_pqueue.rb
|
38
|
+
- RELEASE
|
39
|
+
- LICENSE
|
40
|
+
- README
|
41
|
+
- HISTORY
|
42
|
+
- meta/created
|
43
|
+
- meta/repository
|
44
|
+
- meta/homepage
|
45
|
+
- meta/summary
|
46
|
+
- meta/abstract
|
47
|
+
- meta/package
|
48
|
+
- meta/title
|
49
|
+
- meta/released
|
50
|
+
- meta/version
|
51
|
+
- meta/license
|
52
|
+
- meta/authors
|
53
|
+
- meta/project
|
54
|
+
- lib/pqueue.rb
|
55
|
+
- MANIFEST
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://death.rubyforge.org/
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options:
|
62
|
+
- --inline-source
|
63
|
+
- --title
|
64
|
+
- pqueue api
|
65
|
+
- --main
|
66
|
+
- README
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: "0"
|
74
|
+
version:
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: "0"
|
80
|
+
version:
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project: death
|
84
|
+
rubygems_version: 1.3.4
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: A priority queue is like a standard queue, except that each inserted
|
88
|
+
test_files:
|
89
|
+
- test/test_pqueue.rb
|