From 2327cdc900ad62451bc9e8776d26e4f820df5d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 29 May 2014 21:51:33 +0200 Subject: [PATCH] Math: C++14 constexpr in BoolVector. --- src/Magnum/Math/BoolVector.h | 40 ++++++------- src/Magnum/Math/Test/BoolVectorTest.cpp | 76 ++++++++++++++++--------- 2 files changed, 68 insertions(+), 48 deletions(-) diff --git a/src/Magnum/Math/BoolVector.h b/src/Magnum/Math/BoolVector.h index 4eee4e7a0..ff8c6bee5 100644 --- a/src/Magnum/Math/BoolVector.h +++ b/src/Magnum/Math/BoolVector.h @@ -94,7 +94,7 @@ template class BoolVector { constexpr BoolVector(const BoolVector&) = default; /** @brief Copy assignment */ - BoolVector& operator=(const BoolVector&) = default; + constexpr BoolVector& operator=(const BoolVector&) = default; /** * @brief Raw data @@ -102,7 +102,7 @@ template class BoolVector { * * @see @ref operator[](), @ref set() */ - UnsignedByte* data() { return _data; } + constexpr UnsignedByte* data() { return _data; } constexpr const UnsignedByte* data() const { return _data; } /**< @overload */ /** @brief Bit at given position */ @@ -111,37 +111,37 @@ template class BoolVector { } /** @brief Set bit at given position */ - BoolVector& set(std::size_t i, bool value) { + constexpr BoolVector& set(std::size_t i, bool value) { _data[i/8] |= ((value & 0x01) << i%8); return *this; } /** @brief Equality comparison */ - bool operator==(const BoolVector& other) const; + constexpr bool operator==(const BoolVector& other) const; /** @brief Non-equality comparison */ - bool operator!=(const BoolVector& other) const { + constexpr bool operator!=(const BoolVector& other) const { return !operator==(other); } /** @brief Whether all bits are set */ - bool all() const; + constexpr bool all() const; /** @brief Whether no bits are set */ - bool none() const; + constexpr bool none() const; /** @brief Whether any bit is set */ - bool any() const { return !none(); } + constexpr bool any() const { return !none(); } /** @brief Bitwise inversion */ - BoolVector operator~() const; + constexpr BoolVector operator~() const; /** * @brief Bitwise AND and assign * * The computation is done in-place. */ - BoolVector& operator&=(const BoolVector& other) { + constexpr BoolVector& operator&=(const BoolVector& other) { for(std::size_t i = 0; i != DataSize; ++i) _data[i] &= other._data[i]; @@ -153,7 +153,7 @@ template class BoolVector { * * @see @ref operator&=() */ - BoolVector operator&(const BoolVector& other) const { + constexpr BoolVector operator&(const BoolVector& other) const { return BoolVector(*this) &= other; } @@ -162,7 +162,7 @@ template class BoolVector { * * The computation is done in-place. */ - BoolVector& operator|=(const BoolVector& other) { + constexpr BoolVector& operator|=(const BoolVector& other) { for(std::size_t i = 0; i != DataSize; ++i) _data[i] |= other._data[i]; @@ -174,7 +174,7 @@ template class BoolVector { * * @see @ref operator|=() */ - BoolVector operator|(const BoolVector& other) const { + constexpr BoolVector operator|(const BoolVector& other) const { return BoolVector(*this) |= other; } @@ -183,7 +183,7 @@ template class BoolVector { * * The computation is done in-place. */ - BoolVector& operator^=(const BoolVector& other) { + constexpr BoolVector& operator^=(const BoolVector& other) { for(std::size_t i = 0; i != DataSize; ++i) _data[i] ^= other._data[i]; @@ -195,7 +195,7 @@ template class BoolVector { * * @see @ref operator^=() */ - BoolVector operator^(const BoolVector& other) const { + constexpr BoolVector operator^(const BoolVector& other) const { return BoolVector(*this) ^= other; } @@ -224,7 +224,7 @@ template Corrade::Utility::Debug operator<<(Corrade::Utility:: return debug; } -template inline bool BoolVector::operator==(const BoolVector< size >& other) const { +template inline constexpr bool BoolVector::operator==(const BoolVector< size >& other) const { for(std::size_t i = 0; i != size/8; ++i) if(_data[i] != other._data[i]) return false; @@ -235,7 +235,7 @@ template inline bool BoolVector::operator==(const BoolVe return true; } -template inline bool BoolVector::all() const { +template inline constexpr bool BoolVector::all() const { /* Check all full segments */ for(std::size_t i = 0; i != size/8; ++i) if(_data[i] != FullSegmentMask) return false; @@ -247,7 +247,7 @@ template inline bool BoolVector::all() const { return true; } -template inline bool BoolVector::none() const { +template inline constexpr bool BoolVector::none() const { /* Check all full segments */ for(std::size_t i = 0; i != size/8; ++i) if(_data[i]) return false; @@ -259,8 +259,8 @@ template inline bool BoolVector::none() const { return true; } -template inline BoolVector BoolVector::operator~() const { - BoolVector out; +template inline constexpr BoolVector BoolVector::operator~() const { + BoolVector out{}; for(std::size_t i = 0; i != DataSize; ++i) out._data[i] = ~_data[i]; diff --git a/src/Magnum/Math/Test/BoolVectorTest.cpp b/src/Magnum/Math/Test/BoolVectorTest.cpp index 42e63f29c..ce34c799a 100644 --- a/src/Magnum/Math/Test/BoolVectorTest.cpp +++ b/src/Magnum/Math/Test/BoolVectorTest.cpp @@ -136,67 +136,87 @@ void BoolVectorTest::data() { } void BoolVectorTest::compare() { - BoolVector19 a(0xa5, 0x5f, 0x07); - CORRADE_VERIFY(a == a); + constexpr BoolVector19 a(0xa5, 0x5f, 0x07); + constexpr auto b = a == a; + CORRADE_VERIFY(b); /* Change in full segments */ - BoolVector19 b(0xa3, 0x5f, 0x07); - BoolVector19 c(0xa5, 0x98, 0x07); - CORRADE_VERIFY(a != b); - CORRADE_VERIFY(a != c); + constexpr BoolVector19 c(0xa3, 0x5f, 0x07); + constexpr BoolVector19 d(0xa5, 0x98, 0x07); + constexpr auto e = a != c; + constexpr auto f = a != d; + CORRADE_VERIFY(e); + CORRADE_VERIFY(f); /* Change in last bit */ - BoolVector19 d(0xa5, 0x5f, 0x06); - CORRADE_VERIFY(a != d); + constexpr BoolVector19 g(0xa5, 0x5f, 0x06); + constexpr auto h = a != g; + CORRADE_VERIFY(h); } void BoolVectorTest::compareUndefined() { - BoolVector19 a(0xa5, 0x5f, 0x07); + constexpr BoolVector19 a(0xa5, 0x5f, 0x07); /* Change in unused part of last segment */ - BoolVector19 b(0xa5, 0x5f, 0x0f); - CORRADE_VERIFY(a == b); + constexpr BoolVector19 b(0xa5, 0x5f, 0x0f); + constexpr auto c = a == b; + CORRADE_VERIFY(c); /* Change in used part of last segment */ - BoolVector19 c(0xa5, 0x5f, 0x03); - CORRADE_VERIFY(a != c); + constexpr BoolVector19 d(0xa5, 0x5f, 0x03); + constexpr auto e = a != d; + CORRADE_VERIFY(e); } 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 */ - CORRADE_VERIFY(BoolVector19(0xff, 0xff, 0x0f).all()); - CORRADE_VERIFY(!BoolVector19(0xff, 0xff, 0x04).all()); + constexpr auto b = BoolVector19(0xff, 0xff, 0x0f).all(); + constexpr auto c = BoolVector19(0xff, 0xff, 0x04).all(); + CORRADE_VERIFY(b); + CORRADE_VERIFY(!c); } 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 */ - CORRADE_VERIFY(BoolVector19(0x00, 0x00, 0x08).none()); - CORRADE_VERIFY(!BoolVector19(0x00, 0x00, 0x04).none()); + constexpr auto b = BoolVector19(0x00, 0x00, 0x08).none(); + constexpr auto c = BoolVector19(0x00, 0x00, 0x04).none(); + CORRADE_VERIFY(b); + CORRADE_VERIFY(!c); } 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 */ - CORRADE_VERIFY(BoolVector19(0x00, 0x00, 0x04).any()); - CORRADE_VERIFY(!BoolVector19(0x00, 0x00, 0x08).any()); + constexpr auto b = BoolVector19(0x00, 0x00, 0x04).any(); + constexpr auto c = BoolVector19(0x00, 0x00, 0x08).any(); + CORRADE_VERIFY(b); + CORRADE_VERIFY(!c); } 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() { - BoolVector19 a(0xa5, 0x5f, 0x03); - BoolVector19 b(0x37, 0xf3, 0x06); + constexpr BoolVector19 a(0xa5, 0x5f, 0x03); + constexpr BoolVector19 b(0x37, 0xf3, 0x06); - CORRADE_COMPARE(a & b, BoolVector19(0x25, 0x53, 0x02)); - CORRADE_COMPARE(a | b, BoolVector19(0xb7, 0xff, 0x07)); - CORRADE_COMPARE(a ^ b, BoolVector19(0x92, 0xac, 0x05)); + constexpr auto c = a & b; + constexpr auto d = a | b; + 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() {