Skip to content

Commit

Permalink
Correctly handle empty boxes in queries and updates to 0-dimensional …
Browse files Browse the repository at this point in the history
…region_maps
  • Loading branch information
fknorr committed Feb 5, 2024
1 parent c0b7a27 commit a785316
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 9 additions & 2 deletions include/region_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -1213,9 +1213,16 @@ namespace region_map_detail {
public:
region_map_impl(const box<0>& /* extent */, ValueType default_value) : m_value(default_value) {}

void update_box(const box<1>& /* box */, const ValueType& value) { m_value = value; }
void update_box(const box<1>& box, const ValueType& value) {
assert(detail::box<1>(0, 1).covers(box));
if(!box.empty()) { m_value = value; }
}

std::vector<std::pair<box<1>, ValueType>> get_region_values(const box<1>& /* request */) const { return {{box<1>{0, 1}, m_value}}; }
std::vector<std::pair<box<1>, ValueType>> get_region_values(const box<1>& request) const {
assert(box<1>(0, 1).covers(request));
if(!request.empty()) { return {{box<1>{0, 1}, m_value}}; }
return {};
}

template <typename Functor>
void apply_to_values(const Functor& f) {
Expand Down
16 changes: 15 additions & 1 deletion test/region_map_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ TEST_CASE("region_map handles basic operations in 0D", "[region_map]") {
region_map_impl<int, 0> rm{{}, default_value};

SECTION("query default value") {
const auto results = rm.get_region_values({0, 0});
const auto results = rm.get_region_values({0, 1});
CHECK_RESULTS(results, {{0, 1}, default_value});
}
}
Expand Down Expand Up @@ -807,3 +807,17 @@ TEST_CASE("query regions are clamped from both sides in region maps with non-zer
region_map<int> rm(region_box, 42);
CHECK(rm.get_region_values(box<3>::full_range({20, 19, 18})) == std::vector{std::pair{region_box, 42}});
}

TEMPLATE_TEST_CASE_SIG("get_region_values(<empty-region>) returns no boxes", "[region_map]", ((int Dims), Dims), 0, 1, 2, 3) {
region_map<int> rm(range_cast<3>(test_utils::truncate_range<Dims>({2, 3, 4})), -1);
CHECK(rm.get_region_values(box<3>()).empty());
CHECK(rm.get_region_values(region<3>()).empty());
}

TEMPLATE_TEST_CASE_SIG("update(<empty-box>) has no effect", "[region_map]", ((int Dims), Dims), 0, 1, 2, 3) {
region_map<int> rm(range_cast<3>(test_utils::truncate_range<Dims>({2, 3, 4})), 0);
rm.update_box(box<3>(), 1);
rm.update_region(box<3>(), 2);
const auto unit_box = box_cast<3>(box<0>());
CHECK(rm.get_region_values(unit_box) == std::vector{std::pair{unit_box, 0}});
}

0 comments on commit a785316

Please sign in to comment.