Skip to content

Commit cadf627

Browse files
committed
Replace loop patterns with std algorithms under libs/
1 parent c0401be commit cadf627

28 files changed

+253
-311
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

+7-5
Original file line numberDiff line numberDiff line change
@@ -1609,11 +1609,13 @@ Locations::marks_either_side (timepos_t const & pos, timepos_t& before, timepos_
16091609

16101610
positions.sort ();
16111611

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

16181620
if (i == positions.end ()) {
16191621
/* run out of marks */

libs/ardour/panner_manager.cc

+6-6
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ PannerManager::panner_discover (string path)
113113

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

116-
list<PannerInfo*>::iterator i;
117-
118-
for (i = panner_info.begin(); i != panner_info.end(); ++i) {
119-
if (pinfo->descriptor.name == (*i)->descriptor.name) {
120-
break;
116+
list<PannerInfo*>::iterator i = std::find_if(
117+
panner_info.begin(),
118+
panner_info.end(),
119+
[&] (const auto& pi) {
120+
return pi->descriptor.name == pinfo->descriptor.name;
121121
}
122-
}
122+
);
123123

124124
if (i == panner_info.end()) {
125125
panner_info.push_back (pinfo);

libs/ardour/playlist.cc

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

855855
#if 0
856-
for (set<std::shared_ptr<Region> >::iterator x = all_regions.begin(); x != all_regions.end(); ++x) {
857-
if ((*x) == region) {
858-
all_regions.erase (x);
859-
break;
860-
}
861-
}
856+
all_regions.erase (region);
862857
#else /* sync_all_regions_with_regions */
863-
all_regions.clear ();
864-
for (auto const& r: regions) {
865-
all_regions.insert (r);
866-
}
858+
all_regions = std::set (regions.begin (), regions.end ());
867859
#endif
868860

869861
return -1;
@@ -3450,29 +3442,14 @@ Playlist::share_with (const PBD::ID& id)
34503442
void
34513443
Playlist::unshare_with (const PBD::ID& id)
34523444
{
3453-
list<PBD::ID>::iterator it = _shared_with_ids.begin ();
3454-
while (it != _shared_with_ids.end ()) {
3455-
if (*it == id) {
3456-
_shared_with_ids.erase (it);
3457-
break;
3458-
}
3459-
++it;
3460-
}
3445+
list<PBD::ID>::iterator it = std::find (_shared_with_ids.begin (), _shared_with_ids.end (), id);
3446+
if (it != _shared_with_ids.end ()) _shared_with_ids.erase (it);
34613447
}
34623448

34633449
bool
34643450
Playlist::shared_with (const PBD::ID& id) const
34653451
{
3466-
bool shared = false;
3467-
list<PBD::ID>::const_iterator it = _shared_with_ids.begin ();
3468-
while (it != _shared_with_ids.end () && !shared) {
3469-
if (*it == id) {
3470-
shared = true;
3471-
}
3472-
++it;
3473-
}
3474-
3475-
return shared;
3452+
return std::find(_shared_with_ids.cbegin(), _shared_with_ids.cend(), id) != _shared_with_ids.cend();
34763453
}
34773454

34783455
void

libs/ardour/plugin.cc

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

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

385-
return 0;
387+
return i != _presets.cend () ? &i->second : 0;
386388
}
387389

388390
const Plugin::PresetRecord *
@@ -485,17 +487,21 @@ Plugin::resolve_midi ()
485487
vector<Plugin::PresetRecord>
486488
Plugin::get_presets ()
487489
{
488-
vector<PresetRecord> p;
489-
490490
if (!_have_presets) {
491491
_presets.clear ();
492492
find_presets ();
493493
_have_presets = true;
494494
}
495495

496-
for (const std::pair<const string, PresetRecord>& i : _presets) {
497-
p.push_back (i.second);
498-
}
496+
vector<PresetRecord> p;
497+
static_cast<void> (std::transform (
498+
_presets.cbegin (),
499+
_presets.cend (),
500+
p.begin (),
501+
[] (const std::pair<const string, PresetRecord>& i) {
502+
return i.second;
503+
}
504+
));
499505

500506
std::sort (p.begin(), p.end());
501507

libs/ardour/port_manager.cc

+28-26
Original file line numberDiff line numberDiff line change
@@ -1063,14 +1063,14 @@ 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::string& p : audio_ports) {
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) && _backend->get_port_by_name (p)) && aip->find (p) == aip->end ();
10691072
}
1070-
if (aip->find (p) == aip->end ()) {
1071-
new_audio.push_back (p);
1072-
}
1073-
}
1073+
));
10741074

10751075
/* find stale audio ports */
10761076
for (auto const& p : *aip) {
@@ -1081,19 +1081,20 @@ PortManager::update_input_ports (bool clear)
10811081

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

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

15861587
std::vector<string> ports;
15871588
AudioEngine::instance ()->get_ports (string (), DataType::MIDI, flags, ports);
1588-
for (string& p : ports) {
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-
}
1589+
static_cast<void> (std::copy_if (
1590+
ports.cbegin (),
1591+
ports.cend (),
1592+
copy.end (),
1593+
[this] (const string& p) {
1594+
return !(port_is_mine (p) && !port_is_virtual_piano (p))
1595+
&& p.find (X_("Midi Through")) == string::npos
1596+
&& p.find (X_("Midi-Through")) == string::npos;
1597+
}
1598+
));
15971599
}
15981600

15991601
void

libs/ardour/region_factory.cc

+7-4
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,13 @@ 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+
map<string, PBD::ID>::iterator i = 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 */
438441
if (i != region_name_map.end ()) {

libs/ardour/session.cc

+10-10
Original file line numberDiff line numberDiff line change
@@ -6994,16 +6994,16 @@ Session::ensure_search_path_includes (const string& path, DataType type)
69946994
break;
69956995
}
69966996

6997-
for (std::string& i : sp) {
6998-
/* No need to add this new directory if it has the same inode as
6999-
an existing one; checking inode rather than name prevents duplicated
7000-
directories when we are using symlinks.
7001-
7002-
On Windows, I think we could just do if (*i == path) here.
7003-
*/
7004-
if (PBD::equivalent_paths (i, path)) {
7005-
return;
7006-
}
6997+
/* No need to add this new directory if it has the same inode as
6998+
* an existing one; checking inode rather than name prevents duplicated
6999+
* directories when we are using symlinks.
7000+
*
7001+
* On Windows, I think we could just do if (*i == path) here.
7002+
*/
7003+
if (std::any_of (sp.cbegin (), sp.cend (), [&] (const std::string& i) {
7004+
return PBD::equivalent_paths (i, path);
7005+
})) {
7006+
return;
70077007
}
70087008

70097009
sp += path;

libs/ardour/session_directory.cc

+4-3
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 (std::string& i : sub_dirs) {
85+
return std::all_of (sub_dirs.cbegin (), sub_dirs.cend (), [&] (const std::string& i) {
8686
if (!Glib::file_test (i, Glib::FILE_TEST_IS_DIR)) {
8787
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 (std::shared_ptr<Playlist> & i : pl) {
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)