class Algorithmix::DataStructure::Generic::Stack
Public Class Methods
Creates a new stack.
@param obj [#to_a] any object which responds to to_a
method. @return [Stack] a new stack object
# 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
Public Instance Methods
(see #==)
# 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
Finds intersection of contents of the self stack and stack given as argument, and returns a new stack with the result.
@param [Stack] @return [Stack] a new stack @raise ArgumentError, if given object is not a stack
# 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
Merges contents of the self stack and stack given as argument, without removing repetitions from both stacks.
@param [Stack] @return [Stack] a new stack object, without modifying any of given stacks. @raise ArgumentError, if given object is not a stack
# 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
Finds difference of the self stack object and that given as argument.
@param [Stack] @return [Stack] a new stack @raise ArgumentError, if given object is not a stack
# 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
(see push
)
# File lib/algorithmix/data_structure/generic/stack.rb, line 38 def <<(value) push(value) end
Compares the self stack with given as argument stck.
@return 1, if content of the first stack is greater than content of the second.
0, if both stacks have equal contents -1, if content of the first stack is less than content of the second.
@raise ArgumentError, if given object is not a stack
# 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
Compares the self stack with given as argument stack.
@param [Stack] @return [true, false]
# 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
Finds the symmetric difference of the stack and that given as argument.
@param [Stack] @return [Stack] a new stack @raise ArgumentError, if given object is not a stack
# 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
(see map
)
# File lib/algorithmix/data_structure/generic/stack.rb, line 321 def apply(&block) map(&block) end
(see map!
)
# File lib/algorithmix/data_structure/generic/stack.rb, line 326 def apply!(&block) map!(&block) end
Copies the content of an object to the content of the stack, removing previous elements inserted in it.
@param obj [#to_a] any object which responds to to_a
method @return [Stack] self object, modified
# File lib/algorithmix/data_structure/generic/stack.rb, line 24 def assign(obj, copy: false) from_obj(obj, copy) end
Clears content of current stack.
# File lib/algorithmix/data_structure/generic/stack.rb, line 265 def clear @container = [] self end
(see +
)
# 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
(see merge!
)
# 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
(see #==)
# 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
(see -
)
# 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
As difference, finds the difference of the stack and that given as argument, but modifies self object.
@param [Stack] @return [Stack] self object @raise ArgumentError, if given object is not a stack
# 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
Checks if the stack contains any elements.
# File lib/algorithmix/data_structure/generic/stack.rb, line 68 def empty? @container.empty? end
(see #==)
# 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
(see select
)
# File lib/algorithmix/data_structure/generic/stack.rb, line 285 def filter(&block) select(&block) end
(see select!
)
# File lib/algorithmix/data_structure/generic/stack.rb, line 290 def filter!(&block) select!(&block) end
(see select
)
# File lib/algorithmix/data_structure/generic/stack.rb, line 295 def find_all(&block) select(&block) end
(see select!
)
# File lib/algorithmix/data_structure/generic/stack.rb, line 300 def find_all!(&block) select!(&block) end
(see #&)
# 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
Like intersect, finds intersection of the self stack object and stack given as argument, but modifies the object to which operation was called.
# 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
(see size
)
# File lib/algorithmix/data_structure/generic/stack.rb, line 57 def length @container.size end
Applies a function to each element of the stack.
@param &block @return [Stack] a new stack
# File lib/algorithmix/data_structure/generic/stack.rb, line 308 def map(&block) Stack.new(@container.map { |e| block.call(e)}) end
Same as map
, but the result is kept in the current object.
@param &block
# File lib/algorithmix/data_structure/generic/stack.rb, line 315 def map!(&block) @container.map! { |e| block.call(e) } self end
(see +
)
# 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
Merges contents of the self stack, and stack given as argument. Modifies self object.
@param [Stack] @return [Stack] self object @raise ArgumentError, if given object is not a stack
# 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
Removes the top element of the stack.
@return top element of the stack @raise Algorithmix::EmptyContainerError, if there are no elements in the stack.
# File lib/algorithmix/data_structure/generic/stack.rb, line 46 def pop raise EmptyContainerError, "The stack is empty." if @container.empty? @container.pop end
Inserts a new element at the top of the stack.
@param value @return [Stack] self object, modified
# File lib/algorithmix/data_structure/generic/stack.rb, line 32 def push(value) @container << value self end
Filters elements of the stack, by given condition.
@param &block represents condition by which elements of the stack will be filtered @return [Stack] a new stack object
# File lib/algorithmix/data_structure/generic/stack.rb, line 274 def select(&block) Stack.new(@container.select { |e| block.call(e) }) end
Same as select
, but modifying the self object.
# File lib/algorithmix/data_structure/generic/stack.rb, line 279 def select!(&block) @container.select! { |e| block.call(e)} self end
Returns the number of elements in the stack.
# File lib/algorithmix/data_structure/generic/stack.rb, line 52 def size @container.size end
(see #^)
# 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
Finds the symmetric difference of the self object and stack given as argument.
@param [Stack] @return [Stack] self object, modified @raise ArgumentError, if given object is not a stack
# 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
Returns current object as array.
# File lib/algorithmix/data_structure/generic/stack.rb, line 260 def to_a @container end
Returns the top element of the stack, without removing it. If the stack is empty, returns nil.
# File lib/algorithmix/data_structure/generic/stack.rb, line 63 def top @container.last end
(see #|)
# 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
Like #|, finds the union of the self object and the stack given as argument, but keeps the result in the current stack.
@param [Stack] @return [Stack] self object @raise ArgumentError, if given object is not a stack
# 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
Finds union of the self object and stack given as argument.
@param [Stack] @return [Stack] a new stack @raise ArgumentError, if given object is not a stack
# 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
Private Instance Methods
# File lib/algorithmix/data_structure/generic/stack.rb, line 332 def from_obj(obj, copy) raise ArgumentError, "Object doesn't respond to #to_a method" unless obj.respond_to?(:to_a) @container = copy ? obj.send(:to_a).dup : obj.send(:to_a) self end