Skip to content

Commit

Permalink
Replace loop patterns with std algorithms under libs/
Browse files Browse the repository at this point in the history
  • Loading branch information
aledomu committed Nov 21, 2024
1 parent 4a99026 commit 3a919b8
Show file tree
Hide file tree
Showing 28 changed files with 262 additions and 339 deletions.
10 changes: 3 additions & 7 deletions libs/ardour/export_graph_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,9 @@ ExportGraphBuilder::process (samplecnt_t samples, bool last_cycle)
bool
ExportGraphBuilder::post_process ()
{
for (std::list<Intermediate *>::iterator it = intermediates.begin(); it != intermediates.end(); /* ++ in loop */) {
if ((*it)->process()) {
it = intermediates.erase (it);
} else {
++it;
}
}
static_cast<void>(intermediates.remove_if([] (Intermediate* const& it) {
return it->process();
}));

return intermediates.empty();
}
Expand Down
22 changes: 12 additions & 10 deletions libs/ardour/location.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1611,27 +1611,29 @@ Locations::marks_either_side (timepos_t const & pos, timepos_t& before, timepos_

positions.sort ();

std::list<timepos_t>::iterator i = positions.begin ();

while (i != positions.end () && *i < pos) {
++i;
}
auto time_mark_it = std::find_if(
positions.begin (),
positions.end (),
[&] (const auto& p) {
return p >= pos;
}
);

if (i == positions.end ()) {
if (time_mark_it == positions.end ()) {
/* run out of marks */
before = positions.back ();
return;
}

after = *i;
after = *time_mark_it;

if (i == positions.begin ()) {
if (time_mark_it == positions.begin ()) {
/* none before */
return;
}

--i;
before = *i;
--time_mark_it;
before = *time_mark_it;
}

void
Expand Down
12 changes: 4 additions & 8 deletions libs/ardour/panner_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,11 @@ PannerManager::panner_discover (string path)

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

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

for (i = panner_info.begin(); i != panner_info.end(); ++i) {
if (pinfo->descriptor.name == (*i)->descriptor.name) {
break;
}
}

if (i == panner_info.end()) {
if (pi_it == panner_info.end()) {
panner_info.push_back (pinfo);
DEBUG_TRACE (DEBUG::Panning, string_compose(_("Panner discovered: \"%1\" in %2\n"), pinfo->descriptor.name, path));
} else {
Expand Down
33 changes: 5 additions & 28 deletions libs/ardour/playlist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -858,17 +858,9 @@ Playlist::remove_region_internal (std::shared_ptr<Region> region, ThawList& thaw
}

#if 0
for (set<std::shared_ptr<Region> >::iterator x = all_regions.begin(); x != all_regions.end(); ++x) {
if ((*x) == region) {
all_regions.erase (x);
break;
}
}
all_regions.erase (region);
#else /* sync_all_regions_with_regions */
all_regions.clear ();
for (auto const& r: regions) {
all_regions.insert (r);
}
all_regions = std::set (regions.begin (), regions.end ());
#endif

return -1;
Expand Down Expand Up @@ -3461,29 +3453,14 @@ Playlist::share_with (const PBD::ID& id)
void
Playlist::unshare_with (const PBD::ID& id)
{
list<PBD::ID>::iterator it = _shared_with_ids.begin ();
while (it != _shared_with_ids.end ()) {
if (*it == id) {
_shared_with_ids.erase (it);
break;
}
++it;
}
list<PBD::ID>::iterator it = std::find (_shared_with_ids.begin (), _shared_with_ids.end (), id);
if (it != _shared_with_ids.end ()) _shared_with_ids.erase (it);
}

bool
Playlist::shared_with (const PBD::ID& id) const
{
bool shared = false;
list<PBD::ID>::const_iterator it = _shared_with_ids.begin ();
while (it != _shared_with_ids.end () && !shared) {
if (*it == id) {
shared = true;
}
++it;
}

return shared;
return std::find(_shared_with_ids.cbegin(), _shared_with_ids.cend(), id) != _shared_with_ids.cend();
}

void
Expand Down
24 changes: 13 additions & 11 deletions libs/ardour/plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,11 @@ Plugin::preset_by_label (const string& label)
}

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

return 0;
return i != _presets.cend () ? &i->second : 0;
}

const Plugin::PresetRecord *
Expand Down Expand Up @@ -485,17 +483,21 @@ Plugin::resolve_midi ()
vector<Plugin::PresetRecord>
Plugin::get_presets ()
{
vector<PresetRecord> p;

if (!_have_presets) {
_presets.clear ();
find_presets ();
_have_presets = true;
}

for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
p.push_back (i->second);
}
vector<PresetRecord> p;
static_cast<void> (std::transform (
_presets.cbegin (),
_presets.cend (),
p.begin (),
[] (const std::pair<const string, PresetRecord>& i) {
return i.second;
}
));

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

Expand Down
56 changes: 30 additions & 26 deletions libs/ardour/port_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1063,14 +1063,16 @@ PortManager::update_input_ports (bool clear)
} else {
std::shared_ptr<AudioInputPorts const> aip = _audio_input_ports.reader ();
/* find new audio ports */
for (std::vector<std::string>::iterator p = audio_ports.begin (); p != audio_ports.end (); ++p) {
if (port_is_mine (*p) || !_backend->get_port_by_name (*p)) {
continue;
static_cast<void> (std::copy_if (
audio_ports.cbegin (),
audio_ports.cend (),
new_audio.begin (),
[&] (const std::string& p) {
return !port_is_mine (p)
&& _backend->get_port_by_name (p)
&& aip->find (p) == aip->end ();
}
if (aip->find (*p) == aip->end ()) {
new_audio.push_back (*p);
}
}
));

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

std::shared_ptr<MIDIInputPorts const> mip = _midi_input_ports.reader ();
/* find new MIDI ports */
for (std::vector<std::string>::iterator p = midi_ports.begin (); p != midi_ports.end (); ++p) {
if (port_is_mine (*p) || !_backend->get_port_by_name (*p)) {
continue;
}
static_cast<void> (std::copy_if (
midi_ports.cbegin (),
midi_ports.cend (),
new_audio.begin (),
[&, this] (const std::string& p) {
return !port_is_mine (p)
&& _backend->get_port_by_name (p)
#ifdef HAVE_ALSA
if ((*p).find (X_("Midi Through")) != string::npos || (*p).find (X_("Midi-Through")) != string::npos) {
continue;
}
&& p.find (X_("Midi Through")) == string::npos
&& p.find (X_("Midi-Through")) == string::npos
#endif
if (mip->find (*p) == mip->end ()) {
new_midi.push_back (*p);
&& mip->find (p) == mip->end ();
}
}
));

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

std::vector<string> ports;
AudioEngine::instance ()->get_ports (string (), DataType::MIDI, flags, ports);
for (vector<string>::iterator p = ports.begin (); p != ports.end (); ++p) {
if (port_is_mine (*p) && !port_is_virtual_piano (*p)) {
continue;
}
if ((*p).find (X_("Midi Through")) != string::npos || (*p).find (X_("Midi-Through")) != string::npos) {
continue;
}
copy.push_back (*p);
}
static_cast<void> (std::copy_if (
ports.cbegin (),
ports.cend (),
copy.end (),
[this] (const string& p) {
return !(port_is_mine (p) && !port_is_virtual_piano (p))
&& p.find (X_("Midi Through")) == string::npos
&& p.find (X_("Midi-Through")) == string::npos;
}
));
}

void
Expand Down
15 changes: 9 additions & 6 deletions libs/ardour/region_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,17 @@ RegionFactory::rename_in_region_name_maps (std::shared_ptr<Region> region)

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

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

/* Erase the entry for the old name and put in a new one */
if (i != region_name_map.end ()) {
region_name_map.erase (i);
if (old_region_it != region_name_map.end ()) {
region_name_map.erase (old_region_it);
region_name_map[region->name ()] = region->id ();
}
}
Expand Down
20 changes: 10 additions & 10 deletions libs/ardour/session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7004,16 +7004,16 @@ Session::ensure_search_path_includes (const string& path, DataType type)
break;
}

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

sp += path;
Expand Down
11 changes: 6 additions & 5 deletions libs/ardour/session_directory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ SessionDirectory::is_valid () const

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

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

const std::string
Expand Down
16 changes: 9 additions & 7 deletions libs/ardour/session_playlists.cc
Original file line number Diff line number Diff line change
Expand Up @@ -657,14 +657,16 @@ SessionPlaylists::playlists_for_track (std::shared_ptr<Track> tr) const

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

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

return pl_tr;
}
Expand Down
Loading

0 comments on commit 3a919b8

Please sign in to comment.