Class: Mayak::Collections::Queue
- Inherits:
-
Object
- Object
- Mayak::Collections::Queue
- Extended by:
- T::Generic, T::Helpers, T::Sig
- Defined in:
- lib/mayak/collections/queue.rb
Constant Summary collapse
- Value =
type_member
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #dequeue ⇒ Object
- #empty? ⇒ Boolean
- #enqueue(element) ⇒ Object
-
#initialize(initial: []) ⇒ Queue
constructor
A new instance of Queue.
- #peak ⇒ Object
Constructor Details
#initialize(initial: []) ⇒ Queue
Returns a new instance of Queue.
28 29 30 31 32 33 |
# File 'lib/mayak/collections/queue.rb', line 28 def initialize(initial: []) @head = T.let(nil, T.nilable(Node[Value])) @tail = T.let(nil, T.nilable(Node[Value])) @size = T.let(0, Integer) initial.each { |element| enqueue(element) } end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
25 26 27 |
# File 'lib/mayak/collections/queue.rb', line 25 def size @size end |
Instance Method Details
#dequeue ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mayak/collections/queue.rb', line 56 def dequeue return if @size == 0 return if @head.nil? element = @head.value @head = @head.next @size -= 1 @tail = nil if @size == 0 element end |
#empty? ⇒ Boolean
68 69 70 |
# File 'lib/mayak/collections/queue.rb', line 68 def empty? @size == 0 end |
#enqueue(element) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mayak/collections/queue.rb', line 36 def enqueue(element) if @head.nil? @head = Node[Value].new(value: element, next: nil) @tail = @head @size += 1 else T.must(@tail).next = Node[Value].new(value: element, next: nil) @tail = T.must(@tail).next @size += 1 end end |
#peak ⇒ Object
49 50 51 52 53 |
# File 'lib/mayak/collections/queue.rb', line 49 def peak return if @head.nil? @head.value end |