Class: BurrowsWheeler::Transform

Inherits:
Object
  • Object
show all
Defined in:
lib/burrows_wheeler/transform.rb

Class Method Summary collapse

Class Method Details

.decode(inp, out) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/burrows_wheeler/transform.rb', line 29

def decode(inp, out)
  # read uint64_t
  first = inp.read(8).unpack('Q').first
  data = inp.read.split('')
  column = data.sort

  length = data.length

  index = {}
  nxt = Array.new(length)

  j = 0
  while j < length
    index[column[j]] = j
    j += 1 while j < length - 1 && column[j] == column[j + 1]
    j += 1
  end

  (0..length - 1).each do |i|
    char = data[i]
    nxt[index[char]] = i
    index[char] += 1
  end

  (0..length - 1).each do
    out.write(column[first])
    first = nxt[first]
  end

  out.flush
end

.encode(inp, out) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/burrows_wheeler/transform.rb', line 7

def encode(inp, out)
  str = inp.read
  csa = CircularSuffixArray.new(str)
  first = -1

  symbols = []

  (0..str.length - 1).each do |i|
    idx = csa[i]
    cs = CircularString.new(str, idx)
    symbols << cs.last
    first = i if idx == 0
  end

  # write uint64_t
  out.write([first].pack('Q'))

  symbols.each { |c| out.write(c) }

  out.flush
end