Class: Rivulet::StatsWindow
- Inherits:
-
MinMaxWindow
- Object
- Window
- MinMaxWindow
- Rivulet::StatsWindow
- Defined in:
- lib/rivulet/stats_window.rb
Instance Method Summary collapse
- #add(item) ⇒ Object
- #average ⇒ Object
- #counts ⇒ Object
- #covers?(target_counts) ⇒ Boolean
- #distinct ⇒ Object
- #evict ⇒ Object
-
#initialize ⇒ StatsWindow
constructor
A new instance of StatsWindow.
- #max_count ⇒ Object
- #repeats? ⇒ Boolean
- #sum ⇒ Object
Methods inherited from MinMaxWindow
Methods inherited from Window
Constructor Details
#initialize ⇒ StatsWindow
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 |
#average ⇒ Object
28 |
# File 'lib/rivulet/stats_window.rb', line 28 def average = empty? ? nil : @sum.fdiv(size) |
#counts ⇒ Object
33 |
# File 'lib/rivulet/stats_window.rb', line 33 def counts = @counts |
#covers?(target_counts) ⇒ 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 |
#distinct ⇒ Object
30 |
# File 'lib/rivulet/stats_window.rb', line 30 def distinct = @counts.size |
#evict ⇒ Object
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_count ⇒ Object
32 |
# File 'lib/rivulet/stats_window.rb', line 32 def max_count = @counts.values.max || 0 |
#repeats? ⇒ Boolean
31 |
# File 'lib/rivulet/stats_window.rb', line 31 def repeats? = @counts.any? { |_, n| n > 1 } |
#sum ⇒ Object
27 |
# File 'lib/rivulet/stats_window.rb', line 27 def sum = @sum |