Skip to content

Commit 9d1d1ae

Browse files
optimise: use row map based occupancy grid
1 parent b85c1b9 commit 9d1d1ae

1 file changed

Lines changed: 8 additions & 14 deletions

File tree

yoga/algorithm/grid/AutoPlacement.h

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,22 @@
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

35-
bool isOccupied(int32_t row, int32_t col) const {
36-
return cells.contains(cellKey(row, col));
37-
}
38-
3929
bool hasOverlap(int32_t rowStart, int32_t rowEnd, int32_t colStart, int32_t colEnd) const {
4030
for (int32_t row = rowStart; row < rowEnd; row++) {
41-
for (int32_t col = colStart; col < colEnd; col++) {
42-
if (isOccupied(row, col)) {
31+
auto it = rowIntervals.find(row);
32+
if (it == rowIntervals.end()) {
33+
continue;
34+
}
35+
for (const auto& interval : it->second) {
36+
if (interval.first < colEnd && interval.second > colStart) {
4337
return true;
4438
}
4539
}

0 commit comments

Comments
 (0)