Class: Lookout::Diff::Algorithms::Difflib::Position::To

Inherits:
Range
  • Object
show all
Defined in:
lib/lookout/diff/algorithms/difflib/position/to.rb

Instance Method Summary collapse

Methods inherited from Range

#+, #[], #begin, #begin_at, #begins_before?, #each, #each_with_index, #empty?, #end, #end_at, #ends_after?, #inspect, #size, #to_items

Constructor Details

#initialize(items, range = 0...items.size, indexes = nil) ⇒ To

Returns a new instance of To.



5
6
7
8
# File 'lib/lookout/diff/algorithms/difflib/position/to.rb', line 5

def initialize(items, range = 0...items.size, indexes = nil)
  super items, range
  @indexes = indexes
end

Instance Method Details

#==(other) ⇒ Object



44
45
46
# File 'lib/lookout/diff/algorithms/difflib/position/to.rb', line 44

def ==(other)
  super and indexes == other.indexes
end

#at(range) ⇒ Object



40
41
42
# File 'lib/lookout/diff/algorithms/difflib/position/to.rb', line 40

def at(range)
  Lookout::Diff::Range.new(items, range)
end

#begin_after(other) ⇒ Object



32
33
34
# File 'lib/lookout/diff/algorithms/difflib/position/to.rb', line 32

def begin_after(other)
  self.class.new(items, other.range.end + 1..range.end, indexes)
end

#each_index(item) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/lookout/diff/algorithms/difflib/position/to.rb', line 10

def each_index(item)
  indexes[item].each do |index|
    next if index < range.begin
    break if index > range.end
    yield index
  end
end

#end_before(other) ⇒ Object



36
37
38
# File 'lib/lookout/diff/algorithms/difflib/position/to.rb', line 36

def end_before(other)
  self.class.new(items, range.begin...other.range.begin, indexes)
end

#indexesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lookout/diff/algorithms/difflib/position/to.rb', line 18

def indexes
  return @indexes if @indexes
  @indexes = Hash.new{ |h, k| h[k] = [] }
  each = items.is_a?(String) ?
      (' '[0].is_a?(Integer) ? :each_byte : :each_char) :
      :each
  i = 0
  items.send(each) do |item|
    @indexes[item] << i
    i += 1
  end
  @indexes
end