aboutsummaryrefslogtreecommitdiffstats
path: root/chilbert/FixBitVec.hpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-08-11 22:40:03 +0200
committerDavid Robillard <d@drobilla.net>2018-09-29 14:46:25 +0200
commit50145430b5fb08802bc22a6ae06351a11a091c60 (patch)
tree4ba0d1c8c5b76e7608fbd8ffbc985ba8a9f09622 /chilbert/FixBitVec.hpp
parentdf0d52f4bd78c2197a88a805a1dd402978df3290 (diff)
downloadchilbert-50145430b5fb08802bc22a6ae06351a11a091c60.tar.gz
chilbert-50145430b5fb08802bc22a6ae06351a11a091c60.tar.bz2
chilbert-50145430b5fb08802bc22a6ae06351a11a091c60.zip
Clean up types and fix every even remotely reasonable warning
Diffstat (limited to 'chilbert/FixBitVec.hpp')
-rw-r--r--chilbert/FixBitVec.hpp57
1 files changed, 31 insertions, 26 deletions
diff --git a/chilbert/FixBitVec.hpp b/chilbert/FixBitVec.hpp
index 9df2ada..e47b868 100644
--- a/chilbert/FixBitVec.hpp
+++ b/chilbert/FixBitVec.hpp
@@ -22,6 +22,8 @@
#include "chilbert/Operations.hpp"
#include <cassert>
+#include <climits>
+#include <cstddef>
#include <cstdint>
namespace chilbert {
@@ -33,21 +35,21 @@ typedef uint64_t FBV_UINT;
#define FBV_BITS 64
-#define FBV1 ((FBV_UINT)1)
-#define FBV1S (~(FBV_UINT)0)
+#define FBV1 (FBV_UINT{1})
+#define FBV1S (~FBV_UINT{0})
#define FBVN1S(n) (n == FBV_BITS ? FBV1S : (FBV1 << n) - 1)
class CFixBitVec
{
public:
- CFixBitVec(int bits = FBV_BITS)
+ CFixBitVec(const size_t bits = FBV_BITS)
: m_rack{0}
{
assert(bits <= FBV_BITS);
}
/// Return the size in bits
- int size() const { return FBV_BITS; }
+ size_t size() const { return FBV_BITS; }
/// Set all bits to one
CFixBitVec& set()
@@ -64,40 +66,40 @@ public:
}
/// Return the value of the `index`th bit
- bool test(const int index) const
+ bool test(const size_t index) const
{
- assert(0 <= index && index < FBV_BITS);
+ assert(index < FBV_BITS);
return ((m_rack & (FBV1 << index)) > 0);
}
/// Set the `index`th bit to 1
- CFixBitVec& set(const int index)
+ CFixBitVec& set(const size_t index)
{
- assert(0 <= index && index < FBV_BITS);
- m_rack |= ((FBV_UINT)1 << index);
+ assert(index < FBV_BITS);
+ m_rack |= (FBV_UINT{1} << index);
return *this;
}
/// Reset the `index`th bit to 0
- CFixBitVec& reset(const int index)
+ CFixBitVec& reset(const size_t index)
{
- assert(0 <= index && index < FBV_BITS);
- m_rack &= ~((FBV_UINT)1 << index);
+ assert(index < FBV_BITS);
+ m_rack &= ~(FBV_UINT{1} << index);
return *this;
}
/// Set the `index`th bit to `value`
- CFixBitVec& set(const int index, const bool value)
+ CFixBitVec& set(const size_t index, const bool value)
{
- assert(0 <= index && index < FBV_BITS);
- m_rack ^= (-value ^ m_rack) & ((FBV_UINT)1 << index);
+ assert(index < FBV_BITS);
+ m_rack ^= (-FBV_UINT{value} ^ m_rack) & (FBV_UINT{1U} << index);
return *this;
}
/// Flip the value of the `index`th bit
- CFixBitVec& flip(const int index)
+ CFixBitVec& flip(const size_t index)
{
- assert(0 <= index && index < FBV_BITS);
+ assert(index < FBV_BITS);
m_rack ^= (FBV1 << index);
return *this;
}
@@ -163,26 +165,28 @@ public:
return t;
}
- CFixBitVec& operator<<=(const int bits)
+ CFixBitVec& operator<<=(const size_t bits)
{
+ assert(bits < size());
m_rack <<= bits;
return *this;
}
- CFixBitVec operator<<(const int bits) const
+ CFixBitVec operator<<(const size_t bits) const
{
CFixBitVec t(*this);
t <<= bits;
return t;
}
- CFixBitVec& operator>>=(const int bits)
+ CFixBitVec& operator>>=(const size_t bits)
{
+ assert(bits < size());
m_rack >>= bits;
return *this;
}
- CFixBitVec operator>>(const int bits) const
+ CFixBitVec operator>>(const size_t bits) const
{
CFixBitVec t(*this);
t >>= bits;
@@ -190,9 +194,8 @@ public:
}
/// Right-rotate the least significant `width` bits by `bits` positions
- CFixBitVec& rotr(const int bits, const int width)
+ CFixBitVec& rotr(const size_t bits, const size_t width)
{
- assert(bits >= 0);
assert(width > 0);
assert(bits < width);
m_rack &= FBVN1S(width);
@@ -202,9 +205,8 @@ public:
}
/// Left-rotate the least significant `width` bits by `bits` positions
- CFixBitVec& rotl(int bits, int width)
+ CFixBitVec& rotl(const size_t bits, const size_t width)
{
- assert(bits >= 0);
assert(width > 0);
assert(bits < width);
m_rack &= FBVN1S(width);
@@ -217,7 +219,10 @@ public:
bool none() const { return m_rack == 0; }
/// Return 1 + the index of the first set bit, or 0 if there are none
- int find_first() const { return chilbert::ffs(m_rack); }
+ size_t find_first() const
+ {
+ return static_cast<size_t>(chilbert::ffs(m_rack));
+ }
/// Flip all bits (one's complement)
CFixBitVec& flip()