From 14cfcf2dfd7a838f8f5a3350f8f7e78a06df7fde Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Mon, 1 Sep 2014 14:16:03 +0100 Subject: [PATCH] [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. --- persistent-data/data-structures/bitset.cc | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/persistent-data/data-structures/bitset.cc b/persistent-data/data-structures/bitset.cc index 98805fa..fae1f34 100644 --- a/persistent-data/data-structures/bitset.cc +++ b/persistent-data/data-structures/bitset.cc @@ -82,7 +82,7 @@ namespace persistent_data { } void walk_bitset(bitset_visitor &v) const { - bit_visitor vv(v); + bit_visitor vv(v, nr_bits_); damage_visitor dv(v); array_.visit_values(vv, dv); } @@ -90,18 +90,20 @@ namespace persistent_data { private: class bit_visitor { public: - bit_visitor(bitset_visitor &v) - : v_(v) { + bit_visitor(bitset_visitor &v, unsigned nr_bits) + : v_(v), + nr_bits_(nr_bits) { } void visit(uint32_t word_index, uint64_t word) { uint32_t bit_index = word_index * 64; - for (unsigned bit = 0; bit < 64; bit++, bit_index++) - v_.visit(bit_index, !!(word & (1 << bit))); + for (unsigned bit = 0; bit < 64 && bit_index < nr_bits_; bit++, bit_index++) + v_.visit(bit_index, !!(word & (1ULL << bit))); } private: bitset_visitor &v_; + unsigned nr_bits_; }; class damage_visitor {