-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathIndexingUtils.java
More file actions
201 lines (171 loc) · 7.65 KB
/
Copy pathIndexingUtils.java
File metadata and controls
201 lines (171 loc) · 7.65 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package dev.zarr.zarrjava.utils;
import java.util.Arrays;
public class IndexingUtils {
public static long[][] computeChunkCoords(long[] arrayShape, int[] chunkShape) {
return computeChunkCoords(arrayShape, chunkShape, new long[arrayShape.length],
Utils.toIntArray(arrayShape));
}
public static long[][] computeChunkCoords(int[] arrayShape, int[] chunkShape) {
return computeChunkCoords(Utils.toLongArray(arrayShape), chunkShape);
}
public static long[][] computeChunkCoords(long[] arrayShape, int[] chunkShape, long[] selOffset,
int[] selShape) {
final int ndim = arrayShape.length;
long[] start = new long[ndim];
long[] end = new long[ndim];
int numChunks = 1;
for (int dimIdx = 0; dimIdx < ndim; dimIdx++) {
final int staIdx = (int) (selOffset[dimIdx] / chunkShape[dimIdx]);
final int endIdx = (int) ((selOffset[dimIdx] + selShape[dimIdx] - 1) / chunkShape[dimIdx]);
numChunks *= (endIdx - staIdx + 1);
start[dimIdx] = staIdx;
end[dimIdx] = endIdx;
}
final long[][] chunkCoords = new long[numChunks][];
final long[] currentIdx = Arrays.copyOf(start, ndim);
for (int i = 0; i < chunkCoords.length; i++) {
chunkCoords[i] = Arrays.copyOf(currentIdx, ndim);
int dimIdx = ndim - 1;
while (dimIdx >= 0) {
if (currentIdx[dimIdx] >= end[dimIdx]) {
currentIdx[dimIdx] = start[dimIdx];
dimIdx--;
} else {
currentIdx[dimIdx]++;
dimIdx = -1;
}
}
}
return chunkCoords;
}
public static ChunkProjection computeProjection(long[] chunkCoords, int[] arrayShape,
int[] chunkShape) {
return computeProjection(chunkCoords, Utils.toLongArray(arrayShape), chunkShape);
}
public static ChunkProjection computeProjection(long[] chunkCoords, long[] arrayShape,
int[] chunkShape) {
return computeProjection(chunkCoords, arrayShape, chunkShape, new long[chunkCoords.length],
Utils.toIntArray(arrayShape)
);
}
public static ChunkProjection computeProjection(
final long[] chunkCoords, final long[] arrayShape,
final int[] chunkShape, final long[] selOffset,
final int[] selShape
) {
final int ndim = chunkCoords.length;
final int[] chunkOffset = new int[ndim];
final int[] outOffset = new int[ndim];
final int[] shape = new int[ndim];
for (int dimIdx = 0; dimIdx < chunkCoords.length; dimIdx++) {
// compute offsets for chunk within overall array
final long dimOffset = (long) chunkShape[dimIdx] * chunkCoords[dimIdx];
final long dimLimit = Math.min(arrayShape[dimIdx],
(chunkCoords[dimIdx] + 1) * (long) chunkShape[dimIdx]);
if (selOffset[dimIdx] < dimOffset) {
// selection starts before current chunk
chunkOffset[dimIdx] = 0;
// compute number of previous items, provides offset into output array
outOffset[dimIdx] = (int) (dimOffset - selOffset[dimIdx]);
} else {
// selection starts within current chunk
chunkOffset[dimIdx] = (int) (selOffset[dimIdx] - dimOffset);
outOffset[dimIdx] = 0;
}
if (selOffset[dimIdx] + selShape[dimIdx] > dimLimit) {
// selection ends after current chunk
shape[dimIdx] = chunkShape[dimIdx] - chunkOffset[dimIdx];
} else {
// selection ends within current chunk
shape[dimIdx] = (int) (selOffset[dimIdx] + selShape[dimIdx] - dimOffset
- chunkOffset[dimIdx]);
}
}
return new ChunkProjection(chunkCoords, chunkOffset, outOffset, shape);
}
public static long cOrderIndex(final long[] chunkCoords, final long[] arrayShape) {
long index = 0;
long multiplier = 1;
for (int i = arrayShape.length - 1; i >= 0; i--) {
index += chunkCoords[i] * multiplier;
multiplier *= arrayShape[i];
}
return index;
}
public static long fOrderIndex(final long[] chunkCoords, final long[] arrayShape) {
int index = 0;
int multiplier = 1;
for (int i = 0; i < arrayShape.length; i++) {
index += chunkCoords[i] * multiplier;
multiplier *= arrayShape[i];
}
return index;
}
public static boolean isFullChunk(final int[] selOffset, final int[] selShape,
final int[] chunkShape) {
if (selOffset.length != selShape.length) {
throw new IllegalArgumentException("'selOffset' and 'selShape' need to have the same rank.");
}
if (selOffset.length != chunkShape.length) {
throw new IllegalArgumentException(
"'selOffset' and 'chunkShape' need to have the same rank.");
}
for (int dimIdx = 0; dimIdx < selOffset.length; dimIdx++) {
if (selOffset[dimIdx] != 0 || selShape[dimIdx] != chunkShape[dimIdx]) {
return false;
}
}
return true;
}
public static boolean isSingleFullChunk(final long[] selOffset, final int[] selShape,
final int[] chunkShape) {
if (selOffset.length != selShape.length) {
throw new IllegalArgumentException("'selOffset' and 'selShape' need to have the same rank.");
}
if (selOffset.length != chunkShape.length) {
throw new IllegalArgumentException(
"'selOffset' and 'chunkShape' need to have the same rank.");
}
for (int dimIdx = 0; dimIdx < selOffset.length; dimIdx++) {
if (selOffset[dimIdx] % chunkShape[dimIdx] != 0 || selShape[dimIdx] != chunkShape[dimIdx]) {
return false;
}
}
return true;
}
public static long[] computeSingleChunkCoords(final long[] selOffset, final int[] chunkShape) {
if (selOffset.length != chunkShape.length) {
throw new IllegalArgumentException(
"'selOffset' and 'chunkShape' need to have the same rank.");
}
final long[] chunkCoords = new long[selOffset.length];
for (int dimIdx = 0; dimIdx < selOffset.length; dimIdx++) {
chunkCoords[dimIdx] = (selOffset[dimIdx] / chunkShape[dimIdx]);
}
return chunkCoords;
}
public static final class ChunkProjection {
final public long[] chunkCoords;
final public int[] chunkOffset;
final public int[] outOffset;
final public int[] shape;
public ChunkProjection(
final long[] chunkCoords, final int[] chunkOffset, final int[] outOffset,
final int[] shape
) {
this.chunkCoords = chunkCoords;
this.chunkOffset = chunkOffset;
this.outOffset = outOffset;
this.shape = shape;
}
@Override
public String toString() {
return "ChunkProjection{" +
"chunkCoords=" + Arrays.toString(chunkCoords) +
", chunkOffset=" + Arrays.toString(chunkOffset) +
", outOffset=" + Arrays.toString(outOffset) +
", shape=" + Arrays.toString(shape) +
'}';
}
}
}