Skip to content
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

Fet/Bitmask #134

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open

Fet/Bitmask #134

wants to merge 4 commits into from

Conversation

OlekJC
Copy link

@OlekJC OlekJC commented May 8, 2018

BitMask working with different unsigned data types. In Unit Tests BitList Dynarray size is checked for Debugging purposes.


using namespace Poly;

constexpr BitMask::DataType TYPE_BIT = CHAR_BIT * sizeof(BitMask::DataType);
Copy link
Contributor

Choose a reason for hiding this comment

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

constexpr size_t TYPE_BIT = CHAR_BIT * sizeof(BitMask::DataType);

if (size%TYPE_BIT)
arraySize = size / TYPE_BIT + 1;
else
arraySize = size / TYPE_BIT;
Copy link
Contributor

Choose a reason for hiding this comment

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

Change this whole block to arraySize = (size + TYPE_BIT - 1) / TYPE_BIT;

BitMask operator|(const BitMask& rhs) const;
BitMask operator^(const BitMask& rhs) const;
BitMask operator&(const BitMask& rhs) const;
BitMask& operator~();
Copy link
Contributor

Choose a reason for hiding this comment

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

This operator should be const and return a copy, just like &, | and ^.

bool operator!=(const BitMask rhs) const
{
return !(*this == rhs);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Put this whole method in one line. This should accept const BitMask& as argument

{
return !(*this == rhs);
}
bool operator[](size_t index) const;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add @todo annotation: "Convert this method to return bit proxy", or if you want I can tell you how to write this proxy. In general we want to be able to do sth like this: BitMask b; b[0] = true; which is not possible right now.


BitMask BitMask::operator^(const BitMask& rhs) const
{
if (BitList.GetSize() == rhs.BitList.GetSize())
Copy link
Contributor

Choose a reason for hiding this comment

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

All three operators (&, |, ^) can be written much simpler. Core algorithm logic looks like this:

  1. newBitsNumber = max(BitsNumber, rhs.BitsNumber)
  2. Perform logic operation on values
  3. Trim returned BitMask to size == position of the leftmost bit set to 1.

We can discuss this further via pm.

bool Reset();
bool Toggle(size_t index);

bool Resize(const int offset = 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

size_t size not const int offset

}


bool BitMask::Resize(const int offset)
Copy link
Contributor

Choose a reason for hiding this comment

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

Resize should apply given size, not current size + offset. That way this method is simple
BitsNumber = size; BitList.Resize((size + TYPE_BIT - 1)/TYPE_BIT)

return index / TYPE_BIT;
}

BitMask& BitMask::operator|=(const BitMask& rhs)
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to write this like that, simply write { *this = *this | rhs; }. Repeat this for other operators overloads.

bool BitMask::operator==(const BitMask rhs) const
{
if (BitsNumber != rhs.BitsNumber)
return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Not necessarly, if one of the bitmasks is prefixed with a lot of zeroes this would still be true.
It would probably be helpfull to add method called Trim() which resizes BitMast to size == position of the leftmost bit set to 1. This way you can first trim, or get size after potential trim operation, and compare them to each other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants