|
18 | 18 | namespace facebook::yoga { |
19 | 19 |
|
20 | 20 | 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; |
26 | 22 |
|
27 | 23 | void markOccupied(int32_t rowStart, int32_t rowEnd, int32_t colStart, int32_t colEnd) { |
28 | 24 | 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}); |
32 | 26 | } |
33 | 27 | } |
34 | 28 |
|
35 | 29 | 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; |
37 | 40 | } |
38 | 41 |
|
39 | 42 | bool hasOverlap(int32_t rowStart, int32_t rowEnd, int32_t colStart, int32_t colEnd) const { |
40 | 43 | 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) { |
43 | 51 | return true; |
44 | 52 | } |
45 | 53 | } |
|
0 commit comments