diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2008-05-16 14:51:14 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2008-05-16 14:51:14 +0000 |
| commit | 99f07b515fe8a9facb3093da588f898b8f5ec5c4 (patch) | |
| tree | 012f5bd05a35a59836ef8e3596b30eca521b959e /src | |
| parent | 738996932abd301d1cd9f654d1e693efbbd3fdf5 (diff) | |
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
Diffstat (limited to 'src')
| -rw-r--r-- | src/PeerSessionResource.cc | 24 | ||||
| -rw-r--r-- | src/PeerSessionResource.h | 3 |
2 files changed, 13 insertions, 14 deletions
diff --git a/src/PeerSessionResource.cc b/src/PeerSessionResource.cc index a5c54fc7..1a111a2b 100644 --- a/src/PeerSessionResource.cc +++ b/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 diff --git a/src/PeerSessionResource.h b/src/PeerSessionResource.h index c4897bcd..4388004c 100644 --- a/src/PeerSessionResource.h +++ b/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); |
