@@ -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
0 commit comments