Skip to content

Commit 2d76996

Browse files
optimise: use row map based occupancy grid
1 parent b85c1b9 commit 2d76996

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

yoga/algorithm/grid/AutoPlacement.h

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,36 @@
1818
namespace facebook::yoga {
1919

2020
struct OccupancyGrid {
21-
std::unordered_set<int64_t> cells;
22-
23-
static int64_t cellKey(int32_t row, int32_t col) {
24-
return (static_cast<int64_t>(row) << 32) | static_cast<uint32_t>(col);
25-
}
21+
std::unordered_map<int32_t, std::vector<std::pair<int32_t, int32_t>>> rowIntervals;
2622

2723
void markOccupied(int32_t rowStart, int32_t rowEnd, int32_t colStart, int32_t colEnd) {
2824
for (int32_t row = rowStart; row < rowEnd; row++) {
29-
for (int32_t col = colStart; col < colEnd; col++) {
30-
cells.insert(cellKey(row, col));
31-
}
25+
rowIntervals[row].push_back({colStart, colEnd});
3226
}
3327
}
3428

3529
bool isOccupied(int32_t row, int32_t col) const {
36-
return cells.contains(cellKey(row, col));
30+
auto it = rowIntervals.find(row);
31+
if (it == rowIntervals.end()) {
32+
return false;
33+
}
34+
for (const auto& interval : it->second) {
35+
if (col >= interval.first && col < interval.second) {
36+
return true;
37+
}
38+
}
39+
return false;
3740
}
3841

3942
bool hasOverlap(int32_t rowStart, int32_t rowEnd, int32_t colStart, int32_t colEnd) const {
4043
for (int32_t row = rowStart; row < rowEnd; row++) {
41-
for (int32_t col = colStart; col < colEnd; col++) {
42-
if (isOccupied(row, col)) {
44+
auto it = rowIntervals.find(row);
45+
if (it == rowIntervals.end()) {
46+
continue;
47+
}
48+
for (const auto& interval : it->second) {
49+
// Check if intervals [interval.first, interval.second) and [colStart, colEnd) overlap
50+
if (interval.first < colEnd && interval.second > colStart) {
4351
return true;
4452
}
4553
}

0 commit comments

Comments
 (0)