annotate src/org/tmatesoft/hg/internal/RangePairSeq.java @ 681:4f93bbc73b64

Do not instantiate thousands of small arrays(numerous readInt/readLong calls)
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sun, 21 Jul 2013 17:48:05 +0200
parents 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 */
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
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
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 /**
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 * 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
23 *
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 * @author Artem Tikhomirov
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 * @author TMate Software Ltd.
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 */
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
27 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
28 private final IntSliceSeq ranges = new IntSliceSeq(3);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 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
31 int count = ranges.size();
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 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
33 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
34 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
35 int lastLen = ranges.get(count, 2);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 if (start1 == lastS1 + lastLen && start2 == lastS2 + lastLen) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 // 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
38 ranges.set(count, lastS1, lastS2, lastLen + length);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 return;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 ranges.add(start1, start2, length);
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 public void clear() {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 ranges.clear();
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
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 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
50 return ranges.size();
558
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
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 /**
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 * 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
55 */
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 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
57 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
58 int s1 = t.at(0);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 if (s1 > ln) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 return -1;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 }
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
62 int l = t.at(2);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 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
64 int s2 = t.at(1);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 return s2 + (ln - s1);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 return -1;
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 /**
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 * 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
73 */
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 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
75 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
76 int ts = t.at(1);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 if (ts > targetLine) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 return -1;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 }
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
80 int l = t.at(2);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 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
82 int os = t.at(0);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 return os + (targetLine - ts);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 return -1;
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
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
89 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
90 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
91 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
92 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
93 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
94 int length = t.at(2);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 int startTargetLine = -1, startOriginLine = -1, c = 0;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 for (int j = 0; j < length; j++) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 int lnInFinal = target.mapLineIndex(targetLine + j);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 if (lnInFinal == -1 || (startTargetLine != -1 && lnInFinal != startTargetLine + c)) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 // 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
100 // or belongs to another/next "same" chunk
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 if (startOriginLine == -1) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 continue;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104 v.add(startOriginLine, startTargetLine, c);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 c = 0;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 startOriginLine = startTargetLine = -1;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 // 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
108 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109 if (lnInFinal != -1) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 if (startOriginLine == -1) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
111 startOriginLine = originLine + j;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
112 startTargetLine = lnInFinal;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
113 c = 1;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
114 } else {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
115 // lnInFinal != startTargetLine + s is covered above
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
116 assert lnInFinal == startTargetLine + c;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
117 c++;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
121 if (startOriginLine != -1) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
122 assert c > 0;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
123 v.add(startOriginLine, startTargetLine, c);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
125 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
126 return v;
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 // 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
130 public boolean includesOriginLine(int ln) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
131 return includes(ln, 0);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
132 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
133
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
134 // 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
135 public boolean includesTargetLine(int ln) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
136 return includes(ln, 1);
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
137 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
138
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
139 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
140 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
141 int rangeStart = t.at(o);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
142 if (rangeStart > ln) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
143 return false;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
144 }
674
cce0387c6041 Introduced dedicated IntSliceSeq/IntTuple in place of IntArray with subsequences
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 561
diff changeset
145 int rangeLen = t.at(2);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
146 if (rangeStart + rangeLen > ln) {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
147 return true;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
148 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
149 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
150 return false;
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 public CharSequence dump() {
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
154 StringBuilder sb = new StringBuilder();
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
155 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
156 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
157 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
158 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
159 int len = t.at(2);
558
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
160 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
161 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
162 return sb;
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
163 }
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 @Override
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
166 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
167 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
168 }
154718ae23ed Annotate: refactor/reuse range handling code
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
169 }