Skip to content

Commit 96c9cff

Browse files
committed
Pre-compute cell keys for grid sort avoiding floor()
1 parent 8da5e33 commit 96c9cff

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

include/dbscan/grid.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,24 @@ struct grid {
205205
freeFlag=true;}
206206

207207
parallel_for(0, nn, [&](intT i){I[i] = i;});
208+
209+
// Pre-compute integer cell coordinates to avoid floor() in every sort comparison
210+
auto cellKeys = newA(intT, nn * dim);
211+
floatT invR = 1.0 / r;
212+
parallel_for(0, nn, [&](intT i) {
213+
for (int d = 0; d < dim; d++) {
214+
cellKeys[i * dim + d] = (intT)floor((P[i][d] - pMin[d]) * invR);
215+
}
216+
});
208217
auto ipLess = [&] (intT a, intT b) {
209-
return pointGridCmp<dim, objT, geoPointT>(P[a], P[b], pMin, r);};
218+
for (int d = 0; d < dim; d++) {
219+
intT ca = cellKeys[a * dim + d];
220+
intT cb = cellKeys[b * dim + d];
221+
if (ca != cb) return ca < cb;
222+
}
223+
return false;};
210224
sampleSort(I, nn, ipLess);
225+
free(cellKeys);
211226
parallel_for(0, nn, [&](intT i){PP[i] = P[I[i]];});
212227

213228
flag[0] = 1;

0 commit comments

Comments
 (0)