Skip to content

Commit

Permalink
TiledRange is shiftable
Browse files Browse the repository at this point in the history
  • Loading branch information
evaleev committed Sep 9, 2024
1 parent cb9f085 commit 57907cc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/TiledArray/tiled_range.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,61 @@ class TiledRange {
std::swap(ranges_, other.ranges_);
}

/// Shifts the lower and upper bounds of this range

/// \tparam Index An integral range type
/// \param bound_shift The shift to be applied to the range
/// \return A reference to this range
template <typename Index,
typename = std::enable_if_t<detail::is_integral_range_v<Index>>>
TiledRange_& inplace_shift(const Index& bound_shift) {
elements_range_.inplace_shift(bound_shift);
using std::begin;
auto bound_shift_it = begin(bound_shift);
for (std::size_t d = 0; d != rank(); ++d, ++bound_shift_it) {
ranges_[d].inplace_shift(*bound_shift_it);
}
return *this;
}

/// Shifts the lower and upper bound of this range

/// \tparam Index An integral type
/// \param bound_shift The shift to be applied to the range
/// \return A reference to this range
template <typename Index,
typename = std::enable_if_t<std::is_integral_v<Index>>>
TiledRange_& inplace_shift(const std::initializer_list<Index>& bound_shift) {
return inplace_shift<std::initializer_list<Index>>(bound_shift);
}

/// Create a TiledRange with shifted lower and upper bounds

/// \tparam Index An integral range type
/// \param bound_shift The shift to be applied to the range
/// \return A shifted copy of this range
template <typename Index,
typename = std::enable_if_t<detail::is_integral_range_v<Index>>>
[[nodiscard]] TiledRange_ shift(const Index& bound_shift) const {
TiledRange_ result(*this);
result.inplace_shift(bound_shift);
return result;
}

/// Create a TiledRange with shifted lower and upper bounds

/// \tparam Index An integral type
/// \param bound_shift The shift to be applied to the range
/// \return A shifted copy of this range
template <typename Index,
typename = std::enable_if_t<std::is_integral_v<Index>>>
[[nodiscard]] TiledRange_ shift(
const std::initializer_list<Index>& bound_shift) const {
TiledRange_ result(*this);
result.inplace_shift(bound_shift);
return result;
}

template <typename Archive,
typename std::enable_if<madness::is_input_archive_v<
std::decay_t<Archive>>>::type* = nullptr>
Expand Down
11 changes: 11 additions & 0 deletions tests/tiled_range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ BOOST_AUTO_TEST_CASE(permutation) {
r1); // check that the permutation was assigned correctly.
}

BOOST_AUTO_TEST_CASE(shift) {
TiledRange tr1 = tr;
const auto shift = std::vector<int>(GlobalFixture::dim, 1);
BOOST_CHECK_NO_THROW(tr1.inplace_shift(shift));
BOOST_CHECK_EQUAL(tr1.tiles_range(), tr.tiles_range());
BOOST_CHECK_EQUAL(tr1.elements_range(), tr.elements_range().shift(shift));
TiledRange tr1_copy;
BOOST_CHECK_NO_THROW(tr1_copy = tr.shift(shift));
BOOST_CHECK_EQUAL(tr1, tr1_copy);
}

BOOST_AUTO_TEST_CASE(make_tiles_range) {
tile_index start(GlobalFixture::dim);
tile_index finish(GlobalFixture::dim);
Expand Down

0 comments on commit 57907cc

Please sign in to comment.