Module: Diffable
Instance Method Summary collapse
- #diff(b) ⇒ Object
- #patch(diff) ⇒ Object
- #replacenextlarger(value, high = nil) ⇒ Object
-
#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.
Instance Method Details
#patch(diff) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/oats/diff.rb', line 194 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
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/oats/diff.rb', line 167 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.
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/oats/diff.rb', line 154 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 |