diff options
author | David Robillard <d@drobilla.net> | 2018-08-19 10:34:13 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2018-09-29 14:47:52 +0200 |
commit | 9db42db409cf1d84c13d6659c773db44c9f3e29e (patch) | |
tree | 5fc1f021abb3514497c2340b4a4872b20718622b | |
parent | 8f5f179bf2af2002ed74c0e81ae3661b71b289cf (diff) | |
download | chilbert-9db42db409cf1d84c13d6659c773db44c9f3e29e.tar.gz chilbert-9db42db409cf1d84c13d6659c773db44c9f3e29e.tar.bz2 chilbert-9db42db409cf1d84c13d6659c773db44c9f3e29e.zip |
Clean up type traits
-rw-r--r-- | chilbert/BigBitVec.hpp | 6 | ||||
-rw-r--r-- | chilbert/FixBitVec.hpp | 6 | ||||
-rw-r--r-- | chilbert/Operations.hpp | 45 |
3 files changed, 36 insertions, 21 deletions
diff --git a/chilbert/BigBitVec.hpp b/chilbert/BigBitVec.hpp index c3fe957..8d073b5 100644 --- a/chilbert/BigBitVec.hpp +++ b/chilbert/BigBitVec.hpp @@ -490,6 +490,12 @@ private: }; template <> +struct is_bitvec<CBigBitVec> +{ + constexpr static bool value = true; +}; + +template <> void grayCode(CBigBitVec& value) { diff --git a/chilbert/FixBitVec.hpp b/chilbert/FixBitVec.hpp index f9ce94a..f782723 100644 --- a/chilbert/FixBitVec.hpp +++ b/chilbert/FixBitVec.hpp @@ -402,6 +402,12 @@ private: }; template <> +struct is_bitvec<CFixBitVec> +{ + constexpr static bool value = true; +}; + +template <> void grayCode(CFixBitVec& value) { diff --git a/chilbert/Operations.hpp b/chilbert/Operations.hpp index 3b9dd53..d80a994 100644 --- a/chilbert/Operations.hpp +++ b/chilbert/Operations.hpp @@ -27,13 +27,16 @@ namespace chilbert { -/// IntegralIndex<T> only exists if T is integral -template <typename T> -using IntegralIndex = std::enable_if_t<std::is_integral<T>::value, size_t>; +/// Member `value` is true iff T is a chilbert bitset +template <class T> +struct is_bitvec +{ + static constexpr bool value = false; +}; -/// BitsetIndex<T> only exists if T is not integral (must be a bitset) -template <typename T> -using BitsetIndex = std::enable_if_t<!std::is_integral<T>::value, size_t>; +/// True iff T is a chilbert bitset +template <class T> +static constexpr bool is_bitvec_v = is_bitvec<T>::value; /// Reset all bits in `field` template <typename T> @@ -45,7 +48,7 @@ resetBits(T& field) /// Reset all bits in `field` template <typename T> -std::enable_if_t<!std::is_integral<T>::value> +std::enable_if_t<is_bitvec_v<T>> resetBits(T& field) { field.reset(); @@ -53,8 +56,8 @@ resetBits(T& field) /// Return the `index`th bit in `field` template <typename T> -bool -testBit(const T& field, const IntegralIndex<T> index) +std::enable_if_t<std::is_integral<T>::value, bool> +testBit(const T& field, const size_t index) { assert(size_t(index) < sizeof(field) * CHAR_BIT); return field & (T{1} << index); @@ -62,16 +65,16 @@ testBit(const T& field, const IntegralIndex<T> index) /// Return the `index`th bit in `field` template <typename T> -bool -testBit(const T& field, const BitsetIndex<T> index) +std::enable_if_t<is_bitvec_v<T>, bool> +testBit(const T& field, const size_t index) { return field.test(index); } /// Set the `index`th bit in `field` template <typename T> -void -setBit(T& field, const IntegralIndex<T> index) +std::enable_if_t<std::is_integral<T>::value> +setBit(T& field, const size_t index) { assert(size_t(index) < sizeof(field) * CHAR_BIT); field |= (T{1} << index); @@ -80,8 +83,8 @@ setBit(T& field, const IntegralIndex<T> index) /// Set the `index`th bit in `field` to `value` template <typename T> -void -setBit(T& field, const IntegralIndex<T> index, const bool value) +std::enable_if_t<std::is_integral<T>::value> +setBit(T& field, const size_t index, const bool value) { assert(size_t(index) < sizeof(field) * CHAR_BIT); field ^= (-T{value} ^ field) & (T{1U} << index); @@ -90,16 +93,16 @@ setBit(T& field, const IntegralIndex<T> index, const bool value) /// Set the `index`th bit in `field` template <typename T> -void -setBit(T& field, const BitsetIndex<T> index) +std::enable_if_t<is_bitvec_v<T>> +setBit(T& field, const size_t index) { field.set(index); } /// Set the `index`th bit in `field` to `value` template <typename T> -void -setBit(T& field, const BitsetIndex<T> index, const bool value) +std::enable_if_t<is_bitvec_v<T>> +setBit(T& field, const size_t index, const bool value) { field.set(index, value); } @@ -142,7 +145,7 @@ ffs<unsigned long long>(const unsigned long long field) /// Calculates the Gray Code of `value` in place template <typename T> -std::enable_if_t<!std::is_integral<T>::value> grayCode(T& value); +std::enable_if_t<is_bitvec_v<T>> grayCode(T& value); /// Implementation of grayCode for any integral type template <typename T> @@ -154,7 +157,7 @@ grayCode(T& value) /// Calculates the inverse Gray Code of `value` in place template <typename T> -std::enable_if_t<!std::is_integral<T>::value> grayCodeInv(T& value); +std::enable_if_t<is_bitvec_v<T>> grayCodeInv(T& value); /// Implementation of grayCodeInv for any integral type template <typename T> |