Skip to content

Commit 3a919b8

Browse files
committed
Replace loop patterns with std algorithms under libs/
1 parent 4a99026 commit 3a919b8

28 files changed

+262
-339
lines changed

libs/ardour/export_graph_builder.cc

+3-7
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,9 @@ ExportGraphBuilder::process (samplecnt_t samples, bool last_cycle)
206206
bool
207207
ExportGraphBuilder::post_process ()
208208
{
209-
for (std::list<Intermediate *>::iterator it = intermediates.begin(); it != intermediates.end(); /* ++ in loop */) {
210-
if ((*it)->process()) {
211-
it = intermediates.erase (it);
212-
} else {
213-
++it;
214-
}
215-
}
209+
static_cast<void>(intermediates.remove_if([] (Intermediate* const& it) {
210+
return it->process();
211+
}));
216212

217213
return intermediates.empty();
218214
}

libs/ardour/location.cc

+12-10
Original file line numberDiff line numberDiff line change
@@ -1611,27 +1611,29 @@ Locations::marks_either_side (timepos_t const & pos, timepos_t& before, timepos_
16111611

16121612
positions.sort ();
16131613

1614-
std::list<timepos_t>::iterator i = positions.begin ();
1615-
1616-
while (i != positions.end () && *i < pos) {
1617-
++i;
1618-
}
1614+
auto time_mark_it = std::find_if(
1615+
positions.begin (),
1616+
positions.end (),
1617+
[&] (const auto& p) {
1618+
return p >= pos;
1619+
}
1620+
);
16191621

1620-
if (i == positions.end ()) {
1622+
if (time_mark_it == positions.end ()) {
16211623
/* run out of marks */
16221624
before = positions.back ();
16231625
return;
16241626
}
16251627

1626-
after = *i;
1628+
after = *time_mark_it;
16271629

1628-
if (i == positions.begin ()) {
1630+
if (time_mark_it == positions.begin ()) {
16291631
/* none before */
16301632
return;
16311633
}
16321634

1633-
--i;
1634-
before = *i;
1635+
--time_mark_it;
1636+
before = *time_mark_it;
16351637
}
16361638

16371639
void

libs/ardour/panner_manager.cc

+4-8
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,11 @@ PannerManager::panner_discover (string path)
113113

114114
if ((pinfo = get_descriptor (path)) != 0) {
115115

116-
list<PannerInfo*>::iterator i;
116+
auto pi_it = std::find_if(panner_info.begin(), panner_info.end(), [&] (const auto& pi) {
117+
return pi->descriptor.name == pinfo->descriptor.name;
118+
});
117119

118-
for (i = panner_info.begin(); i != panner_info.end(); ++i) {
119-
if (pinfo->descriptor.name == (*i)->descriptor.name) {
120-
break;
121-
}
122-
}
123-
124-
if (i == panner_info.end()) {
120+
if (pi_it == panner_info.end()) {
125121
panner_info.push_back (pinfo);
126122
DEBUG_TRACE (DEBUG::Panning, string_compose(_("Panner discovered: \"%1\" in %2\n"), pinfo->descriptor.name, path));
127123
} else {

libs/ardour/playlist.cc

+5-28
Original file line numberDiff line numberDiff line change
@@ -858,17 +858,9 @@ Playlist::remove_region_internal (std::shared_ptr<Region> region, ThawList& thaw
858858
}
859859

860860
#if 0
861-
for (set<std::shared_ptr<Region> >::iterator x = all_regions.begin(); x != all_regions.end(); ++x) {
862-
if ((*x) == region) {
863-
all_regions.erase (x);
864-
break;
865-
}
866-
}
861+
all_regions.erase (region);
867862
#else /* sync_all_regions_with_regions */
868-
all_regions.clear ();
869-
for (auto const& r: regions) {
870-
all_regions.insert (r);
871-
}
863+
all_regions = std::set (regions.begin (), regions.end ());
872864
#endif
873865

874866
return -1;
@@ -3461,29 +3453,14 @@ Playlist::share_with (const PBD::ID& id)
34613453
void
34623454
Playlist::unshare_with (const PBD::ID& id)
34633455
{
3464-
list<PBD::ID>::iterator it = _shared_with_ids.begin ();
3465-
while (it != _shared_with_ids.end ()) {
3466-
if (*it == id) {
3467-
_shared_with_ids.erase (it);
3468-
break;
3469-
}
3470-
++it;
3471-
}
3456+
list<PBD::ID>::iterator it = std::find (_shared_with_ids.begin (), _shared_with_ids.end (), id);
3457+
if (it != _shared_with_ids.end ()) _shared_with_ids.erase (it);
34723458
}
34733459

34743460
bool
34753461
Playlist::shared_with (const PBD::ID& id) const
34763462
{
3477-
bool shared = false;
3478-
list<PBD::ID>::const_iterator it = _shared_with_ids.begin ();
3479-
while (it != _shared_with_ids.end () && !shared) {
3480-
if (*it == id) {
3481-
shared = true;
3482-
}
3483-
++it;
3484-
}
3485-
3486-
return shared;
3463+
return std::find(_shared_with_ids.cbegin(), _shared_with_ids.cend(), id) != _shared_with_ids.cend();
34873464
}
34883465

34893466
void

libs/ardour/plugin.cc

+13-11
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,11 @@ Plugin::preset_by_label (const string& label)
376376
}
377377

378378
// FIXME: O(n)
379-
for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
380-
if (i->second.label == label) {
381-
return &i->second;
382-
}
383-
}
379+
auto i = std::find_if (_presets.cbegin (), _presets.cend (), [&] (const auto& sp) {
380+
return sp.second.label == label;
381+
});
384382

385-
return 0;
383+
return i != _presets.cend () ? &i->second : 0;
386384
}
387385

388386
const Plugin::PresetRecord *
@@ -485,17 +483,21 @@ Plugin::resolve_midi ()
485483
vector<Plugin::PresetRecord>
486484
Plugin::get_presets ()
487485
{
488-
vector<PresetRecord> p;
489-
490486
if (!_have_presets) {
491487
_presets.clear ();
492488
find_presets ();
493489
_have_presets = true;
494490
}
495491

496-
for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
497-
p.push_back (i->second);
498-
}
492+
vector<PresetRecord> p;
493+
static_cast<void> (std::transform (
494+
_presets.cbegin (),
495+
_presets.cend (),
496+
p.begin (),
497+
[] (const std::pair<const string, PresetRecord>& i) {
498+
return i.second;
499+
}
500+
));
499501

500502
std::sort (p.begin(), p.end());
501503

libs/ardour/port_manager.cc

+30-26
Original file line numberDiff line numberDiff line change
@@ -1063,14 +1063,16 @@ PortManager::update_input_ports (bool clear)
10631063
} else {
10641064
std::shared_ptr<AudioInputPorts const> aip = _audio_input_ports.reader ();
10651065
/* find new audio ports */
1066-
for (std::vector<std::string>::iterator p = audio_ports.begin (); p != audio_ports.end (); ++p) {
1067-
if (port_is_mine (*p) || !_backend->get_port_by_name (*p)) {
1068-
continue;
1066+
static_cast<void> (std::copy_if (
1067+
audio_ports.cbegin (),
1068+
audio_ports.cend (),
1069+
new_audio.begin (),
1070+
[&] (const std::string& p) {
1071+
return !port_is_mine (p)
1072+
&& _backend->get_port_by_name (p)
1073+
&& aip->find (p) == aip->end ();
10691074
}
1070-
if (aip->find (*p) == aip->end ()) {
1071-
new_audio.push_back (*p);
1072-
}
1073-
}
1075+
));
10741076

10751077
/* find stale audio ports */
10761078
for (auto const& p : *aip) {
@@ -1081,19 +1083,20 @@ PortManager::update_input_ports (bool clear)
10811083

10821084
std::shared_ptr<MIDIInputPorts const> mip = _midi_input_ports.reader ();
10831085
/* find new MIDI ports */
1084-
for (std::vector<std::string>::iterator p = midi_ports.begin (); p != midi_ports.end (); ++p) {
1085-
if (port_is_mine (*p) || !_backend->get_port_by_name (*p)) {
1086-
continue;
1087-
}
1086+
static_cast<void> (std::copy_if (
1087+
midi_ports.cbegin (),
1088+
midi_ports.cend (),
1089+
new_audio.begin (),
1090+
[&, this] (const std::string& p) {
1091+
return !port_is_mine (p)
1092+
&& _backend->get_port_by_name (p)
10881093
#ifdef HAVE_ALSA
1089-
if ((*p).find (X_("Midi Through")) != string::npos || (*p).find (X_("Midi-Through")) != string::npos) {
1090-
continue;
1091-
}
1094+
&& p.find (X_("Midi Through")) == string::npos
1095+
&& p.find (X_("Midi-Through")) == string::npos
10921096
#endif
1093-
if (mip->find (*p) == mip->end ()) {
1094-
new_midi.push_back (*p);
1097+
&& mip->find (p) == mip->end ();
10951098
}
1096-
}
1099+
));
10971100

10981101
/* find stale audio ports */
10991102
for (auto const& p : *mip) {
@@ -1585,15 +1588,16 @@ PortManager::get_configurable_midi_ports (vector<string>& copy, bool for_input)
15851588

15861589
std::vector<string> ports;
15871590
AudioEngine::instance ()->get_ports (string (), DataType::MIDI, flags, ports);
1588-
for (vector<string>::iterator p = ports.begin (); p != ports.end (); ++p) {
1589-
if (port_is_mine (*p) && !port_is_virtual_piano (*p)) {
1590-
continue;
1591-
}
1592-
if ((*p).find (X_("Midi Through")) != string::npos || (*p).find (X_("Midi-Through")) != string::npos) {
1593-
continue;
1594-
}
1595-
copy.push_back (*p);
1596-
}
1591+
static_cast<void> (std::copy_if (
1592+
ports.cbegin (),
1593+
ports.cend (),
1594+
copy.end (),
1595+
[this] (const string& p) {
1596+
return !(port_is_mine (p) && !port_is_virtual_piano (p))
1597+
&& p.find (X_("Midi Through")) == string::npos
1598+
&& p.find (X_("Midi-Through")) == string::npos;
1599+
}
1600+
));
15971601
}
15981602

15991603
void

libs/ardour/region_factory.cc

+9-6
Original file line numberDiff line numberDiff line change
@@ -429,14 +429,17 @@ RegionFactory::rename_in_region_name_maps (std::shared_ptr<Region> region)
429429

430430
Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
431431

432-
map<string, PBD::ID>::iterator i = region_name_map.begin ();
433-
while (i != region_name_map.end () && i->second != region->id ()) {
434-
++i;
435-
}
432+
auto old_region_it = std::find_if (
433+
region_name_map.begin (),
434+
region_name_map.end (),
435+
[&] (auto& region_name) {
436+
return region_name.second == region->id ();
437+
}
438+
);
436439

437440
/* Erase the entry for the old name and put in a new one */
438-
if (i != region_name_map.end ()) {
439-
region_name_map.erase (i);
441+
if (old_region_it != region_name_map.end ()) {
442+
region_name_map.erase (old_region_it);
440443
region_name_map[region->name ()] = region->id ();
441444
}
442445
}

libs/ardour/session.cc

+10-10
Original file line numberDiff line numberDiff line change
@@ -7004,16 +7004,16 @@ Session::ensure_search_path_includes (const string& path, DataType type)
70047004
break;
70057005
}
70067006

7007-
for (vector<std::string>::iterator i = sp.begin(); i != sp.end(); ++i) {
7008-
/* No need to add this new directory if it has the same inode as
7009-
an existing one; checking inode rather than name prevents duplicated
7010-
directories when we are using symlinks.
7011-
7012-
On Windows, I think we could just do if (*i == path) here.
7013-
*/
7014-
if (PBD::equivalent_paths (*i, path)) {
7015-
return;
7016-
}
7007+
/* No need to add this new directory if it has the same inode as
7008+
* an existing one; checking inode rather than name prevents duplicated
7009+
* directories when we are using symlinks.
7010+
*
7011+
* On Windows, I think we could just do if (*i == path) here.
7012+
*/
7013+
if (std::any_of (sp.cbegin (), sp.cend (), [&] (const std::string& i) {
7014+
return PBD::equivalent_paths (i, path);
7015+
})) {
7016+
return;
70177017
}
70187018

70197019
sp += path;

libs/ardour/session_directory.cc

+6-5
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,14 @@ SessionDirectory::is_valid () const
8282

8383
vector<std::string> sub_dirs = sub_directories ();
8484

85-
for (vector<std::string>::iterator i = sub_dirs.begin(); i != sub_dirs.end(); ++i) {
86-
if (!Glib::file_test (*i, Glib::FILE_TEST_IS_DIR)) {
87-
PBD::warning << string_compose(_("Session subdirectory does not exist at path %1"), *i) << endmsg;
85+
return std::all_of (sub_dirs.cbegin (), sub_dirs.cend (), [&] (const std::string& i) {
86+
if (!Glib::file_test (i, Glib::FILE_TEST_IS_DIR)) {
87+
PBD::warning << string_compose(_("Session subdirectory does not exist at path %1"), i) << endmsg;
8888
return false;
89+
} else {
90+
return true;
8991
}
90-
}
91-
return true;
92+
});
9293
}
9394

9495
const std::string

libs/ardour/session_playlists.cc

+9-7
Original file line numberDiff line numberDiff line change
@@ -657,14 +657,16 @@ SessionPlaylists::playlists_for_track (std::shared_ptr<Track> tr) const
657657

658658
vector<std::shared_ptr<Playlist> > pl_tr;
659659

660-
for (vector<std::shared_ptr<Playlist> >::iterator i = pl.begin(); i != pl.end(); ++i) {
661-
if ( ((*i)->get_orig_track_id() == tr->id()) ||
662-
(tr->playlist()->id() == (*i)->id()) ||
663-
((*i)->shared_with (tr->id())) )
664-
{
665-
pl_tr.push_back (*i);
660+
static_cast<void> (std::copy_if (
661+
pl.cbegin (),
662+
pl.cend (),
663+
pl_tr.begin (),
664+
[&] (const std::shared_ptr<Playlist>& i) {
665+
return i->get_orig_track_id() == tr->id()
666+
|| tr->playlist()->id() == i->id()
667+
|| i->shared_with (tr->id());
666668
}
667-
}
669+
));
668670

669671
return pl_tr;
670672
}

0 commit comments

Comments
 (0)