From 918d4f68b17e3412d7c70b9094f5b3132e9f0c72 Mon Sep 17 00:00:00 2001 From: Eduard Valeyev Date: Sun, 8 Sep 2024 17:02:24 -0400 Subject: [PATCH] introduced TiledRange1::make_shifted --- src/TiledArray/tiled_range1.h | 22 ++++++++++++++++++++++ tests/tiled_range1.cpp | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/TiledArray/tiled_range1.h b/src/TiledArray/tiled_range1.h index 102ea1bcc8..ca3408213e 100644 --- a/src/TiledArray/tiled_range1.h +++ b/src/TiledArray/tiled_range1.h @@ -50,6 +50,7 @@ class TiledRange1 { public: using range_type = Range1; using index1_type = range_type::index1_type; + using signed_index1_type = std::make_signed_t; using const_iterator = std::vector::const_iterator; /// Default constructor creates an empty range (tile and element ranges are @@ -305,6 +306,27 @@ class TiledRange1 { return make_uniform(Range1(0, range_extent), target_tile_size); } + /// same as make_uniform(const Range1&, std::size_t) for a 0-based range + /// specified by its extent + TiledRange1 make_shifted(signed_index1_type shift) { + // ensure that it's safe to shift + TA_ASSERT(shift <= 0 || shift <= (std::numeric_limits::max() - + elements_range().upbound())); + TA_ASSERT(shift >= 0 || + std::abs(shift) <= (elements_range().lobound() - + std::numeric_limits::min())); + std::vector hashmarks; + hashmarks.reserve(tile_extent() + 1); + if (tiles_ranges_.empty()) + hashmarks.emplace_back(elements_range_.lobound() + shift); + else { + for (auto& t : tiles_ranges_) { + hashmarks.push_back(t.first + shift); + } + } + return TiledRange1(hashmarks.begin(), hashmarks.end()); + } + /// swapper /// \param other the range with which the contents of this range will be diff --git a/tests/tiled_range1.cpp b/tests/tiled_range1.cpp index 056f752e33..1fe4c5cb6e 100644 --- a/tests/tiled_range1.cpp +++ b/tests/tiled_range1.cpp @@ -360,4 +360,23 @@ BOOST_AUTO_TEST_CASE(make_uniform) { (TiledRange1{0, 10, 20, 30, 40, 50, 59})); } +BOOST_AUTO_TEST_CASE(make_shifted) { + TiledRange1 r0; + BOOST_REQUIRE_NO_THROW(r0.make_shifted(1)); + auto r0_plus_1 = r0.make_shifted(1); + BOOST_CHECK_EQUAL(r0_plus_1, TiledRange1(1)); + + BOOST_CHECK_TA_ASSERT( + TiledRange1{std::numeric_limits::max()}.make_shifted(1), + Exception); + BOOST_CHECK_TA_ASSERT( + TiledRange1{std::numeric_limits::min()}.make_shifted(-1), + Exception); + + TiledRange1 r1{1, 3, 7, 9}; + BOOST_REQUIRE_NO_THROW(r0.make_shifted(-1)); + auto r1_minus_1 = r1.make_shifted(-1); + BOOST_CHECK_EQUAL(r1_minus_1, TiledRange1(0, 2, 6, 8)); +} + BOOST_AUTO_TEST_SUITE_END()