bruteforce and efficient implementation of mixing groups management in

session.
This commit is contained in:
brunoherbelin
2021-03-12 20:25:36 +01:00
parent 10f9c1b329
commit 5ab5f1b60f
8 changed files with 84 additions and 165 deletions

View File

@@ -334,142 +334,53 @@ void Session::move(int current_index, int target_index)
bool Session::canlink (SourceList sources)
{
bool canlink = false;
bool canlink = true;
// verify that all sources given are valid in the sesion
validate(sources);
// we need at least 2 sources to make a group
if (sources.size() > 1) {
// does this interfere with existing groups?
SourceListCompare c = SOURCELIST_DISTINCT;
for (auto group_it = mixing_groups_.begin(); group_it!= mixing_groups_.end(); group_it++) {
// get the list of sources in the group
SourceList group_sources = (*group_it)->getCopy();
c = compare( group_sources, sources );
if ( c != SOURCELIST_DISTINCT ){
// in case the source list containing entirely the group
// then extend the group with extra sources
if ( c == SOURCELIST_FIRST_IN_SECOND ) {
canlink = true;
}
// stop the loop
// (ignore INTERSECT and EQUAL cases)
break;
}
for (auto it = sources.begin(); it != sources.end(); it++) {
// this source is linked
if ( (*it)->mixingGroup() != nullptr ) {
// askt its group to detach it
canlink = false;
}
canlink |= ( c == SOURCELIST_DISTINCT );
}
return canlink;
}
bool Session::link(SourceList sources, Group *parent)
void Session::link(SourceList sources, Group *parent)
{
// pointer to created MixingGroup
MixingGroup *g = nullptr;
// verify that all sources given are valid in the sesion
validate(sources);
// we need at least 2 sources to make a group
if (sources.size() > 1) {
// does this interfere with existing groups?
SourceListCompare c = SOURCELIST_DISTINCT;
for (auto group_it = mixing_groups_.begin(); group_it!= mixing_groups_.end(); group_it++) {
// get the list of sources in the group
SourceList group_sources = (*group_it)->getCopy();
c = compare( group_sources, sources );
if ( c != SOURCELIST_DISTINCT ){
// in case the source list containing entirely the group
// then extend the group with extra sources
if ( c == SOURCELIST_FIRST_IN_SECOND ) {
g = *group_it;
// add all sources (will ignore already linked sources)
g->attach( sources );
}
// in case the mixing group contains entirely the source list
// then reduce the group to the source list
else if ( c == SOURCELIST_SECOND_IN_FIRST ) {
g = *group_it;
// i.e. keep elements of group_sources that are not in list
for (auto it = sources.begin(); it != sources.end(); it++)
group_sources.remove(*it);
// remove all extra sources
g->detach( group_sources );
}
// stop the loop
// (we ignore INTERSECT and EQUAL cases)
break;
}
}
unlink(sources);
// remaining case: the sourcelist is distinct from all existing groups
if ( c == SOURCELIST_DISTINCT ) {
// create and add a new mixing group
g = new MixingGroup(sources);
mixing_groups_.push_back(g);
}
// create and add a new mixing group
MixingGroup *g = new MixingGroup(sources);
mixing_groups_.push_back(g);
// if provided, attach the group to the parent
if (g && parent != nullptr)
g->attachTo( parent );
}
return ( g != nullptr );
}
bool Session::unlink (SourceList sources)
void Session::unlink (SourceList sources)
{
bool ret = false;
// verify that all sources given are valid in the sesion
validate(sources);
// we need at least 1 sources to make a decision
if (sources.size() > 0) {
// does this list intersect with existing groups?
SourceListCompare c = SOURCELIST_DISTINCT;
for (auto group_it = mixing_groups_.begin(); group_it!= mixing_groups_.end(); group_it++) {
// get the list of sources in the group
SourceList group_sources = (*group_it)->getCopy();
c = compare( group_sources, sources );
// in case the group source is the same or inside the list,
// then delete the group entirely
if ( c == SOURCELIST_EQUAL || c == SOURCELIST_FIRST_IN_SECOND) {
delete (*group_it);
mixing_groups_.erase(group_it);
// did something
ret = true;
// stop the loop
break;
}
// in case of the source list is inside the group,
// then remove the listed sources from the group
else if ( c == SOURCELIST_SECOND_IN_FIRST ) {
// remove all extra sources
(*group_it)->detach( sources );
// did something
ret = true;
}
// in case of the group source intersects with the list,
// then remove the listed sources from the group
else if ( c == SOURCELIST_INTERSECT ) {
group_sources = intersect( group_sources, sources);
// remove all extra sources
(*group_it)->detach( group_sources );
// did something
ret = true;
}
// (NB: we ignore DISTINCT case)
// brute force : detach all given sources
for (auto it = sources.begin(); it != sources.end(); it++) {
// this source is linked
if ( (*it)->mixingGroup() != nullptr ) {
// askt its group to detach it
(*it)->mixingGroup()->detach(*it);
}
}
return ret;
}
std::list<SourceList> Session::getMixingGroups () const