Class: Nineteen::Eighty::Two::Decorators::RunLengthEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/nineteen/eighty/two/decorators/run_length_encoder.rb

Class Method Summary collapse

Class Method Details

.encode(row) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nineteen/eighty/two/decorators/run_length_encoder.rb', line 6

def self.encode row
  result = []
  count = 0
  step = 0
  while step < row.length
    current = row[step]
    step += 1
    nxt = row[step]
    if nxt == current
      count += 1
    else
      result << (Span.new current, count + 1)
      count = 0
      current = nxt
    end
  end

  result
end