Skip to content

Commit 0513366

Browse files
committed
Fixed bugs in NonConcurrentPriorityQueue.
1 parent 28f6d35 commit 0513366

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/concurrent/collection/non_concurrent_priority_queue.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module Collection
3030
# @see http://algs4.cs.princeton.edu/24pq/index.php#2.6
3131
# @see http://algs4.cs.princeton.edu/24pq/MaxPQ.java.html
3232
#
33-
# @see http://docs.oracle.com/javase/7/docs/api/java/util/NonConcurrentPriorityQueue.html
33+
# @see http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html
3434
#
3535
# @!visibility private
3636
# @!macro internal_implementation_note
@@ -66,6 +66,7 @@ def clear
6666
# @param [Object] item the item to be removed from the queue
6767
# @return [Object] true if the item is found else false
6868
def delete(item)
69+
return false if empty?
6970
original_length = @length
7071
k = 1
7172
while k <= @length
@@ -120,7 +121,7 @@ def length
120121
#
121122
# @return [Object] the head of the queue or `nil` when empty
122123
def peek
123-
@queue[1]
124+
empty? ? nil : @queue[1]
124125
end
125126

126127
# @!macro [attach] priority_queue_method_pop
@@ -130,6 +131,7 @@ def peek
130131
#
131132
# @return [Object] the head of the queue or `nil` when empty
132133
def pop
134+
return nil if empty?
133135
max = @queue[1]
134136
swap(1, @length)
135137
@length -= 1
@@ -146,6 +148,7 @@ def pop
146148
#
147149
# @param [Object] item the item to insert onto the queue
148150
def push(item)
151+
raise ArgumentError.new('cannot enqueue nil') if item.nil?
149152
@length += 1
150153
@queue << item
151154
swim(@length)
@@ -168,7 +171,7 @@ def self.from_list(list, opts = {})
168171
queue
169172
end
170173

171-
protected
174+
private
172175

173176
# Exchange the values at the given indexes within the internal array.
174177
#
@@ -287,6 +290,7 @@ def pop
287290

288291
# @!macro priority_queue_method_push
289292
def push(item)
293+
raise ArgumentError.new('cannot enqueue nil') if item.nil?
290294
@queue.add(item)
291295
end
292296
alias_method :<<, :push

spec/concurrent/collection/non_concurrent_priority_queue_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@
215215
expect(subject.pop).to be_nil
216216
end
217217

218+
it 'returns nil when called multiple times while empty' do
219+
10.times do
220+
expect(subject.pop).to be nil
221+
end
222+
end
223+
218224
it 'is aliased as #deq' do
219225
10.times{|i| subject << i}
220226
expect(subject.deq).to eq 9
@@ -228,6 +234,12 @@
228234

229235
context '#push' do
230236

237+
it 'raises an exception when attempting to enqueue nil' do
238+
expect {
239+
subject.push(nil)
240+
}.to raise_error(ArgumentError)
241+
end
242+
231243
it 'adds the item to the queue' do
232244
subject.push(1)
233245
expect(subject).to include(1)

0 commit comments

Comments
 (0)