Class: Rivulet::CountWindow
- Defined in:
- lib/rivulet/count_window.rb
Instance Method Summary collapse
- #add(item) ⇒ Object
- #counts ⇒ Object
- #covers?(target_counts) ⇒ Boolean
- #distinct ⇒ Object
- #evict ⇒ Object
-
#initialize ⇒ CountWindow
constructor
A new instance of CountWindow.
- #max_count ⇒ Object
- #repeats? ⇒ Boolean
Methods inherited from Window
Constructor Details
#initialize ⇒ CountWindow
Returns a new instance of CountWindow.
5 6 7 8 9 10 |
# File 'lib/rivulet/count_window.rb', line 5 def initialize super @counts = Hash.new(0) @items = [] @front = 0 end |
Instance Method Details
#add(item) ⇒ Object
12 13 14 15 16 |
# File 'lib/rivulet/count_window.rb', line 12 def add(item) @counts[item] += 1 @items.push(item) super end |
#counts ⇒ Object
35 |
# File 'lib/rivulet/count_window.rb', line 35 def counts = @counts |
#covers?(target_counts) ⇒ Boolean
37 38 39 |
# File 'lib/rivulet/count_window.rb', line 37 def covers?(target_counts) target_counts.all? { |k, v| @counts[k] >= v } end |
#distinct ⇒ Object
33 |
# File 'lib/rivulet/count_window.rb', line 33 def distinct = @counts.size |
#evict ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rivulet/count_window.rb', line 18 def evict if @front < @items.size item = @items[@front] @counts[item] -= 1 @counts.delete(item) if @counts[item].zero? @front += 1 super item else super nil end end |
#max_count ⇒ Object
34 |
# File 'lib/rivulet/count_window.rb', line 34 def max_count = @counts.values.max || 0 |
#repeats? ⇒ Boolean
32 |
# File 'lib/rivulet/count_window.rb', line 32 def repeats? = @counts.any? { |_, n| n > 1 } |