Class: Rivulet::MinMaxWindow

Inherits:
Window
  • Object
show all
Defined in:
lib/rivulet/minmax_window.rb

Direct Known Subclasses

StatsWindow

Instance Method Summary collapse

Methods inherited from Window

#empty?, #size

Constructor Details

#initializeMinMaxWindow

Returns a new instance of MinMaxWindow.



5
6
7
8
9
10
11
# File 'lib/rivulet/minmax_window.rb', line 5

def initialize
  super
  @items = []
  @min_deque = Deque.new
  @max_deque = Deque.new
  @front = 0
end

Instance Method Details

#add(item) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/rivulet/minmax_window.rb', line 13

def add(item)
  idx = @items.size
  @min_deque.pop while @min_deque.any? && @items[@min_deque.last] >= item
  @max_deque.pop while @max_deque.any? && @items[@max_deque.last] <= item
  @min_deque.push(idx)
  @max_deque.push(idx)
  @items.push(item)
  super
end

#evictObject



23
24
25
26
27
28
29
30
# File 'lib/rivulet/minmax_window.rb', line 23

def evict
  return super if @front >= @items.size

  @min_deque.shift if @min_deque.first == @front
  @max_deque.shift if @max_deque.first == @front
  @front += 1
  super
end

#maxObject



36
37
38
# File 'lib/rivulet/minmax_window.rb', line 36

def max
  @items[@max_deque.first] unless empty?
end

#minObject



32
33
34
# File 'lib/rivulet/minmax_window.rb', line 32

def min
  @items[@min_deque.first] unless empty?
end

#rangeObject



40
41
42
# File 'lib/rivulet/minmax_window.rb', line 40

def range
  max - min unless empty?
end