Module: TraceVisualization::SuffixArray

Defined in:
lib/trace_visualization/suffix_array.rb

Class Method Summary collapse

Class Method Details

.effective(str) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/trace_visualization/suffix_array.rb', line 22

def self.effective(str)
  n = str.length
  s = []

  if str.instance_of? String
    str.each_char { |c| s << c.ord }
  elsif str.instance_of? Array
    str.each { |c| s << c.ord }
  else
    s = str
  end

  3.times { s << 0 }

  suffix_array = Array.new(n + 3, 0)

  effective_linear(s, suffix_array, n, s.max + 1)

  3.times { s.pop }

  suffix_array[0 ... -3]
end

.effective_linear(s, suffix_array, n, alphabet_size) ⇒ Object

by Juha Karkkainen, Peter Sanders and Stefan Burkhardt



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/trace_visualization/suffix_array.rb', line 48

def self.effective_linear(s, suffix_array, n, alphabet_size)
n0 = (n + 2) / 3
n1 = (n + 1) / 3
n2 = n / 3
n02 = n0 + n2
		
  s12 = Array.new(n02 + 3, 0)
  sa12 = Array.new(n02 + 3, 0)
  s0 = Array.new(n0, 0)
  sa0 = Array.new(n0, 0)

# Generate positions of mod 1 and mod 2 suffixes
# the "+(n0-n1)" adds a dummy mod 1 suffix if n%3 == 1
  i = j = 0
  while (i < n + (n0 - n1))
    if i % 3 != 0
      s12[j] = i
      j += 1
    end
    i += 1
  end

# LSB radix sort the mod 1 and mod 2 triples
radix_pass(s12, sa12, s[2 ... s.length], n02, alphabet_size)
radix_pass(sa12, s12, s[1 ... s.length], n02, alphabet_size)
radix_pass(s12, sa12, s, n02, alphabet_size)

# Find lexicographic names of triples
name, c0, c1, c2 = 0, -1, -1, -1
  for i in 0 ... n02
	if (s[sa12[i]] != c0 || s[sa12[i] + 1] != c1 || s[sa12[i] + 2] != c2)
		name += 1
		c0 = s[sa12[i]]
		c1 = s[sa12[i] + 1]
		c2 = s[sa12[i] + 2]
    end
			
	if (sa12[i] % 3 == 1) 
		s12[sa12[i]/3] = name      # Left half
    else
		s12[sa12[i]/3 + n0] = name # Right half
    end
  end

# Recurse if names are not yet unique
if name < n02
	effective_linear(s12, sa12, n02, name)
			
	# Store unique names in s12 using the suffix array
    for i in 0 ... n02
		s12[sa12[i]] = i + 1
    end
  else
	# Generate the suffix array of s12 directly
    for i in 0 ... n02
		sa12[s12[i] - 1] = i
    end
  end

# Stably sort the mod 0 suffixes from sa12 by their first character
  i, j = 0, 0
  while i < n02
    if sa12[i] < n0
      s0[j] = 3 * sa12[i]
      j += 1
    end
    i += 1
  end
radix_pass(s0, sa0, s, n0, alphabet_size)

# Merge sorted sa0 suffixes and sorted sa12 suffixes
  p, t, k = 0, n0 - n1, 0

  while k < n
	# Pos of current offset 12 suffix
	i = get_i(n0, sa12, t)
  
	# Pos of current offset 0 suffix
	j = sa0[p]
  
    # Different compares for mod 1 and mod 2 suffixes
	if (sa12[t] < n0 ? leq_pairs(s[i], s12[sa12[t] + n0], s[j], s12[j/3]) : leq_triples(s[i], s[i + 1], s12[sa12[t] - n0 + 1], s[j], s[j + 1], s12[j/3 + n0]))
		suffix_array[k] = i
		t += 1
		if t == n02
			# Done: only sa0 suffixes left
        k += 1
        while p < n0
          suffix_array[k] = sa0[p]
          p += 1
          k += 1
        end
      end
    else
		suffix_array[k] = j
		p += 1;
		if p == n0
			# Done: only sa12 suffixes left
        k += 1
        while t < n02
          suffix_array[k] = get_i(n0, sa12, t)
          t += 1
          k += 1
        end				
      end
    end
  
    k += 1
  end

end

.naive(str) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/trace_visualization/suffix_array.rb', line 3

def self.naive(str)
  n = str.length

  tmp    = Array.new(n)
  result = Array.new(n)

  for i in 0 ... n
    tmp[i] = [str[i .. -1], i]
  end

  tmp.sort! { |x, y| x[0] <=> y[0] }    

  for i in 0 ... n
    result[i] = tmp[i][1]
  end

  result
end