comparison src/org/tmatesoft/hg/internal/IntSliceSeq.java @ 680:58a6900f845d

Blame: alternative strategy to handle merge revisions: map(diff(p1->base->p2)) to understand merge intentions better
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sun, 21 Jul 2013 17:15:34 +0200
parents 19f5167c2155
children
comparison
equal deleted inserted replaced
679:19f5167c2155 680:58a6900f845d
22 /** 22 /**
23 * 23 *
24 * @author Artem Tikhomirov 24 * @author Artem Tikhomirov
25 * @author TMate Software Ltd. 25 * @author TMate Software Ltd.
26 */ 26 */
27 public final class IntSliceSeq implements Iterable<IntTuple> { 27 public final class IntSliceSeq implements Iterable<IntTuple>, Cloneable {
28 private final IntVector slices; 28 private final IntVector slices;
29 private final int slice; 29 private final int slice;
30 30
31 public IntSliceSeq(int sliceSize) { 31 public IntSliceSeq(int sliceSize) {
32 // initial size/grow values are pure guess 32 // initial size/grow values are pure guess
37 slices = new IntVector(sliceSize * initialSlices, sliceSize*slicesToGrow); 37 slices = new IntVector(sliceSize * initialSlices, sliceSize*slicesToGrow);
38 slice = sliceSize; 38 slice = sliceSize;
39 } 39 }
40 40
41 public IntSliceSeq add(int... values) { 41 public IntSliceSeq add(int... values) {
42 checkValues(values); 42 checkValuesAny(values);
43 slices.add(values); 43 slices.add(values);
44 return this; 44 return this;
45 } 45 }
46 46
47 public IntSliceSeq set(int sliceIndex, int... values) { 47 public IntSliceSeq set(int sliceIndex, int... values) {
48 checkValues(values); 48 checkValuesExact(values);
49 for (int i = 0, j = sliceIndex*slice; i < slice; i++,j++) { 49 for (int i = 0, j = sliceIndex*slice; i < slice; i++,j++) {
50 slices.set(j, values[i]); 50 slices.set(j, values[i]);
51 } 51 }
52 return this; 52 return this;
53 } 53 }
59 59
60 public int get(int sliceIndex, int valueIndex) { 60 public int get(int sliceIndex, int valueIndex) {
61 checkArgRange(size(), sliceIndex); 61 checkArgRange(size(), sliceIndex);
62 checkArgRange(slice, valueIndex); 62 checkArgRange(slice, valueIndex);
63 return slices.get(sliceIndex*slice + valueIndex); 63 return slices.get(sliceIndex*slice + valueIndex);
64 }
65
66 public void addAll(IntSliceSeq other) {
67 if (other.slice != this.slice) {
68 throw new IllegalArgumentException(String.format("Tuple size doesn't match: %d and %d", slice, other.slice));
69 }
70 slices.addAll(other.slices);
64 } 71 }
65 72
66 public int size() { 73 public int size() {
67 return slices.size() / slice; 74 return slices.size() / slice;
68 } 75 }
115 sb.append(')'); 122 sb.append(')');
116 sb.append(' '); 123 sb.append(' ');
117 } 124 }
118 return sb.toString(); 125 return sb.toString();
119 } 126 }
127
128 @Override
129 public IntSliceSeq clone() {
130 try {
131 return (IntSliceSeq) super.clone();
132 } catch (CloneNotSupportedException ex) {
133 throw new Error(ex);
134 }
135 }
120 136
121 private void checkArgRange(int rangeSize, int index) { 137 private void checkArgRange(int rangeSize, int index) {
122 if (index >= 0 && index < rangeSize) { 138 if (index >= 0 && index < rangeSize) {
123 return; 139 return;
124 } 140 }
125 throw new IllegalArgumentException(String.valueOf(index)); 141 throw new IllegalArgumentException(String.valueOf(index));
126 } 142 }
127 private void checkValues(int[] values) { 143 private void checkValuesExact(int[] values) {
128 if (values == null || values.length != slice) { 144 if (values == null || values.length != slice) {
129 throw new IllegalArgumentException(String.valueOf(values == null ? values : values.length)); 145 throw new IllegalArgumentException(String.valueOf(values == null ? values : values.length));
130 } 146 }
131 } 147 }
148 private void checkValuesAny(int[] values) {
149 if (values == null || values.length % slice != 0) {
150 throw new IllegalArgumentException(String.valueOf(values == null ? values : values.length));
151 }
152 }
132 } 153 }