Browse Source

Math: C++14 constexpr in BoolVector.

pull/276/head
Vladimír Vondruš 12 years ago
parent
commit
2327cdc900
  1. 40
      src/Magnum/Math/BoolVector.h
  2. 76
      src/Magnum/Math/Test/BoolVectorTest.cpp

40
src/Magnum/Math/BoolVector.h

@ -94,7 +94,7 @@ template<std::size_t size> class BoolVector {
constexpr BoolVector(const BoolVector<size>&) = default; constexpr BoolVector(const BoolVector<size>&) = default;
/** @brief Copy assignment */ /** @brief Copy assignment */
BoolVector<size>& operator=(const BoolVector<size>&) = default; constexpr BoolVector<size>& operator=(const BoolVector<size>&) = default;
/** /**
* @brief Raw data * @brief Raw data
@ -102,7 +102,7 @@ template<std::size_t size> class BoolVector {
* *
* @see @ref operator[](), @ref set() * @see @ref operator[](), @ref set()
*/ */
UnsignedByte* data() { return _data; } constexpr UnsignedByte* data() { return _data; }
constexpr const UnsignedByte* data() const { return _data; } /**< @overload */ constexpr const UnsignedByte* data() const { return _data; } /**< @overload */
/** @brief Bit at given position */ /** @brief Bit at given position */
@ -111,37 +111,37 @@ template<std::size_t size> class BoolVector {
} }
/** @brief Set bit at given position */ /** @brief Set bit at given position */
BoolVector<size>& set(std::size_t i, bool value) { constexpr BoolVector<size>& set(std::size_t i, bool value) {
_data[i/8] |= ((value & 0x01) << i%8); _data[i/8] |= ((value & 0x01) << i%8);
return *this; return *this;
} }
/** @brief Equality comparison */ /** @brief Equality comparison */
bool operator==(const BoolVector<size>& other) const; constexpr bool operator==(const BoolVector<size>& other) const;
/** @brief Non-equality comparison */ /** @brief Non-equality comparison */
bool operator!=(const BoolVector<size>& other) const { constexpr bool operator!=(const BoolVector<size>& other) const {
return !operator==(other); return !operator==(other);
} }
/** @brief Whether all bits are set */ /** @brief Whether all bits are set */
bool all() const; constexpr bool all() const;
/** @brief Whether no bits are set */ /** @brief Whether no bits are set */
bool none() const; constexpr bool none() const;
/** @brief Whether any bit is set */ /** @brief Whether any bit is set */
bool any() const { return !none(); } constexpr bool any() const { return !none(); }
/** @brief Bitwise inversion */ /** @brief Bitwise inversion */
BoolVector<size> operator~() const; constexpr BoolVector<size> operator~() const;
/** /**
* @brief Bitwise AND and assign * @brief Bitwise AND and assign
* *
* The computation is done in-place. * The computation is done in-place.
*/ */
BoolVector<size>& operator&=(const BoolVector<size>& other) { constexpr BoolVector<size>& operator&=(const BoolVector<size>& other) {
for(std::size_t i = 0; i != DataSize; ++i) for(std::size_t i = 0; i != DataSize; ++i)
_data[i] &= other._data[i]; _data[i] &= other._data[i];
@ -153,7 +153,7 @@ template<std::size_t size> class BoolVector {
* *
* @see @ref operator&=() * @see @ref operator&=()
*/ */
BoolVector<size> operator&(const BoolVector<size>& other) const { constexpr BoolVector<size> operator&(const BoolVector<size>& other) const {
return BoolVector<size>(*this) &= other; return BoolVector<size>(*this) &= other;
} }
@ -162,7 +162,7 @@ template<std::size_t size> class BoolVector {
* *
* The computation is done in-place. * The computation is done in-place.
*/ */
BoolVector<size>& operator|=(const BoolVector<size>& other) { constexpr BoolVector<size>& operator|=(const BoolVector<size>& other) {
for(std::size_t i = 0; i != DataSize; ++i) for(std::size_t i = 0; i != DataSize; ++i)
_data[i] |= other._data[i]; _data[i] |= other._data[i];
@ -174,7 +174,7 @@ template<std::size_t size> class BoolVector {
* *
* @see @ref operator|=() * @see @ref operator|=()
*/ */
BoolVector<size> operator|(const BoolVector<size>& other) const { constexpr BoolVector<size> operator|(const BoolVector<size>& other) const {
return BoolVector<size>(*this) |= other; return BoolVector<size>(*this) |= other;
} }
@ -183,7 +183,7 @@ template<std::size_t size> class BoolVector {
* *
* The computation is done in-place. * The computation is done in-place.
*/ */
BoolVector<size>& operator^=(const BoolVector<size>& other) { constexpr BoolVector<size>& operator^=(const BoolVector<size>& other) {
for(std::size_t i = 0; i != DataSize; ++i) for(std::size_t i = 0; i != DataSize; ++i)
_data[i] ^= other._data[i]; _data[i] ^= other._data[i];
@ -195,7 +195,7 @@ template<std::size_t size> class BoolVector {
* *
* @see @ref operator^=() * @see @ref operator^=()
*/ */
BoolVector<size> operator^(const BoolVector<size>& other) const { constexpr BoolVector<size> operator^(const BoolVector<size>& other) const {
return BoolVector<size>(*this) ^= other; return BoolVector<size>(*this) ^= other;
} }
@ -224,7 +224,7 @@ template<std::size_t size> Corrade::Utility::Debug operator<<(Corrade::Utility::
return debug; return debug;
} }
template<std::size_t size> inline bool BoolVector<size>::operator==(const BoolVector< size >& other) const { template<std::size_t size> inline constexpr bool BoolVector<size>::operator==(const BoolVector< size >& other) const {
for(std::size_t i = 0; i != size/8; ++i) for(std::size_t i = 0; i != size/8; ++i)
if(_data[i] != other._data[i]) return false; if(_data[i] != other._data[i]) return false;
@ -235,7 +235,7 @@ template<std::size_t size> inline bool BoolVector<size>::operator==(const BoolVe
return true; return true;
} }
template<std::size_t size> inline bool BoolVector<size>::all() const { template<std::size_t size> inline constexpr bool BoolVector<size>::all() const {
/* Check all full segments */ /* Check all full segments */
for(std::size_t i = 0; i != size/8; ++i) for(std::size_t i = 0; i != size/8; ++i)
if(_data[i] != FullSegmentMask) return false; if(_data[i] != FullSegmentMask) return false;
@ -247,7 +247,7 @@ template<std::size_t size> inline bool BoolVector<size>::all() const {
return true; return true;
} }
template<std::size_t size> inline bool BoolVector<size>::none() const { template<std::size_t size> inline constexpr bool BoolVector<size>::none() const {
/* Check all full segments */ /* Check all full segments */
for(std::size_t i = 0; i != size/8; ++i) for(std::size_t i = 0; i != size/8; ++i)
if(_data[i]) return false; if(_data[i]) return false;
@ -259,8 +259,8 @@ template<std::size_t size> inline bool BoolVector<size>::none() const {
return true; return true;
} }
template<std::size_t size> inline BoolVector<size> BoolVector<size>::operator~() const { template<std::size_t size> inline constexpr BoolVector<size> BoolVector<size>::operator~() const {
BoolVector<size> out; BoolVector<size> out{};
for(std::size_t i = 0; i != DataSize; ++i) for(std::size_t i = 0; i != DataSize; ++i)
out._data[i] = ~_data[i]; out._data[i] = ~_data[i];

76
src/Magnum/Math/Test/BoolVectorTest.cpp

@ -136,67 +136,87 @@ void BoolVectorTest::data() {
} }
void BoolVectorTest::compare() { void BoolVectorTest::compare() {
BoolVector19 a(0xa5, 0x5f, 0x07); constexpr BoolVector19 a(0xa5, 0x5f, 0x07);
CORRADE_VERIFY(a == a); constexpr auto b = a == a;
CORRADE_VERIFY(b);
/* Change in full segments */ /* Change in full segments */
BoolVector19 b(0xa3, 0x5f, 0x07); constexpr BoolVector19 c(0xa3, 0x5f, 0x07);
BoolVector19 c(0xa5, 0x98, 0x07); constexpr BoolVector19 d(0xa5, 0x98, 0x07);
CORRADE_VERIFY(a != b); constexpr auto e = a != c;
CORRADE_VERIFY(a != c); constexpr auto f = a != d;
CORRADE_VERIFY(e);
CORRADE_VERIFY(f);
/* Change in last bit */ /* Change in last bit */
BoolVector19 d(0xa5, 0x5f, 0x06); constexpr BoolVector19 g(0xa5, 0x5f, 0x06);
CORRADE_VERIFY(a != d); constexpr auto h = a != g;
CORRADE_VERIFY(h);
} }
void BoolVectorTest::compareUndefined() { void BoolVectorTest::compareUndefined() {
BoolVector19 a(0xa5, 0x5f, 0x07); constexpr BoolVector19 a(0xa5, 0x5f, 0x07);
/* Change in unused part of last segment */ /* Change in unused part of last segment */
BoolVector19 b(0xa5, 0x5f, 0x0f); constexpr BoolVector19 b(0xa5, 0x5f, 0x0f);
CORRADE_VERIFY(a == b); constexpr auto c = a == b;
CORRADE_VERIFY(c);
/* Change in used part of last segment */ /* Change in used part of last segment */
BoolVector19 c(0xa5, 0x5f, 0x03); constexpr BoolVector19 d(0xa5, 0x5f, 0x03);
CORRADE_VERIFY(a != c); constexpr auto e = a != d;
CORRADE_VERIFY(e);
} }
void BoolVectorTest::all() { void BoolVectorTest::all() {
CORRADE_VERIFY(BoolVector19(0xff, 0xff, 0x07).all()); constexpr auto a = BoolVector19(0xff, 0xff, 0x07).all();
CORRADE_VERIFY(a);
/* Last segment - bit in used and unused part */ /* Last segment - bit in used and unused part */
CORRADE_VERIFY(BoolVector19(0xff, 0xff, 0x0f).all()); constexpr auto b = BoolVector19(0xff, 0xff, 0x0f).all();
CORRADE_VERIFY(!BoolVector19(0xff, 0xff, 0x04).all()); constexpr auto c = BoolVector19(0xff, 0xff, 0x04).all();
CORRADE_VERIFY(b);
CORRADE_VERIFY(!c);
} }
void BoolVectorTest::none() { void BoolVectorTest::none() {
CORRADE_VERIFY(BoolVector19(0x00, 0x00, 0x00).none()); constexpr auto a = BoolVector19(0x00, 0x00, 0x00).none();
CORRADE_VERIFY(a);
/* Last segment - bit in used and unused part */ /* Last segment - bit in used and unused part */
CORRADE_VERIFY(BoolVector19(0x00, 0x00, 0x08).none()); constexpr auto b = BoolVector19(0x00, 0x00, 0x08).none();
CORRADE_VERIFY(!BoolVector19(0x00, 0x00, 0x04).none()); constexpr auto c = BoolVector19(0x00, 0x00, 0x04).none();
CORRADE_VERIFY(b);
CORRADE_VERIFY(!c);
} }
void BoolVectorTest::any() { void BoolVectorTest::any() {
CORRADE_VERIFY(BoolVector19(0x00, 0x01, 0x00).any()); constexpr auto a = BoolVector19(0x00, 0x01, 0x00).any();
CORRADE_VERIFY(a);
/* Last segment - bit in used and unused part */ /* Last segment - bit in used and unused part */
CORRADE_VERIFY(BoolVector19(0x00, 0x00, 0x04).any()); constexpr auto b = BoolVector19(0x00, 0x00, 0x04).any();
CORRADE_VERIFY(!BoolVector19(0x00, 0x00, 0x08).any()); constexpr auto c = BoolVector19(0x00, 0x00, 0x08).any();
CORRADE_VERIFY(b);
CORRADE_VERIFY(!c);
} }
void BoolVectorTest::bitInverse() { void BoolVectorTest::bitInverse() {
CORRADE_COMPARE(~BoolVector19(0xa5, 0x5f, 0x03), BoolVector19(0x5a, 0xa0, 0x04)); constexpr auto a = ~BoolVector19(0xa5, 0x5f, 0x03);
CORRADE_COMPARE(a, BoolVector19(0x5a, 0xa0, 0x04));
} }
void BoolVectorTest::bitAndOrXor() { void BoolVectorTest::bitAndOrXor() {
BoolVector19 a(0xa5, 0x5f, 0x03); constexpr BoolVector19 a(0xa5, 0x5f, 0x03);
BoolVector19 b(0x37, 0xf3, 0x06); constexpr BoolVector19 b(0x37, 0xf3, 0x06);
CORRADE_COMPARE(a & b, BoolVector19(0x25, 0x53, 0x02)); constexpr auto c = a & b;
CORRADE_COMPARE(a | b, BoolVector19(0xb7, 0xff, 0x07)); constexpr auto d = a | b;
CORRADE_COMPARE(a ^ b, BoolVector19(0x92, 0xac, 0x05)); constexpr auto e = a ^ b;
CORRADE_COMPARE(c, BoolVector19(0x25, 0x53, 0x02));
CORRADE_COMPARE(d, BoolVector19(0xb7, 0xff, 0x07));
CORRADE_COMPARE(e, BoolVector19(0x92, 0xac, 0x05));
} }
void BoolVectorTest::debug() { void BoolVectorTest::debug() {

Loading…
Cancel
Save