-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathRectangleSpliterator.java
More file actions
executable file
·118 lines (98 loc) · 4 KB
/
RectangleSpliterator.java
File metadata and controls
executable file
·118 lines (98 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package spliterators.part1.exercise;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.IntConsumer;
public class RectangleSpliterator extends Spliterators.AbstractIntSpliterator {
private int[][] array;
private int startOuterIncl;
private int endOuterExcl;
private int innerLength;
private int startInnerIncl;
private int endInnerExcl;
public RectangleSpliterator(int[][] array) {
this(array, 0, array.length,
0, 0);
this.endInnerExcl = innerLength;
}
private RectangleSpliterator(int[][] array, int startOuterIncl, int endOuterExcl,
int startInnerIncl, int endInnerExcl) {
super(Long.MAX_VALUE,
Spliterator.IMMUTABLE |
Spliterator.ORDERED |
Spliterator.SIZED |
Spliterator.SUBSIZED |
Spliterator.NONNULL);
this.innerLength = array.length == 0 ? 0 : array[0].length;
this.array = array;
this.startOuterIncl = startOuterIncl;
this.endOuterExcl = endOuterExcl;
this.startInnerIncl = startInnerIncl;
this.endInnerExcl = endInnerExcl;
}
@Override
public OfInt trySplit() {
int rowNumber = endOuterExcl - startOuterIncl;
int estimatedSize = (int) estimateSize();
if (rowNumber < 2 && estimatedSize < 2) {
return null;
}
int halfEstimatedSize = estimatedSize / 2;
int halfSizeWithoutStartLine = halfEstimatedSize - (innerLength - startInnerIncl);
int midOuterIncl = startOuterIncl;
int midInnerIncl = startInnerIncl;
if (halfSizeWithoutStartLine > 0) {
int amountFullRows = halfSizeWithoutStartLine / innerLength;
if (halfSizeWithoutStartLine % innerLength > 0) {
midOuterIncl = midOuterIncl + amountFullRows + 1;
midInnerIncl = halfSizeWithoutStartLine - amountFullRows*innerLength;
} else {
midOuterIncl += amountFullRows;
midInnerIncl = innerLength;
}
} else {
midInnerIncl += halfEstimatedSize;
}
final RectangleSpliterator rs = new RectangleSpliterator(array, startOuterIncl, midOuterIncl + 1,
startInnerIncl, midInnerIncl);
startOuterIncl = midInnerIncl == innerLength ? midOuterIncl + 1 : midOuterIncl;
startInnerIncl = midInnerIncl == innerLength ? 0 : midInnerIncl;
return rs;
}
@Override
public long estimateSize() {
int rowNumber = endOuterExcl - startOuterIncl;
if (rowNumber > 2) {
return innerLength - startInnerIncl + endInnerExcl + (rowNumber - 2) * innerLength;
} else if (rowNumber == 2) {
return innerLength - startInnerIncl + endInnerExcl;
} else {
return endInnerExcl - startInnerIncl;
}
}
@Override
public boolean tryAdvance(IntConsumer action) {
if (startOuterIncl == endOuterExcl - 1 && startInnerIncl >= endInnerExcl || startOuterIncl >= endOuterExcl) {
return false;
}
int value = array[startOuterIncl][startInnerIncl];
action.accept(value);
startInnerIncl++;
if (startInnerIncl == innerLength) {
startInnerIncl = 0;
startOuterIncl++;
}
return true;
}
@Override
public void forEachRemaining(IntConsumer action) {
for (int out = startOuterIncl; out < endOuterExcl; out++) {
int endInnerLocalExcl = out == (endOuterExcl - 1) ? endInnerExcl : innerLength;
for (int in = startInnerIncl; in < endInnerLocalExcl; in++) {
action.accept(array[out][in]);
}
startInnerIncl = 0;
}
startOuterIncl = endOuterExcl;
startInnerIncl = endInnerExcl;
}
}