mirror of
https://github.com/aria2/aria2.git
synced 2025-04-05 05:27:38 +03:00
Simplified PieceStatMan and RarestPieceSelector.
Simplified PieceStatMan and RarestPieceSelector, but computation order to select piece index is still O(N) and unchanged. Updating piece stat is improved to O(N) for bitfield update and O(1) for single index update, while old implementation needs O(NlogN) and O(N) respectively.
This commit is contained in:
parent
37016c6587
commit
6ee913b0bc
4 changed files with 73 additions and 252 deletions
|
@ -31,56 +31,33 @@ void PieceStatManTest::testAddPieceStats_index()
|
|||
PieceStatMan pieceStatMan(10, false);
|
||||
pieceStatMan.addPieceStats(1);
|
||||
{
|
||||
size_t indexes[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 1 };
|
||||
size_t counts[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
|
||||
|
||||
const std::vector<size_t>& statsidx(pieceStatMan.getRarerPieceIndexes());
|
||||
const std::vector<SharedHandle<PieceStat> >& stats
|
||||
(pieceStatMan.getPieceStats());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)10, stats.size());
|
||||
|
||||
int ans[] = { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
|
||||
const std::vector<size_t>& order(pieceStatMan.getOrder());
|
||||
const std::vector<int>& counts(pieceStatMan.getCounts());
|
||||
for(size_t i = 0; i < 10; ++i) {
|
||||
CPPUNIT_ASSERT_EQUAL(indexes[i], statsidx[i]);
|
||||
CPPUNIT_ASSERT_EQUAL(counts[i], stats[statsidx[i]]->getCount());
|
||||
CPPUNIT_ASSERT_EQUAL(i, order[i]);
|
||||
CPPUNIT_ASSERT_EQUAL(ans[i], counts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
pieceStatMan.addPieceStats(1);
|
||||
|
||||
{
|
||||
size_t indexes[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 1 };
|
||||
size_t counts[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 };
|
||||
|
||||
const std::vector<size_t>& statsidx(pieceStatMan.getRarerPieceIndexes());
|
||||
const std::vector<SharedHandle<PieceStat> >& stats
|
||||
(pieceStatMan.getPieceStats());
|
||||
|
||||
int ans[] = { 0, 2, 0, 0, 0, 0, 0, 0, 0, 0 };
|
||||
const std::vector<int>& counts(pieceStatMan.getCounts());
|
||||
for(size_t i = 0; i < 10; ++i) {
|
||||
CPPUNIT_ASSERT_EQUAL(indexes[i], statsidx[i]);
|
||||
CPPUNIT_ASSERT_EQUAL(counts[i], stats[statsidx[i]]->getCount());
|
||||
CPPUNIT_ASSERT_EQUAL(ans[i], counts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
pieceStatMan.addPieceStats(3);
|
||||
pieceStatMan.addPieceStats(9);
|
||||
pieceStatMan.addPieceStats(3);
|
||||
pieceStatMan.addPieceStats(0);
|
||||
|
||||
{
|
||||
size_t indexes[] = { 2, 4, 5, 6, 7, 8, 0, 9, 1, 3 };
|
||||
size_t counts[] = { 0, 0, 0, 0, 0, 0, 1, 1, 2, 2 };
|
||||
|
||||
const std::vector<size_t>& statsidx(pieceStatMan.getRarerPieceIndexes());
|
||||
const std::vector<SharedHandle<PieceStat> >& stats
|
||||
(pieceStatMan.getPieceStats());
|
||||
|
||||
int ans[] = { 1, 2, 0, 2, 0, 0, 0, 0, 0, 1 };
|
||||
const std::vector<int>& counts(pieceStatMan.getCounts());
|
||||
for(size_t i = 0; i < 10; ++i) {
|
||||
CPPUNIT_ASSERT_EQUAL(indexes[i], statsidx[i]);
|
||||
CPPUNIT_ASSERT_EQUAL(counts[i], stats[statsidx[i]]->getCount());
|
||||
CPPUNIT_ASSERT_EQUAL(ans[i], counts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void PieceStatManTest::testAddPieceStats_bitfield()
|
||||
|
@ -89,36 +66,18 @@ void PieceStatManTest::testAddPieceStats_bitfield()
|
|||
const unsigned char bitfield[] = { 0xaa, 0x80 };
|
||||
pieceStatMan.addPieceStats(bitfield, sizeof(bitfield));
|
||||
{
|
||||
size_t indexes[] = { 1, 3, 5, 7, 9, 0, 2, 4, 6, 8 };
|
||||
size_t counts[] = { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 };
|
||||
|
||||
const std::vector<size_t>& statsidx(pieceStatMan.getRarerPieceIndexes());
|
||||
const std::vector<SharedHandle<PieceStat> >& stats
|
||||
(pieceStatMan.getPieceStats());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)10, stats.size());
|
||||
|
||||
int ans[] = { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 };
|
||||
const std::vector<int>& counts(pieceStatMan.getCounts());
|
||||
for(size_t i = 0; i < 10; ++i) {
|
||||
CPPUNIT_ASSERT_EQUAL(indexes[i], statsidx[i]);
|
||||
CPPUNIT_ASSERT_EQUAL(counts[i], stats[statsidx[i]]->getCount());
|
||||
CPPUNIT_ASSERT_EQUAL(ans[i], counts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
pieceStatMan.addPieceStats(bitfield, sizeof(bitfield));
|
||||
|
||||
{
|
||||
size_t indexes[] = { 1, 3, 5, 7, 9, 0, 2, 4, 6, 8 };
|
||||
size_t counts[] = { 0, 0, 0, 0, 0, 2, 2, 2, 2, 2 };
|
||||
|
||||
const std::vector<size_t>& statsidx(pieceStatMan.getRarerPieceIndexes());
|
||||
const std::vector<SharedHandle<PieceStat> >& stats
|
||||
(pieceStatMan.getPieceStats());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)10, stats.size());
|
||||
|
||||
int ans[] = { 2, 0, 2, 0, 2, 0, 2, 0, 2, 0 };
|
||||
const std::vector<int>& counts(pieceStatMan.getCounts());
|
||||
for(size_t i = 0; i < 10; ++i) {
|
||||
CPPUNIT_ASSERT_EQUAL(indexes[i], statsidx[i]);
|
||||
CPPUNIT_ASSERT_EQUAL(counts[i], stats[statsidx[i]]->getCount());
|
||||
CPPUNIT_ASSERT_EQUAL(ans[i], counts[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -126,13 +85,10 @@ void PieceStatManTest::testAddPieceStats_bitfield()
|
|||
void PieceStatManTest::testUpdatePieceStats()
|
||||
{
|
||||
PieceStatMan pieceStatMan(10, false);
|
||||
|
||||
const unsigned char bitfield[] = { 0xff, 0xc0 };
|
||||
pieceStatMan.addPieceStats(bitfield, sizeof(bitfield));
|
||||
|
||||
const unsigned char oldBitfield[] = { 0xf0, 0x00 };
|
||||
const unsigned char newBitfield[] = { 0x1f, 0x00 };
|
||||
|
||||
pieceStatMan.updatePieceStats(newBitfield, sizeof(newBitfield), oldBitfield);
|
||||
{
|
||||
// idx: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
|
||||
|
@ -141,19 +97,10 @@ void PieceStatManTest::testUpdatePieceStats()
|
|||
// new: 0, 0, 0, 1, 1, 1, 1, 1, 0, 0
|
||||
// ---------------------------------
|
||||
// res: 0, 0, 0, 1, 2, 2, 2, 2, 1, 1
|
||||
|
||||
size_t indexes[] = { 0, 1, 2, 3, 8, 9, 4, 5, 6, 7 };
|
||||
size_t counts[] = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 2 };
|
||||
|
||||
const std::vector<size_t>& statsidx(pieceStatMan.getRarerPieceIndexes());
|
||||
const std::vector<SharedHandle<PieceStat> >& stats
|
||||
(pieceStatMan.getPieceStats());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)10, stats.size());
|
||||
|
||||
int ans[] = { 0, 0, 0, 1, 2, 2, 2, 2, 1, 1 };
|
||||
const std::vector<int>& counts(pieceStatMan.getCounts());
|
||||
for(size_t i = 0; i < 10; ++i) {
|
||||
CPPUNIT_ASSERT_EQUAL(indexes[i], statsidx[i]);
|
||||
CPPUNIT_ASSERT_EQUAL(counts[i], stats[statsidx[i]]->getCount());
|
||||
CPPUNIT_ASSERT_EQUAL(ans[i], counts[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -161,12 +108,9 @@ void PieceStatManTest::testUpdatePieceStats()
|
|||
void PieceStatManTest::testSubtractPieceStats()
|
||||
{
|
||||
PieceStatMan pieceStatMan(10, false);
|
||||
|
||||
const unsigned char bitfield[] = { 0xf0, 0x00 };
|
||||
pieceStatMan.addPieceStats(bitfield, sizeof(bitfield));
|
||||
|
||||
const unsigned char newBitfield[] = { 0x3f, 0x00 };
|
||||
|
||||
pieceStatMan.subtractPieceStats(newBitfield, sizeof(newBitfield));
|
||||
{
|
||||
// idx: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
|
||||
|
@ -174,19 +118,10 @@ void PieceStatManTest::testSubtractPieceStats()
|
|||
// new: 0, 0, 1, 1, 1, 1, 1, 1, 0, 0
|
||||
// ---------------------------------
|
||||
// res: 1, 1, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
|
||||
size_t indexes[] = { 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 };
|
||||
size_t counts[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 };
|
||||
|
||||
const std::vector<size_t>& statsidx(pieceStatMan.getRarerPieceIndexes());
|
||||
const std::vector<SharedHandle<PieceStat> >& stats
|
||||
(pieceStatMan.getPieceStats());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)10, stats.size());
|
||||
|
||||
int ans[] = { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 };
|
||||
const std::vector<int>& counts(pieceStatMan.getCounts());
|
||||
for(size_t i = 0; i < 10; ++i) {
|
||||
CPPUNIT_ASSERT_EQUAL(indexes[i], statsidx[i]);
|
||||
CPPUNIT_ASSERT_EQUAL(counts[i], stats[statsidx[i]]->getCount());
|
||||
CPPUNIT_ASSERT_EQUAL(ans[i], counts[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue