Skip to content

Commit c7b954b

Browse files
committed
Updated YARD docs for PriorityQueue.
1 parent 1218284 commit c7b954b

File tree

2 files changed

+144
-13
lines changed

2 files changed

+144
-13
lines changed

.yardopts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--protected --private --embed-mixins --output-dir ./doc --markup markdown
1+
--protected --no-private --embed-mixins --output-dir ./doc --markup markdown

lib/concurrent/collection/priority_queue.rb

Lines changed: 143 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,60 @@
11
module Concurrent
22

3-
# @!macro [attach] priority_queue
3+
# @!macro [new] priority_queue
4+
#
5+
# A queue collection in which the elements are sorted based on their
6+
# comparison (spaceship) operator `<=>`. Items are added to the queue
7+
# at a position relative to their priority. On removal the element
8+
# with the "highest" priority is removed. By default the sort order is
9+
# from highest to lowest, but a lowest-to-highest sort order can be
10+
# set on construction.
11+
#
12+
# The API is based on the `Queue` class from the Ruby standard library.
13+
#
14+
# The pure Ruby implementation, `MutexPriorityQueue` uses a heap algorithm
15+
# stored in an array. The algorithm is based on the work of Robert Sedgewick
16+
# and Kevin Wayne.
17+
#
18+
# The JRuby native implementation is a thin wrapper around the standard
19+
# library `java.util.PriorityQueue`.
20+
#
21+
# When running under JRuby the class `PriorityQueue` extends `JavaPriorityQueue`.
22+
# When running under all other interpreters it extends `MutexPriorityQueue`.
23+
#
24+
# @note This implementation is *not* thread safe and performs no blocking.
425
#
5-
# @see http://ruby-doc.org/stdlib-2.0.0/libdoc/thread/rdoc/Queue.html
626
# @see http://en.wikipedia.org/wiki/Priority_queue
27+
# @see http://ruby-doc.org/stdlib-2.0.0/libdoc/thread/rdoc/Queue.html
28+
#
29+
# @see http://algs4.cs.princeton.edu/24pq/index.php#2.6
730
# @see http://algs4.cs.princeton.edu/24pq/MaxPQ.java.html
31+
#
32+
# @see http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html
833
class MutexPriorityQueue
934

10-
attr_reader :length
11-
alias_method :size, :length
12-
35+
# Create a new priority queue with no items.
36+
#
37+
# @param [Hash] opts the options for creating the queue
38+
# @option opts [Symbol] :order (:max) dictates the order in which items are
39+
# stored: from highest to lowest when `:max` or `:high`; from lowest to
40+
# highest when `:min` or `:low`
1341
def initialize(opts = {})
1442
order = opts.fetch(:order, :max)
15-
@comparator = [:min, :low].include?(order) ? 1 : -1
43+
@comparator = [:min, :low].include?(order) ? -1 : 1
1644
clear
1745
end
1846

47+
# Removes all of the elements from this priority queue.
1948
def clear
2049
@queue = [nil]
2150
@length = 0
2251
true
2352
end
2453

54+
# Deletes all items from `self` that are equal to `item`.
55+
#
56+
# @param [Object] item the item to be removed from the queue
57+
# @return [Object] true if the item is found else false
2558
def delete(item)
2659
original_length = @length
2760
k = 1
@@ -38,19 +71,44 @@ def delete(item)
3871
@length != original_length
3972
end
4073

74+
# Returns `true` if `self` contains no elements.
75+
#
76+
# @return [Boolean] true if there are no items in the queue else false
4177
def empty?
4278
size == 0
4379
end
4480

81+
# Returns `true` if the given item is present in `self` (that is, if any
82+
# element == `item`), otherwise returns false.
83+
#
84+
# @param [Object] item the item to search for
85+
#
86+
# @return [Boolean] true if the item is found else false
4587
def include?(item)
4688
@queue.include?(item)
4789
end
4890
alias_method :has_priority?, :include?
4991

92+
# The current length of the queue.
93+
#
94+
# @return [Fixnum] the number of items in the queue
95+
def length
96+
@length
97+
end
98+
alias_method :size, :length
99+
100+
# Retrieves, but does not remove, the head of this queue, or returns `nil`
101+
# if this queue is empty.
102+
#
103+
# @return [Object] the head of the queue or `nil` when empty
50104
def peek
51105
@queue[1]
52106
end
53107

108+
# Retrieves and removes the head of this queue, or returns `nil` if this
109+
# queue is empty.
110+
#
111+
# @return [Object] the head of the queue or `nil` when empty
54112
def pop
55113
max = @queue[1]
56114
swap(1, @length)
@@ -62,6 +120,9 @@ def pop
62120
alias_method :deq, :pop
63121
alias_method :shift, :pop
64122

123+
# Inserts the specified element into this priority queue.
124+
#
125+
# @param [Object] item the item to insert onto the queue
65126
def push(item)
66127
@length += 1
67128
@queue << item
@@ -71,6 +132,12 @@ def push(item)
71132
alias_method :<<, :push
72133
alias_method :enq, :push
73134

