[bitset] Don't visit bits in the tail end of the final word if they're beyond the nr bits in the bitset.

Also shift 1ULL in the test.
This commit is contained in:
Joe Thornber 2014-09-01 14:16:03 +01:00
parent 67865e0732
commit 14cfcf2dfd

View File

@ -82,7 +82,7 @@ namespace persistent_data {
} }
void walk_bitset(bitset_visitor &v) const { void walk_bitset(bitset_visitor &v) const {
bit_visitor vv(v); bit_visitor vv(v, nr_bits_);
damage_visitor dv(v); damage_visitor dv(v);
array_.visit_values(vv, dv); array_.visit_values(vv, dv);
} }
@ -90,18 +90,20 @@ namespace persistent_data {
private: private:
class bit_visitor { class bit_visitor {
public: public:
bit_visitor(bitset_visitor &v) bit_visitor(bitset_visitor &v, unsigned nr_bits)
: v_(v) { : v_(v),
nr_bits_(nr_bits) {
} }
void visit(uint32_t word_index, uint64_t word) { void visit(uint32_t word_index, uint64_t word) {
uint32_t bit_index = word_index * 64; uint32_t bit_index = word_index * 64;
for (unsigned bit = 0; bit < 64; bit++, bit_index++) for (unsigned bit = 0; bit < 64 && bit_index < nr_bits_; bit++, bit_index++)
v_.visit(bit_index, !!(word & (1 << bit))); v_.visit(bit_index, !!(word & (1ULL << bit)));
} }
private: private:
bitset_visitor &v_; bitset_visitor &v_;
unsigned nr_bits_;
}; };
class damage_visitor { class damage_visitor {