Class: BurrowsWheeler::CircularString
- Inherits:
-
Object
- Object
- BurrowsWheeler::CircularString
- Includes:
- Comparable
- Defined in:
- lib/burrows_wheeler/circular_string.rb
Overview
> CircularString.new(str, 5).to_s
> “ADABRA!ABRAC”,
Instance Attribute Summary collapse
-
#shift ⇒ Object
readonly
Returns the value of attribute shift.
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #[](index) ⇒ Object
-
#initialize(string, shift) ⇒ CircularString
constructor
A new instance of CircularString.
- #last ⇒ Object
- #length ⇒ Object
- #subsequence(start, finish) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(string, shift) ⇒ CircularString
Returns a new instance of CircularString.
11 12 13 14 |
# File 'lib/burrows_wheeler/circular_string.rb', line 11 def initialize(string, shift) @string = string @shift = shift end |
Instance Attribute Details
#shift ⇒ Object (readonly)
Returns the value of attribute shift.
9 10 11 |
# File 'lib/burrows_wheeler/circular_string.rb', line 9 def shift @shift end |
#string ⇒ Object (readonly)
Returns the value of attribute string.
8 9 10 |
# File 'lib/burrows_wheeler/circular_string.rb', line 8 def string @string end |
Instance Method Details
#<=>(other) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# 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) ⇒ Object
16 17 18 19 20 |
# 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 ⇒ Object
22 23 24 |
# File 'lib/burrows_wheeler/circular_string.rb', line 22 def last self[length - 1] end |
#length ⇒ Object
42 43 44 |
# File 'lib/burrows_wheeler/circular_string.rb', line 42 def length @string.length end |
#subsequence(start, finish) ⇒ Object
26 27 28 29 |
# 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 ⇒ Object
46 47 48 49 50 |
# 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 |