Module: Redmine::StringArrayDiff::Diffable

Included in:
Array, String
Defined in:
lib/redmine/string_array_diff/diffable.rb

Instance Method Summary collapse

Instance Method Details

#diff(b) ⇒ Object



43
44
45
# File 'lib/redmine/string_array_diff/diffable.rb', line 43

def diff(b)
  Redmine::StringArrayDiff::Diff.new(self, b)
end

#patch(diff) ⇒ Object



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
# File 'lib/redmine/string_array_diff/diffable.rb', line 90

def patch(diff)
  newary = nil
  if diff.difftype == String
    newary = diff.difftype.new('')
  else
    newary = diff.difftype.new
  end
  ai = 0
  bi = 0
  diff.diffs.each { |d|
    d.each { |mod|
      case mod[0]
      when '-'
        while ai < mod[1]
          newary << self[ai]
          ai += 1
          bi += 1
        end
        ai += 1
      when '+'
        while bi < mod[1]
          newary << self[ai]
          ai += 1
          bi += 1
        end
        newary << mod[2]
        bi += 1
      else
        raise "Unknown diff action"
      end
    }
  }
  while ai < self.length
    newary << self[ai]
    ai += 1
    bi += 1
  end
  return newary
end

#replacenextlarger(value, high = nil) ⇒ Object



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
# File 'lib/redmine/string_array_diff/diffable.rb', line 63

def replacenextlarger(value, high = nil)
  high ||= self.length
  if self.empty? || value > self[-1]
    push value
    return high
  end
  # binary search for replacement point
  low = 0
  while low < high
    index = (high+low)/2
    found = self[index]
    return nil if value == found
    if value > found
      low = index + 1
    else
      high = index
    end
  end

  self[low] = value
  #  $stderr << "replace #{value} : 0/#{low}/#{init_high} (#{steps} steps) (#{init_high-low} off )\n"
  #  $stderr.puts self.inspect
  # gets
  # p length - low
  return low
end

#reverse_hash(range = (0...self.length)) ⇒ Object

Create a hash that maps elements of the array to arrays of indices where the elements are found.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/redmine/string_array_diff/diffable.rb', line 50

def reverse_hash(range = (0...self.length))
  revmap = {}
  range.each { |i|
    elem = self[i]
    if revmap.has_key? elem
      revmap[elem].push i
    else
      revmap[elem] = [i]
    end
  }
  return revmap
end