Extract common code when resizing arrays (#2257)

This commit is contained in:
Hannes Greule 2023-06-02 19:39:00 +02:00 committed by GitHub
parent 82418155f6
commit 4fa927f996
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 92 deletions

View File

@ -410,67 +410,47 @@ public class CharSetBlocks extends CharBlocks implements IChunkSet {
if (layer < minSectionPosition) { if (layer < minSectionPosition) {
int diff = minSectionPosition - layer; int diff = minSectionPosition - layer;
sectionCount += diff; sectionCount += diff;
char[][] tmpBlocks = new char[sectionCount][];
Section[] tmpSections = new Section[sectionCount];
Object[] tmpSectionLocks = new Object[sectionCount];
System.arraycopy(blocks, 0, tmpBlocks, diff, blocks.length);
System.arraycopy(sections, 0, tmpSections, diff, sections.length);
System.arraycopy(sectionLocks, 0, tmpSectionLocks, diff, sections.length);
for (int i = 0; i < diff; i++) {
tmpSections[i] = EMPTY;
tmpSectionLocks[i] = new Object();
}
blocks = tmpBlocks;
sections = tmpSections;
sectionLocks = tmpSectionLocks;
minSectionPosition = layer; minSectionPosition = layer;
if (biomes != null) { resizeSectionsArrays(diff, false); // prepend new layer(s)
BiomeType[][] tmpBiomes = new BiomeType[sectionCount][64];
System.arraycopy(biomes, 0, tmpBiomes, diff, biomes.length);
biomes = tmpBiomes;
}
if (light != null) {
char[][] tmplight = new char[sectionCount][];
System.arraycopy(light, 0, tmplight, diff, light.length);
light = tmplight;
}
if (skyLight != null) {
char[][] tmplight = new char[sectionCount][];
System.arraycopy(skyLight, 0, tmplight, diff, skyLight.length);
skyLight = tmplight;
}
} else { } else {
int diff = layer - maxSectionPosition; int diff = layer - maxSectionPosition;
sectionCount += diff; sectionCount += diff;
char[][] tmpBlocks = new char[sectionCount][];
Section[] tmpSections = new Section[sectionCount];
Object[] tmpSectionLocks = new Object[sectionCount];
System.arraycopy(blocks, 0, tmpBlocks, 0, blocks.length);
System.arraycopy(sections, 0, tmpSections, 0, sections.length);
System.arraycopy(sectionLocks, 0, tmpSectionLocks, 0, sections.length);
for (int i = sectionCount - diff; i < sectionCount; i++) {
tmpSections[i] = EMPTY;
tmpSectionLocks[i] = new Object();
}
blocks = tmpBlocks;
sections = tmpSections;
sectionLocks = tmpSectionLocks;
maxSectionPosition = layer; maxSectionPosition = layer;
if (biomes != null) { resizeSectionsArrays(diff, true); // append new layer(s)
BiomeType[][] tmpBiomes = new BiomeType[sectionCount][64]; }
System.arraycopy(biomes, 0, tmpBiomes, 0, biomes.length); }
biomes = tmpBiomes;
} private void resizeSectionsArrays(int diff, boolean appendNew) {
if (light != null) { char[][] tmpBlocks = new char[sectionCount][];
char[][] tmplight = new char[sectionCount][]; Section[] tmpSections = new Section[sectionCount];
System.arraycopy(light, 0, tmplight, 0, light.length); Object[] tmpSectionLocks = new Object[sectionCount];
light = tmplight; int destPos = appendNew ? 0 : diff;
} System.arraycopy(blocks, 0, tmpBlocks, destPos, blocks.length);
if (skyLight != null) { System.arraycopy(sections, 0, tmpSections, destPos, sections.length);
char[][] tmplight = new char[sectionCount][]; System.arraycopy(sectionLocks, 0, tmpSectionLocks, destPos, sections.length);
System.arraycopy(skyLight, 0, tmplight, 0, skyLight.length); int toFillFrom = appendNew ? sectionCount - diff : 0;
skyLight = tmplight; int toFillTo = appendNew ? sectionCount : diff;
} for (int i = toFillFrom; i < toFillTo; i++) {
tmpSections[i] = EMPTY;
tmpSectionLocks[i] = new Object();
}
blocks = tmpBlocks;
sections = tmpSections;
sectionLocks = tmpSectionLocks;
if (biomes != null) {
BiomeType[][] tmpBiomes = new BiomeType[sectionCount][64];
System.arraycopy(biomes, 0, tmpBiomes, destPos, biomes.length);
biomes = tmpBiomes;
}
if (light != null) {
char[][] tmplight = new char[sectionCount][];
System.arraycopy(light, 0, tmplight, destPos, light.length);
light = tmplight;
}
if (skyLight != null) {
char[][] tmplight = new char[sectionCount][];
System.arraycopy(skyLight, 0, tmplight, destPos, skyLight.length);
skyLight = tmplight;
} }
} }

