Skip to content

Commit 0eb07f1

Browse files
Moved file structure knowledge out of SubArea into DataAccess
1 parent 2ead579 commit 0eb07f1

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

src/main/java/com/mapcode/DataAccess.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class DataAccess {
3939
// Read data only once in static initializer.
4040
static {
4141
LOG.info("DataAccess: reading regions from file: {}", FILE_NAME);
42-
final int bufferSize = 100000;
42+
final int bufferSize = 131072;
4343
final byte[] readBuffer = new byte[bufferSize];
4444
int total = 0;
4545
try {
@@ -57,10 +57,16 @@ class DataAccess {
5757
// Copy stream as unsigned bytes (ints).
5858
final byte[] bytes = outputStream.toByteArray();
5959
assert total == bytes.length;
60-
FILE_DATA = new int[total];
61-
for (int i = 0; i < total; ++i) {
62-
FILE_DATA[i] = (bytes[i] < 0) ? (bytes[i] + 256) : bytes[i];
63-
60+
FILE_DATA = new int[total / 4];
61+
for (int i = 0; i < total; i++) {
62+
final int b1 = (bytes[i] < 0) ? (bytes[i] + 256) : bytes[i];
63+
i++;
64+
final int b2 = (bytes[i] < 0) ? (bytes[i] + 256) : bytes[i];
65+
i++;
66+
final int b3 = (bytes[i] < 0) ? (bytes[i] + 256) : bytes[i];
67+
i++;
68+
final int b4 = (bytes[i] < 0) ? (bytes[i] + 256) : bytes[i];
69+
FILE_DATA[i / 4] = b1 + (b2 << 8) + (b3 << 16) + (b4 << 24);
6470
}
6571
} finally {
6672
outputStream.close();
@@ -79,21 +85,28 @@ private DataAccess() {
7985
// Empty.
8086
}
8187

82-
static int dataFlags(final int i) {
83-
return FILE_DATA[(i * 20) + 16] +
84-
(FILE_DATA[(i * 20) + 17] * 256);
88+
static int minx(final int i) {
89+
return FILE_DATA[i * 5];
90+
}
91+
92+
static int miny(final int i) {
93+
return FILE_DATA[(i * 5) + 1];
94+
}
95+
96+
static int maxx(final int i) {
97+
return FILE_DATA[(i * 5) + 2];
8598
}
8699

87-
static int asLong(final int i) {
88-
return FILE_DATA[i] +
89-
(FILE_DATA[i + 1] << 8) +
90-
(FILE_DATA[i + 2] << 16) +
91-
(FILE_DATA[i + 3] << 24);
100+
static int maxy(final int i) {
101+
return FILE_DATA[(i * 5) + 3];
102+
}
103+
104+
static int dataFlags(final int i) {
105+
return FILE_DATA[(i * 5) + 4] & 65535;
92106
}
93107

94108
static int smartDiv(final int i) {
95-
return FILE_DATA[(i * 20) + 18] +
96-
(FILE_DATA[(i * 20) + 19] * 256);
109+
return FILE_DATA[(i * 5) + 4] >> 16;
97110
}
98111

99112
private final static int[] DATA_START = {
@@ -161,8 +174,4 @@ static int dataFirstRecord(final int ccode) {
161174
static int dataLastRecord(final int ccode) {
162175
return DATA_START[ccode + 1] - 1;
163176
}
164-
165-
static int numberOfSubAreas() {
166-
return DATA_START[DATA_START.length - 1];
167-
}
168177
}

src/main/java/com/mapcode/MapcodeCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public static boolean multipleBordersNearby(@Nonnull final Point point, @Nonnull
276276
for (int m = upto; m >= from; m--) {
277277
if (!Data.isRestricted(m)) {
278278
final SubArea boundaries = Data.getBoundaries(m);
279-
final int xdiv8 = Common.xDivider(boundaries.getMinY(),boundaries.getMaxY()) / 4; // @@@
279+
final int xdiv8 = Common.xDivider(boundaries.getMinY(),boundaries.getMaxY()) / 4;
280280
if (boundaries.extendBounds(xdiv8, 60).containsPoint(point)) {
281281
if (!boundaries.extendBounds(-xdiv8, -60).containsPoint(point)) {
282282
nrFound++;

src/main/java/com/mapcode/SubArea.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,18 +319,10 @@ boolean containsLongitude(final int argLonMicroDeg) {
319319
}
320320

321321
private void minMaxSetup(final int arg) {
322-
int i = arg * 20;
323-
final int minX = DataAccess.asLong(i);
324-
325-
i += 4;
326-
final int minY = DataAccess.asLong(i);
327-
328-
i += 4;
329-
final int maxX = DataAccess.asLong(i);
330-
331-
i += 4;
332-
final int maxY = DataAccess.asLong(i);
333-
322+
final int minX = DataAccess.minx(arg);
323+
final int minY = DataAccess.miny(arg);
324+
final int maxX = DataAccess.maxx(arg);
325+
final int maxY = DataAccess.maxy(arg);
334326
latRange = new Range<Integer>(minY, maxY);
335327
lonRange = new Range<Integer>(minX, maxX);
336328
}

0 commit comments

Comments
 (0)