Skip to content

Commit 50aa2fe

Browse files
committed
🐛 Fix issue #2.
1 parent 8f3fa0d commit 50aa2fe

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

History.md

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# CHANGE HISTORY
22

3+
## 2.1.0 / 2014-03-03
4+
5+
Ostatnicky has found a bug! As it turns out both #deq and #shift were
6+
aliased incorrectly to `push`, when they should have been to `pop`.
7+
With this release that has been fixes, but we have modified `shift`
8+
to instead return the lowest priority item, which is a more polymorphic
9+
definition with its use in Array. In addition we have added `peek` as
10+
an alias for `top`, and added `bottom`, which it the opposite of `top`.
11+
12+
Changes:
13+
14+
* Fix `deq` as alias of `pop`, not `push`.
15+
* Fix `shift` to be like `pop` but opposite priority.
16+
* Add `peek` as alias of `top`.
17+
* Add `bottom` method as opposite of `top`.
18+
19+
320
## 2.0.2 / 2011-10-29
421

522
It's been one of those days. I went to to get a wash cloth for the shower,

lib/pqueue.rb

+33-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
class PQueue
1919

2020
#
21-
VERSION = "2.0.0" #:erb: VERSION = "<%= version %>"
21+
VERSION = "2.1.0" #:erb: VERSION = "<%= version %>"
2222

2323
#
2424
# Returns a new priority queue.
@@ -78,17 +78,17 @@ def push(v)
7878
end
7979

8080
#
81-
# Alias of #push.
81+
# Traditional alias for #push.
8282
#
83-
alias :<< :push
83+
alias enq push
8484

8585
#
8686
# Alias of #push.
8787
#
88-
alias enq push
88+
alias :<< :push
8989

9090
#
91-
# Return the element with the highest priority and remove it from
91+
# Get the element with the highest priority and remove it from
9292
# the queue.
9393
#
9494
# The highest priority is determined by the block given at instantiation
@@ -104,14 +104,24 @@ def pop
104104
end
105105

106106
#
107-
# Alias of #push.
107+
# Traditional alias for #pop.
108108
#
109-
alias shift push
109+
alias deq pop
110110

111+
# Get the element with the lowest priority and remove it from
112+
# the queue.
113+
#
114+
# The lowest priority is determined by the block given at instantiation
115+
# time.
111116
#
112-
# Alias of #pop.
117+
# The deletion time is O(log n), with n is the size of the queue.
118+
#
119+
# Return nil if the queue is empty.
113120
#
114-
alias deq push
121+
def shift
122+
return nil if empty?
123+
@que.shift
124+
end
115125

116126
#
117127
# Returns the element with the highest priority, but
@@ -122,6 +132,20 @@ def top
122132
return @que.last
123133
end
124134

135+
#
136+
# Traditional alias for #top.
137+
#
138+
alias peek top
139+
140+
#
141+
# Returns the element with the lowest priority, but
142+
# does not remove it from the queue.
143+
#
144+
def bottom
145+
return nil if empty?
146+
return @que.first
147+
end
148+
125149
#
126150
# Add more than one element at the same time. See #push.
127151
#

0 commit comments

Comments
 (0)