-
Notifications
You must be signed in to change notification settings - Fork 36
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
base: dev
Are you sure you want to change the base?
Fet/Bitmask #134
Conversation
|
||
using namespace Poly; | ||
|
||
constexpr BitMask::DataType TYPE_BIT = CHAR_BIT * sizeof(BitMask::DataType); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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~(); |
There was a problem hiding this comment.
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); | ||
} |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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:
- newBitsNumber = max(BitsNumber, rhs.BitsNumber)
- Perform logic operation on values
- 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); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
BitMask working with different unsigned data types. In Unit Tests BitList Dynarray size is checked for Debugging purposes.