Class: Algorithms::Containers::Queue
- Inherits:
-
Object
- Object
- Algorithms::Containers::Queue
- Includes:
- Enumerable
- Defined in:
- lib/containers/queue.rb
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterate over the Queue in FIFO order.
-
#empty? ⇒ Boolean
Returns true if the queue is empty, false otherwise.
-
#initialize(ary = []) ⇒ Queue
constructor
Create a new queue.
-
#next ⇒ Object
Returns the next item from the queue but does not remove it.
-
#pop ⇒ Object
Removes the next item from the queue and returns it.
-
#push(obj) ⇒ Object
(also: #<<)
Adds an item to the queue.
-
#size ⇒ Object
Return the number of items in the queue.
Constructor Details
#initialize(ary = []) ⇒ Queue
Create a new queue. Takes an optional array argument to initialize the queue.
q = Algorithms::Containers::Queue.new([1, 2, 3])
q.pop #=> 1
q.pop #=> 2
19 20 21 |
# File 'lib/containers/queue.rb', line 19 def initialize(ary=[]) @container = Deque.new(ary) end |
Instance Method Details
#each(&block) ⇒ Object
Iterate over the Queue in FIFO order.
66 67 68 |
# File 'lib/containers/queue.rb', line 66 def each(&block) @container.each_forward(&block) end |
#empty? ⇒ Boolean
Returns true if the queue is empty, false otherwise.
61 62 63 |
# File 'lib/containers/queue.rb', line 61 def empty? @container.empty? end |
#next ⇒ Object
Returns the next item from the queue but does not remove it.
q = Algorithms::Containers::Queue.new([1, 2, 3])
q.next #=> 1
q.size #=> 3
28 29 30 |
# File 'lib/containers/queue.rb', line 28 def next @container.front end |
#pop ⇒ Object
Removes the next item from the queue and returns it.
q = Algorithms::Containers::Queue.new([1, 2, 3])
q.pop #=> 1
q.size #=> 2
48 49 50 |
# File 'lib/containers/queue.rb', line 48 def pop @container.pop_front end |
#push(obj) ⇒ Object Also known as: <<
Adds an item to the queue.
q = Algorithms::Containers::Queue.new([1])
q.push(2)
q.pop #=> 1
q.pop #=> 2
38 39 40 |
# File 'lib/containers/queue.rb', line 38 def push(obj) @container.push_back(obj) end |
#size ⇒ Object
Return the number of items in the queue.
q = Algorithms::Containers::Queue.new([1, 2, 3])
q.size #=> 3
56 57 58 |
# File 'lib/containers/queue.rb', line 56 def size @container.size end |