View File

@ -489,47 +489,35 @@ public class ThreadUnsafeCharBlocks implements IChunkSet, IBlocks {
if (layer < minSectionPosition) { if (layer < minSectionPosition) {
int diff = minSectionPosition - layer; int diff = minSectionPosition - layer;
sectionCount += diff; sectionCount += diff;
char[][] tmpBlocks = new char[sectionCount][];
System.arraycopy(blocks, 0, tmpBlocks, diff, blocks.length);
blocks = tmpBlocks;
minSectionPosition = layer; minSectionPosition = layer;
if (biomes != null) { resizeSectionsArrays(layer, diff, false); // prepend new layer(s)
BiomeType[][] tmpBiomes = new BiomeType[sectionCount][64];
System.arraycopy(biomes, 0, tmpBiomes, diff, biomes.length);
biomes = tmpBiomes;
}
if (light != null) {
char[][] tmplight = new char[sectionCount][];
System.arraycopy(light, 0, tmplight, diff, light.length);
light = tmplight;
}
if (skyLight != null) {
char[][] tmplight = new char[sectionCount][];
System.arraycopy(skyLight, 0, tmplight, diff, skyLight.length);
skyLight = tmplight;
}
} else { } else {
int diff = layer - maxSectionPosition; int diff = layer - maxSectionPosition;
sectionCount += diff; sectionCount += diff;
char[][] tmpBlocks = new char[sectionCount][];
System.arraycopy(blocks, 0, tmpBlocks, 0, blocks.length);
blocks = tmpBlocks;
maxSectionPosition = layer; maxSectionPosition = layer;
if (biomes != null) { resizeSectionsArrays(layer, diff, true); // append new layer(s)
BiomeType[][] tmpBiomes = new BiomeType[sectionCount][64]; }
System.arraycopy(biomes, 0, tmpBiomes, 0, biomes.length); }
biomes = tmpBiomes;
} private void resizeSectionsArrays(int layer, int diff, boolean appendNew) {
if (light != null) { char[][] tmpBlocks = new char[sectionCount][];
char[][] tmplight = new char[sectionCount][]; int destPos = appendNew ? 0 : diff;
System.arraycopy(light, 0, tmplight, 0, light.length); System.arraycopy(blocks, 0, tmpBlocks, destPos, blocks.length);
light = tmplight; blocks = tmpBlocks;
} if (biomes != null) {
if (skyLight != null) { BiomeType[][] tmpBiomes = new BiomeType[sectionCount][64];
char[][] tmplight = new char[sectionCount][]; System.arraycopy(biomes, 0, tmpBiomes, destPos, biomes.length);
System.arraycopy(skyLight, 0, tmplight, 0, skyLight.length); biomes = tmpBiomes;
skyLight = tmplight; }
} if (light != null) {
char[][] tmplight = new char[sectionCount][];
System.arraycopy(light, 0, tmplight, destPos, light.length);
light = tmplight;
}
if (skyLight != null) {
char[][] tmplight = new char[sectionCount][];
System.arraycopy(skyLight, 0, tmplight, destPos, skyLight.length);
skyLight = tmplight;
} }
} }