Skip to content

Commit 6949899

Browse files
committed
Clang-Tidy: modernize-use-ranges
1 parent 9ed35ab commit 6949899

58 files changed

Lines changed: 159 additions & 181 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.clang-tidy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,6 @@ HeaderFilterRegex: '([^n].....|[^o]....|[^l]...|[^i]..|[^n].|[^t])\.H$'
7272
# Only available in clang-tidy >= 17
7373
HeaderFileExtensions: ['', "H", 'h', 'hh', 'hpp', 'hxx']
7474

75-
# We will try to modernize this after switching to C++20
76-
# -modernize-use-ranges
75+
# modernize-use-ranges is not a required check:
76+
# (1) Clang <= 15 has bugs with std::ranges.
77+
# (2) std::ranges::sort requires std::sortable, which is stricter than operator<.

CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ parenthesis of the parameter list (but not when simply calling the function). Fo
346346
```
347347
* Or define the member **inline** in the class body, where referencing the class
348348
template parameters in the constraint is fine.
349+
* Avoid `std::ranges::find_if` and `std::ranges::unique` — Clang ≤ 15
350+
has bugs when compiling them with libstdc++. Use `std::find_if` and `std::unique` instead.
351+
349352
These guidelines should be adhered to in new contributions to AMReX, but
350353
please refrain from making stylistic changes to unrelated sections of code in your PRs.
351354

Src/Amr/AMReX_Amr.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -607,14 +607,14 @@ Amr::finalizeInSitu()
607607
bool
608608
Amr::isStatePlotVar (const std::string& name)
609609
{
610-
auto it = std::find(state_plot_vars.begin(), state_plot_vars.end(), name);
610+
auto it = std::ranges::find(state_plot_vars, name);
611611
return (it != state_plot_vars.end());
612612
}
613613

614614
bool
615615
Amr::isStateSmallPlotVar (const std::string& name)
616616
{
617-
auto it = std::find(state_small_plot_vars.begin(), state_small_plot_vars.end(), name);
617+
auto it = std::ranges::find(state_small_plot_vars, name);
618618
return (it != state_small_plot_vars.end());
619619
}
620620

@@ -685,14 +685,14 @@ Amr::deleteStatePlotVar (const std::string& name)
685685
bool
686686
Amr::isDerivePlotVar (const std::string& name) noexcept
687687
{
688-
auto it = std::find(derive_plot_vars.begin(), derive_plot_vars.end(), name);
688+
auto it = std::ranges::find(derive_plot_vars, name);
689689
return (it != derive_plot_vars.end());
690690
}
691691

692692
bool
693693
Amr::isDeriveSmallPlotVar (const std::string& name) noexcept
694694
{
695-
auto it = std::find(derive_small_plot_vars.begin(), derive_small_plot_vars.end(), name);
695+
auto it = std::ranges::find(derive_small_plot_vars, name);
696696
return (it != derive_small_plot_vars.end());
697697
}
698698

Src/AmrCore/AMReX_AmrMesh.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ AmrMesh::ChopGrids (int lev, BoxArray& ba, int target_size) const
485485
chunk_dir{AMREX_D_DECL(std::make_pair(chunk[0],int(0)),
486486
std::make_pair(chunk[1],int(1)),
487487
std::make_pair(chunk[2],int(2)))};
488-
std::sort(chunk_dir.begin(), chunk_dir.end());
488+
std::ranges::sort(chunk_dir);
489489

490490
for (int idx = AMREX_SPACEDIM-1; idx >= 0; idx--) {
491491
int idim = chunk_dir[idx].second;

Src/AmrCore/AMReX_TagBox.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,8 +661,8 @@ TagBoxArray::collate (Gpu::PinnedVector<IntVect>& TheGlobalCollateSpace) const
661661
auto countvec_int = std::vector<int>(countvec.size());
662662
auto offset_int = std::vector<int>(offset.size());
663663
const auto mul_funct = [](const auto el){return el*AMREX_SPACEDIM;};
664-
std::transform(countvec.begin(), countvec.end(), countvec_int.begin(), mul_funct);
665-
std::transform(offset.begin(), offset.end(), offset_int.begin(), mul_funct);
664+
std::ranges::transform(countvec, countvec_int.begin(), mul_funct);
665+
std::ranges::transform(offset, offset_int.begin(), mul_funct);
666666
ParallelDescriptor::Gatherv(
667667
psend_int, count_int, precv_int, countvec_int, offset_int, IOProcNumber);
668668
#endif

Src/Base/AMReX_BoxArray.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,7 @@ bool match (const BoxArray& x, const BoxArray& y)
19431943
BoxArray decompose (Box const& domain, int nboxes,
19441944
Array<bool,AMREX_SPACEDIM> const& decomp, bool no_overlap)
19451945
{
1946-
auto ndecomp = std::count(decomp.begin(), decomp.end(), true);
1946+
auto ndecomp = std::ranges::count(decomp, true);
19471947

19481948
if (nboxes <= 1 || ndecomp == 0) {
19491949
return BoxArray(domain);
@@ -2017,7 +2017,7 @@ BoxArray decompose (Box const& domain, int nboxes,
20172017

20182018
int nprocs_tot = 1;
20192019
while (!factors.empty()) {
2020-
std::sort(procdim.begin(), procdim.end(), comp);
2020+
std::ranges::sort(procdim, comp);
20212021
auto f = factors.back();
20222022
factors.pop_back();
20232023
procdim.back().nproc *= f;
@@ -2031,7 +2031,7 @@ BoxArray decompose (Box const& domain, int nboxes,
20312031
// swap to see if the decomposition can be improved.
20322032
while (true)
20332033
{
2034-
std::sort(procdim.begin(), procdim.end(), comp);
2034+
std::ranges::sort(procdim, comp);
20352035
auto fit = std::find_if(procdim.begin(),procdim.end(),
20362036
[] (ProcDim const& x) { return x.nproc > 1; });
20372037
if (fit == procdim.end()) { break; } // This should not actually happen.

Src/Base/AMReX_BoxDomain.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ BoxDomain::add (const Box& b)
138138
cbx = Box();
139139
}
140140
}
141-
check.erase(std::remove_if(check.begin(), check.end(),
142-
[](const Box& x) { return x.isEmpty(); }),
143-
check.end());
141+
std::erase_if(check, [](const Box& x) { return x.isEmpty(); });
144142
check.insert(std::end(check), std::begin(tmp), std::end(tmp));
145143
}
146144
join(check);

Src/Base/AMReX_BoxList.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ BoxList::catenate (BoxList& blist)
9292
BoxList&
9393
BoxList::removeEmpty()
9494
{
95-
m_lbox.erase(std::remove_if(m_lbox.begin(), m_lbox.end(),
96-
[](const Box& x) { return x.isEmpty(); }),
97-
m_lbox.end());
95+
std::erase_if(m_lbox, [](const Box& x) { return x.isEmpty(); });
9896
return *this;
9997
}
10098

@@ -236,8 +234,8 @@ BoxList::BoxList (const Box& bx, int nboxes, Direction dir)
236234
bool
237235
BoxList::ok () const noexcept
238236
{
239-
return std::all_of(this->cbegin(), this->cend(),
240-
[] (Box const& b) { return b.ok(); });
237+
return std::ranges::all_of(*this,
238+
[] (Box const& b) { return b.ok(); });
241239
}
242240

243241
bool
@@ -259,8 +257,8 @@ BoxList::contains (const BoxList& bl) const
259257

260258
BoxArray ba(*this);
261259

262-
return std::all_of(bl.cbegin(), bl.cend(),
263-
[&ba] (Box const& b) { return ba.contains(b); });
260+
return std::ranges::all_of(bl,
261+
[&ba] (Box const& b) { return ba.contains(b); });
264262
}
265263

266264
BoxList&
@@ -653,7 +651,7 @@ boxDiff (BoxList& bl_diff, const Box& b1in, const Box& b2)
653651
int
654652
BoxList::simplify (bool best)
655653
{
656-
std::sort(m_lbox.begin(), m_lbox.end(), [](const Box& l, const Box& r) {
654+
std::ranges::sort(m_lbox, [](const Box& l, const Box& r) {
657655
return l.smallEnd() < r.smallEnd(); });
658656

659657
//

Src/Base/AMReX_CArena.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -408,24 +408,21 @@ CArena::freeUnused_protected ()
408408
{
409409
std::size_t nbytes = 0;
410410
std::vector<std::pair<void*, std::size_t>> to_free{};
411-
m_alloc.erase(std::remove_if(m_alloc.begin(), m_alloc.end(),
412-
[&nbytes,&to_free,this] (std::pair<void*,std::size_t> a)
413-
{
414-
// We cannot simply use std::set::erase because
415-
// Node::operator== only compares the starting address.
416-
auto it = m_freelist.find(Node(a.first,nullptr,0));
417-
if (it != m_freelist.end() &&
418-
it->owner() == a.first &&
419-
it->size() == a.second)
420-
{
421-
it = m_freelist.erase(it);
422-
nbytes += a.second;
423-
to_free.emplace_back(a.first, a.second);
424-
return true;
425-
}
426-
return false;
427-
}),
428-
m_alloc.end());
411+
std::erase_if(m_alloc, [&nbytes,&to_free,this] (std::pair<void*,std::size_t> a) {
412+
// We cannot simply use std::set::erase because
413+
// Node::operator== only compares the starting address.
414+
auto it = m_freelist.find(Node(a.first,nullptr,0));
415+
if (it != m_freelist.end() &&
416+
it->owner() == a.first &&
417+
it->size() == a.second)
418+
{
419+
it = m_freelist.erase(it);
420+
nbytes += a.second;
421+
to_free.emplace_back(a.first, a.second);
422+
return true;
423+
}
424+
return false;
425+
});
429426
m_used -= nbytes;
430427

431428
// deallocate_system can call cudafree which may perform implicit synchronization

Src/Base/AMReX_DistributionMapping.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ DistributionMapping::Sort (std::vector<LIpair>& vec,
182182
if (std::ssize(vec) > 1)
183183
{
184184
if (reverse) {
185-
std::stable_sort(vec.begin(), vec.end(), LIpairGT());
185+
std::ranges::stable_sort(vec, LIpairGT());
186186
}
187187
else {
188-
std::stable_sort(vec.begin(), vec.end(), LIpairLT());
188+
std::ranges::stable_sort(vec, LIpairLT());
189189
}
190190
}
191191
}
@@ -1057,8 +1057,8 @@ DistributionMapping::KnapSackProcessorMap (const DistributionMapping& olddm,
10571057
}
10581058
}
10591059

1060-
AMREX_ASSERT(std::none_of(m_ref->m_pmap.cbegin(), m_ref->m_pmap.cend(),
1061-
[] (int i) { return i < 0; }));
1060+
AMREX_ASSERT(std::ranges::none_of(m_ref->m_pmap,
1061+
[] (int i) { return i < 0; }));
10621062
}
10631063
}
10641064
}
@@ -1324,7 +1324,7 @@ DistributionMapping::SFCProcessorMapDoIt (const BoxArray& boxes,
13241324
//
13251325
// Put'm in Morton space filling curve order.
13261326
//
1327-
std::sort(tokens.begin(), tokens.end(), SFCToken::Compare());
1327+
std::ranges::sort(tokens, SFCToken::Compare());
13281328
//
13291329
// Split'm up as equitably as possible per team.
13301330
//
@@ -1569,7 +1569,7 @@ DistributionMapping::RRSFCDoIt (const BoxArray& boxes,
15691569
//
15701570
// Put'm in Morton space filling curve order.
15711571
//
1572-
std::sort(tokens.begin(), tokens.end(), SFCToken::Compare());
1572+
std::ranges::sort(tokens, SFCToken::Compare());
15731573

15741574
Vector<int> ord;
15751575

@@ -1600,7 +1600,7 @@ DistributionMapping::ConvertCostRealToLong (const Vector<Real>& rcost)
16001600

16011601
if (rcost.empty()) { return cost; }
16021602

1603-
Real wmax = *std::max_element(rcost.begin(), rcost.end());
1603+
Real wmax = *std::ranges::max_element(rcost);
16041604
Real scale = (wmax == 0) ? 1.e9_rt : 1.e9_rt/wmax;
16051605

16061606
for (Long i = 0; i < rcost.size(); ++i) {
@@ -1897,7 +1897,7 @@ DistributionMapping::makeSFC (const BoxArray& ba, bool use_box_vol, int nprocs)
18971897
//
18981898
// Put'm in Morton space filling curve order.
18991899
//
1900-
std::sort(tokens.begin(), tokens.end(), SFCToken::Compare());
1900+
std::ranges::sort(tokens, SFCToken::Compare());
19011901

19021902
Real volper = static_cast<Real>(vol_sum) / static_cast<Real>(nprocs);
19031903

0 commit comments

Comments
 (0)