Class: DiffLCS::Counter

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/diff_l_c_s/counter.rb

Instance Method Summary collapse

Constructor Details

#initialize(old_i, new_i) ⇒ Counter

Creates a new counter and sets the initial positions and size



6
7
8
9
10
# File 'lib/diff_l_c_s/counter.rb', line 6

def initialize(old_i, new_i)
  @in_old_begin = old_i
  @in_new_begin = new_i
  @size = 1
end

Instance Method Details

#<=>(other) ⇒ Object

Compares it’s own size with the size of the other



92
93
94
# File 'lib/diff_l_c_s/counter.rb', line 92

def <=> (other)
  return self.size <=> other.size
end

#in_newObject

Returns the PositionRange for the range in the new string

NOTE: No step_up’s possible anymore after this function has been called.



36
37
38
39
40
41
# File 'lib/diff_l_c_s/counter.rb', line 36

def in_new
  if !@in_new
    @in_new = PositionRange.new(@in_new_begin, @in_new_begin + @size)
  end
  return @in_new
end

#in_new=(new_in_new) ⇒ Object

Sets the in_new PositionRange, and updates the in_old too

If new_in_old is nil, the counter is set empty

NOTE: Assumed to be smaller than before, and not moved.



64
65
66
67
68
69
70
71
# File 'lib/diff_l_c_s/counter.rb', line 64

def in_new=(new_in_new)
  if new_in_new
    @in_old = self.adjust(self.in_old, self.in_new, new_in_new)
    @in_new = new_in_new
  else
    @empty = true
  end
end

#in_oldObject

Returns the PositionRange for the range in the old string.

NOTE: No step_up’s possible anymore after this function has been called.



24
25
26
27
28
29
# File 'lib/diff_l_c_s/counter.rb', line 24

def in_old
  if !@in_old
    @in_old = PositionRange.new(@in_old_begin, @in_old_begin + @size)
  end
  return @in_old
end

#in_old=(new_in_old) ⇒ Object

Sets the in_old PositionRange, and updates the in_new too

If new_in_old is nil, the counter is set empty

NOTE: Assumed to be smaller than before, and not moved.



49
50
51
52
53
54
55
56
# File 'lib/diff_l_c_s/counter.rb', line 49

def in_old=(new_in_old)
  if new_in_old
    @in_new = self.adjust(self.in_new, self.in_old, new_in_old)
    @in_old = new_in_old
  else
    @empty = true
  end
end

#sizeObject

Returns the size of this Counter



82
83
84
85
86
87
88
# File 'lib/diff_l_c_s/counter.rb', line 82

def size
  if @empty
    return 0
  else
    return self.in_old.size
  end
end

#step_sizeObject

Faster than size, but only tells the size to which was stepped.



76
77
78
# File 'lib/diff_l_c_s/counter.rb', line 76

def step_size
  return @size
end

#step_upObject

Increases the size



14
15
16
17
# File 'lib/diff_l_c_s/counter.rb', line 14

def step_up
  @size += 1
  return self
end