Class: Algorithmix::DataStructure::Generic::Stack
- Inherits:
-
Object
- Object
- Algorithmix::DataStructure::Generic::Stack
- Defined in:
- lib/algorithmix/data_structure/generic/stack.rb
Instance Method Summary collapse
-
#!=(stack) ⇒ true, false
Compares the self stack with given as argument stack.
-
#&(stack) ⇒ Stack
Finds intersection of contents of the self stack and stack given as argument, and returns a new stack with the result.
-
#+(stack) ⇒ Stack
Merges contents of the self stack and stack given as argument, without removing repetitions from both stacks.
-
#-(stack) ⇒ Stack
Finds difference of the self stack object and that given as argument.
-
#<<(value) ⇒ Stack
Inserts a new element at the top of the stack.
-
#<=>(stack) ⇒ Object
Compares the self stack with given as argument stck.
-
#==(stack) ⇒ true, false
Compares the self stack with given as argument stack.
-
#^(stack) ⇒ Stack
Finds the symmetric difference of the stack and that given as argument.
-
#apply(&block) ⇒ Stack
Applies a function to each element of the stack.
-
#apply!(&block) ⇒ Object
Same as #map, but the result is kept in the current object.
-
#assign(obj, copy: false) ⇒ Stack
Copies the content of an object to the content of the stack, removing previous elements inserted in it.
-
#clear ⇒ Object
Clears content of current stack.
-
#concat(stack) ⇒ Stack
Merges contents of the self stack and stack given as argument, without removing repetitions from both stacks.
-
#concat!(stack) ⇒ Stack
Merges contents of the self stack, and stack given as argument.
-
#diff?(stack) ⇒ true, false
Compares the self stack with given as argument stack.
-
#difference(stack) ⇒ Stack
Finds difference of the self stack object and that given as argument.
-
#difference!(stack) ⇒ Stack
As difference, finds the difference of the stack and that given as argument, but modifies self object.
-
#empty? ⇒ Boolean
Checks if the stack contains any elements.
-
#eql?(stack) ⇒ true, false
Compares the self stack with given as argument stack.
-
#filter(&block) ⇒ Stack
Filters elements of the stack, by given condition.
-
#filter!(&block) ⇒ Object
Same as #select, but modifying the self object.
-
#find_all(&block) ⇒ Stack
Filters elements of the stack, by given condition.
-
#find_all!(&block) ⇒ Object
Same as #select, but modifying the self object.
-
#initialize(obj = nil, copy: false) ⇒ Stack
constructor
Creates a new stack.
-
#intersect(stack) ⇒ Stack
Finds intersection of contents of the self stack and stack given as argument, and returns a new stack with the result.
-
#intersect!(stack) ⇒ Object
Like intersect, finds intersection of the self stack object and stack given as argument, but modifies the object to which operation was called.
-
#length ⇒ Object
Returns the number of elements in the stack.
-
#map(&block) ⇒ Stack
Applies a function to each element of the stack.
-
#map!(&block) ⇒ Object
Same as #map, but the result is kept in the current object.
-
#merge(stack) ⇒ Stack
Merges contents of the self stack and stack given as argument, without removing repetitions from both stacks.
-
#merge!(stack) ⇒ Stack
Merges contents of the self stack, and stack given as argument.
-
#pop ⇒ Object
Removes the top element of the stack.
-
#push(value) ⇒ Stack
Inserts a new element at the top of the stack.
-
#select(&block) ⇒ Stack
Filters elements of the stack, by given condition.
-
#select!(&block) ⇒ Object
Same as #select, but modifying the self object.
-
#size ⇒ Object
Returns the number of elements in the stack.
-
#symmetric_difference(stack) ⇒ Stack
Finds the symmetric difference of the stack and that given as argument.
-
#symmetric_difference!(stack) ⇒ Stack
Finds the symmetric difference of the self object and stack given as argument.
-
#to_a ⇒ Object
Returns current object as array.
-
#top ⇒ Object
Returns the top element of the stack, without removing it.
-
#union(stack) ⇒ Stack
Finds union of the self object and stack given as argument.
-
#union!(stack) ⇒ Stack
Like #|, finds the union of the self object and the stack given as argument, but keeps the result in the current stack.
-
#|(stack) ⇒ Stack
Finds union of the self object and stack given as argument.
Constructor Details
#initialize(obj = nil, copy: false) ⇒ Stack
Creates a new stack.
13 14 15 16 17 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 13 def initialize(obj = nil, copy: false) @container = [] obj.nil? ? nil : from_obj(obj, copy) end |
Instance Method Details
#!=(stack) ⇒ true, false
Compares the self stack with given as argument stack.
88 89 90 91 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 88 def !=(stack) raise ArgumentError, "Undefined method Stack#!= for #{stack}:#{stack.class}" unless stack.is_a?(Stack) @container != stack.to_a end |
#&(stack) ⇒ Stack
Finds intersection of contents of the self stack and stack given as argument, and returns a new stack with the result.
157 158 159 160 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 157 def &(stack) raise ArgumentError, "Undefined method Stack#& for #{stack}:#{stack.class}" unless stack.is_a?(Stack) Stack.new(@container & stack.to_a) end |
#+(stack) ⇒ Stack
Merges contents of the self stack and stack given as argument, without removing repetitions from both stacks.
115 116 117 118 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 115 def +(stack) raise ArgumentError, "Undefined method Stack#+ for #{stack}:#{stack.class}" unless stack.is_a?(Stack) Stack.new(@container + stack.to_a) end |
#-(stack) ⇒ Stack
Finds difference of the self stack object and that given as argument.
181 182 183 184 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 181 def -(stack) raise ArgumentError, "Undefined method Stack#- for #{stack}:#{stack.class}" unless stack.is_a?(Stack) Stack.new(@container - stack.to_a) end |
#<<(value) ⇒ Stack
Inserts a new element at the top of the stack.
38 39 40 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 38 def <<(value) push(value) end |
#<=>(stack) ⇒ Object
Compares the self stack with given as argument stck.
105 106 107 108 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 105 def <=>(stack) raise ArgumentError, "Undefined method Stack#<=> for #{stack}:#{stack.class}" unless stack.is_a?(Stack) @container <=> stack.to_a end |
#==(stack) ⇒ true, false
Compares the self stack with given as argument stack.
76 77 78 79 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 76 def ==(stack) raise ArgumentError, "Undefined method Stack#== for #{stack}:#{stack.class}" unless stack.is_a?(Stack) @container == stack.to_a end |
#^(stack) ⇒ Stack
Finds the symmetric difference of the stack and that given as argument.
237 238 239 240 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 237 def ^(stack) raise ArgumentError, "Undefined method Stack#^ for #{stack}:#{stack.class}" unless stack.is_a?(Stack) Stack.new((@container | stack.to_a) - (@container & stack.to_a)) end |
#apply(&block) ⇒ Stack
Applies a function to each element of the stack.
321 322 323 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 321 def apply(&block) map(&block) end |
#apply!(&block) ⇒ Object
Same as #map, but the result is kept in the current object.
326 327 328 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 326 def apply!(&block) map!(&block) end |
#assign(obj, copy: false) ⇒ Stack
Copies the content of an object to the content of the stack, removing previous elements inserted in it.
24 25 26 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 24 def assign(obj, copy: false) from_obj(obj, copy) end |
#clear ⇒ Object
Clears content of current stack.
265 266 267 268 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 265 def clear @container = [] self end |
#concat(stack) ⇒ Stack
Merges contents of the self stack and stack given as argument, without removing repetitions from both stacks.
139 140 141 142 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 139 def concat(stack) raise ArgumentError, "Undefined method Stack#concat for #{stack}:#{stack.class}" unless stack.is_a?(Stack) self + stack end |
#concat!(stack) ⇒ Stack
Merges contents of the self stack, and stack given as argument. Modifies self object.
145 146 147 148 149 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 145 def concat!(stack) raise ArgumentError, "Undefined method Stack#concat! for #{stack}:#{stack.class}" unless stack.is_a?(Stack) @container += stack.to_a self end |
#diff?(stack) ⇒ true, false
Compares the self stack with given as argument stack.
94 95 96 97 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 94 def diff?(stack) raise ArgumentError, "Undefined method Stack#diff? for #{stack}:#{stack.class}" unless stack.is_a?(Stack) self != stack end |
#difference(stack) ⇒ Stack
Finds difference of the self stack object and that given as argument.
187 188 189 190 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 187 def difference(stack) raise ArgumentError, "Undefined method Stack#difference for #{stack}:#{stack.class}" unless stack.is_a?(Stack) self - stack end |
#difference!(stack) ⇒ Stack
As difference, finds the difference of the stack and that given as argument, but modifies self object.
198 199 200 201 202 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 198 def difference!(stack) raise ArgumentError, "Undefined method Stack#difference! for #{stack}:#{stack.class}" unless stack.is_a?(Stack) @container -= stack.to_a self end |
#empty? ⇒ Boolean
Checks if the stack contains any elements.
68 69 70 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 68 def empty? @container.empty? end |
#eql?(stack) ⇒ true, false
Compares the self stack with given as argument stack.
82 83 84 85 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 82 def eql?(stack) raise ArgumentError, "Undefined method Stack#eql? for #{stack}:#{stack.class}" unless stack.is_a?(Stack) self == stack end |
#filter(&block) ⇒ Stack
Filters elements of the stack, by given condition.
285 286 287 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 285 def filter(&block) select(&block) end |
#filter!(&block) ⇒ Object
Same as #select, but modifying the self object.
290 291 292 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 290 def filter!(&block) select!(&block) end |
#find_all(&block) ⇒ Stack
Filters elements of the stack, by given condition.
295 296 297 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 295 def find_all(&block) select(&block) end |
#find_all!(&block) ⇒ Object
Same as #select, but modifying the self object.
300 301 302 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 300 def find_all!(&block) select!(&block) end |
#intersect(stack) ⇒ Stack
Finds intersection of contents of the self stack and stack given as argument, and returns a new stack with the result.
163 164 165 166 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 163 def intersect(stack) raise ArgumentError, "Undefined method Stack#intersect for #{stack}:#{stack.class}" unless stack.is_a?(Stack) self & stack end |
#intersect!(stack) ⇒ Object
Like intersect, finds intersection of the self stack object and stack given as argument, but modifies the object to which operation was called.
170 171 172 173 174 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 170 def intersect!(stack) raise ArgumentError, "Undefined method Stack#intersect! for #{stack}:#{stack.class}" unless stack.is_a?(Stack) @container &= stack.to_a self end |
#length ⇒ Object
Returns the number of elements in the stack.
57 58 59 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 57 def length @container.size end |
#map(&block) ⇒ Stack
Applies a function to each element of the stack.
308 309 310 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 308 def map(&block) Stack.new(@container.map { |e| block.call(e)}) end |
#map!(&block) ⇒ Object
Same as #map, but the result is kept in the current object.
315 316 317 318 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 315 def map!(&block) @container.map! { |e| block.call(e) } self end |
#merge(stack) ⇒ Stack
Merges contents of the self stack and stack given as argument, without removing repetitions from both stacks.
121 122 123 124 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 121 def merge(stack) raise ArgumentError, "Undefined method Stack#merge for #{stack}:#{stack.class}" unless stack.is_a?(Stack) self + stack end |
#merge!(stack) ⇒ Stack
Merges contents of the self stack, and stack given as argument. Modifies self object.
132 133 134 135 136 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 132 def merge!(stack) raise ArgumentError, "Undefined method Stack#merge! for #{stack}:#{stack.class}" unless stack.is_a?(Stack) @container += stack.to_a self end |
#pop ⇒ Object
Removes the top element of the stack.
46 47 48 49 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 46 def pop raise EmptyContainerError, "The stack is empty." if @container.empty? @container.pop end |
#push(value) ⇒ Stack
Inserts a new element at the top of the stack.
32 33 34 35 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 32 def push(value) @container << value self end |
#select(&block) ⇒ Stack
Filters elements of the stack, by given condition.
274 275 276 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 274 def select(&block) Stack.new(@container.select { |e| block.call(e) }) end |
#select!(&block) ⇒ Object
Same as #select, but modifying the self object.
279 280 281 282 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 279 def select!(&block) @container.select! { |e| block.call(e)} self end |
#size ⇒ Object
Returns the number of elements in the stack.
52 53 54 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 52 def size @container.size end |
#symmetric_difference(stack) ⇒ Stack
Finds the symmetric difference of the stack and that given as argument.
243 244 245 246 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 243 def symmetric_difference(stack) raise ArgumentError, "Undefined method Stack#symmetric_difference for #{stack}:#{stack.class}" unless stack.is_a?(Stack) self ^ stack end |
#symmetric_difference!(stack) ⇒ Stack
Finds the symmetric difference of the self object and stack given as argument.
253 254 255 256 257 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 253 def symmetric_difference!(stack) raise ArgumentError, "Undefined method Stack#symmetric_difference! for #{stack}:#{stack.class}" unless stack.is_a?(Stack) @container = (@container | stack.to_a) - (@container & stack.to_a) self end |
#to_a ⇒ Object
Returns current object as array.
260 261 262 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 260 def to_a @container end |
#top ⇒ Object
Returns the top element of the stack, without removing it. If the stack is empty, returns nil.
63 64 65 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 63 def top @container.last end |
#union(stack) ⇒ Stack
Finds union of the self object and stack given as argument.
215 216 217 218 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 215 def union(stack) raise ArgumentError, "Undefined method Stack#union for #{stack}:#{stack.class}" unless stack.is_a?(Stack) self | stack end |
#union!(stack) ⇒ Stack
Like #|, finds the union of the self object and the stack given as argument, but keeps the result in the current stack.
226 227 228 229 230 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 226 def union!(stack) raise ArgumentError, "Undefined method Stack#union! for #{stack}:#{stack.class}" unless stack.is_a?(Stack) @container |= stack.to_a self end |
#|(stack) ⇒ Stack
Finds union of the self object and stack given as argument.
209 210 211 212 |
# File 'lib/algorithmix/data_structure/generic/stack.rb', line 209 def |(stack) raise ArgumentError, "Undefined method Stack#| for #{stack}:#{stack.class}" unless stack.is_a?(Stack) Stack.new(@container | stack.to_a) end |