class BurrowsWheeler::CircularString

> CircularString.new(str, 5).to_s

> “ADABRA!ABRAC”,

Attributes

shift[R]
string[R]

Public Class Methods

new(string, shift) click to toggle source
# File lib/burrows_wheeler/circular_string.rb, line 11
def initialize(string, shift)
  @string = string
  @shift = shift
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/burrows_wheeler/circular_string.rb, line 31
def <=>(other)
  len = [length, other.length].min

  (0..len - 1).each do |i|
    c1, c2 = self[i], other[i]
    return (c1 <=> c2) if c1 != c2
  end

  length <=> other.length
end
[](index) click to toggle source
# File lib/burrows_wheeler/circular_string.rb, line 16
def [](index)
  return nil if index < 0 || index >= string.length
  idx = (index + @shift) % @string.length
  @string[idx]
end
last() click to toggle source
# File lib/burrows_wheeler/circular_string.rb, line 22
def last
  self[length - 1]
end
length() click to toggle source
# File lib/burrows_wheeler/circular_string.rb, line 42
def length
  @string.length
end
subsequence(start, finish) click to toggle source
# File lib/burrows_wheeler/circular_string.rb, line 26
def subsequence(start, finish)
  len = finish - start
  CircularString.new(@string[start, len], @shift)
end
to_s() click to toggle source
# File lib/burrows_wheeler/circular_string.rb, line 46
def to_s
  (0..@string.length - 1).reduce('') do |str, idx|
    str + self[idx]
  end
end