Browse Source

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

Adam Ierymenko 10 years ago
parent
commit
76ad19f411
2 changed files with 27 additions and 21 deletions
  1. 24 1
      node/Network.cpp
  2. 3 20
      node/Network.hpp

+ 24 - 1
node/Network.cpp

@@ -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)
 void Network::multicastSubscribe(const MulticastGroup &mg)
 {
 {
 	Mutex::Lock _l(_lock);
 	Mutex::Lock _l(_lock);
-	if (std::find(_myMulticastGroups.begin(),_myMulticastGroups.end(),mg) != _myMulticastGroups.end())
+	if (std::binary_search(_myMulticastGroups.begin(),_myMulticastGroups.end(),mg))
 		return;
 		return;
 	_myMulticastGroups.push_back(mg);
 	_myMulticastGroups.push_back(mg);
 	std::sort(_myMulticastGroups.begin(),_myMulticastGroups.end());
 	std::sort(_myMulticastGroups.begin(),_myMulticastGroups.end());

+ 3 - 20
node/Network.hpp

@@ -100,31 +100,14 @@ public:
 	/**
 	/**
 	 * @return All multicast groups including learned groups that are behind any bridges we're attached to
 	 * @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 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
 	 * @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
 	 * Subscribe to a multicast group