@@ -30,7 +30,7 @@ module Collection
30
30
# @see http://algs4.cs.princeton.edu/24pq/index.php#2.6
31
31
# @see http://algs4.cs.princeton.edu/24pq/MaxPQ.java.html
32
32
#
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
34
34
#
35
35
# @!visibility private
36
36
# @!macro internal_implementation_note
@@ -66,6 +66,7 @@ def clear
66
66
# @param [Object] item the item to be removed from the queue
67
67
# @return [Object] true if the item is found else false
68
68
def delete ( item )
69
+ return false if empty?
69
70
original_length = @length
70
71
k = 1
71
72
while k <= @length
@@ -120,7 +121,7 @@ def length
120
121
#
121
122
# @return [Object] the head of the queue or `nil` when empty
122
123
def peek
123
- @queue [ 1 ]
124
+ empty? ? nil : @queue [ 1 ]
124
125
end
125
126
126
127
# @!macro [attach] priority_queue_method_pop
@@ -130,6 +131,7 @@ def peek
130
131
#
131
132
# @return [Object] the head of the queue or `nil` when empty
132
133
def pop
134
+ return nil if empty?
133
135
max = @queue [ 1 ]
134
136
swap ( 1 , @length )
135
137
@length -= 1
@@ -146,6 +148,7 @@ def pop
146
148
#
147
149
# @param [Object] item the item to insert onto the queue
148
150
def push ( item )
151
+ raise ArgumentError . new ( 'cannot enqueue nil' ) if item . nil?
149
152
@length += 1
150
153
@queue << item
151
154
swim ( @length )
@@ -168,7 +171,7 @@ def self.from_list(list, opts = {})
168
171
queue
169
172
end
170
173
171
- protected
174
+ private
172
175
173
176
# Exchange the values at the given indexes within the internal array.
174
177
#
@@ -287,6 +290,7 @@ def pop
287
290
288
291
# @!macro priority_queue_method_push
289
292
def push ( item )
293
+ raise ArgumentError . new ( 'cannot enqueue nil' ) if item . nil?
290
294
@queue . add ( item )
291
295
end
292
296
alias_method :<< , :push
0 commit comments