使用两个标识符(rowIndex,colIndex)比较数组的快速方法

Fast way to compare arrays using two identifyer (rowIndex, colIndex)

你好,我正在开发一个使用块的 2d 地图加载器。

我正在使用这种方法来计算块与屏幕边界相交的位置。

// left bottom
rect.x = x - x % chunkSize;
if (x < 0) rect.x -= chunkSize
rect.colIndex = (int) (rect.x / chunkSize);

rect.y = y - y % chunkSize;
if (y < 0) rect.y -= chunkSize;
rect.rowIndex = (int) (rect.y / chunkSize);

// right top
float x2 = x + width, y2 = y + height;

float rectX = x2 - x2 % chunkSize;
if (x2 < 0) rectX -= chunkSize;
int colIndex = (int) (rectX / chunkSize);

float rectY = y2 - y2 % chunkSize;
if (y2 < 0) rectY -= chunkSize;
int rowIndex = (int) (rectY / chunkSize);

rect.rows = rowIndex - rect.rowIndex + 1;
rect.cols = colIndex - rect.colIndex + 1;

for (int i = 0; i < rect.rows; i++) {
    for (int j = 0; j < rect.cols; j++) {
        Chunk chunk = chunkPool.obtain();
        chunk.x = rect.x + chunkSize * j;
        chunk.y = rect.y + chunkSize * i;
        chunk.rowIndex = rect.rowIndex + i;
        chunk.colIndex = rect.colIndex + j;
    }
}

信息:

我的问题是:我需要知道从最后一个数组中删除/添加了哪些块。我怎样才能做到这一点。为了识别,我会使用 rowIndex / colIndex。它应该很快,因为它会被频繁调用。可以选择哈希表吗?

一个散列table只给你一个查找键,你需要按行和列查找值。因此,您需要创建复合键或使用类似 table 的数据结构来获取所需的二维。恰巧 Guava 已经在这里实现了这个:https://github.com/google/guava/wiki/NewCollectionTypesExplained#table