Skip to content

[Bug Fix] Edge case: BitSet when highest bit index is a multiplication of 32 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from Jun 3, 2021
Merged

[Bug Fix] Edge case: BitSet when highest bit index is a multiplication of 32 #10

merged 3 commits into from Jun 3, 2021

Conversation

yanivshaked
Copy link
Contributor

Hi,

The following test will fail on existing code:

    test('bit sets (edge case)', () {
      final counter = BitCounter(0);
      counter.addBitSet(ListSet.fromSorted([0, 2, 5, 2048]));
      expect(counter[2048], 1);
    });

The reason for that is that 2048 / 32 is an integer number, so the hasExtra value in _bufferLength32 will be 0.
In this edge case (whenever the highest bit index for bit set is a multiplication of 32), the bit array size will not be sufficient (one bit missing).
The corret fix is to use last bit index +1 for the length of the bit array (see commit).

…ith 32 (From last bit index to bit array length: +1)
@@ -71,8 +71,9 @@ class BitCounter {
///
/// The add starts at the bit position specified by [shiftLeft].
void addBitSet(BitSet set, {int shiftLeft = 0}) {
if (_length < set.length) {
_length = set.length;
// From last bit index to bit array length: +1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if the BitSet.length's definition is wrong, especially because the documentation differs on the range between BitSet and BitArray. Maybe it would be better to rename/change the field and update what it returns? wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. I think the name length should remain, but the value returned should be changed:

  int get length => _list.isEmpty ? 0 : _list.last + 1;

wdyt?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that should be the right fix here.

@yanivshaked
Copy link
Contributor Author

I have updated the fix, please review and merge.

@isoos isoos merged commit 4503143 into isoos:master Jun 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants