From 226d75a3520dea9c87ce6ea8d25bdb42966c659b Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 21 Apr 2016 13:23:42 +0200 Subject: [PATCH] Core/Bitset: Add conversion from integer (to mimic internal representation, experimental) Former-commit-id: 609a2bbed02cad13a3487b2a95c6f24c0756e829 --- include/Nazara/Core/Bitset.hpp | 4 ++- include/Nazara/Core/Bitset.inl | 47 +++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/include/Nazara/Core/Bitset.hpp b/include/Nazara/Core/Bitset.hpp index 630d14a40..a371212ab 100644 --- a/include/Nazara/Core/Bitset.hpp +++ b/include/Nazara/Core/Bitset.hpp @@ -26,11 +26,12 @@ namespace Nz class Bit; Bitset(); - explicit Bitset(unsigned int bitCount, bool val = false); + explicit Bitset(unsigned int bitCount, bool val); explicit Bitset(const char* bits); Bitset(const char* bits, unsigned int bitCount); Bitset(const Bitset& bitset) = default; explicit Bitset(const String& bits); + template explicit Bitset(T value); Bitset(Bitset&& bitset) noexcept = default; ~Bitset() noexcept = default; @@ -84,6 +85,7 @@ namespace Nz Bitset& operator=(const Bitset& bitset) = default; Bitset& operator=(const String& bits); + template Bitset& operator=(T value); Bitset& operator=(Bitset&& bitset) noexcept = default; Bitset& operator&=(const Bitset& bitset); diff --git a/include/Nazara/Core/Bitset.inl b/include/Nazara/Core/Bitset.inl index bf89628f6..d689ff18a 100644 --- a/include/Nazara/Core/Bitset.inl +++ b/include/Nazara/Core/Bitset.inl @@ -108,6 +108,35 @@ namespace Nz { } + /*! + * \brief Constructs a Bitset copying an unsigned integral number + * + * \param value Number to be used as a base + */ + template + template + Bitset::Bitset(T value) : + Bitset() + { + if (sizeof(T) <= sizeof(Block)) + { + m_bitCount = CountBits(value); + m_blocks.push_back(value); + } + else + { + // Note: I was kinda tired when I wrote this, there's probably a much easier method than checking bits to write bits + unsigned int bitPos = 0; + for (T bit = 1; bit < std::numeric_limits::digits; bit <<= 1) + { + if (value & bit) + UnboundedSet(bitPos, true); + + bitPos++; + } + } + } + /*! * \brief Clears the content of the bitset, GetSize() is now equals to 0 * @@ -633,7 +662,7 @@ namespace Nz * \param bit Index of the bit * \param val Value of the bit * - * \remark if bit is greather than the number of bits, the bitset is enlarged and the added bits are set to false and the one at bit is set to val + * \remark if bit is greater than the number of bits, the bitset is enlarged and the added bits are set to false and the one at bit is set to val * * \see Set */ @@ -721,6 +750,22 @@ namespace Nz return *this; } + /*! + * \brief Copies the internal representation of an unsigned integer + * \return A reference to this + * + * \param value Unsigned number which will be used as a source + */ + template + template + Bitset& Bitset::operator=(T value) + { + Bitset bitset(value); + std::swap(*this, bitset); + + return *this; + } + /*! * \brief Performs an "AND" with another bitset * \return A reference to this