///按指定的分片大小bitfield更新rangefield. // @param bf为指定的bitfield. // @param piece_size是指定的分片大小. inline void bitfield_to_range(const bitfield& bf, int piece_size) { #ifndef AVHTTP_DISABLE_THREAD boost::mutex::scoped_lock lock(*m_mutex); #endif m_ranges.clear(); boost::int64_t left = 0; boost::int64_t right = 0; bool left_record = false; int index = 0; for (bitfield::const_iterator i = bf.begin(); i != bf.end(); i++, index++) { BOOST_ASSERT(index * piece_size < m_size); if (*i && !left_record) { left = index * piece_size; right = left + piece_size; left_record = true; continue; } if (*i && left_record) { right += piece_size; } if (left_record && !(*i)) { // 得到区间. right = (std::min)(right, m_size); m_ranges[left] = right; left_record = false; } } if (left_record) { right = (std::min)(right, m_size); m_ranges[left] = right; left_record = false; } m_need_merge = true; }
void test_iterators(bitfield& test1) { test1.set_all(); int num = 0; std::printf("expecting %d ones\n", test1.size()); for (bitfield::const_iterator i = test1.begin(); i != test1.end(); ++i) { std::printf("%d", *i); TEST_EQUAL(*i, true); num += *i; } std::printf("\n"); TEST_EQUAL(num, test1.size()); TEST_EQUAL(num, test1.count()); }