Class: Algorithmix::DataStructure::Generic::Deque
- Inherits:
-
Object
- Object
- Algorithmix::DataStructure::Generic::Deque
- Defined in:
- lib/algorithmix/data_structure/generic/deque.rb
Instance Method Summary collapse
-
#!=(deque) ⇒ true, false
Compares contents of the deque and a deque given as argument.
-
#&(deque) ⇒ Deque
Finds intersection of contents of the deque and a deque given as argument.
-
#+(deque) ⇒ Deque
Concatenates contents of the deque and a deque given as argument.
-
#-(deque) ⇒ Deque
Finds difference of contents of the deque and a deque given as argument.
-
#<<(value) ⇒ Object
Inserts a new element at the end of the deque.
-
#<=>(deque) ⇒ Object
Compares contents of the deque and a deque given as argument.
-
#==(deque) ⇒ true, false
Compares contents of the deque and a deque given as argument.
-
#>>(value) ⇒ Object
Inserts a new element at the beginning of the deque.
-
#[](idx) ⇒ Object
Returns the element at given index.
-
#^(deque) ⇒ Deque
Finds symmetric difference of contents of the deque and a deque given as argument.
-
#apply(&block) ⇒ Deque
Applies a function to each element of the deque.
-
#apply!(&block) ⇒ Deque
Applies a function to each element of the deque.
-
#assign(obj, copy: false) ⇒ Object
Assigns content of an obj, to content of the deque.
-
#at(idx) ⇒ Object
Returns the element at given index.
-
#back ⇒ Object
Returns last element of the deque.
-
#clear ⇒ Object
Clears content of the deque.
-
#concat(deque) ⇒ Deque
Concatenates contents of the deque and a deque given as argument.
-
#concat!(deque) ⇒ Deque
Concatenates contents of the deque and a deque given as argument.
-
#diff?(deque) ⇒ true, false
Compares contents of the deque and a deque given as argument.
-
#difference(deque) ⇒ Deque
Finds difference of contents of the deque and a deque given as argument.
-
#difference!(deque) ⇒ Deque
Finds difference of contents of the deque and a deque given as argument.
-
#empty? ⇒ Boolean
Checks if the deque is empty.
-
#eql?(deque) ⇒ true, false
Compares contents of the deque and a deque given as argument.
-
#filter(&block) ⇒ Deque
Filters elements of the deque by given condition.
-
#filter!(&block) ⇒ Deque
Filters elements of the deque by given condition.
-
#find_all(&block) ⇒ Deque
Filters elements of the deque by given condition.
-
#find_all!(&block) ⇒ Deque
Filters elements of the deque by given condition.
-
#front ⇒ Object
Returns first element of the deque.
-
#initialize(obj = nil, copy: false) ⇒ Object
constructor
Creates a new deque.
-
#intersect(deque) ⇒ Deque
Finds intersection of contents of the deque and a deque given as argument.
-
#intersect!(deque) ⇒ Deque
Finds intersection of contents of the deque and a deque given as argument.
-
#length ⇒ Object
Returns size of the deque.
-
#map(&block) ⇒ Deque
Applies a function to each element of the deque.
-
#map!(&block) ⇒ Deque
Applies a function to each element of the deque.
-
#merge(deque) ⇒ Deque
Concatenates contents of the deque and a deque given as argument.
-
#merge!(deque) ⇒ Deque
Concatenates contents of the deque and a deque given as argument.
-
#pop_back ⇒ Object
Removes last element of the deque.
-
#pop_front ⇒ Object
Removes first element of the deque.
-
#push_back(value) ⇒ Object
Inserts a new element at the end of the deque.
-
#push_front(value) ⇒ Object
Inserts a new element at the beginning of the deque.
-
#select(&block) ⇒ Deque
Filters elements of the deque by given condition.
-
#select!(&block) ⇒ Deque
Filters elements of the deque by given condition.
-
#size ⇒ Object
Returns size of the deque.
- #symmetric_difference(deque) ⇒ Object
-
#symmetric_difference!(deque) ⇒ Deque
Finds symmetric difference of contents of the deque and a deque given as argument.
-
#to_a ⇒ Object
Converts content of the deque to an array witl elements.
-
#union(deque) ⇒ Deque
Unites contents of the deque and a deque given as argument.
-
#union!(deque) ⇒ Deque
Unites contents of the deque and a deque given as argument.
-
#|(deque) ⇒ Deque
Unites contents of the deque and a deque given as argument.
Constructor Details
#initialize(obj = nil, copy: false) ⇒ Object
Creates a new deque.
15 16 17 18 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 15 def initialize(obj = nil, copy: false) @container = [] obj.nil? ? nil : from_obj(obj, copy) end |
Instance Method Details
#!=(deque) ⇒ true, false
Compares contents of the deque and a deque given as argument.
135 136 137 138 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 135 def !=(deque) raise ArgumentError, "Undefined method Deque#!= for #{deque}:#{deque.class}" unless deque.is_a?(Deque) @container != deque.to_a end |
#&(deque) ⇒ Deque
Finds intersection of contents of the deque and a deque given as argument.
234 235 236 237 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 234 def &(deque) raise ArgumentError, "Undefined method Deque#& for #{deque}:#{deque.class}" unless deque.is_a?(Deque) Deque.new(@container & deque.to_a) end |
#+(deque) ⇒ Deque
Concatenates contents of the deque and a deque given as argument.
163 164 165 166 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 163 def +(deque) raise ArgumentError, "Undefined method Deque#+ for #{deque}:#{deque.class}" unless deque.is_a?(Deque) Deque.new(@container + deque.to_a) end |
#-(deque) ⇒ Deque
Finds difference of contents of the deque and a deque given as argument.
261 262 263 264 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 261 def -(deque) raise ArgumentError, "Undefined method Deque#- for #{deque}:#{deque.class}" unless deque.is_a?(Deque) Deque.new(@container - deque.to_a) end |
#<<(value) ⇒ Object
Inserts a new element at the end of the deque.
54 55 56 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 54 def <<(value) push_back(value) end |
#<=>(deque) ⇒ Object
Compares contents of the deque and a deque given as argument.
153 154 155 156 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 153 def <=>(deque) raise ArgumentError, "Undefined method Deque#<=> for #{deque}:#{deque.class}" unless deque.is_a?(Deque) @container <=> deque.to_a end |
#==(deque) ⇒ true, false
Compares contents of the deque and a deque given as argument.
119 120 121 122 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 119 def ==(deque) raise ArgumentError, "Undefined method Deque#== for #{deque}:#{deque.class}" unless deque.is_a?(Deque) @container == deque.to_a end |
#>>(value) ⇒ Object
Inserts a new element at the beginning of the deque.
40 41 42 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 40 def >>(value) push_front(value) end |
#[](idx) ⇒ Object
Returns the element at given index.
105 106 107 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 105 def [](idx) @container[idx] end |
#^(deque) ⇒ Deque
Finds symmetric difference of contents of the deque and a deque given as argument.
288 289 290 291 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 288 def ^(deque) raise ArgumentError, "Undefined method Deque#^ for #{deque}:#{deque.class}" unless deque.is_a?(Deque) Deque.new((@container | deque.to_a) - (@container & deque.to_a)) end |
#apply(&block) ⇒ Deque
Applies a function to each element of the deque.
378 379 380 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 378 def apply(&block) map(&block) end |
#apply!(&block) ⇒ Deque
Applies a function to each element of the deque.
383 384 385 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 383 def apply!(&block) map!(&block) end |
#assign(obj, copy: false) ⇒ Object
Assigns content of an obj, to content of the deque.
26 27 28 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 26 def assign(obj, copy: false) from_obj(obj, copy) end |
#at(idx) ⇒ Object
Returns the element at given index.
110 111 112 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 110 def at(idx) @container[idx] end |
#back ⇒ Object
Returns last element of the deque.
82 83 84 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 82 def back @container.last end |
#clear ⇒ Object
Clears content of the deque.
318 319 320 321 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 318 def clear @container = [] self end |
#concat(deque) ⇒ Deque
Concatenates contents of the deque and a deque given as argument.
169 170 171 172 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 169 def concat(deque) raise ArgumentError, "Undefined method Deque#concat for #{deque}:#{deque.class}" unless deque.is_a?(Deque) self + deque end |
#concat!(deque) ⇒ Deque
Concatenates contents of the deque and a deque given as argument.
185 186 187 188 189 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 185 def concat!(deque) raise ArgumentError, "Undefined method Deque#concat! for #{deque}:#{deque.class}" unless deque.is_a?(Deque) @container += deque.to_a self end |
#diff?(deque) ⇒ true, false
Compares contents of the deque and a deque given as argument.
141 142 143 144 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 141 def diff?(deque) raise ArgumentError, "Undefined method Deque#diff? for #{deque}:#{deque.class}" unless deque.is_a?(Deque) self != deque end |
#difference(deque) ⇒ Deque
Finds difference of contents of the deque and a deque given as argument.
267 268 269 270 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 267 def difference(deque) raise ArgumentError, "Undefined method Deque#difference for #{deque}:#{deque.class}" unless deque.is_a?(Deque) self - deque end |
#difference!(deque) ⇒ Deque
Finds difference of contents of the deque and a deque given as argument.
277 278 279 280 281 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 277 def difference!(deque) raise ArgumentError, "Undefined method Deque#difference! for #{deque}:#{deque.class}" unless deque.is_a?(Deque) @container -= deque.to_a self end |
#empty? ⇒ Boolean
Checks if the deque is empty.
87 88 89 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 87 def empty? @container.empty? end |
#eql?(deque) ⇒ true, false
Compares contents of the deque and a deque given as argument.
125 126 127 128 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 125 def eql?(deque) raise ArgumentError, "Undefined method Deque#eql? for #{deque}:#{deque.class}" unless deque.is_a?(Deque) self == deque end |
#filter(&block) ⇒ Deque
Filters elements of the deque by given condition.
341 342 343 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 341 def filter(&block) select(&block) end |
#filter!(&block) ⇒ Deque
Filters elements of the deque by given condition.
346 347 348 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 346 def filter!(&block) select!(&block) end |
#find_all(&block) ⇒ Deque
Filters elements of the deque by given condition.
351 352 353 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 351 def find_all(&block) select(&block) end |
#find_all!(&block) ⇒ Deque
Filters elements of the deque by given condition.
356 357 358 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 356 def find_all!(&block) select!(&block) end |
#front ⇒ Object
Returns first element of the deque.
77 78 79 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 77 def front @container.first end |
#intersect(deque) ⇒ Deque
Finds intersection of contents of the deque and a deque given as argument.
240 241 242 243 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 240 def intersect(deque) raise ArgumentError, "Undefined method Deque#intersect for #{deque}:#{deque.class}" unless deque.is_a?(Deque) self & deque end |
#intersect!(deque) ⇒ Deque
Finds intersection of contents of the deque and a deque given as argument.
250 251 252 253 254 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 250 def intersect!(deque) raise ArgumentError, "Undefined method Deque#intersect! for #{deque}:#{deque.class}" unless deque.is_a?(Deque) @container &= deque.to_a self end |
#length ⇒ Object
Returns size of the deque.
97 98 99 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 97 def length @container.size end |
#map(&block) ⇒ Deque
Applies a function to each element of the deque.
364 365 366 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 364 def map(&block) Deque.new(@container.map { |e| block.call(e)}) end |
#map!(&block) ⇒ Deque
Applies a function to each element of the deque.
372 373 374 375 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 372 def map!(&block) @container.map! { |e| block.call(e)} self end |
#merge(deque) ⇒ Deque
Concatenates contents of the deque and a deque given as argument.
175 176 177 178 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 175 def merge(deque) raise ArgumentError, "Undefined method Deque#merge for #{deque}:#{deque.class}" unless deque.is_a?(Deque) self + deque end |
#merge!(deque) ⇒ Deque
Concatenates contents of the deque and a deque given as argument.
196 197 198 199 200 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 196 def merge!(deque) raise ArgumentError, "Undefined method Deque#merge! for #{deque}:#{deque.class}" unless deque.is_a?(Deque) @container += deque.to_a self end |
#pop_back ⇒ Object
Removes last element of the deque.
71 72 73 74 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 71 def pop_back raise EmptyContainerError, "The deque is empty." if @container.empty? @container.pop end |
#pop_front ⇒ Object
Removes first element of the deque.
62 63 64 65 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 62 def pop_front raise EmptyContainerError, "The deque is empty." if @container.empty? @container.shift end |
#push_back(value) ⇒ Object
Inserts a new element at the end of the deque.
48 49 50 51 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 48 def push_back(value) @container << value self end |
#push_front(value) ⇒ Object
Inserts a new element at the beginning of the deque.
34 35 36 37 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 34 def push_front(value) @container.unshift(value) self end |
#select(&block) ⇒ Deque
Filters elements of the deque by given condition.
327 328 329 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 327 def select(&block) Deque.new( @container.select { |e| block.call(e)}) end |
#select!(&block) ⇒ Deque
Filters elements of the deque by given condition.
335 336 337 338 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 335 def select!(&block) @container.select! { |e| block.call(e)} self end |
#size ⇒ Object
Returns size of the deque.
92 93 94 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 92 def size @container.size end |
#symmetric_difference(deque) ⇒ Object
294 295 296 297 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 294 def symmetric_difference(deque) raise ArgumentError, "Undefined method Deque#symmetric_difference for #{deque}:#{deque.class}" unless deque.is_a?(Deque) self ^ deque end |
#symmetric_difference!(deque) ⇒ Deque
Finds symmetric difference of contents of the deque and a deque given as argument.
304 305 306 307 308 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 304 def symmetric_difference!(deque) raise ArgumentError, "Undefined method Deque#symmetric_difference! for #{deque}:#{deque.class}" unless deque.is_a?(Deque) @container = (@container | deque.to_a) - (@container & deque.to_a) self end |
#to_a ⇒ Object
Converts content of the deque to an array witl elements.
311 312 313 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 311 def to_a @container end |
#union(deque) ⇒ Deque
Unites contents of the deque and a deque given as argument.
213 214 215 216 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 213 def union(deque) raise ArgumentError, "Undefined method Deque#union for #{deque}:#{deque.class}" unless deque.is_a?(Deque) self | deque end |
#union!(deque) ⇒ Deque
Unites contents of the deque and a deque given as argument.
223 224 225 226 227 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 223 def union!(deque) raise ArgumentError, "Undefined method Deque#union! for #{deque}:#{deque.class}" unless deque.is_a?(Deque) @container |= deque.to_a self end |
#|(deque) ⇒ Deque
Unites contents of the deque and a deque given as argument.
207 208 209 210 |
# File 'lib/algorithmix/data_structure/generic/deque.rb', line 207 def |(deque) raise ArgumentError, "Undefined method Deque#| for #{deque}:#{deque.class}" unless deque.is_a?(Deque) Deque.new(@container | deque.to_a) end |