annotate src/org/tmatesoft/hg/internal/diff/RangePairSeq.java @ 703:7839ff0bfd78

Refactor: move diff/blame related code to a separate package
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 14 Aug 2013 14:51:51 +0200
parents src/org/tmatesoft/hg/internal/RangePairSeq.java@cce0387c6041
children
rev   line source
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2013 TMate Software Ltd
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
703
7839ff0bfd78 Refactor: move diff/blame related code to a separate package
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 674
diff changeset
17 package org.tmatesoft.hg.internal.diff;
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.util.Formatter;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20
703
7839ff0bfd78 Refactor: move diff/blame related code to a separate package
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 674
diff changeset
21 import org.tmatesoft.hg.internal.IntSliceSeq;
7839ff0bfd78 Refactor: move diff/blame related code to a separate package
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 674
diff changeset
22 import org.tmatesoft.hg.internal.IntTuple;
7839ff0bfd78 Refactor: move diff/blame related code to a separate package
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 674
diff changeset
23
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 /**
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 * Sequence of range pairs (denoted origin and target), {originStart, targetStart, length}, tailored for diff/annotate
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 *
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 * @author Artem Tikhomirov
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 * @author TMate Software Ltd.
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 */
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
30 public final class RangePairSeq {
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
31 private final IntSliceSeq ranges = new IntSliceSeq(3);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 public void add(int start1, int start2, int length) {
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
34 int count = ranges.size();
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 if (count > 0) {
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
36 int lastS1 = ranges.get(--count, 0);
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
37 int lastS2 = ranges.get(count, 1);
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
38 int lastLen = ranges.get(count, 2);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 if (start1 == lastS1 + lastLen && start2 == lastS2 + lastLen) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 // new range continues the previous one - just increase the length
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
41 ranges.set(count, lastS1, lastS2, lastLen + length);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 return;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 ranges.add(start1, start2, length);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 public void clear() {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 ranges.clear();
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 public int size() {
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
53 return ranges.size();
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 /**
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 * find out line index in the target that matches specified origin line
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 */
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 public int mapLineIndex(int ln) {
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
60 for (IntTuple t : ranges) {
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
61 int s1 = t.at(0);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 if (s1 > ln) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 return -1;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 }
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
65 int l = t.at(2);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 if (s1 + l > ln) {
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
67 int s2 = t.at(1);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 return s2 + (ln - s1);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 return -1;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 /**
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 * find out line index in origin that matches specified target line
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 */
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 public int reverseMapLine(int targetLine) {
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
78 for (IntTuple t : ranges) {
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
79 int ts = t.at(1);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 if (ts > targetLine) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 return -1;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 }
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
83 int l = t.at(2);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 if (ts + l > targetLine) {
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
85 int os = t.at(0);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 return os + (targetLine - ts);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 return -1;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
92 public RangePairSeq intersect(RangePairSeq target) {
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
93 RangePairSeq v = new RangePairSeq();
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
94 for (IntTuple t : ranges) {
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
95 int originLine = t.at(0);
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
96 int targetLine = t.at(1);
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
97 int length = t.at(2);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 int startTargetLine = -1, startOriginLine = -1, c = 0;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 for (int j = 0; j < length; j++) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 int lnInFinal = target.mapLineIndex(targetLine + j);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 if (lnInFinal == -1 || (startTargetLine != -1 && lnInFinal != startTargetLine + c)) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 // the line is not among "same" in ultimate origin
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 // or belongs to another/next "same" chunk
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104 if (startOriginLine == -1) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 continue;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 v.add(startOriginLine, startTargetLine, c);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
108 c = 0;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109 startOriginLine = startTargetLine = -1;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 // fall-through to check if it's not complete miss but a next chunk
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
111 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
112 if (lnInFinal != -1) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
113 if (startOriginLine == -1) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
114 startOriginLine = originLine + j;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
115 startTargetLine = lnInFinal;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
116 c = 1;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
117 } else {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 // lnInFinal != startTargetLine + s is covered above
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119 assert lnInFinal == startTargetLine + c;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120 c++;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
121 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
122 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
123 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124 if (startOriginLine != -1) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
125 assert c > 0;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
126 v.add(startOriginLine, startTargetLine, c);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
127 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
128 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
129 return v;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
130 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
131
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
132 // true when specified line in origin is equal to a line in target
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
133 public boolean includesOriginLine(int ln) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
134 return includes(ln, 0);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
135 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
136
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
137 // true when specified line in target is equal to a line in origin
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
138 public boolean includesTargetLine(int ln) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
139 return includes(ln, 1);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
140 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
141
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
142 private boolean includes(int ln, int o) {
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
143 for (IntTuple t : ranges) {
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
144 int rangeStart = t.at(o);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
145 if (rangeStart > ln) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
146 return false;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
147 }
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
148 int rangeLen = t.at(2);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
149 if (rangeStart + rangeLen > ln) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
150 return true;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
151 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
152 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
153 return false;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
154 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
155
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
156 public CharSequence dump() {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
157 StringBuilder sb = new StringBuilder();
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
158 Formatter f = new Formatter(sb);
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
159 for (IntTuple t : ranges) {
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
160 int s1 = t.at(0);
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
161 int s2 = t.at(1);
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
162 int len = t.at(2);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
163 f.format("[%d..%d) == [%d..%d); ", s1, s1 + len, s2, s2 + len);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
164 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
165 return sb;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
166 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
167
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
168 @Override
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
169 public String toString() {
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
170 return String.format("RangeSeq[%d]:%s", size(), dump());
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
171 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
172 }