Use binary_search for multicast groups, which are kept in sorted order.

This commit is contained in:
Adam Ierymenko 2015-04-06 19:41:55 -07:00
parent 758bf949db
commit 76ad19f411
2 changed files with 27 additions and 21 deletions

View File

@ -137,10 +137,33 @@ Network::~Network()
}
}
std::vector<MulticastGroup> Network::allMulticastGroups() const
{
Mutex::Lock _l(_lock);
std::vector<MulticastGroup> mgs(_myMulticastGroups);
std::vector<MulticastGroup>::iterator oldend(mgs.end());
for(std::map< MulticastGroup,uint64_t >::const_iterator i(_multicastGroupsBehindMe.begin());i!=_multicastGroupsBehindMe.end();++i) {
if (!std::binary_search(mgs.begin(),oldend,i->first))
mgs.push_back(i->first);
}
std::sort(mgs.begin(),mgs.end());
return mgs;
}
bool Network::subscribedToMulticastGroup(const MulticastGroup &mg,bool includeBridgedGroups) const
{
Mutex::Lock _l(_lock);
if (std::binary_search(_myMulticastGroups.begin(),_myMulticastGroups.end(),mg))
return true;
else if (includeBridgedGroups)
return (_multicastGroupsBehindMe.find(mg) != _multicastGroupsBehindMe.end());
else return false;
}
void Network::multicastSubscribe(const MulticastGroup &mg)
{
Mutex::Lock _l(_lock);
if (std::find(_myMulticastGroups.begin(),_myMulticastGroups.end(),mg) != _myMulticastGroups.end())
if (std::binary_search(_myMulticastGroups.begin(),_myMulticastGroups.end(),mg))
return;
_myMulticastGroups.push_back(mg);
std::sort(_myMulticastGroups.begin(),_myMulticastGroups.end());

View File

@ -100,31 +100,14 @@ public:
/**
* @return All multicast groups including learned groups that are behind any bridges we're attached to
*/
inline std::vector<MulticastGroup> allMulticastGroups() const
{
Mutex::Lock _l(_lock);
std::vector<MulticastGroup> mgs(_myMulticastGroups);
for(std::map< MulticastGroup,uint64_t >::const_iterator i(_multicastGroupsBehindMe.begin());i!=_multicastGroupsBehindMe.end();++i) {
if (std::find(mgs.begin(),mgs.end(),i->first) == mgs.end())
mgs.push_back(i->first);
}
std::sort(mgs.begin(),mgs.end());
return mgs;
}
std::vector<MulticastGroup> allMulticastGroups() const;
/**
* @param mg Multicast group
* @param includeBridgedGroups If true, also include any groups we've learned via bridging
* @return True if this network endpoint / peer is a member
*/
bool subscribedToMulticastGroup(const MulticastGroup &mg,bool includeBridgedGroups) const
{
Mutex::Lock _l(_lock);
if (std::find(_myMulticastGroups.begin(),_myMulticastGroups.end(),mg) != _myMulticastGroups.end())
return true;
else if (includeBridgedGroups)
return (_multicastGroupsBehindMe.find(mg) != _multicastGroupsBehindMe.end());
else return false;
}
bool subscribedToMulticastGroup(const MulticastGroup &mg,bool includeBridgedGroups) const;
/**
* Subscribe to a multicast group