Class: Rivulet::StatsWindow

Inherits:
MinMaxWindow show all
Defined in:
lib/rivulet/stats_window.rb

Instance Method Summary collapse

Methods inherited from MinMaxWindow

#max, #min, #range

Methods inherited from Window

#empty?, #size

Constructor Details

#initializeStatsWindow

Returns a new instance of StatsWindow.



5
6
7
8
9
# File 'lib/rivulet/stats_window.rb', line 5

def initialize
  super
  @sum = 0
  @counts = Hash.new(0)
end

Instance Method Details

#add(item) ⇒ Object



11
12
13
14
15
# File 'lib/rivulet/stats_window.rb', line 11

def add(item)
  @sum += item
  @counts[item] += 1
  super
end

#averageObject



28
# File 'lib/rivulet/stats_window.rb', line 28

def average = empty? ? nil : @sum.fdiv(size)

#countsObject



33
# File 'lib/rivulet/stats_window.rb', line 33

def counts = @counts

#covers?(target_counts) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rivulet/stats_window.rb', line 34

def covers?(target_counts)
  target_counts.all? { |k, v| @counts[k] >= v }
end

#distinctObject



30
# File 'lib/rivulet/stats_window.rb', line 30

def distinct = @counts.size

#evictObject



17
18
19
20
21
22
23
24
25
# File 'lib/rivulet/stats_window.rb', line 17

def evict
  if @front < @items.size
    item = @items[@front]
    @sum -= item
    @counts[item] -= 1
    @counts.delete(item) if @counts[item].zero?
  end
  super
end

#max_countObject



32
# File 'lib/rivulet/stats_window.rb', line 32

def max_count = @counts.values.max || 0

#repeats?Boolean

Returns:

  • (Boolean)


31
# File 'lib/rivulet/stats_window.rb', line 31

def repeats? = @counts.any? { |_, n| n > 1 }

#sumObject



27
# File 'lib/rivulet/stats_window.rb', line 27

def sum = @sum