@@ -111,31 +111,32 @@ struct t_compressed_block_grid {
111
111
*
112
112
* Useful when the point is of a different block type from coords.
113
113
*
114
- * @param point represents a coordinate in one dimension of the point
115
- * @param coords represents vector of coordinate values of a single type only
116
- *
117
- * Hence, the exact point coordinate will not be found in coords if they are of different block types. In this case the function will return
118
- * the nearest compressed location to point by rounding it down
114
+ * @param grid_loc non-compressed physical tile location in the grid
115
+ * @return Neared x and y compressed locations in the grid (in the same layer)
119
116
*/
120
117
inline t_physical_tile_loc grid_loc_to_compressed_loc_approx (t_physical_tile_loc grid_loc) const {
121
118
auto find_closest_compressed_point = [](int loc, const std::vector<int >& compressed_grid_dim) -> int {
119
+ VTR_ASSERT (compressed_grid_dim.size () > 0 );
120
+
121
+ // Find the first element not less than loc
122
122
auto itr = std::lower_bound (compressed_grid_dim.begin (), compressed_grid_dim.end (), loc);
123
- int cx;
124
- if (itr < compressed_grid_dim.end () - 1 ) {
125
- int dist_prev = abs (loc - *itr);
126
- int dist_next = abs (loc - *(itr+1 ));
127
- if (dist_prev < dist_next) {
128
- cx = std::distance (compressed_grid_dim.begin (), itr);
129
- } else {
130
- cx = std::distance (compressed_grid_dim.begin (), itr + 1 );
131
- }
132
- } else if (itr == compressed_grid_dim.end ()) {
133
- cx = std::distance (compressed_grid_dim.begin (), itr - 1 );
134
- } else {
135
- cx = std::distance (compressed_grid_dim.begin (), itr);
123
+
124
+ // If we found exact match or at beginning, return current position
125
+ if (itr == compressed_grid_dim.begin () || (itr != compressed_grid_dim.end () && *itr == loc)) {
126
+ return std::distance (compressed_grid_dim.begin (), itr);
127
+ }
128
+
129
+ // If we're past the end, return last element
130
+ if (itr == compressed_grid_dim.end ()) {
131
+ return compressed_grid_dim.size () - 1 ;
136
132
}
137
133
138
- return cx;
134
+ // Compare distances to previous and current elements
135
+ int dist_prev = loc - *(itr - 1 );
136
+ int dist_next = *itr - loc;
137
+
138
+ return std::distance (compressed_grid_dim.begin (),
139
+ (dist_prev <= dist_next) ? (itr - 1 ) : itr);
139
140
};
140
141
141
142
const int layer_num = grid_loc.layer_num ;
0 commit comments