Class: Rivulet::MinMaxWindow
- Defined in:
- lib/rivulet/minmax_window.rb
Direct Known Subclasses
Instance Method Summary collapse
- #add(item) ⇒ Object
- #evict ⇒ Object
-
#initialize ⇒ MinMaxWindow
constructor
A new instance of MinMaxWindow.
- #max ⇒ Object
- #min ⇒ Object
- #range ⇒ Object
Methods inherited from Window
Constructor Details
#initialize ⇒ MinMaxWindow
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 |
#evict ⇒ Object
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 |
#max ⇒ Object
36 37 38 |
# File 'lib/rivulet/minmax_window.rb', line 36 def max @items[@max_deque.first] unless empty? end |
#min ⇒ Object
32 33 34 |
# File 'lib/rivulet/minmax_window.rb', line 32 def min @items[@min_deque.first] unless empty? end |
#range ⇒ Object
40 41 42 |
# File 'lib/rivulet/minmax_window.rb', line 40 def range max - min unless empty? end |