diff --git a/src/ConsoleStatCalc.cc b/src/ConsoleStatCalc.cc index 9e02aa46..5aa75046 100644 --- a/src/ConsoleStatCalc.cc +++ b/src/ConsoleStatCalc.cc @@ -142,9 +142,9 @@ void printProgressCompact(std::ostream& o, const DownloadEngine* e, e->getRequestGroupMan()->getRequestGroups(); size_t cnt = 0; const size_t MAX_ITEM = 5; - for(RequestGroupList::SeqType::const_iterator i = groups.begin(), + for(RequestGroupList::const_iterator i = groups.begin(), eoi = groups.end(); i != eoi && cnt < MAX_ITEM; ++i, ++cnt) { - const SharedHandle& rg = (*i).second; + const SharedHandle& rg = *i; TransferStat stat = rg->calculateStat(); o << "[#" << GroupId::toAbbrevHex(rg->getGID()) << " "; printSizeProgress(o, rg, stat, sizeFormatter); @@ -209,9 +209,8 @@ public: const SizeFormatter& sizeFormatter): cols_(cols), e_(e), sizeFormatter_(sizeFormatter) {} - void operator()(const RequestGroupList::SeqType::value_type& val) + void operator()(const RequestGroupList::value_type& rg) { - const SharedHandle& rg = val.second; const char SEP_CHAR = '-'; std::stringstream o; printProgress(o, rg, e_, sizeFormatter_); @@ -323,7 +322,7 @@ ConsoleStatCalc::calculateStat(const DownloadEngine* e) size_t numGroup = e->getRequestGroupMan()->countRequestGroup(); if(numGroup == 1) { const SharedHandle& rg = - (*e->getRequestGroupMan()->getRequestGroups().begin()).second; + *e->getRequestGroupMan()->getRequestGroups().begin(); printProgress(o, rg, e, sizeFormatter); } else if(numGroup > 1) { // For more than 2 RequestGroups, use compact readout form diff --git a/src/HaveEraseCommand.cc b/src/HaveEraseCommand.cc index 29163860..69e7b817 100644 --- a/src/HaveEraseCommand.cc +++ b/src/HaveEraseCommand.cc @@ -57,9 +57,9 @@ void HaveEraseCommand::process() { const RequestGroupList& groups = getDownloadEngine()->getRequestGroupMan()->getRequestGroups(); - for(RequestGroupList::SeqType::const_iterator i = groups.begin(), + for(RequestGroupList::const_iterator i = groups.begin(), eoi = groups.end(); i != eoi; ++i) { - const SharedHandle& ps = (*i).second->getPieceStorage(); + const SharedHandle& ps = (*i)->getPieceStorage(); if(ps) { ps->removeAdvertisedPiece(5); } diff --git a/src/IndexedList.h b/src/IndexedList.h index c32ed760..abccf295 100644 --- a/src/IndexedList.h +++ b/src/IndexedList.h @@ -50,14 +50,319 @@ enum A2_HOW { A2_POS_END }; +template +struct IndexedListIterator { + typedef IndexedListIterator iterator; + typedef IndexedListIterator const_iterator; + + typedef typename SeqIteratorType::iterator_category iterator_category; + typedef ValueType value_type; + typedef PointerType pointer; + typedef ReferenceType reference; + typedef typename SeqIteratorType::size_type size_type; + typedef typename SeqIteratorType::difference_type difference_type; + typedef IndexedListIterator SelfType; + + IndexedListIterator() {} + IndexedListIterator(const iterator& other) + : p(other.p) {} + IndexedListIterator(const SeqIteratorType& p) + : p(p) {} + + reference operator*() const + { + return (*p).second; + } + + pointer operator->() const + { + return &(*p).second; + } + + SelfType& operator++() + { + ++p; + return *this; + } + + SelfType operator++(int) + { + SelfType copy = *this; + ++*this; + return copy; + } + + SelfType& operator--() + { + --p; + return *this; + } + + SelfType& operator--(int) + { + SelfType copy = *this; + --*this; + return copy; + } + + SelfType& operator+=(difference_type n) + { + std::advance(p, n); + return *this; + } + + SelfType operator+(difference_type n) const + { + SelfType copy = *this; + return copy += n; + } + + SelfType& operator-=(difference_type n) + { + std::advance(p, -n); + return *this; + } + + SelfType operator-(difference_type n) const + { + SelfType copy = *this; + return copy -= n; + } + + reference operator[](size_type n) const + { + return p[n].second; + } + + SeqIteratorType p; +}; + +template +bool operator==(const IndexedListIterator& lhs, + const IndexedListIterator& rhs) +{ + return lhs.p == rhs.p; +} + +template +bool operator==(const IndexedListIterator& lhs, + const IndexedListIterator& rhs) +{ + return lhs.p == rhs.p; +} + +template +bool operator!=(const IndexedListIterator& lhs, + const IndexedListIterator& rhs) +{ + return lhs.p != rhs.p; +} + +template +bool operator!=(const IndexedListIterator& lhs, + const IndexedListIterator& rhs) +{ + return lhs.p != rhs.p; +} + +template +bool operator<(const IndexedListIterator& lhs, + const IndexedListIterator& rhs) +{ + return lhs.p < rhs.p; +} + +template +bool operator<(const IndexedListIterator& lhs, + const IndexedListIterator& rhs) +{ + return lhs.p < rhs.p; +} + +template +bool operator>(const IndexedListIterator& lhs, + const IndexedListIterator& rhs) +{ + return lhs.p > rhs.p; +} + +template +bool operator>(const IndexedListIterator& lhs, + const IndexedListIterator& rhs) +{ + return lhs.p > rhs.p; +} + +template +bool operator<=(const IndexedListIterator& lhs, + const IndexedListIterator& rhs) +{ + return lhs.p <= rhs.p; +} + +template +bool operator<=(const IndexedListIterator& lhs, + const IndexedListIterator& rhs) +{ + return lhs.p <= rhs.p; +} + +template +bool operator>=(const IndexedListIterator& lhs, + const IndexedListIterator& rhs) +{ + return lhs.p >= rhs.p; +} + +template +bool operator>=(const IndexedListIterator& lhs, + const IndexedListIterator& rhs) +{ + return lhs.p >= rhs.p; +} + +template +IndexedListIterator +operator+(typename IndexedListIterator::difference_type n, + const IndexedListIterator& lhs) +{ + return lhs + n; +} + +template +typename IndexedListIterator::difference_type +operator-(const IndexedListIterator& lhs, + const IndexedListIterator& rhs) +{ + return typename IndexedListIterator::difference_type + (lhs.p - rhs.p); +} + +template +typename IndexedListIterator::difference_type +operator-(const IndexedListIterator& lhs, + const IndexedListIterator& rhs) +{ + return typename IndexedListIterator::difference_type + (lhs.p - rhs.p); +} + template class IndexedList { public: IndexedList() {} ~IndexedList() {} - typedef std::deque > SeqType; + typedef KeyType key_type; + typedef ValuePtrType value_type; typedef std::map IndexType; + typedef std::deque > SeqType; + + + + typedef IndexedListIterator iterator; + typedef IndexedListIterator const_iterator; + + ValuePtrType& operator[](size_t n) + { + return seq_[n].second; + } + + const ValuePtrType& operator[](size_t n) const + { + return seq_[n].second; + } // Inserts (|key|, |value|) to the end of the list. If the same key // has been already added, this function fails. This function @@ -66,9 +371,8 @@ public: { typename IndexType::iterator i = index_.lower_bound(key); if(i == index_.end() || (*i).first != key) { - std::pair p(key, value); - seq_.push_back(p); - index_.insert(i, p); + i = index_.insert(i, std::make_pair(key, value)); + seq_.push_back(std::make_pair(i, value)); return true; } else { return false; @@ -82,9 +386,8 @@ public: { typename IndexType::iterator i = index_.lower_bound(key); if(i == index_.end() || (*i).first != key) { - std::pair p(key, value); - seq_.push_front(p); - index_.insert(i, p); + i = index_.insert(i, std::make_pair(key, value)); + seq_.push_front(std::make_pair(i, value)); return true; } else { return false; @@ -95,8 +398,7 @@ public: // has been already added, this function fails. This function // returns the iterator to the newly added element if it is // succeeds, or end(). Complexity: O(N) - typename SeqType::iterator insert(size_t dest, KeyType key, - ValuePtrType value) + iterator insert(size_t dest, KeyType key, ValuePtrType value) { if(dest > size()) { return seq_.end(); @@ -105,12 +407,11 @@ public: if(i == index_.end() || (*i).first != key) { typename SeqType::iterator j = seq_.begin(); std::advance(j, dest); - std::pair p(key, value); - j = seq_.insert(j, p); - index_.insert(i, p); - return j; + i = index_.insert(i, std::make_pair(key, value)); + j = seq_.insert(j, std::make_pair(i, value)); + return iterator(j); } else { - return seq_.end(); + return iterator(seq_.end()); } } @@ -119,18 +420,14 @@ public: // returns the iterator to the newly added element if it is // succeeds, or end(). Complexity: O(logN) if inserted to the first // or last, otherwise O(N) - typename SeqType::iterator insert(typename SeqType::iterator dest, - KeyType key, - ValuePtrType value) + iterator insert(iterator dest, KeyType key, ValuePtrType value) { typename IndexType::iterator i = index_.lower_bound(key); if(i == index_.end() || (*i).first != key) { - std::pair p(key, value); - dest = seq_.insert(dest, p); - index_.insert(i, p); - return dest; + i = index_.insert(i, std::make_pair(key, value)); + return iterator(seq_.insert(dest.p, std::make_pair(i, value))); } else { - return seq_.end(); + return iterator(seq_.end()); } } @@ -138,7 +435,7 @@ public: // value is retrieved by functor |keyFunc|. The insertion position // is given by |dest|. template - void insert(typename SeqType::iterator dest, KeyFunc keyFunc, + void insert(iterator dest, KeyFunc keyFunc, InputIterator first, InputIterator last) { std::vector v; @@ -147,12 +444,31 @@ public: KeyType key = keyFunc(*first); typename IndexType::iterator i = index_.lower_bound(key); if(i == index_.end() || (*i).first != key) { - std::pair p(key, *first); - v.push_back(p); - index_.insert(i, p); + i = index_.insert(i, std::make_pair(key, *first)); + v.push_back(std::make_pair(i, *first)); } } - seq_.insert(dest, v.begin(), v.end()); + seq_.insert(dest.p, v.begin(), v.end()); + } + + template + void insert(size_t pos, KeyFunc keyFunc, + InputIterator first, InputIterator last) + { + if(pos > size()) { + return; + } + std::vector v; + v.reserve(std::distance(first, last)); + for(; first != last; ++first) { + KeyType key = keyFunc(*first); + typename IndexType::iterator i = index_.lower_bound(key); + if(i == index_.end() || (*i).first != key) { + i = index_.insert(i, std::make_pair(key, *first)); + v.push_back(std::make_pair(i, *first)); + } + } + seq_.insert(seq_.begin() + pos, v.begin(), v.end()); } // Removes |key| from the list. If the element is not found, this @@ -164,14 +480,14 @@ public: if(i == index_.end()) { return false; } - index_.erase(i); for(typename SeqType::iterator j = seq_.begin(), eoj = seq_.end(); j != eoj; ++j) { - if((*j).first == key) { + if((*j).first == i) { seq_.erase(j); break; } } + index_.erase(i); return true; } @@ -179,10 +495,33 @@ public: // iterator must be valid. This function returns the iterator // pointing to the element following the erased element. Complexity: // O(N) - typename SeqType::iterator erase(typename SeqType::iterator k) + iterator erase(iterator k) { - index_.erase((*k).first); - return seq_.erase(k); + index_.erase((*k.p).first); + return iterator(seq_.erase(k.p)); + } + + // Removes elements for which Pred returns true. The pred is called + // against each each element once per each. + template + void remove_if(Pred pred) + { + typename SeqType::iterator first = seq_.begin(), last = seq_.end(); + for(; first != last && !pred((*first).second); ++first); + if(first == last) { + return; + } + index_.erase((*first).first); + typename SeqType::iterator store = first; + ++first; + for(; first != last; ++first) { + if(pred((*first).second)) { + index_.erase((*first).first); + } else { + *store++ = *first; + } + } + seq_.erase(store, last); } // Removes element at the front of the list. If the list is empty, @@ -193,8 +532,8 @@ public: if(seq_.empty()) { return false; } - KeyType key = seq_.front().first; - index_.erase(key); + typename IndexType::iterator i = seq_.front().first; + index_.erase(i); seq_.pop_front(); return true; } @@ -216,7 +555,7 @@ public: } typename SeqType::iterator x = seq_.begin(), eseq = seq_.end(); for(; x != eseq; ++x) { - if((*x).first == key) { + if((*x).first == idxent) { break; } } @@ -271,24 +610,24 @@ public: return index_.empty(); } - typename SeqType::iterator begin() + iterator begin() { - return seq_.begin(); + return iterator(seq_.begin()); } - typename SeqType::iterator end() + iterator end() { - return seq_.end(); + return iterator(seq_.end()); } - typename SeqType::const_iterator begin() const + const_iterator begin() const { - return seq_.begin(); + return const_iterator(seq_.begin()); } - typename SeqType::const_iterator end() const + const_iterator end() const { - return seq_.end(); + return const_iterator(seq_.end()); } // Removes all elements from the list. diff --git a/src/RequestGroupMan.cc b/src/RequestGroupMan.cc index e704b5fc..25e8ce33 100644 --- a/src/RequestGroupMan.cc +++ b/src/RequestGroupMan.cc @@ -165,9 +165,7 @@ void RequestGroupMan::insertReservedGroup { requestQueueCheck(); pos = std::min(reservedGroups_.size(), pos); - RequestGroupList::SeqType::iterator dest = reservedGroups_.begin(); - std::advance(dest, pos); - reservedGroups_.insert(dest, RequestGroupKeyFunc(), + reservedGroups_.insert(pos, RequestGroupKeyFunc(), groups.begin(), groups.end()); } @@ -282,9 +280,8 @@ public: reservedGroups_(reservedGroups) {} - void operator()(const RequestGroupList::SeqType::value_type& val) + void operator()(const RequestGroupList::value_type& group) { - const SharedHandle& group = val.second; if(group->getNumCommand() == 0) { const SharedHandle& dctx = group->getDownloadContext(); // DownloadContext::resetDownloadStopTime() is only called when @@ -385,9 +382,8 @@ public: CollectServerStat(RequestGroupMan* requestGroupMan): requestGroupMan_(requestGroupMan) {} - void operator()(const RequestGroupList::SeqType::value_type& val) + void operator()(const RequestGroupList::value_type& group) { - const SharedHandle& group = val.second; if(group->getNumCommand() == 0) { // Collect statistics during download in PeerStats and update/register // ServerStatMan @@ -436,9 +432,9 @@ void RequestGroupMan::removeStoppedGroup(DownloadEngine* e) std::for_each(requestGroups_.begin(), requestGroups_.end(), ProcessStoppedRequestGroup(e, reservedGroups_)); - for(RequestGroupList::SeqType::iterator i = requestGroups_.begin(), + for(RequestGroupList::iterator i = requestGroups_.begin(), eoi = requestGroups_.end(); i != eoi;) { - const SharedHandle& rg = (*i).second; + const SharedHandle& rg = *i; if(rg->getNumCommand() == 0) { i = requestGroups_.erase(i); eoi = requestGroups_.end(); @@ -505,7 +501,7 @@ void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e) } } } - SharedHandle groupToAdd = (*reservedGroups_.begin()).second; + SharedHandle groupToAdd = *reservedGroups_.begin(); reservedGroups_.pop_front(); if((rpc_ && groupToAdd->isPauseRequested()) || !groupToAdd->isDependencyResolved()) { @@ -556,9 +552,9 @@ void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e) void RequestGroupMan::save() { - for(RequestGroupList::SeqType::iterator itr = requestGroups_.begin(), + for(RequestGroupList::iterator itr = requestGroups_.begin(), eoi = requestGroups_.end(); itr != eoi; ++itr) { - const SharedHandle& rg = (*itr).second; + const SharedHandle& rg = *itr; if(rg->allDownloadFinished() && !rg->getDownloadContext()->isChecksumVerificationNeeded()) { rg->removeControlFile(); @@ -574,9 +570,9 @@ void RequestGroupMan::save() void RequestGroupMan::closeFile() { - for(RequestGroupList::SeqType::iterator itr = requestGroups_.begin(), + for(RequestGroupList::iterator itr = requestGroups_.begin(), eoi = requestGroups_.end(); itr != eoi; ++itr) { - (*itr).second->closeFile(); + (*itr)->closeFile(); } } @@ -587,10 +583,10 @@ RequestGroupMan::DownloadStat RequestGroupMan::getDownloadStat() const int inprogress = 0; int removed = 0; error_code::Value lastError = removedLastErrorResult_; - for(DownloadResultList::SeqType::const_iterator itr = + for(DownloadResultList::const_iterator itr = downloadResults_.begin(), eoi = downloadResults_.end(); itr != eoi; ++itr) { - const SharedHandle& dr = (*itr).second; + const SharedHandle& dr = *itr; if(dr->belongsTo != 0) { continue; } @@ -676,10 +672,10 @@ void RequestGroupMan::showDownloadResults(OutputFile& o, bool full) const int err = 0; int inpr = 0; int rm = 0; - for(DownloadResultList::SeqType::const_iterator itr = + for(DownloadResultList::const_iterator itr = downloadResults_.begin(), eoi = downloadResults_.end(); itr != eoi; ++itr) { - const SharedHandle& dr = (*itr).second; + const SharedHandle& dr = *itr; if(dr->belongsTo != 0) { continue; } @@ -826,9 +822,9 @@ bool RequestGroupMan::isSameFileBeingDownloaded(RequestGroup* requestGroup) cons return false; } std::vector files; - for(RequestGroupList::SeqType::const_iterator itr = requestGroups_.begin(), + for(RequestGroupList::const_iterator itr = requestGroups_.begin(), eoi = requestGroups_.end(); itr != eoi; ++itr) { - const SharedHandle& rg = (*itr).second; + const SharedHandle& rg = *itr; if(rg.get() != requestGroup) { const std::vector >& entries = rg->getDownloadContext()->getFileEntries(); @@ -846,17 +842,17 @@ bool RequestGroupMan::isSameFileBeingDownloaded(RequestGroup* requestGroup) cons void RequestGroupMan::halt() { - for(RequestGroupList::SeqType::const_iterator i = requestGroups_.begin(), + for(RequestGroupList::const_iterator i = requestGroups_.begin(), eoi = requestGroups_.end(); i != eoi; ++i) { - (*i).second->setHaltRequested(true); + (*i)->setHaltRequested(true); } } void RequestGroupMan::forceHalt() { - for(RequestGroupList::SeqType::const_iterator i = requestGroups_.begin(), + for(RequestGroupList::const_iterator i = requestGroups_.begin(), eoi = requestGroups_.end(); i != eoi; ++i) { - (*i).second->setForceHaltRequested(true); + (*i)->setForceHaltRequested(true); } } @@ -882,10 +878,10 @@ void RequestGroupMan::addDownloadResult(const SharedHandle& dr) bool rv = downloadResults_.push_back(dr->gid->getNumericId(), dr); assert(rv); while(downloadResults_.size() > maxDownloadResult_){ - DownloadResultList::SeqType::iterator i = downloadResults_.begin(); + DownloadResultList::iterator i = downloadResults_.begin(); // Save last encountered error code so that we can report it // later. - const SharedHandle& dr = (*i).second; + const SharedHandle& dr = *i; if(dr->belongsTo == 0 && dr->result != error_code::FINISHED) { removedLastErrorResult_ = dr->result; ++removedErrorResult_; @@ -958,9 +954,9 @@ void RequestGroupMan::getUsedHosts // speed. We use -download speed so that we can sort them using // operator<(). std::vector > tempHosts; - for(RequestGroupList::SeqType::const_iterator i = requestGroups_.begin(), + for(RequestGroupList::const_iterator i = requestGroups_.begin(), eoi = requestGroups_.end(); i != eoi; ++i) { - const SharedHandle& rg = (*i).second; + const SharedHandle& rg = *i; const FileEntry::InFlightRequestSet& inFlightReqs = rg->getDownloadContext()->getFirstFileEntry()->getInFlightRequests(); for(FileEntry::InFlightRequestSet::iterator j = diff --git a/src/RpcMethodImpl.cc b/src/RpcMethodImpl.cc index a2ed963b..56b5bbcf 100644 --- a/src/RpcMethodImpl.cc +++ b/src/RpcMethodImpl.cc @@ -485,7 +485,7 @@ void pauseRequestGroups (InputIterator first, InputIterator last, bool reserved, bool forcePause) { for(; first != last; ++first) { - pauseRequestGroup((*first).second, reserved, forcePause); + pauseRequestGroup(*first, reserved, forcePause); } } } // namespace @@ -540,9 +540,9 @@ SharedHandle UnpauseAllRpcMethod::process { const RequestGroupList& groups = e->getRequestGroupMan()->getReservedGroups(); - for(RequestGroupList::SeqType::const_iterator i = groups.begin(), + for(RequestGroupList::const_iterator i = groups.begin(), eoi = groups.end(); i != eoi; ++i) { - (*i).second->setPauseRequested(false); + (*i)->setPauseRequested(false); } e->getRequestGroupMan()->requestQueueCheck(); return VLB_OK; @@ -1039,13 +1039,13 @@ SharedHandle TellActiveRpcMethod::process toStringList(std::back_inserter(keys), keysParam); SharedHandle list = List::g(); const RequestGroupList& groups = e->getRequestGroupMan()->getRequestGroups(); - for(RequestGroupList::SeqType::const_iterator i = groups.begin(), + for(RequestGroupList::const_iterator i = groups.begin(), eoi = groups.end(); i != eoi; ++i) { SharedHandle entryDict = Dict::g(); if(requested_key(keys, KEY_STATUS)) { entryDict->put(KEY_STATUS, VLB_ACTIVE); } - gatherProgress(entryDict, (*i).second, e, keys); + gatherProgress(entryDict, *i, e, keys); list->append(entryDict); } return list; diff --git a/src/RpcMethodImpl.h b/src/RpcMethodImpl.h index caf27d51..00d64193 100644 --- a/src/RpcMethodImpl.h +++ b/src/RpcMethodImpl.h @@ -388,13 +388,13 @@ protected: std::vector keys; toStringList(std::back_inserter(keys), keysParam); const ItemListType& items = getItems(e); - std::pair range = + std::pair range = getPaginationRange(offset, num, items.begin(), items.end()); SharedHandle list = List::g(); for(; range.first != range.second; ++range.first) { SharedHandle entryDict = Dict::g(); - createEntry(entryDict, (*range.first).second, e, keys); + createEntry(entryDict, *range.first, e, keys); list->append(entryDict); } if(offset < 0) { diff --git a/src/SessionSerializer.cc b/src/SessionSerializer.cc index 45e7b591..d3d16f09 100644 --- a/src/SessionSerializer.cc +++ b/src/SessionSerializer.cc @@ -216,9 +216,9 @@ bool SessionSerializer::save(BufferedFile& fp) const { std::set metainfoCache; const DownloadResultList& results = rgman_->getDownloadResults(); - for(DownloadResultList::SeqType::const_iterator itr = results.begin(), + for(DownloadResultList::const_iterator itr = results.begin(), eoi = results.end(); itr != eoi; ++itr) { - const SharedHandle& dr = (*itr).second; + const SharedHandle& dr = *itr; if(dr->result == error_code::FINISHED || dr->result == error_code::REMOVED) { if(dr->option->getAsBool(PREF_FORCE_SAVE)) { @@ -246,9 +246,9 @@ bool SessionSerializer::save(BufferedFile& fp) const { // Save active downloads. const RequestGroupList& groups = rgman_->getRequestGroups(); - for(RequestGroupList::SeqType::const_iterator itr = groups.begin(), + for(RequestGroupList::const_iterator itr = groups.begin(), eoi = groups.end(); itr != eoi; ++itr) { - const SharedHandle& rg = (*itr).second; + const SharedHandle& rg = *itr; SharedHandle dr = rg->createDownloadResult(); bool stopped = dr->result == error_code::FINISHED || dr->result == error_code::REMOVED; @@ -262,9 +262,9 @@ bool SessionSerializer::save(BufferedFile& fp) const } if(saveWaiting_) { const RequestGroupList& groups = rgman_->getReservedGroups(); - for(RequestGroupList::SeqType::const_iterator itr = groups.begin(), + for(RequestGroupList::const_iterator itr = groups.begin(), eoi = groups.end(); itr != eoi; ++itr) { - const SharedHandle& rg = (*itr).second; + const SharedHandle& rg = *itr; SharedHandle result = rg->createDownloadResult(); if(!writeDownloadResult(fp, metainfoCache, result)) { return false; diff --git a/test/IndexedListTest.cc b/test/IndexedListTest.cc index 57b78f40..f4bd7d68 100644 --- a/test/IndexedListTest.cc +++ b/test/IndexedListTest.cc @@ -24,6 +24,8 @@ class IndexedListTest:public CppUnit::TestFixture { CPPUNIT_TEST(testGet); CPPUNIT_TEST(testInsert); CPPUNIT_TEST(testInsert_keyFunc); + CPPUNIT_TEST(testIterator); + CPPUNIT_TEST(testRemoveIf); CPPUNIT_TEST_SUITE_END(); public: void setUp() @@ -38,6 +40,8 @@ public: void testGet(); void testInsert(); void testInsert_keyFunc(); + void testIterator(); + void testRemoveIf(); }; CPPUNIT_TEST_SUITE_REGISTRATION( IndexedListTest ); @@ -53,9 +57,9 @@ void IndexedListTest::testPushBack() CPPUNIT_ASSERT_EQUAL(a[i], *list.get(i)); } int ai = 0; - for(IndexedList::SeqType::iterator i = list.begin(); + for(IndexedList::iterator i = list.begin(); i != list.end(); ++i) { - CPPUNIT_ASSERT_EQUAL(a[ai++], *((*i).second)); + CPPUNIT_ASSERT_EQUAL(a[ai++], **i); } } @@ -70,9 +74,9 @@ void IndexedListTest::testPushFront() CPPUNIT_ASSERT_EQUAL(a[i], *list.get(i)); } int ai = 4; - for(IndexedList::SeqType::iterator i = list.begin(); + for(IndexedList::iterator i = list.begin(); i != list.end(); ++i) { - CPPUNIT_ASSERT_EQUAL(a[ai--], *((*i).second)); + CPPUNIT_ASSERT_EQUAL(a[ai--], **i); } } @@ -100,15 +104,14 @@ void IndexedListTest::testErase() list.push_back(i, &a[i]); } int* p = a; - for(IndexedList::SeqType::iterator i = list.begin(); - i != list.end();) { + for(IndexedList::iterator i = list.begin(); i != list.end();) { i = list.erase(i); CPPUNIT_ASSERT_EQUAL((size_t)(std::distance(i, list.end())), list.size()); int* pp = ++p; - for(IndexedList::SeqType::iterator j = list.begin(); + for(IndexedList::iterator j = list.begin(); j != list.end(); ++j, ++pp) { - CPPUNIT_ASSERT_EQUAL(*pp, *(*j).second); + CPPUNIT_ASSERT_EQUAL(*pp, **j); } } } @@ -132,9 +135,9 @@ void IndexedListTest::testPopFront() #define LIST_CHECK(a, list) \ { \ int ai = 0; \ - for(IndexedList::SeqType::iterator i = list.begin(); \ + for(IndexedList::iterator i = list.begin(); \ i != list.end(); ++i) { \ - CPPUNIT_ASSERT_EQUAL(a[ai++], *((*i).second)); \ + CPPUNIT_ASSERT_EQUAL(a[ai++], **i); \ } \ } @@ -261,48 +264,48 @@ void IndexedListTest::testInsert_keyFunc() for(size_t i = slen; i < slen*2; ++i) { CPPUNIT_ASSERT_EQUAL(*s[i - slen], *list.get(i)); } - IndexedList >::SeqType::iterator itr; + IndexedList >::iterator itr; itr = list.begin(); - CPPUNIT_ASSERT_EQUAL(std::string("a"), *(*itr++).second); - CPPUNIT_ASSERT_EQUAL(std::string("b"), *(*itr++).second); - CPPUNIT_ASSERT_EQUAL(std::string("a"), *(*itr++).second); - CPPUNIT_ASSERT_EQUAL(std::string("b"), *(*itr++).second); - CPPUNIT_ASSERT_EQUAL(std::string("c"), *(*itr++).second); - CPPUNIT_ASSERT_EQUAL(std::string("d"), *(*itr++).second); - CPPUNIT_ASSERT_EQUAL(std::string("c"), *(*itr++).second); - CPPUNIT_ASSERT_EQUAL(std::string("d"), *(*itr++).second); + CPPUNIT_ASSERT_EQUAL(std::string("a"), *(*itr++)); + CPPUNIT_ASSERT_EQUAL(std::string("b"), *(*itr++)); + CPPUNIT_ASSERT_EQUAL(std::string("a"), *(*itr++)); + CPPUNIT_ASSERT_EQUAL(std::string("b"), *(*itr++)); + CPPUNIT_ASSERT_EQUAL(std::string("c"), *(*itr++)); + CPPUNIT_ASSERT_EQUAL(std::string("d"), *(*itr++)); + CPPUNIT_ASSERT_EQUAL(std::string("c"), *(*itr++)); + CPPUNIT_ASSERT_EQUAL(std::string("d"), *(*itr++)); list.insert(list.begin(), KeyFunc(2*slen-1), vbegin(s), vend(s)); CPPUNIT_ASSERT_EQUAL((size_t)slen*3-1, list.size()); itr = list.begin(); - CPPUNIT_ASSERT_EQUAL(std::string("b"), *(*itr++).second); - CPPUNIT_ASSERT_EQUAL(std::string("c"), *(*itr++).second); - CPPUNIT_ASSERT_EQUAL(std::string("d"), *(*itr++).second); - CPPUNIT_ASSERT_EQUAL(std::string("a"), *(*itr++).second); + CPPUNIT_ASSERT_EQUAL(std::string("b"), *(*itr++)); + CPPUNIT_ASSERT_EQUAL(std::string("c"), *(*itr++)); + CPPUNIT_ASSERT_EQUAL(std::string("d"), *(*itr++)); + CPPUNIT_ASSERT_EQUAL(std::string("a"), *(*itr++)); } void IndexedListTest::testInsert() { int a[] = {0,1,2,3,4,5,6,7,8,9}; IndexedList list; - IndexedList::SeqType::iterator itr; + IndexedList::iterator itr; CPPUNIT_ASSERT(list.end() == list.insert(1, 0, &a[5])); itr = list.insert(0, 5, &a[5]); - CPPUNIT_ASSERT_EQUAL(5, *(*itr).second); + CPPUNIT_ASSERT_EQUAL(5, **itr); itr = list.insert(1, 3, &a[3]); - CPPUNIT_ASSERT_EQUAL(3, *(*itr).second); + CPPUNIT_ASSERT_EQUAL(3, **itr); itr = list.insert(1, 4, &a[4]); - CPPUNIT_ASSERT_EQUAL(4, *(*itr).second); + CPPUNIT_ASSERT_EQUAL(4, **itr); itr = list.insert(0, 9, &a[9]); - CPPUNIT_ASSERT_EQUAL(9, *(*itr).second); + CPPUNIT_ASSERT_EQUAL(9, **itr); int a1[] = { 9,5,4,3 }; LIST_CHECK(a1, list); // use iterator to insert itr = list.insert(itr, 2, &a[2]); - CPPUNIT_ASSERT_EQUAL(2, *(*itr).second); + CPPUNIT_ASSERT_EQUAL(2, **itr); itr = list.insert(list.end(), 1, &a[1]); - CPPUNIT_ASSERT_EQUAL(1, *(*itr).second); + CPPUNIT_ASSERT_EQUAL(1, **itr); int a2[] = { 2,9,5,4,3,1 }; LIST_CHECK(a2, list); @@ -310,4 +313,118 @@ void IndexedListTest::testInsert() CPPUNIT_ASSERT(list.end() == list.insert(list.end(), 2, &a[2])); } +void IndexedListTest::testIterator() +{ + int a[] = {0,1,2,3,4,5,6,7,8,9}; + IndexedList list; + IndexedList::iterator itr; + IndexedList::const_iterator citr; + for(int *i = vbegin(a); i < vend(a); ++i) { + CPPUNIT_ASSERT(list.push_back(*i, i)); + } + CPPUNIT_ASSERT(list.begin() == list.begin()); + itr = list.begin(); + citr = list.begin(); + // operator*() + CPPUNIT_ASSERT_EQUAL(&a[0], *itr); + CPPUNIT_ASSERT_EQUAL(&a[0], *citr); + // operator==(iterator, iterator) + CPPUNIT_ASSERT(itr == list.begin()); + CPPUNIT_ASSERT(!(itr == list.end())); + CPPUNIT_ASSERT(citr == list.begin()); + CPPUNIT_ASSERT(!(citr == list.end())); + // operator++() + ++itr; + ++citr; + // operator!=(iterator, iterator) + CPPUNIT_ASSERT(itr != list.begin()); + CPPUNIT_ASSERT(!(itr != itr)); + CPPUNIT_ASSERT(citr != list.begin()); + CPPUNIT_ASSERT(!(citr != citr)); + // operator+(difference_type) + CPPUNIT_ASSERT(itr == list.begin() + 1); + CPPUNIT_ASSERT(citr == list.begin() + 1); + // operator-(difference_type) + CPPUNIT_ASSERT(itr - 1 == list.begin()); + CPPUNIT_ASSERT(citr - 1 == list.begin()); + // operator++(int) + IndexedList::iterator itr2 = itr++; + IndexedList::const_iterator citr2 = citr++; + CPPUNIT_ASSERT(itr2 + 1 == itr); + CPPUNIT_ASSERT(citr2 + 1 == citr); + // operator+(difference_type, iterator) + CPPUNIT_ASSERT(-1 + itr == itr2); + CPPUNIT_ASSERT(-1 + citr == citr2); + // operator<(iterator, iterator) + CPPUNIT_ASSERT(list.begin() < itr); + CPPUNIT_ASSERT(!(itr < list.begin())); + CPPUNIT_ASSERT(list.begin() < citr); + CPPUNIT_ASSERT(!(citr < list.begin())); + // operator>(iterator, iterator) + CPPUNIT_ASSERT(itr > list.begin()); + CPPUNIT_ASSERT(!(list.begin() > itr)); + CPPUNIT_ASSERT(citr > list.begin()); + CPPUNIT_ASSERT(!(list.begin() > citr)); + // operator<=(iterator, iterator) + CPPUNIT_ASSERT(itr <= itr); + CPPUNIT_ASSERT(list.begin() <= itr); + CPPUNIT_ASSERT(!(itr <= list.begin())); + CPPUNIT_ASSERT(citr <= citr); + CPPUNIT_ASSERT(list.begin() <= citr); + CPPUNIT_ASSERT(!(citr <= list.begin())); + // operator>=(iterator, iterator) + CPPUNIT_ASSERT(itr >= itr); + CPPUNIT_ASSERT(itr >= list.begin()); + CPPUNIT_ASSERT(!(list.begin() >= itr)); + CPPUNIT_ASSERT(citr >= citr); + CPPUNIT_ASSERT(citr >= list.begin()); + CPPUNIT_ASSERT(!(list.begin() >= citr)); + // operator-(iterator, iterator) + CPPUNIT_ASSERT(2 == itr - list.begin()); + CPPUNIT_ASSERT(-2 == list.begin() - itr); + CPPUNIT_ASSERT(2 == citr - list.begin()); + CPPUNIT_ASSERT(-2 == list.begin() - citr); + // operator+=(difference_type) + itr = list.begin(); + itr += 2; + CPPUNIT_ASSERT(itr == list.begin() + 2); + citr = list.begin(); + citr += 2; + CPPUNIT_ASSERT(citr == list.begin() + 2); + // operator-=(difference_type) + itr -= 2; + CPPUNIT_ASSERT(itr == list.begin()); + citr -= 2; + CPPUNIT_ASSERT(citr == list.begin()); + // operator[](size_type) + itr = list.begin(); + itr += 3; + CPPUNIT_ASSERT_EQUAL(*(itr[1]), a[4]); + citr = list.begin(); + citr += 3; + CPPUNIT_ASSERT_EQUAL(*(citr[1]), a[4]); +} + +namespace { +struct RemoveOdd { + bool operator()(int* p) const + { + return *p % 2 == 1; + } +}; +} +void IndexedListTest::testRemoveIf() +{ + int a[] = {0,1,2,3,4,5,6,7,8,9}; + IndexedList list; + for(int *i = vbegin(a); i < vend(a); ++i) { + CPPUNIT_ASSERT(list.push_back(*i, i)); + } + list.remove_if(RemoveOdd()); + CPPUNIT_ASSERT_EQUAL((size_t)5, list.size()); + for(int i = 0; i < 5; ++i) { + CPPUNIT_ASSERT_EQUAL(i*2, *list[i]); + } +} + } // namespace aria2 diff --git a/test/RequestGroupManTest.cc b/test/RequestGroupManTest.cc index 5eda76de..209ea47f 100644 --- a/test/RequestGroupManTest.cc +++ b/test/RequestGroupManTest.cc @@ -253,10 +253,10 @@ void RequestGroupManTest::testFillRequestGroupFromReserver_uriParser() rgman_->fillRequestGroupFromReserver(e_.get()); - RequestGroupList::SeqType::const_iterator itr; + RequestGroupList::const_iterator itr; CPPUNIT_ASSERT_EQUAL((size_t)1, rgman_->getReservedGroups().size()); itr = rgman_->getReservedGroups().begin(); - CPPUNIT_ASSERT_EQUAL(rgs[0]->getGID(), (*itr).second->getGID()); + CPPUNIT_ASSERT_EQUAL(rgs[0]->getGID(), (*itr)->getGID()); CPPUNIT_ASSERT_EQUAL((size_t)3, rgman_->getRequestGroups().size()); } @@ -277,18 +277,18 @@ void RequestGroupManTest::testInsertReservedGroup() std::vector > groups(vbegin(rgs1), vend(rgs1)); rgman_->insertReservedGroup(0, groups); CPPUNIT_ASSERT_EQUAL((size_t)2, rgman_->getReservedGroups().size()); - RequestGroupList::SeqType::const_iterator itr; + RequestGroupList::const_iterator itr; itr = rgman_->getReservedGroups().begin(); - CPPUNIT_ASSERT_EQUAL(rgs1[0]->getGID(), (*itr++).second->getGID()); - CPPUNIT_ASSERT_EQUAL(rgs1[1]->getGID(), (*itr++).second->getGID()); + CPPUNIT_ASSERT_EQUAL(rgs1[0]->getGID(), (*itr++)->getGID()); + CPPUNIT_ASSERT_EQUAL(rgs1[1]->getGID(), (*itr++)->getGID()); groups.assign(vbegin(rgs2), vend(rgs2)); rgman_->insertReservedGroup(1, groups); CPPUNIT_ASSERT_EQUAL((size_t)4, rgman_->getReservedGroups().size()); itr = rgman_->getReservedGroups().begin(); ++itr; - CPPUNIT_ASSERT_EQUAL(rgs2[0]->getGID(), (*itr++).second->getGID()); - CPPUNIT_ASSERT_EQUAL(rgs2[1]->getGID(), (*itr++).second->getGID()); + CPPUNIT_ASSERT_EQUAL(rgs2[0]->getGID(), (*itr++)->getGID()); + CPPUNIT_ASSERT_EQUAL(rgs2[1]->getGID(), (*itr++)->getGID()); } void RequestGroupManTest::testAddDownloadResult() diff --git a/test/RpcMethodTest.cc b/test/RpcMethodTest.cc index 8f77800d..043a9ec8 100644 --- a/test/RpcMethodTest.cc +++ b/test/RpcMethodTest.cc @@ -165,7 +165,7 @@ void RpcMethodTest::testAddUri() e_->getRequestGroupMan()->getReservedGroups(); CPPUNIT_ASSERT_EQUAL((size_t)1, rgs.size()); CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/"), - (*rgs.begin()).second->getDownloadContext()-> + (*rgs.begin())->getDownloadContext()-> getFirstFileEntry()->getRemainingUris().front()); } // with options diff --git a/test/TestUtil.cc b/test/TestUtil.cc index 0020f483..ee8a685a 100644 --- a/test/TestUtil.cc +++ b/test/TestUtil.cc @@ -122,10 +122,9 @@ SharedHandle getReservedGroup (const SharedHandle& rgman, size_t index) { assert(rgman->getReservedGroups().size() > index); - RequestGroupList::SeqType::const_iterator i = - rgman->getReservedGroups().begin(); + RequestGroupList::const_iterator i = rgman->getReservedGroups().begin(); std::advance(i, index); - return (*i).second; + return *i; } SharedHandle createRequestGroup(int32_t pieceLength,