135+
# Create a new priority queue from the given list.
136+
#
137+
# @param [Enumerable] list the list to build the queue from
138+
# @param [Hash] opts the options for creating the queue
139+
#
140+
# @return [PriorityQueue] the newly created and populated queue
74141
def self.from_list(list, opts = {})
75142
queue = new(opts)
76143
list.each{|item| queue << item }
@@ -79,27 +146,53 @@ def self.from_list(list, opts = {})
79146

80147
protected
81148

149+
# Exchange the values at the given indexes within the internal array.
150+
#
151+
# @param [Integer] x the first index to swap
152+
# @param [Integer] y the second index to swap
153+
#
154+
# @!visibility private
82155
def swap(x, y)
83156
temp = @queue[x]
84157
@queue[x] = @queue[y]
85158
@queue[y] = temp
86159
end
87160

88-
def prioritize?(x, y)
161+
# Are the items at the given indexes ordered based on the priority
162+
# order specified at construction?
163+
#
164+
# @param [Integer] x the first index from which to retrieve a comparable value
165+
# @param [Integer] y the second index from which to retrieve a comparable value
166+
#
167+
# @return [Boolean] true if the two elements are in the correct priority order
168+
# else false
169+
#
170+
# @!visibility private
171+
def ordered?(x, y)
89172
(@queue[x] <=> @queue[y]) == @comparator
90173
end
91174

175+
# Percolate down to maintain heap invariant.
176+
#
177+
# @param [Integer] k the index at which to start the percolation
178+
#
179+
# @!visibility private
92180
def sink(k)
93181
while (j = (2 * k)) <= @length do
94-
j += 1 if j < @length && prioritize?(j, j+1)
95-
break unless prioritize?(k, j)
182+
j += 1 if j < @length && ! ordered?(j, j+1)
183+
break if ordered?(k, j)
96184
swap(k, j)
97185
k = j
98186
end
99187
end
100188

189+
# Percolate up to maintain heap invariant.
190+
#
191+
# @param [Integer] k the index at which to start the percolation
192+
#
193+
# @!visibility private
101194
def swim(k)
102-
while k > 1 && prioritize?(k/2, k) do
195+
while k > 1 && ! ordered?(k/2, k) do
103196
swap(k, k/2)
104197
k = k/2
105198
end
@@ -109,10 +202,14 @@ def swim(k)
109202
if RUBY_PLATFORM == 'java'
110203

111204
# @!macro priority_queue
112-
#
113-
# @see http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html
114205
class JavaPriorityQueue
115206

207+
# Create a new priority queue with no items.
208+
#
209+
# @param [Hash] opts the options for creating the queue
210+
# @option opts [Symbol] :order (:max) dictates the order in which items are
211+
# stored: from highest to lowest when `:max` or `:high`; from lowest to
212+
# highest when `:min` or `:low`
116213
def initialize(opts = {})
117214
order = opts.fetch(:order, :max)
118215
if [:min, :low].include?(order)
@@ -122,11 +219,16 @@ def initialize(opts = {})
122219
end
123220
end
124221

222+
# Removes all of the elements from this priority queue.
125223
def clear
126224
@queue.clear
127225
true
128226
end
129227

228+
# Deletes all items from `self` that are equal to `item`.
229+
#
230+
# @param [Object] item the item to be removed from the queue
231+
# @return [Object] true if the item is found else false
130232
def delete(item)
131233
found = false
132234
while @queue.remove(item) do
@@ -135,36 +237,65 @@ def delete(item)
135237
found
136238
end
137239

240+
# Returns `true` if `self` contains no elements.
241+
#
242+
# @return [Boolean] true if there are no items in the queue else false
138243
def empty?
139244
@queue.size == 0
140245
end
141246

247+
# Returns `true` if the given item is present in `self` (that is, if any
248+
# element == `item`), otherwise returns false.
249+
#
250+
# @param [Object] item the item to search for
251+
#
252+
# @return [Boolean] true if the item is found else false
142253
def include?(item)
143254
@queue.contains(item)
144255
end
145256
alias_method :has_priority?, :include?
146257

258+
# The current length of the queue.
259+
#
260+
# @return [Fixnum] the number of items in the queue
147261
def length
148262
@queue.size
149263
end
150264
alias_method :size, :length
151265

266+
# Retrieves, but does not remove, the head of this queue, or returns `nil`
267+
# if this queue is empty.
268+
#
269+
# @return [Object] the head of the queue or `nil` when empty
152270
def peek
153271
@queue.peek
154272
end
155273

274+
# Retrieves and removes the head of this queue, or returns `nil` if this
275+
# queue is empty.
276+
#
277+
# @return [Object] the head of the queue or `nil` when empty
156278
def pop
157279
@queue.poll
158280
end
159281
alias_method :deq, :pop
160282
alias_method :shift, :pop
161283

284+
# Inserts the specified element into this priority queue.
285+
#
286+
# @param [Object] item the item to insert onto the queue
162287
def push(item)
163288
@queue.add(item)
164289
end
165290
alias_method :<<, :push
166291
alias_method :enq, :push
167292

293+
# Create a new priority queue from the given list.
294+
#
295+
# @param [Enumerable] list the list to build the queue from
296+
# @param [Hash] opts the options for creating the queue
297+
#
298+
# @return [PriorityQueue] the newly created and populated queue
168299
def self.from_list(list, opts = {})
169300
queue = new(opts)
170301
list.each{|item| queue << item }

0 commit comments

Comments
 (0)