Class: Rivulet::CountWindow

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

Instance Method Summary collapse

Methods inherited from Window

#empty?, #size

Constructor Details

#initializeCountWindow

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

#countsObject



35
# File 'lib/rivulet/count_window.rb', line 35

def counts = @counts

#covers?(target_counts) ⇒ Boolean

Returns:

  • (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

#distinctObject



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

def distinct = @counts.size

#evictObject



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_countObject



34
# File 'lib/rivulet/count_window.rb', line 34

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

#repeats?Boolean

Returns:

  • (Boolean)


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

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