Class: AbstractStack

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/abstractstack.rb,
lib/abstractstack/lifo.rb,
lib/abstractstack/fifo.rb,
lib/abstractstack/version.rb,
lib/abstractstack/exceptions.rb,
lib/abstractstack/instance_methods.rb

Overview

Examples:

class SimpleStack < AbstractStack; end
class LIFO < AbstractStack
  include Enumerable

  alias_method :each, :lifo_each
  alias_method :reverse_each, :fifo_each
  alias_method :first, :top
  alias_method :last, :bottom
end

Direct Known Subclasses

FIFO, LIFO

Defined Under Namespace

Classes: FIFO, InvalidStackOperation, LIFO, OverFlow, UnderFlow

Constant Summary collapse

FILO =
LIFO
LILO =
FIFO
VERSION =
'0.1.1'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(limit = nil) ⇒ AbstractStack

Returns a new instance of AbstractStack

Parameters:

  • limit (Integer) (defaults to: nil)

10
11
12
13
# File 'lib/abstractstack/instance_methods.rb', line 10

def initialize(limit=nil)
  @list = []
  @limit = _limit_for limit
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit


7
8
9
# File 'lib/abstractstack/instance_methods.rb', line 7

def limit
  @limit
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)

79
80
81
# File 'lib/abstractstack/instance_methods.rb', line 79

def ==(other)
  (self.class == other.class) && (@list == other._list)
end

#[](pos) ⇒ Object

Parameters:

  • pos (Integer, #to_int)

101
102
103
# File 'lib/abstractstack/instance_methods.rb', line 101

def [](pos)
  at pos
end

#at(pos) ⇒ Object

Parameters:

  • pos (Integer, #to_int)

Raises:


94
95
96
97
98
# File 'lib/abstractstack/instance_methods.rb', line 94

def at(pos)
  raise InvalidStackOperation if empty?

  @list.fetch _index_for(pos.to_int)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)

83
84
85
86
# File 'lib/abstractstack/instance_methods.rb', line 83

def eql?(other)
  (self.class == other.class) &&
    [limit, _list].eql?([other.limit, other._list])
end

#fifo_each {|value| ... } ⇒ Enumerator Also known as: lilo_each

Yields:

  • (value)

Yield Returns:

  • (self)

Returns:

  • (Enumerator)

41
42
43
44
45
46
# File 'lib/abstractstack/instance_methods.rb', line 41

def fifo_each
  return to_enum(__callee__) { size } unless block_given?

  @list.each{|v|yield v}
  self
end

#filo_each {|value| ... } ⇒ Enumerator Also known as: lifo_each

Yields:

  • (value)

Yield Returns:

  • (self)

Returns:

  • (Enumerator)

53
54
55
56
57
58
# File 'lib/abstractstack/instance_methods.rb', line 53

def filo_each
  return to_enum(__callee__) { size } unless block_given?

  @list.reverse_each{|v|yield v}
  self
end

#freezeself

Returns:

  • (self)

68
69
70
71
# File 'lib/abstractstack/instance_methods.rb', line 68

def freeze
  @list.freeze
  super
end

#hashInteger

Returns:

  • (Integer)

89
90
91
# File 'lib/abstractstack/instance_methods.rb', line 89

def hash
  [@limit, @list].hash
end

#inspectString

Returns:

  • (String)

74
75
76
# File 'lib/abstractstack/instance_methods.rb', line 74

def inspect
  "#<#{self.class} limit=#{@limit.inspect} #{@list.inspect}>"
end

#popObject

Raises:

  • (UnderFlow)

    if self is empty when you call


32
33
34
35
36
# File 'lib/abstractstack/instance_methods.rb', line 32

def pop
  raise UnderFlow if empty?

  @list.pop
end

#push(value) ⇒ value Also known as: <<

Returns:

  • (value)

Raises:

  • (OverFlow)

    if over the limit size of self


22
23
24
25
26
27
# File 'lib/abstractstack/instance_methods.rb', line 22

def push(value)
  raise OverFlow if limit && (limit <= length)

  @list.push value
  self
end

#to_aArray

Returns:

  • (Array)

63
64
65
# File 'lib/abstractstack/instance_methods.rb', line 63

def to_a
  @list.dup
end