class Algorithmix::DataStructure::Generic::Stack

Public Class Methods

new(obj = nil, copy: false) click to toggle source

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

!=(stack) click to toggle source

(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
&(stack) click to toggle source

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
+(stack) click to toggle source

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
-(stack) click to toggle source

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
<<(value) click to toggle source

(see push)

# File lib/algorithmix/data_structure/generic/stack.rb, line 38
def <<(value)
  push(value)
end
<=>(stack) click to toggle source

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
==(stack) click to toggle source

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
^(stack) click to toggle source

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
apply(&block) click to toggle source

(see map)

# File lib/algorithmix/data_structure/generic/stack.rb, line 321
def apply(&block)
  map(&block)
end
apply!(&block) click to toggle source

(see map!)

# File lib/algorithmix/data_structure/generic/stack.rb, line 326
def apply!(&block)
  map!(&block)
end
assign(obj, copy: false) click to toggle source

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
clear() click to toggle source

Clears content of current stack.

# File lib/algorithmix/data_structure/generic/stack.rb, line 265
def clear
  @container = []
  self
end
concat(stack) click to toggle source

(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
concat!(stack) click to toggle source

(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
diff?(stack) click to toggle source

(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
difference(stack) click to toggle source

(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
difference!(stack) click to toggle source

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
empty?() click to toggle source

Checks if the stack contains any elements.

# File lib/algorithmix/data_structure/generic/stack.rb, line 68
def empty?
  @container.empty?
end
eql?(stack) click to toggle source

(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
filter(&block) click to toggle source

(see select)

# File lib/algorithmix/data_structure/generic/stack.rb, line 285
def filter(&block)
  select(&block)
end
filter!(&block) click to toggle source

(see select!)

# File lib/algorithmix/data_structure/generic/stack.rb, line 290
def filter!(&block)
  select!(&block)
end
find_all(&block) click to toggle source

(see select)

# File lib/algorithmix/data_structure/generic/stack.rb, line 295
def find_all(&block)
  select(&block)
end
find_all!(&block) click to toggle source

(see select!)

# File lib/algorithmix/data_structure/generic/stack.rb, line 300
def find_all!(&block)
  select!(&block)
end
intersect(stack) click to toggle source

(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
intersect!(stack) click to toggle source

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
length() click to toggle source

(see size)

# File lib/algorithmix/data_structure/generic/stack.rb, line 57
def length
  @container.size
end
map(&block) click to toggle source

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
map!(&block) click to toggle source

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
merge(stack) click to toggle source

(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
merge!(stack) click to toggle source

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
pop() click to toggle source

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
push(value) click to toggle source

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
select(&block) click to toggle source

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
select!(&block) click to toggle source

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
size() click to toggle source

Returns the number of elements in the stack.

# File lib/algorithmix/data_structure/generic/stack.rb, line 52
def size
  @container.size
end
symmetric_difference(stack) click to toggle source

(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
symmetric_difference!(stack) click to toggle source

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
to_a() click to toggle source

Returns current object as array.

# File lib/algorithmix/data_structure/generic/stack.rb, line 260
def to_a
  @container
end
top() click to toggle source

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
union(stack) click to toggle source

(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
union!(stack) click to toggle source

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
|(stack) click to toggle source

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

from_obj(obj, copy) click to toggle source
# 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