Mercurial > jhg
comparison src/org/tmatesoft/hg/internal/Patch.java @ 584:ed243b668502
Conditionally enable effective patch merge alternative for revlog reading
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Thu, 25 Apr 2013 16:08:17 +0200 |
| parents | 47dfa0ec7e35 |
| children | a52f4cc56f9c |
comparison
equal
deleted
inserted
replaced
| 583:47dfa0ec7e35 | 584:ed243b668502 |
|---|---|
| 31 * @author TMate Software Ltd. | 31 * @author TMate Software Ltd. |
| 32 */ | 32 */ |
| 33 public final class Patch { | 33 public final class Patch { |
| 34 private final IntVector starts, ends; | 34 private final IntVector starts, ends; |
| 35 private final ArrayList<byte[]> data; | 35 private final ArrayList<byte[]> data; |
| 36 private final boolean shallNormalize; | |
| 36 | 37 |
| 37 private static byte[] generate(int c) { | 38 private static byte[] generate(int c) { |
| 38 byte[] rv = new byte[c]; | 39 byte[] rv = new byte[c]; |
| 39 for (int i = 0; i < c; i++) { | 40 for (int i = 0; i < c; i++) { |
| 40 byte x = (byte) ('a' + i); | 41 byte x = (byte) ('a' + i); |
| 63 System.out.println("p1: " + p1); | 64 System.out.println("p1: " + p1); |
| 64 System.out.println("p2: " + p2); | 65 System.out.println("p2: " + p2); |
| 65 Patch r = p1.apply(p2); | 66 Patch r = p1.apply(p2); |
| 66 System.out.println("r: " + r); | 67 System.out.println("r: " + r); |
| 67 } | 68 } |
| 68 | 69 |
| 69 public Patch() { | 70 public Patch() { |
| 71 this(false); | |
| 72 } | |
| 73 | |
| 74 public Patch(boolean normalizeOnChange) { | |
| 75 shallNormalize = normalizeOnChange; | |
| 70 starts = new IntVector(); | 76 starts = new IntVector(); |
| 71 ends = new IntVector(); | 77 ends = new IntVector(); |
| 72 data = new ArrayList<byte[]>(); | 78 data = new ArrayList<byte[]>(); |
| 73 } | 79 } |
| 74 | 80 |
| 180 private void add(Patch p, int i) { | 186 private void add(Patch p, int i) { |
| 181 add(p.starts.get(i), p.ends.get(i), p.data.get(i)); | 187 add(p.starts.get(i), p.ends.get(i), p.data.get(i)); |
| 182 } | 188 } |
| 183 | 189 |
| 184 /*package-local*/ void add(int start, int end, byte[] d) { | 190 /*package-local*/ void add(int start, int end, byte[] d) { |
| 185 // FIXME if start == end(-1), merge data | |
| 186 if (start == end && d.length == 0) { | 191 if (start == end && d.length == 0) { |
| 187 System.currentTimeMillis(); | 192 System.currentTimeMillis(); |
| 188 return; | 193 return; |
| 194 } | |
| 195 int last; | |
| 196 if (shallNormalize && (last = starts.size()) > 0) { | |
| 197 last--; | |
| 198 if (ends.get(last) == start) { | |
| 199 byte[] d1 = data.get(last); | |
| 200 byte[] nd; | |
| 201 if (d1.length > 0 && d.length > 0) { | |
| 202 nd = new byte[d1.length + d.length]; | |
| 203 System.arraycopy(d1, 0, nd, 0, d1.length); | |
| 204 System.arraycopy(d, 0, nd, d1.length, d.length); | |
| 205 } else { | |
| 206 nd = d1.length == 0 ? d : d1 ; | |
| 207 } | |
| 208 ends.set(last, end); | |
| 209 data.set(last, nd); | |
| 210 return; | |
| 211 } | |
| 212 // fall-through | |
| 189 } | 213 } |
| 190 starts.add(start); | 214 starts.add(start); |
| 191 ends.add(end); | 215 ends.add(end); |
| 192 data.add(d); | 216 data.add(d); |
| 193 } | 217 } |
| 201 | 225 |
| 202 /** | 226 /** |
| 203 * Modify this patch with subsequent patch | 227 * Modify this patch with subsequent patch |
| 204 */ | 228 */ |
| 205 public /*SHALL BE PUBLIC ONCE TESTING ENDS*/ Patch apply(Patch another) { | 229 public /*SHALL BE PUBLIC ONCE TESTING ENDS*/ Patch apply(Patch another) { |
| 206 Patch r = new Patch(); | 230 Patch r = new Patch(shallNormalize); |
| 207 int p1TotalAppliedDelta = 0; // value to add to start and end indexes of the older patch to get their values as if | 231 int p1TotalAppliedDelta = 0; // value to add to start and end indexes of the older patch to get their values as if |
| 208 // in the patched text, iow, directly comparable with respective indexes from the newer patch. | 232 // in the patched text, iow, directly comparable with respective indexes from the newer patch. |
| 209 int p1EntryStart = 0, p1EntryEnd = 0, p1EntryLen = 0; | 233 int p1EntryStart = 0, p1EntryEnd = 0, p1EntryLen = 0; |
| 210 byte[] p1Data = null; | 234 byte[] p1Data = null; |
| 211 boolean insideP1entry = false; | 235 boolean insideP1entry = false; |
