Class: Algorithmix::DataStructure::Generic::Queue
- Inherits:
-
Object
- Object
- Algorithmix::DataStructure::Generic::Queue
- Defined in:
- lib/algorithmix/data_structure/generic/queue.rb
Instance Method Summary collapse
-
#!=(queue) ⇒ true, false
Compares contents of the self object and that given as argument.
-
#&(queue) ⇒ Queue
Finds the intersection of the self object and queue given as argument.
-
#+(queue) ⇒ Queue
Concatenates contents of two queues.
-
#-(queue) ⇒ Queue
Finds the difference of the current queue and queue given as argument.
-
#<<(value) ⇒ Queue
Inserts a value at the end of the queue.
-
#<=>(queue) ⇒ Object
Compares contents of the self object and that given as argument.
-
#==(queue) ⇒ true, false
Compares contents of the self object and that given as argument.
-
#^(queue) ⇒ Queue
Finds the symmetric difference of the self object and queue given as argument.
-
#apply(&block) ⇒ Queue
Applies a function to each element of the queue.
-
#apply!(&block) ⇒ Object
Same as #map, but modifies the current object.
-
#assign(obj, copy: false) ⇒ Object
Assigns content of an object, to stack’s content, removing previous elements.
-
#clear ⇒ Object
Clears content of the queue.
-
#concat(queue) ⇒ Queue
Concatenates contents of two queues.
-
#concat!(queue) ⇒ Object
Same as #+, but modifies the current object.
-
#diff?(queue) ⇒ true, false
Compares contents of the self object and that given as argument.
-
#difference(queue) ⇒ Queue
Finds the difference of the current queue and queue given as argument.
-
#difference!(queue) ⇒ Object
Same as #-, but modifies the current queue.
-
#empty? ⇒ Boolean
Checks if the queue is empty.
-
#eql?(queue) ⇒ true, false
Compares contents of the self object and that given as argument.
-
#filter(&block) ⇒ Queue
Filters elements of the queue by given condition.
-
#filter!(&block) ⇒ Object
Same as #select, but modofies the current object.
-
#find_all(&block) ⇒ Queue
Filters elements of the queue by given condition.
-
#find_all!(&block) ⇒ Object
Same as #select, but modofies the current object.
-
#front ⇒ Object
Returns the front element of the queue, or nil if it’s empty.
-
#initialize(obj = nil, copy: false) ⇒ Queue
constructor
Creates a new queue.
-
#intersect(queue) ⇒ Queue
Finds the intersection of the self object and queue given as argument.
-
#intersect!(queue) ⇒ Object
Same as #&, but modifies the current object.
-
#length ⇒ Object
Returns number of elements in the queue.
-
#map(&block) ⇒ Queue
Applies a function to each element of the queue.
-
#map!(&block) ⇒ Object
Same as #map, but modifies the current object.
-
#merge(queue) ⇒ Queue
Concatenates contents of two queues.
-
#merge!(queue) ⇒ Object
Same as #+, but modifies the current object.
-
#pop ⇒ Object
Removes the front element of the queue.
-
#push(value) ⇒ Queue
Inserts a value at the end of the queue.
-
#select(&block) ⇒ Queue
Filters elements of the queue by given condition.
-
#select!(&block) ⇒ Object
Same as #select, but modofies the current object.
-
#size ⇒ Object
Returns number of elements in the queue.
-
#symmetric_difference(queue) ⇒ Queue
Finds the symmetric difference of the self object and queue given as argument.
-
#symmetric_difference!(queue) ⇒ Object
Same as #^, but modifies the current object.
-
#to_a ⇒ Object
Converts the queue to an array with elements.
-
#union(queue) ⇒ Queue
Unites contents of the self queue and queue given as argument.
-
#union!(queue) ⇒ Object
Same as #union, but modifies the current object.
-
#|(queue) ⇒ Queue
Unites contents of the self queue and queue given as argument.
Constructor Details
#initialize(obj = nil, copy: false) ⇒ Queue
Creates a new queue.
14 15 16 17 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 14 def initialize(obj = nil, copy: false) @container = [] obj.nil? ? nil : from_obj(obj, copy) end |
Instance Method Details
#!=(queue) ⇒ true, false
Compares contents of the self object and that given as argument.
92 93 94 95 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 92 def !=(queue) raise ArgumentError, "Undefined method Queue#!= for #{queue}:#{queue.class}" unless queue.is_a?(Queue) @container != queue.to_a end |
#&(queue) ⇒ Queue
Finds the intersection of the self object and queue given as argument.
178 179 180 181 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 178 def &(queue) raise ArgumentError, "Undefined method Queue#& for #{queue}:#{queue.class}" unless queue.is_a?(Queue) Queue.new(@container & queue.to_a) end |
#+(queue) ⇒ Queue
Concatenates contents of two queues.
119 120 121 122 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 119 def +(queue) raise ArgumentError, "Undefined method Queue#+ for #{queue}:#{queue.class}" unless queue.is_a?(Queue) Queue.new(@container + queue.to_a) end |
#-(queue) ⇒ Queue
Finds the difference of the current queue and queue given as argument.
201 202 203 204 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 201 def -(queue) raise ArgumentError, "Undefined method Queue#- for #{queue}:#{queue.class}" unless queue.is_a?(Queue) Queue.new(@container - queue.to_a) end |
#<<(value) ⇒ Queue
Inserts a value at the end of the queue.
38 39 40 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 38 def <<(value) push(value) end |
#<=>(queue) ⇒ Object
Compares contents of the self object and that given as argument.
110 111 112 113 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 110 def <=>(queue) raise ArgumentError, "Undefined method Queue#<=> for #{queue}:#{queue.class}" unless queue.is_a?(Queue) @container <=> queue.to_a end |
#==(queue) ⇒ true, false
Compares contents of the self object and that given as argument.
76 77 78 79 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 76 def ==(queue) raise ArgumentError, "Undefined method Queue#== for #{queue}:#{queue.class}" unless queue.is_a?(Queue) @container == queue.to_a end |
#^(queue) ⇒ Queue
Finds the symmetric difference of the self object and queue given as argument.
224 225 226 227 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 224 def ^(queue) raise ArgumentError, "Undefined method Queue#^ for #{queue}:#{queue.class}" unless queue.is_a?(Queue) Queue.new((@container | queue.to_a) - (@container & queue.to_a)) end |
#apply(&block) ⇒ Queue
Applies a function to each element of the queue.
301 302 303 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 301 def apply(&block) map(&block) end |
#apply!(&block) ⇒ Object
Same as #map, but modifies the current object.
306 307 308 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 306 def apply!(&block) map!(&block) end |
#assign(obj, copy: false) ⇒ Object
Assigns content of an object, to stack’s content, removing previous elements.
24 25 26 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 24 def assign(obj, copy: false) from_obj(obj, copy) end |
#clear ⇒ Object
Clears content of the queue.
247 248 249 250 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 247 def clear @container = [] self end |
#concat(queue) ⇒ Queue
Concatenates contents of two queues.
125 126 127 128 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 125 def concat(queue) raise ArgumentError, "Undefined method Queue#concat for #{queue}:#{queue.class}" unless queue.is_a?(Queue) self + queue end |
#concat!(queue) ⇒ Object
Same as #+, but modifies the current object
137 138 139 140 141 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 137 def concat!(queue) raise ArgumentError, "Undefined method Queue#concat! for #{queue}:#{queue.class}" unless queue.is_a?(Queue) @container += queue.to_a self end |
#diff?(queue) ⇒ true, false
Compares contents of the self object and that given as argument.
98 99 100 101 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 98 def diff?(queue) raise ArgumentError, "Undefined method Queue#diff? for #{queue}:#{queue.class}" unless queue.is_a?(Queue) self != queue end |
#difference(queue) ⇒ Queue
Finds the difference of the current queue and queue given as argument.
207 208 209 210 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 207 def difference(queue) raise ArgumentError, "Undefined method Queue#difference for #{queue}:#{queue.class}" unless queue.is_a?(Queue) self - queue end |
#difference!(queue) ⇒ Object
Same as #-, but modifies the current queue.
213 214 215 216 217 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 213 def difference!(queue) raise ArgumentError, "Undefined method Queue#difference! for #{queue}:#{queue.class}" unless queue.is_a?(Queue) @container -= queue.to_a self end |
#empty? ⇒ Boolean
Checks if the queue is empty.
67 68 69 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 67 def empty? @container.empty? end |
#eql?(queue) ⇒ true, false
Compares contents of the self object and that given as argument.
82 83 84 85 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 82 def eql?(queue) raise ArgumentError, "Undefined method Queue#eql? for #{queue}:#{queue.class}" unless queue.is_a?(Queue) self == queue end |
#filter(&block) ⇒ Queue
Filters elements of the queue by given condition.
267 268 269 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 267 def filter(&block) select(&block) end |
#filter!(&block) ⇒ Object
Same as #select, but modofies the current object.
272 273 274 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 272 def filter!(&block) select!(&block) end |
#find_all(&block) ⇒ Queue
Filters elements of the queue by given condition.
277 278 279 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 277 def find_all(&block) select(&block) end |
#find_all!(&block) ⇒ Object
Same as #select, but modofies the current object.
282 283 284 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 282 def find_all!(&block) select!(&block) end |
#front ⇒ Object
Returns the front element of the queue, or nil if it’s empty.
52 53 54 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 52 def front @container.first end |
#intersect(queue) ⇒ Queue
Finds the intersection of the self object and queue given as argument.
184 185 186 187 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 184 def intersect(queue) raise ArgumentError, "Undefined method Queue#intersect for #{queue}:#{queue.class}" unless queue.is_a?(Queue) self & queue end |
#intersect!(queue) ⇒ Object
Same as #&, but modifies the current object.
190 191 192 193 194 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 190 def intersect!(queue) raise ArgumentError, "Undefined method Queue#intersect! for #{queue}:#{queue.class}" unless queue.is_a?(Queue) @container &= queue.to_a self end |
#length ⇒ Object
Returns number of elements in the queue.
62 63 64 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 62 def length @container.size end |
#map(&block) ⇒ Queue
Applies a function to each element of the queue.
290 291 292 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 290 def map(&block) Queue.new(@container.map { |e| block.call(e)}) end |
#map!(&block) ⇒ Object
Same as #map, but modifies the current object.
295 296 297 298 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 295 def map!(&block) @container.map! { |e| block.call(e) } self end |
#merge(queue) ⇒ Queue
Concatenates contents of two queues.
131 132 133 134 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 131 def merge(queue) raise ArgumentError, "Undefined method Queue#merge for #{queue}:#{queue.class}" unless queue.is_a?(Queue) self + queue end |
#merge!(queue) ⇒ Object
Same as #+, but modifies the current object
144 145 146 147 148 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 144 def merge!(queue) raise ArgumentError, "Undefined method Queue#merge! for #{queue}:#{queue.class}" unless queue.is_a?(Queue) @container += queue.to_a self end |
#pop ⇒ Object
Removes the front element of the queue.
46 47 48 49 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 46 def pop raise EmptyContainerError, "The queue is empty." if @container.empty? @container.shift end |
#push(value) ⇒ Queue
Inserts a value at the end of the queue.
32 33 34 35 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 32 def push(value) @container << value self end |
#select(&block) ⇒ Queue
Filters elements of the queue by given condition.
256 257 258 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 256 def select(&block) Queue.new(@container.select { |e| block.call(e) }) end |
#select!(&block) ⇒ Object
Same as #select, but modofies the current object.
261 262 263 264 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 261 def select!(&block) @container.select! { |e| block.call(e) } self end |
#size ⇒ Object
Returns number of elements in the queue.
57 58 59 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 57 def size @container.size end |
#symmetric_difference(queue) ⇒ Queue
Finds the symmetric difference of the self object and queue given as argument.
230 231 232 233 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 230 def symmetric_difference(queue) raise ArgumentError, "Undefined method Queue#symmetric_difference for #{queue}:#{queue.class}" unless queue.is_a?(Queue) self ^ queue end |
#symmetric_difference!(queue) ⇒ Object
Same as #^, but modifies the current object.
236 237 238 239 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 236 def symmetric_difference!(queue) raise ArgumentError, "Undefined method Queue#symmetric_difference! for #{queue}:#{queue.class}" unless queue.is_a?(Queue) @container = (@container | queue.to_a) - (@container & queue.to_a) end |
#to_a ⇒ Object
Converts the queue to an array with elements.
242 243 244 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 242 def to_a @container end |
#union(queue) ⇒ Queue
Unites contents of the self queue and queue given as argument.
161 162 163 164 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 161 def union(queue) raise ArgumentError, "Undefined method Queue#union for #{queue}:#{queue.class}" unless queue.is_a?(Queue) self | queue end |
#union!(queue) ⇒ Object
Same as #union, but modifies the current object
167 168 169 170 171 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 167 def union!(queue) raise ArgumentError, "Undefined method Queue#union! for #{queue}:#{queue.class}" unless queue.is_a?(Queue) @container |= queue.to_a self end |
#|(queue) ⇒ Queue
Unites contents of the self queue and queue given as argument.
155 156 157 158 |
# File 'lib/algorithmix/data_structure/generic/queue.rb', line 155 def |(queue) raise ArgumentError, "Undefined method Queue#| for #{queue}:#{queue.class}" unless queue.is_a?(Queue) Queue.new(@container | queue.to_a) end |