TheAlgorithms-Ruby/data_structures/queues/queue.rb

107 lines
2.3 KiB
Ruby
Raw Permalink Normal View History

# A queue is like a waiting list.
2020-12-28 00:19:28 +01:00
# Imagine you are waiting in line to buy the latest Android product
# or getting a parking ticket. These are queues!
#
#
# 1. An array can behave like a Queue if you use the right methods.
#
# These methods are:
2020-12-28 00:19:28 +01:00
# - unshift: when you unshift, you are adding one item to the queue
# - pop
2020-12-28 05:55:52 +01:00
#
class ArrayQueue
def initialize(queue = [])
@queue = queue
end
attr_accessor :queue
def add(item)
queue.unshift(item)
end
def pop
queue.pop
end
def peek
queue[-1]
end
end
queue = ArrayQueue.new
queue.add(3)
queue.add(4)
queue.add(5)
puts queue.inspect
# => #<ArrayQueue:0x00007fc78200f5e0 @queue=[5, 4, 3]>
queue.pop
puts queue.inspect
# => #<ArrayQueue:0x00007fc78200f5e0 @queue=[5, 4]>
puts(queue.peek)
# => 4
#
#
# 2. Ruby Concurrent Queue
# Ruby has a proper thread-safe, blocking, Queue class.
# You can use this queue for coordinating work in a multi-threaded program.
#
2020-12-28 00:19:28 +01:00
# Reference: https://ruby-doc.org/core-2.5.0/Queue.html
#
2020-12-28 05:55:52 +01:00
queue = Queue.new
2020-12-28 05:55:52 +01:00
queue << 1
queue << 2
queue << 3
2020-12-28 05:55:52 +01:00
queue.pop
# 1
2020-12-28 05:55:52 +01:00
queue.pop
# 2
# If the queue is empty, calling pop will put your current
# thread to sleep & wait until something is added to the queue.
#
#
2020-12-28 00:19:28 +01:00
# 3. How to Use a Ruby SizedQueue
# A sized queue is the same as a regular queue but with a size limit.
#
2020-12-28 00:19:28 +01:00
# Reference: https://ruby-doc.org/core-2.5.0/SizedQueue.html
#
2020-12-28 05:55:52 +01:00
queue = SizedQueue.new(5)
# When the queue is full, the push (same as <<) operation
# will suspend the current thread until an item is taken off the queue.
2020-12-28 05:55:52 +01:00
queue.push(:oranges)
queue.push(:apples)
queue.push(:blue)
queue.push(:orange)
queue.push(:green)
# At this point, the SizedQueue is full
2020-12-28 05:55:52 +01:00
queue.push(:throw_expection)
# data_structures/queues/queue.rb:81:in `push': No live threads left. Deadlock? (fatal)
# 1 threads, 1 sleeps current:0x00007ff54f407130 main thread:0x00007ff54f407130
# * #<Thread:0x00007ff54f86ef38 sleep_forever>
# rb_thread_t:0x00007ff54f407130 native:0x000000010dd24dc0 int:0
# data_structures/queues/queue.rb:81:in `push'
# data_structures/queues/queue.rb:81:in `<main>'
# from data_structures/queues/queue.rb:81:in `<main>'
# You can choose to raise an exception, passing true as an argument as follows:
2020-12-28 05:55:52 +01:00
queue.push(:throw_expection, true)
# data_structures/queues/queue.rb:83:in `push': queue full (ThreadError)
# from data_structures/queues/queue.rb:83:in `<main>'