From a9a6a0672aa3e6e23e9368b7c0d860e15adb5d8c Mon Sep 17 00:00:00 2001 From: Bimal Gaudel Date: Tue, 18 Jun 2024 09:10:15 -0400 Subject: [PATCH 1/2] `constexpr` reserved bitset and null bitset. --- SeQuant/core/space.hpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/SeQuant/core/space.hpp b/SeQuant/core/space.hpp index 0fd3d69cf..a7ca29aa0 100644 --- a/SeQuant/core/space.hpp +++ b/SeQuant/core/space.hpp @@ -30,6 +30,8 @@ class Index; // friend of TypeAttr class TypeAttr { public: using bitset_t = int32_t; + static constexpr bitset_t reserved_bitset = 0x80000000; + static constexpr bitset_t null_bitset = 0; /// default ctor creates a null TypeAttr constexpr TypeAttr() noexcept = default; @@ -43,7 +45,8 @@ class TypeAttr { /// @param bitset bitset representation of this Type /// @pre `(bitset & make_reserved().bitset) == null.bitset` explicit constexpr TypeAttr(bitset_t bitset) noexcept : bitset(bitset) { - assert((this->bitset & reserved.bitset) == null.bitset); + assert(this->bitset == reserved_bitset); + assert((this->bitset & reserved_bitset) == null_bitset); } /// construct TypeAddr from things that can be cast to bitset_t, but exclude @@ -160,7 +163,7 @@ class QuantumNumbersAttr { /// @pre `(bitset & make_reserved().bitset()) == null.bitset()` explicit constexpr QuantumNumbersAttr(bitset_t bitset) noexcept : bitset(bitset) { - assert((this->bitset & reserved.bitset) == null.bitset); + assert((this->bitset & TypeAttr::reserved_bitset) == TypeAttr::null_bitset); } template Date: Tue, 18 Jun 2024 15:09:05 -0400 Subject: [PATCH 2/2] Refactor `bitset_t` to its own namespace. --- SeQuant/core/space.hpp | 23 ++++++++++++----------- SeQuant/domain/mbpt/spin.hpp | 21 +++++++++------------ 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/SeQuant/core/space.hpp b/SeQuant/core/space.hpp index a7ca29aa0..cc928c65e 100644 --- a/SeQuant/core/space.hpp +++ b/SeQuant/core/space.hpp @@ -18,6 +18,14 @@ namespace sequant { +namespace bitset { +using type = int32_t; +constexpr type reserved = 0x80000000; +constexpr type null = 0x00000000; +} // namespace bitset + +using bitset_t = bitset::type; + class QuantumNumbersAttr; // used to constrain TypeAttr ctor class Index; // friend of TypeAttr @@ -29,10 +37,6 @@ class Index; // friend of TypeAttr /// (intersection, union, etc.) it is encoded as a fixed-width (32) bitset. class TypeAttr { public: - using bitset_t = int32_t; - static constexpr bitset_t reserved_bitset = 0x80000000; - static constexpr bitset_t null_bitset = 0; - /// default ctor creates a null TypeAttr constexpr TypeAttr() noexcept = default; @@ -45,8 +49,7 @@ class TypeAttr { /// @param bitset bitset representation of this Type /// @pre `(bitset & make_reserved().bitset) == null.bitset` explicit constexpr TypeAttr(bitset_t bitset) noexcept : bitset(bitset) { - assert(this->bitset == reserved_bitset); - assert((this->bitset & reserved_bitset) == null_bitset); + assert((this->bitset & bitset::reserved) == bitset::null); } /// construct TypeAddr from things that can be cast to bitset_t, but exclude @@ -147,8 +150,6 @@ inline const TypeAttr TypeAttr::reserved = TypeAttr::make_reserved(); /// denotes other quantum numbers (particle type, spin, etc.) class QuantumNumbersAttr { public: - using bitset_t = int32_t; - /// default ctor creates a null QuantumNumbersAttr /// @post `static_cast(*this) == false` constexpr QuantumNumbersAttr() noexcept = default; @@ -163,7 +164,7 @@ class QuantumNumbersAttr { /// @pre `(bitset & make_reserved().bitset()) == null.bitset()` explicit constexpr QuantumNumbersAttr(bitset_t bitset) noexcept : bitset(bitset) { - assert((this->bitset & TypeAttr::reserved_bitset) == TypeAttr::null_bitset); + assert((this->bitset & bitset::reserved) == bitset::null); } template ); +static_assert(!std::is_convertible_v); // QuantumNumbersAttr::bitset_t cannot be constructed from Spin -static_assert(!std::is_constructible_v); +static_assert(!std::is_constructible_v); // but Spin can be cast to QuantumNumbersAttr::bitset_t -static_assert(meta::is_statically_castable_v); +static_assert(meta::is_statically_castable_v); // Spin cannot be cast to nonsense ... static_assert( !meta::is_statically_castable_v); inline Spin operator~(Spin s) { - return static_cast(~(static_cast(s))); + return static_cast(~(static_cast(s))); } inline Spin operator|(Spin s1, Spin s2) { - return static_cast(static_cast(s1) | - static_cast(s2)); + return static_cast(static_cast(s1) | + static_cast(s2)); } inline Spin operator&(Spin s1, Spin s2) { - return static_cast(static_cast(s1) & - static_cast(s2)); + return static_cast(static_cast(s1) & + static_cast(s2)); } /// converts QuantumNumbersAttr to Spin