diff --git a/src/TiledArray/tensor/tensor.h b/src/TiledArray/tensor/tensor.h index 171dac2eea..bf729e59d9 100644 --- a/src/TiledArray/tensor/tensor.h +++ b/src/TiledArray/tensor/tensor.h @@ -705,7 +705,7 @@ class Tensor { const_reference operator[](const Ordinal ord) const { TA_ASSERT(!this->empty()); // can't distinguish between operator[](Index...) and operator[](ordinal) - // thus assume at_ordinal() if this->rank()==1 + // thus insist on at_ordinal() if this->rank()==1 TA_ASSERT(this->range_.rank() != 1 && "use Tensor::operator[](index) or " "Tensor::at_ordinal(index_ordinal) if this->range().rank()==1"); @@ -726,7 +726,7 @@ class Tensor { reference operator[](const Ordinal ord) { TA_ASSERT(!this->empty()); // can't distinguish between operator[](Index...) and operator[](ordinal) - // thus assume at_ordinal() if this->rank()==1 + // thus insist on at_ordinal() if this->rank()==1 TA_ASSERT(this->range_.rank() != 1 && "use Tensor::operator[](index) or " "Tensor::at_ordinal(index_ordinal) if this->range().rank()==1"); @@ -848,7 +848,7 @@ class Tensor { TA_ASSERT(!this->empty()); TA_ASSERT(this->nbatch() == 1); // can't distinguish between operator[](Index...) and operator[](ordinal) - // thus assume at_ordinal() if this->rank()==1 + // thus insist on at_ordinal() if this->rank()==1 TA_ASSERT(this->range_.rank() != 1 && "use Tensor::operator()(index) or " "Tensor::at_ordinal(index_ordinal) if this->range().rank()==1"); @@ -869,7 +869,7 @@ class Tensor { TA_ASSERT(!this->empty()); TA_ASSERT(this->nbatch() == 1); // can't distinguish between operator[](Index...) and operator[](ordinal) - // thus assume at_ordinal() if this->rank()==1 + // thus insist on at_ordinal() if this->rank()==1 TA_ASSERT(this->range_.rank() != 1 && "use Tensor::operator()(index) or " "Tensor::at_ordinal(index_ordinal) if this->range().rank()==1"); @@ -960,6 +960,12 @@ class Tensor { const_reference operator()(const Index&... i) const { TA_ASSERT(!this->empty()); TA_ASSERT(this->nbatch() == 1); + TA_ASSERT(this->range().rank() == sizeof...(Index)); + // can't distinguish between operator()(Index...) and operator()(ordinal) + // thus insist on at_ordinal() if this->rank()==1 + TA_ASSERT(this->range_.rank() != 1 && + "use Tensor::operator()(index) or " + "Tensor::at_ordinal(index_ordinal) if this->range().rank()==1"); using Int = std::common_type_t; const auto iord = this->range_.ordinal( std::array{{static_cast(i)...}}); @@ -982,6 +988,12 @@ class Tensor { reference operator()(const Index&... i) { TA_ASSERT(!this->empty()); TA_ASSERT(this->nbatch() == 1); + TA_ASSERT(this->range().rank() == sizeof...(Index)); + // can't distinguish between operator()(Index...) and operator()(ordinal) + // thus insist on at_ordinal() if this->rank()==1 + TA_ASSERT(this->range_.rank() != 1 && + "use Tensor::operator()(index) or " + "Tensor::at_ordinal(index_ordinal) if this->range().rank()==1"); using Int = std::common_type_t; const auto iord = this->range_.ordinal( std::array{{static_cast(i)...}}); diff --git a/src/TiledArray/tile.h b/src/TiledArray/tile.h index b8c62d95b8..39fca37d9e 100644 --- a/src/TiledArray/tile.h +++ b/src/TiledArray/tile.h @@ -250,6 +250,11 @@ class Tile { std::enable_if_t::value>* = nullptr> const_reference operator[](const Ordinal ord) const { TA_ASSERT(pimpl_); + // can't distinguish between operator[](Index...) and operator[](ordinal) + // thus insist on at_ordinal() if this->rank()==1 + TA_ASSERT(this->range().rank() != 1 && + "use Tile::operator[](index) or " + "Tile::at_ordinal(index_ordinal) if this->range().rank()==1"); TA_ASSERT(tensor().range().includes_ordinal(ord)); return tensor().data()[ord]; } @@ -264,6 +269,41 @@ class Tile { template ::value>* = nullptr> reference operator[](const Ordinal ord) { + TA_ASSERT(pimpl_); + // can't distinguish between operator[](Index...) and operator[](ordinal) + // thus insist on at_ordinal() if this->rank()==1 + TA_ASSERT(this->range().rank() != 1 && + "use Tile::operator[](index) or " + "Tile::at_ordinal(index_ordinal) if this->range().rank()==1"); + TA_ASSERT(tensor().range().includes_ordinal(ord)); + return tensor().data()[ord]; + } + + /// Const element accessor + + /// \tparam Ordinal an integer type that represents an ordinal + /// \param[in] ord an ordinal index + /// \return Const reference to the element at position \c ord . + /// \note This asserts (using TA_ASSERT) that this is not empty and ord is + /// included in the range + template ::value>* = nullptr> + const_reference at_ordinal(const Ordinal ord) const { + TA_ASSERT(pimpl_); + TA_ASSERT(tensor().range().includes_ordinal(ord)); + return tensor().data()[ord]; + } + + /// Element accessor + + /// \tparam Ordinal an integer type that represents an ordinal + /// \param[in] ord an ordinal index + /// \return Reference to the element at position \c ord . + /// \note This asserts (using TA_ASSERT) that this is not empty and ord is + /// included in the range + template ::value>* = nullptr> + reference at_ordinal(const Ordinal ord) { TA_ASSERT(pimpl_); TA_ASSERT(tensor().range().includes_ordinal(ord)); return tensor().data()[ord]; @@ -401,6 +441,12 @@ class Tile { detail::is_integral_list::value>* = nullptr> const_reference operator()(const Index&... i) const { TA_ASSERT(pimpl_); + TA_ASSERT(this->range().rank() == sizeof...(Index)); + // can't distinguish between operator()(Index...) and operator()(ordinal) + // thus insist on at_ordinal() if this->rank()==1 + TA_ASSERT(this->range().rank() != 1 && + "use Tile::operator()(index) or " + "Tile::at_ordinal(index_ordinal) if this->range().rank()==1"); TA_ASSERT(tensor().range().includes(i...)); return tensor().data()[tensor().range().ordinal(i...)]; } @@ -417,6 +463,12 @@ class Tile { detail::is_integral_list::value>* = nullptr> reference operator()(const Index&... i) { TA_ASSERT(pimpl_); + TA_ASSERT(this->range().rank() == sizeof...(Index)); + // can't distinguish between operator()(Index...) and operator()(ordinal) + // thus insist on at_ordinal() if this->rank()==1 + TA_ASSERT(this->range().rank() != 1 && + "use Tile::operator()(index) or " + "Tile::at_ordinal(index_ordinal) if this->range().rank()==1"); TA_ASSERT(tensor().range().includes(i...)); return tensor().data()[tensor().range().ordinal(i...)]; }