我寫了一個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;
}
}
爲什麼關閉lool?請看這個答案和我的評論:http://stackoverflow.com/a/19541461/529543 75-78Mb分配可能會有點浪費資源,但一個布爾[]恢復到18Mb,並在3Mb下位級管理!我會存儲這些3Mb而不是迭代列表。 – 2013-10-23 12:41:19
看到3Mb的實施:http://stackoverflow.com/a/19542553/529543修復索引,如果是錯誤的 – 2013-10-23 13:08:32