Class: MinMaxStack

Inherits:
Object
  • Object
show all
Defined in:
lib/min_max_stack.rb

Direct Known Subclasses

DataStruct::MinMaxStack

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMinMaxStack

Returns a new instance of MinMaxStack.


4
5
6
# File 'lib/min_max_stack.rb', line 4

def initialize
  @store = []
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store


2
3
4
# File 'lib/min_max_stack.rb', line 2

def store
  @store
end

Instance Method Details

#lengthObject


8
9
10
# File 'lib/min_max_stack.rb', line 8

def length
  @store.length
end

#maxObject


12
13
14
# File 'lib/min_max_stack.rb', line 12

def max
  @store.empty? ? nil : @store[-1][2]
end

#minObject


16
17
18
# File 'lib/min_max_stack.rb', line 16

def min
  @store.empty? ? nil : @store[-1][1]
end

#popObject


20
21
22
23
24
# File 'lib/min_max_stack.rb', line 20

def pop
  val, _ = @store.pop

  val
end

#push(val) ⇒ Object


26
27
28
29
30
31
32
# File 'lib/min_max_stack.rb', line 26

def push(val)
  if @store.empty?
    @store << [val, val, val]
  else
    @store << [val, [self.min, val].min, [self.max, val].max]
  end
end