这次我写了一个Java——作为例外——一个完整的代码:)我没有运行可能我会在索引时错过一些东西,但如果要存储的点超过20个,我会使用与此类似的东西。超过 1000 点毫无疑问使用这个或列表..
public class Spatial {
public static final int maxX = 1024;
public static final int maxY = 768;
public static final int maxZ = 100;
// 1024x768x100= 78 643 200
// int max value:2,147,483,647
private byte[] indexData;
public Spatial() {
int totalDataCount = maxX * maxY * maxZ;
int byteAarraySizeNeeded = totalDataCount / 8 + totalDataCount % 8;
indexData = new byte[byteAarraySizeNeeded]; // inited with all 0
}
public void markPresent(int x, int y, int z, boolean present) {
// TODO: check parameters!!! minimum and max values!
int index = (z * 1 + y * maxZ + maxX * (maxX * maxY));
// transform the index to our storage index : maybe a bug here, cheack t pls!
int arrayIndex = index / 8 + index % 8;
byte dataChunck = indexData[arrayIndex];
if (present) { // bitwise Or with 1
dataChunck = (byte) (dataChunck | (1 << index % 8));
} else { // bitwise And with 0
byte helper = (byte) (1 << index % 8);
byte all1ExceptOne = (byte) (~helper & 0xFF);
dataChunck = (byte) (dataChunck & all1ExceptOne);
}
// put back:
indexData[arrayIndex] = dataChunck;
}
public boolean isPresent(int x, int y, int z) {
// TODO: check parameters!!! minimum and max values!
int index = (z * 1 + y * maxZ + maxX * (maxX * maxY));
// transform the index to our storage index : maybe a bug here, cheack t pls!
int arrayIndex = index / 8 + index % 8;
byte dataChunck = indexData[arrayIndex];
return (dataChunck & (1 << index % 8)) > 0;
}
}