Class: Containers::Queue
- Inherits:
-
Object
- Object
- Containers::Queue
- Includes:
- Enumerable
- Defined in:
- lib/containers/queue.rb
Overview
rdoc
A Queue is a container that keeps elements in a first-in first-out (FIFO) order. Because of its
properties, it is often used as a buffer.
This implementation uses a doubly-linked list, guaranteeing O(1) complexity for all operations.
MIT License
Copyright (c) 2009 Kanwei Li
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
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 = Containers::Queue.new([1, 2, 3])
q.pop #=> 1
q.pop #=> 2
38 39 40 |
# File 'lib/containers/queue.rb', line 38 def initialize(ary=[]) @container = Containers::Deque.new(ary) end |
Instance Method Details
#each(&block) ⇒ Object
Iterate over the Queue in FIFO order.
85 86 87 |
# File 'lib/containers/queue.rb', line 85 def each(&block) @container.each_forward(&block) end |
#empty? ⇒ Boolean
Returns true if the queue is empty, false otherwise.
80 81 82 |
# File 'lib/containers/queue.rb', line 80 def empty? @container.empty? end |
#next ⇒ Object
Returns the next item from the queue but does not remove it.
q = Containers::Queue.new([1, 2, 3])
q.next #=> 1
q.size #=> 3
47 48 49 |
# File 'lib/containers/queue.rb', line 47 def next @container.front end |
#pop ⇒ Object
Removes the next item from the queue and returns it.
q = Containers::Queue.new([1, 2, 3])
q.pop #=> 1
q.size #=> 2
67 68 69 |
# File 'lib/containers/queue.rb', line 67 def pop @container.pop_front end |
#push(obj) ⇒ Object Also known as: <<
Adds an item to the queue.
q = Containers::Queue.new([1])
q.push(2)
q.pop #=> 1
q.pop #=> 2
57 58 59 |
# File 'lib/containers/queue.rb', line 57 def push(obj) @container.push_back(obj) end |
#size ⇒ Object
Return the number of items in the queue.
q = Containers::Queue.new([1, 2, 3])
q.size #=> 3
75 76 77 |
# File 'lib/containers/queue.rb', line 75 def size @container.size end |