Skip to content

Commit

Permalink
clang-tidy: Address cppcoreguidelines-missing-std-forward
Browse files Browse the repository at this point in the history
  • Loading branch information
krivenko committed May 13, 2024
1 parent 5622fbc commit a8cc687
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 48 deletions.
7 changes: 6 additions & 1 deletion include/libcommute/expression/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <memory>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>

namespace libcommute {
Expand Down Expand Up @@ -178,7 +179,7 @@ template <typename ScalarType, typename... IndexTypes> class expression {
expression_t<NewScalarType> res;
auto& res_mons = res.get_monomials();
for(auto const& m : expr.monomials_) {
auto val = f(m.first, m.second);
auto val = std::forward<F>(f)(m.first, m.second);
if(!scalar_traits<NewScalarType>::is_zero(val))
res_mons.emplace_hint(res_mons.end(), m.first, val);
}
Expand Down Expand Up @@ -741,6 +742,7 @@ template <typename ScalarType, typename... IndexTypes> class expression {
// Multiplication by scalar (postfix form)
// mul_type<ScalarType, S> == ScalarType
template <typename S>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
inline expression mul_const_postfix_impl(S&& alpha, std::true_type) const {
expression res(*this);
for(auto& p : res.monomials_)
Expand All @@ -752,6 +754,7 @@ template <typename ScalarType, typename... IndexTypes> class expression {
// mul_type<ScalarType, S> != ScalarType
template <typename S>
inline expression_t<mul_type<ScalarType, S>>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
mul_const_postfix_impl(S&& alpha, std::false_type) const {
expression_t<mul_type<ScalarType, S>> res;
auto& res_mons = res.get_monomials();
Expand All @@ -763,6 +766,7 @@ template <typename ScalarType, typename... IndexTypes> class expression {
// Multiplication by scalar (prefix form)
// mul_type<S, ScalarType> == ScalarType
template <typename S>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
inline expression mul_const_prefix_impl(S&& alpha, std::true_type) const {
expression res(*this);
for(auto& p : res.monomials_)
Expand All @@ -774,6 +778,7 @@ template <typename ScalarType, typename... IndexTypes> class expression {
// mul_type<S, ScalarType> != ScalarType
template <typename S>
inline expression_t<mul_type<S, ScalarType>>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
mul_const_prefix_impl(S&& alpha, std::false_type) const {
expression_t<mul_type<S, ScalarType>> res;
auto& res_mons = res.get_monomials();
Expand Down
10 changes: 5 additions & 5 deletions include/libcommute/expression/factories.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ DEFINE_FACTORY(c_dag, (static_indices::make_fermion(true, INDICES)))
// Annihilation operator
DEFINE_FACTORY(c, (static_indices::make_fermion(false, INDICES)))
// Number of fermions
DEFINE_FACTORY(n,
DEFINE_FACTORY(n, // NOLINT(cppcoreguidelines-missing-std-forward)
(static_indices::make_fermion(true, indices...)),
(static_indices::make_fermion(false, indices...)))

Expand Down Expand Up @@ -109,26 +109,26 @@ DEFINE_FACTORY_SPIN(S_z,

template <typename... IndexTypes>
inline expression<std::complex<double>, c_str_to_string_t<IndexTypes>...>
S_x(IndexTypes&&... indices) {
S_x(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward)
return std::complex<double>(0.5) *
(static_indices::S_p(indices...) + static_indices::S_m(indices...));
}
template <typename... IndexTypes>
inline expression<std::complex<double>, c_str_to_string_t<IndexTypes>...>
S_y(IndexTypes&&... indices) {
S_y(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward)
return std::complex<double>(0, -0.5) *
(static_indices::S_p(indices...) - static_indices::S_m(indices...));
}

template <int Mult, typename... IndexTypes>
inline expression<std::complex<double>, c_str_to_string_t<IndexTypes>...>
S_x(IndexTypes&&... indices) {
S_x(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward)
return std::complex<double>(0.5) * (static_indices::S_p<Mult>(indices...) +
static_indices::S_m<Mult>(indices...));
}
template <int Mult, typename... IndexTypes>
inline expression<std::complex<double>, c_str_to_string_t<IndexTypes>...>
S_y(IndexTypes&&... indices) {
S_y(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward)
return std::complex<double>(0, -0.5) *
(static_indices::S_p<Mult>(indices...) -
static_indices::S_m<Mult>(indices...));
Expand Down
10 changes: 5 additions & 5 deletions include/libcommute/expression/factories_dyn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ DEFINE_FACTORY(c_dag, (dynamic_indices::make_fermion(true, INDICES)))
// Annihilation operator
DEFINE_FACTORY(c, (dynamic_indices::make_fermion(false, INDICES)))
// Number of fermions
DEFINE_FACTORY(n,
DEFINE_FACTORY(n, // NOLINT(cppcoreguidelines-missing-std-forward)
(dynamic_indices::make_fermion(true, indices...)),
(dynamic_indices::make_fermion(false, indices...)))

Expand Down Expand Up @@ -112,26 +112,26 @@ DEFINE_FACTORY_SPIN(S_z,

template <typename... IndexTypes>
inline expression<std::complex<double>, dyn_indices>
S_x(IndexTypes&&... indices) {
S_x(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward)
return std::complex<double>(0.5) *
(dynamic_indices::S_p(indices...) + dynamic_indices::S_m(indices...));
}
template <typename... IndexTypes>
inline expression<std::complex<double>, dyn_indices>
S_y(IndexTypes&&... indices) {
S_y(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward)
return std::complex<double>(0, -0.5) *
(dynamic_indices::S_p(indices...) - dynamic_indices::S_m(indices...));
}

template <int Mult, typename... IndexTypes>
inline expression<std::complex<double>, dyn_indices>
S_x(IndexTypes&&... indices) {
S_x(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward)
return std::complex<double>(0.5) * (dynamic_indices::S_p<Mult>(indices...) +
dynamic_indices::S_m<Mult>(indices...));
}
template <int Mult, typename... IndexTypes>
inline expression<std::complex<double>, dyn_indices>
S_y(IndexTypes&&... indices) {
S_y(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward)
return std::complex<double>(0, -0.5) *
(dynamic_indices::S_p<Mult>(indices...) -
dynamic_indices::S_m<Mult>(indices...));
Expand Down
11 changes: 7 additions & 4 deletions include/libcommute/expression/monomial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ template <typename... IndexTypes> class monomial {
template <typename GenType1, typename... GenTypesTail>
void constructor_impl(GenType1&& generator, GenTypesTail&&... more_gens) {
using gen1_t = typename std::remove_reference<GenType1>::type;
generators_.emplace_back(make_unique<gen1_t>(generator));
generators_.emplace_back(
make_unique<gen1_t>(std::forward<GenType1>(generator)));
constructor_impl(std::forward<GenTypesTail>(more_gens)...);
}
void constructor_impl() {}
Expand Down Expand Up @@ -211,7 +212,7 @@ template <typename... IndexTypes> class monomial {
friend monomial concatenate(PartTypes&&... parts) {
monomial res;
res.generators_.reserve(concat_parts_total_size(parts...));
res.concat_impl(parts...);
res.concat_impl(std::forward<PartTypes>(parts)...);
return res;
}

Expand Down Expand Up @@ -286,10 +287,12 @@ template <typename... IndexTypes> class monomial {
// Append generators from a mixed list of monomials and monomial ranges
template <typename P1, typename... PTail>
void concat_impl(P1&& p1, PTail&&... p_tail) {
append(p1);
append(std::forward<P1>(p1));
concat_impl(std::forward<PTail>(p_tail)...);
}
template <typename P1> void concat_impl(P1&& p1) { append(p1); }
template <typename P1> void concat_impl(P1&& p1) {
append(std::forward<P1>(p1));
}

std::vector<gen_ptr_type> generators_;
};
Expand Down
5 changes: 3 additions & 2 deletions include/libcommute/loperator/hilbert_space.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ template <typename... IndexTypes> class hilbert_space {
ESConstructor&& es_constr = {}) {
for(auto const& m : expr) {
for(auto const& g : m.monomial) {
elementary_spaces_.emplace(es_constr(g), bit_range_t(0, 0));
elementary_spaces_.emplace(std::forward<ESConstructor>(es_constr)(g),
bit_range_t(0, 0));
}
}
recompute_bit_ranges();
Expand Down Expand Up @@ -234,7 +235,7 @@ template <typename... IndexTypes> class hilbert_space {
inline friend void foreach(hilbert_space const& hs, Functor&& f) {
sv_index_type d = hs.dim();
for(sv_index_type i = 0; i < d; ++i) {
f(i);
std::forward<Functor>(f)(i);
}
}

Expand Down
3 changes: 2 additions & 1 deletion include/libcommute/loperator/loperator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,14 @@ class loperator : public loperator_base<ScalarType, AlgebraIDs...> {
private:
// Implementation details of operator()
template <typename SrcStateVector, typename DstStateVector>
// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
inline void act_impl(SrcStateVector&& src, DstStateVector&& dst) const {

// A workaround for GCC Bug 58972
// (base::m_actions() would be inaccessible from within the lambda below)
auto const& m_act = base::m_actions();

foreach(src,
foreach(std::forward<SrcStateVector>(src),
[&](sv_index_type in_index,
element_type_t<remove_cvref_t<SrcStateVector>> const& a) {
for(auto const& ma : m_act) {
Expand Down
9 changes: 5 additions & 4 deletions include/libcommute/loperator/mapped_basis_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ inline void update_add_element(mapped_basis_view<StateVector, Ref>& view,

// update_add_element() is not defined for constant views
template <typename StateVector, bool Ref, typename T>
inline void update_add_element(mapped_basis_view<StateVector const, Ref>&,
sv_index_type,
T&&) {
inline void
update_add_element(mapped_basis_view<StateVector const, Ref>&,
sv_index_type,
T&&) { // NOLINT(cppcoreguidelines-missing-std-forward)
static_assert(!std::is_same<StateVector, StateVector>::value,
"update_add_element() is not supported for constant views");
}
Expand Down Expand Up @@ -119,7 +120,7 @@ inline void foreach(mapped_basis_view<StateVector, Ref> const& view,
if(scalar_traits<T>::is_zero(a))
continue;
else
f(p.first, a);
std::forward<Functor>(f)(p.first, a);
}
}

Expand Down
29 changes: 16 additions & 13 deletions include/libcommute/loperator/n_fermion_sector_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ struct for_each_composition {
// Apply 'f' to each composition
template <typename F> void operator()(F&& f) const {
if(params.N_counted == 0) {
f(lambdas);
std::forward<F>(f)(lambdas);
return;
}

Expand Down Expand Up @@ -336,9 +336,10 @@ inline void update_add_element(n_fermion_sector_view<StateVector, Ref>& view,

// update_add_element() is not defined for constant views
template <typename StateVector, bool Ref, typename T>
inline void update_add_element(n_fermion_sector_view<StateVector const, Ref>&,
sv_index_type,
T&&) {
inline void
update_add_element(n_fermion_sector_view<StateVector const, Ref>&,
sv_index_type,
T&&) { // NOLINT(cppcoreguidelines-missing-std-forward)
static_assert(!std::is_same<StateVector, StateVector>::value,
"update_add_element() is not supported for constant views");
}
Expand Down Expand Up @@ -366,8 +367,9 @@ inline void set_zeros(n_fermion_sector_view<StateVector const, Ref>&) {
// Apply functor `f` to all index/non-zero amplitude pairs
// in the adapted StateVector object
template <typename StateVector, bool Ref, typename Functor>
inline void foreach(n_fermion_sector_view<StateVector, Ref> const& view,
Functor&& f) {
inline void
foreach(n_fermion_sector_view<StateVector, Ref> const& view,
Functor&& f) { // NOLINT(cppcoreguidelines-missing-std-forward)
if(view.M == 0 && view.M_nonfermion == 0) return;

auto dim_nonfermion = detail::pow2(view.M_nonfermion);
Expand All @@ -385,7 +387,7 @@ inline void foreach(n_fermion_sector_view<StateVector, Ref> const& view,
if(scalar_traits<T>::is_zero(a))
continue;
else
f(view.comp_to_index(lambdas, index_nf), a);
std::forward<Functor>(f)(view.comp_to_index(lambdas, index_nf), a);
}
});
}
Expand Down Expand Up @@ -551,7 +553,7 @@ struct for_each_composition_multi {
// Apply 'f' to each composition
template <typename F> void operator()(F&& f) const {
if(s_min == -1) { // All compositions are empty
f(lambdas);
std::forward<F>(f)(lambdas);
return;
}

Expand Down Expand Up @@ -580,7 +582,7 @@ struct for_each_composition_multi {

if(s == s_max &&
static_cast<unsigned int>(i + 1) == sector_params[s].N_counted) {
f(lambdas);
std::forward<F>(f)(lambdas);
} else {
++i;
if(i == static_cast<int>(sector_params[s].N_counted)) {
Expand Down Expand Up @@ -918,7 +920,7 @@ template <typename StateVector, bool Ref, typename T>
inline void
update_add_element(n_fermion_multisector_view<StateVector const, Ref>&,
sv_index_type,
T&&) {
T&&) { // NOLINT(cppcoreguidelines-missing-std-forward)
static_assert(!std::is_same<StateVector, StateVector>::value,
"update_add_element() is not supported for constant views");
}
Expand Down Expand Up @@ -947,8 +949,9 @@ inline void set_zeros(n_fermion_multisector_view<StateVector const, Ref>&) {
// Apply functor `f` to all index/non-zero amplitude pairs
// in the adapted StateVector object
template <typename StateVector, bool Ref, typename Functor>
inline void foreach(n_fermion_multisector_view<StateVector, Ref> const& view,
Functor&& f) {
inline void
foreach(n_fermion_multisector_view<StateVector, Ref> const& view,
Functor&& f) { // NOLINT(cppcoreguidelines-missing-std-forward)
if(view.sector_params.size() == 0 && view.M_nonmultisector == 0) return;

auto dim_nonmultisector = detail::pow2(view.M_nonmultisector);
Expand All @@ -969,7 +972,7 @@ inline void foreach(n_fermion_multisector_view<StateVector, Ref> const& view,
if(scalar_traits<T>::is_zero(a))
continue;
else
f(view.comp_to_index(lambdas, index_nms), a);
std::forward<Functor>(f)(view.comp_to_index(lambdas, index_nms), a);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion include/libcommute/loperator/space_partition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class space_partition {
// and index of the subspace this basis state belongs to.
template <typename F> friend void foreach(space_partition const& sp, F&& f) {
for(sv_index_type n = 0; n < sp.dim(); ++n) {
f(n, sp[n]);
std::forward<F>(f)(n, sp[n]);
}
}

Expand Down
10 changes: 6 additions & 4 deletions include/libcommute/loperator/sparse_state_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <cassert>
#include <unordered_map>
#include <utility>

namespace libcommute {

Expand Down Expand Up @@ -59,9 +60,10 @@ template <typename ScalarType> class sparse_state_vector {
update_add_element(sparse_state_vector& sv, sv_index_type n, T&& value) {
auto it = sv.data_.find(n);
if(it == sv.data_.end()) {
if(!scalar_traits<ScalarType>::is_zero(value)) sv.data_[n] = value;
if(!scalar_traits<ScalarType>::is_zero(value))
sv.data_[n] = std::forward<T>(value);
} else {
it->second += value;
it->second += std::forward<T>(value);
if(scalar_traits<ScalarType>::is_zero(it->second)) sv.data_.erase(it);
}
}
Expand All @@ -78,7 +80,7 @@ template <typename ScalarType> class sparse_state_vector {
template <typename Functor>
inline friend void foreach(sparse_state_vector const& sv, Functor&& f) {
for(auto const& p : sv.data_)
f(p.first, p.second);
std::forward<Functor>(f)(p.first, p.second);
}

// Force removal of all zero amplitudes from the storage
Expand All @@ -94,7 +96,7 @@ template <typename ScalarType> class sparse_state_vector {
// Force removal of all amplitudes meeting a specified criterion
template <typename UnaryPredicate> inline void prune(UnaryPredicate&& p) {
for(auto it = data_.begin(); it != data_.end();) {
if(p(it->second))
if(std::forward<UnaryPredicate>(p)(it->second))
it = data_.erase(it);
else
++it;
Expand Down
3 changes: 2 additions & 1 deletion include/libcommute/loperator/state_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <algorithm>
#include <cstdint>
#include <utility>
#include <vector>

namespace libcommute {
Expand Down Expand Up @@ -74,7 +75,7 @@ inline void foreach(std::vector<T> const& sv, Functor&& f) {
if(scalar_traits<T>::is_zero(a))
continue;
else
f(n, a);
std::forward<Functor>(f)(n, a);
}
}

Expand Down
Loading

0 comments on commit a8cc687

Please sign in to comment.