Browse Source

2008-05-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	Rewritten the portion of the code that mangaes allowed fast 
indexes.
	* src/PeerSessionResource.cc
	* src/PeerSessionResource.h
Tatsuhiro Tsujikawa 17 years ago
parent
commit
99f07b515f
3 changed files with 19 additions and 14 deletions
  1. 6 0
      ChangeLog
  2. 13 11
      src/PeerSessionResource.cc
  3. 0 3
      src/PeerSessionResource.h

+ 6 - 0
ChangeLog

@@ -1,3 +1,9 @@
+2008-05-16  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	Rewritten the portion of the code that mangaes allowed fast indexes.
+	* src/PeerSessionResource.cc
+	* src/PeerSessionResource.h
+	
 2008-05-16  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
 
 	Removed the invalid string including '\r' from ru.po and

+ 13 - 11
src/PeerSessionResource.cc

@@ -203,22 +203,24 @@ const std::deque<size_t>& PeerSessionResource::peerAllowedIndexSet() const
   return _peerAllowedIndexSet;
 }
 
-template<typename T>
-bool PeerSessionResource::indexIncluded(const std::deque<T>& c, T index) const
+static void updateIndexSet(std::deque<size_t>& c, size_t index)
 {
-  return std::find(c.begin(), c.end(), index) != c.end();
+  std::deque<size_t>::iterator i = std::lower_bound(c.begin(), c.end(), index);
+  if(i == c.end() || (*i) != index) {
+    c.insert(i, index);
+  } 
 }
 
 void PeerSessionResource::addPeerAllowedIndex(size_t index)
 {
-  if(!indexIncluded(_peerAllowedIndexSet, index)) {
-    _peerAllowedIndexSet.push_back(index);
-  }
+  updateIndexSet(_peerAllowedIndexSet, index);
 }
 
 bool PeerSessionResource::peerAllowedIndexSetContains(size_t index) const
 {
-  return indexIncluded(_peerAllowedIndexSet, index);
+  return std::binary_search(_peerAllowedIndexSet.begin(),
+			    _peerAllowedIndexSet.end(),
+			    index);
 }
 
 const std::deque<size_t>& PeerSessionResource::amAllowedIndexSet() const
@@ -228,14 +230,14 @@ const std::deque<size_t>& PeerSessionResource::amAllowedIndexSet() const
 
 void PeerSessionResource::addAmAllowedIndex(size_t index)
 {
-  if(!indexIncluded(_amAllowedIndexSet, index)) {
-    _amAllowedIndexSet.push_back(index);
-  }
+  updateIndexSet(_amAllowedIndexSet, index);
 }
 
 bool PeerSessionResource::amAllowedIndexSetContains(size_t index) const
 {
-  return indexIncluded(_amAllowedIndexSet, index);
+  return std::binary_search(_amAllowedIndexSet.begin(),
+			    _amAllowedIndexSet.end(),
+			    index);
 }
 
 bool PeerSessionResource::extendedMessagingEnabled() const

+ 0 - 3
src/PeerSessionResource.h

@@ -80,9 +80,6 @@ private:
   Time _lastDownloadUpdate;
 
   Time _lastAmUnchoking;
-
-  template<typename T>
-  bool indexIncluded(const std::deque<T>& c, T index) const;
 public:
   PeerSessionResource(size_t pieceLength, uint64_t totalLength);