From e405658f6a6d0240faf2deb670f93e5e6ce287ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Mon, 13 Dec 2021 15:47:19 +0200 Subject: [PATCH] Code style and cleanup --- src/openrct2/core/BitSet.hpp | 242 ++++++++++++------------- src/openrct2/entity/Guest.h | 4 +- src/openrct2/peep/GuestPathfinding.cpp | 2 + src/openrct2/rct2/S6Importer.cpp | 3 +- test/tests/BitSetTests.cpp | 16 +- 5 files changed, 132 insertions(+), 135 deletions(-) diff --git a/src/openrct2/core/BitSet.hpp b/src/openrct2/core/BitSet.hpp index 48b8e93205..9256010a96 100644 --- a/src/openrct2/core/BitSet.hpp +++ b/src/openrct2/core/BitSet.hpp @@ -22,41 +22,41 @@ namespace OpenRCT2 { namespace BitSet { - static constexpr size_t byte_bits = std::numeric_limits>::digits; + static constexpr size_t BitsPerByte = std::numeric_limits>::digits; - template static constexpr size_t round_bits() + template static constexpr size_t ByteAlignBits() { - const auto reminder = TNumBits % byte_bits; + const auto reminder = TNumBits % BitsPerByte; if constexpr (reminder == 0u) { return TNumBits; } else { - return TNumBits + (byte_bits - (TNumBits % byte_bits)); + return TNumBits + (BitsPerByte - (TNumBits % BitsPerByte)); } } - static_assert(round_bits<1>() == 8); - static_assert(round_bits<4>() == 8); - static_assert(round_bits<8>() == 8); - static_assert(round_bits<9>() == 16); - static_assert(round_bits<9>() == 16); - static_assert(round_bits<17>() == 24); - static_assert(round_bits<24>() == 24); - static_assert(round_bits<31>() == 32); + static_assert(ByteAlignBits<1>() == 8); + static_assert(ByteAlignBits<4>() == 8); + static_assert(ByteAlignBits<8>() == 8); + static_assert(ByteAlignBits<9>() == 16); + static_assert(ByteAlignBits<9>() == 16); + static_assert(ByteAlignBits<17>() == 24); + static_assert(ByteAlignBits<24>() == 24); + static_assert(ByteAlignBits<31>() == 32); // Returns the amount of bytes required for a single block. - template static constexpr size_t storage_block_size() + template static constexpr size_t ComputeBlockSize() { - constexpr size_t numBits = round_bits(); + constexpr size_t numBits = ByteAlignBits(); if constexpr (numBits >= std::numeric_limits::digits) { return sizeof(uintptr_t); } else { - const auto numBytes = numBits / byte_bits; + const auto numBytes = numBits / BitsPerByte; auto mask = 1u; while (mask < numBytes) { @@ -66,18 +66,28 @@ namespace OpenRCT2 } } - template static constexpr size_t storage_block_count() + template static constexpr size_t ComputeBlockCount() { size_t numBits = TNumBits; size_t numBlocks = 0; while (numBits > 0) { numBlocks++; - numBits -= std::min(TBlockSizeBytes * byte_bits, numBits); + numBits -= std::min(TBlockSizeBytes * BitsPerByte, numBits); } return numBlocks; } + static_assert(ComputeBlockSize<1>() == sizeof(uint8_t)); + static_assert(ComputeBlockSize<4>() == sizeof(uint8_t)); + static_assert(ComputeBlockSize<8>() == sizeof(uint8_t)); + static_assert(ComputeBlockSize<9>() == sizeof(uint16_t)); + static_assert(ComputeBlockSize<14>() == sizeof(uint16_t)); + static_assert(ComputeBlockSize<16>() == sizeof(uint16_t)); + static_assert(ComputeBlockSize<18>() == sizeof(uint32_t)); + static_assert(ComputeBlockSize<31>() == sizeof(uint32_t)); + static_assert(ComputeBlockSize<33>() == sizeof(uintptr_t)); + // TODO: Replace with std::popcount when C++20 is enabled. template static constexpr size_t popcount(const T val) { @@ -92,89 +102,77 @@ namespace OpenRCT2 return res; } - static_assert(storage_block_size<1>() == sizeof(uint8_t)); - static_assert(storage_block_size<4>() == sizeof(uint8_t)); - static_assert(storage_block_size<8>() == sizeof(uint8_t)); - static_assert(storage_block_size<9>() == sizeof(uint16_t)); - static_assert(storage_block_size<14>() == sizeof(uint16_t)); - static_assert(storage_block_size<16>() == sizeof(uint16_t)); - static_assert(storage_block_size<18>() == sizeof(uint32_t)); - static_assert(storage_block_size<31>() == sizeof(uint32_t)); - static_assert(storage_block_size<33>() == sizeof(uintptr_t)); + template struct StorageBlockType; - template struct storage_block_type; - - template<> struct storage_block_type<1> + template<> struct StorageBlockType<1> { using value_type = uint8_t; }; - template<> struct storage_block_type<2> + template<> struct StorageBlockType<2> { using value_type = uint16_t; }; - template<> struct storage_block_type<4> + template<> struct StorageBlockType<4> { using value_type = uint32_t; }; - template<> struct storage_block_type<8> + template<> struct StorageBlockType<8> { using value_type = uint64_t; }; template struct storage_block_type_aligned { - using value_type = typename storage_block_type()>::value_type; + using value_type = typename StorageBlockType()>::value_type; }; } // namespace BitSet } // namespace Detail template class BitSet { - static constexpr size_t byte_aligned_bitsize = Detail::BitSet::round_bits(); + static constexpr size_t ByteAlignedBitSize = Detail::BitSet::ByteAlignBits(); - using storage_block_type = typename Detail::BitSet::storage_block_type_aligned::value_type; + using StorageBlockType = typename Detail::BitSet::storage_block_type_aligned::value_type; - static constexpr size_t block_byte_size = sizeof(storage_block_type); - static constexpr size_t block_type_bit_size = block_byte_size * Detail::BitSet::byte_bits; - static constexpr size_t block_count = Detail::BitSet::storage_block_count(); - static constexpr size_t capacity_bits = block_count * block_type_bit_size; + static constexpr size_t BlockByteSize = sizeof(StorageBlockType); + static constexpr size_t BlockBitSize = BlockByteSize * Detail::BitSet::BitsPerByte; + static constexpr size_t BlockCount = Detail::BitSet::ComputeBlockCount(); + static constexpr size_t CapacityBits = BlockCount * BlockBitSize; - static constexpr storage_block_type value_zero = storage_block_type{ 0u }; - static constexpr storage_block_type value_one = storage_block_type{ 1u }; + static constexpr StorageBlockType BlockValueZero = StorageBlockType{ 0u }; + static constexpr StorageBlockType BlockValueOne = StorageBlockType{ 1u }; + static constexpr StorageBlockType BlockValueMask = static_cast(~BlockValueZero); - static constexpr storage_block_type block_init_value{}; - static constexpr storage_block_type block_mask_value = static_cast(~block_init_value); - - static constexpr bool requires_trim = TBitSize != capacity_bits; + static constexpr bool RequiresTrim = TBitSize != CapacityBits; public: - using block_type = storage_block_type; - using storage_data = std::array; + using BlockType = StorageBlockType; + using Storage = std::array; // Proxy object to access the bits as single value. template class reference_base { - T& storage_; - const size_t blockIndex_; - const size_t blockOffset_; + T& _storage; + const size_t _blockIndex; + const size_t _blockOffset; public: constexpr reference_base(T& data, size_t blockIndex, size_t blockOffset) noexcept - : storage_(data) - , blockIndex_(blockIndex) - , blockOffset_(blockOffset) + : _storage(data) + , _blockIndex(blockIndex) + , _blockOffset(blockOffset) { } constexpr reference_base& operator=(const bool value) noexcept { if (!value) - storage_[blockIndex_] &= ~(value_one << blockOffset_); + _storage[_blockIndex] &= ~(BlockValueOne << _blockOffset); else - storage_[blockIndex_] |= (value_one << blockOffset_); + _storage[_blockIndex] |= (BlockValueOne << _blockOffset); return *this; } @@ -185,7 +183,7 @@ namespace OpenRCT2 constexpr bool value() const noexcept { - return (storage_[blockIndex_] & (value_one << blockOffset_)) != value_zero; + return (_storage[_blockIndex] & (BlockValueOne << _blockOffset)) != BlockValueZero; } constexpr operator bool() const noexcept @@ -194,8 +192,8 @@ namespace OpenRCT2 } }; - using reference = reference_base; - using const_reference = reference_base; + using reference = reference_base; + using const_reference = reference_base; template class iterator_base { @@ -213,8 +211,8 @@ namespace OpenRCT2 constexpr auto operator*() const { - const auto blockIndex = compute_block_index(_pos); - const auto blockOffset = compute_block_offset(_pos); + const auto blockIndex = ComputeBlockIndex(_pos); + const auto blockOffset = ComputeBlockOffset(_pos); return TValue(_bitset->data(), blockIndex, blockOffset); } @@ -266,13 +264,13 @@ namespace OpenRCT2 using const_iterator = iterator_base; private: - storage_data data_{}; + Storage _data{}; public: constexpr BitSet() = default; - constexpr BitSet(const storage_block_type val) - : data_{ val } + constexpr BitSet(const StorageBlockType val) + : _data{ val } { } @@ -292,7 +290,7 @@ namespace OpenRCT2 constexpr size_t count() const noexcept { size_t numBits = 0; - for (auto& data : data_) + for (auto& data : _data) { numBits += Detail::BitSet::popcount(data); } @@ -301,71 +299,71 @@ namespace OpenRCT2 constexpr size_t capacity() const noexcept { - return capacity_bits; + return CapacityBits; } - constexpr storage_data& data() noexcept + constexpr Storage& data() noexcept { - return data_; + return _data; } - constexpr const storage_data& data() const noexcept + constexpr const Storage& data() const noexcept { - return data_; + return _data; } constexpr BitSet& set(size_t index, bool value) noexcept { - const auto blockIndex = compute_block_index(index); - const auto blockOffset = compute_block_offset(index); + const auto blockIndex = ComputeBlockIndex(index); + const auto blockOffset = ComputeBlockOffset(index); if (!value) - data_[blockIndex] &= ~(value_one << blockOffset); + _data[blockIndex] &= ~(BlockValueOne << blockOffset); else - data_[blockIndex] |= (value_one << blockOffset); + _data[blockIndex] |= (BlockValueOne << blockOffset); return *this; } constexpr bool get(size_t index) const noexcept { - const auto blockIndex = compute_block_index(index); - const auto blockOffset = compute_block_offset(index); - return (data_[blockIndex] & (value_one << blockOffset)) != value_zero; + const auto blockIndex = ComputeBlockIndex(index); + const auto blockOffset = ComputeBlockOffset(index); + return (_data[blockIndex] & (BlockValueOne << blockOffset)) != BlockValueZero; } constexpr bool operator[](const size_t index) const noexcept { - const auto blockIndex = compute_block_index(index); - const auto blockOffset = compute_block_offset(index); - const_reference ref(data_, blockIndex, blockOffset); + const auto blockIndex = ComputeBlockIndex(index); + const auto blockOffset = ComputeBlockOffset(index); + const_reference ref(_data, blockIndex, blockOffset); return ref.value(); } constexpr reference operator[](const size_t index) noexcept { - const auto blockIndex = compute_block_index(index); - const auto blockOffset = compute_block_offset(index); - return reference(data_, blockIndex, blockOffset); + const auto blockIndex = ComputeBlockIndex(index); + const auto blockOffset = ComputeBlockOffset(index); + return reference(_data, blockIndex, blockOffset); } constexpr BitSet& flip() noexcept { - for (auto& data : data_) + for (auto& data : _data) { - data ^= block_mask_value; + data ^= BlockValueMask; } - if constexpr (requires_trim) + if constexpr (RequiresTrim) { - trim(); + Trim(); } return *this; } constexpr BitSet& reset() noexcept { - std::fill(data_.begin(), data_.end(), block_init_value); - if constexpr (requires_trim) + std::fill(_data.begin(), _data.end(), BlockValueZero); + if constexpr (RequiresTrim) { - trim(); + Trim(); } return *this; } @@ -406,13 +404,13 @@ namespace OpenRCT2 constexpr BitSet operator^(const BitSet& other) const noexcept { BitSet res = *this; - for (size_t i = 0; i < data_.size(); i++) + for (size_t i = 0; i < _data.size(); i++) { - res.data_[i] ^= other.data_[i]; + res._data[i] ^= other._data[i]; } - if constexpr (requires_trim) + if constexpr (RequiresTrim) { - res.trim(); + res.Trim(); } return res; } @@ -426,13 +424,13 @@ namespace OpenRCT2 constexpr BitSet operator|(const BitSet& other) const noexcept { BitSet res = *this; - for (size_t i = 0; i < data_.size(); i++) + for (size_t i = 0; i < _data.size(); i++) { - res.data_[i] |= other.data_[i]; + res._data[i] |= other._data[i]; } - if constexpr (requires_trim) + if constexpr (RequiresTrim) { - res.trim(); + res.Trim(); } return res; } @@ -446,13 +444,13 @@ namespace OpenRCT2 constexpr BitSet operator&(const BitSet& other) const noexcept { BitSet res = *this; - for (size_t i = 0; i < data_.size(); i++) + for (size_t i = 0; i < _data.size(); i++) { - res.data_[i] &= other.data_[i]; + res._data[i] &= other._data[i]; } - if constexpr (requires_trim) + if constexpr (RequiresTrim) { - res.trim(); + res.Trim(); } return res; } @@ -466,13 +464,13 @@ namespace OpenRCT2 constexpr BitSet operator~() const noexcept { BitSet res = *this; - for (size_t i = 0; i < data_.size(); i++) + for (size_t i = 0; i < _data.size(); i++) { - res.data_[i] = ~res.data_[i]; + res._data[i] = ~res._data[i]; } - if constexpr (requires_trim) + if constexpr (RequiresTrim) { - res.trim(); + res.Trim(); } return res; } @@ -480,65 +478,65 @@ namespace OpenRCT2 constexpr bool operator<(const BitSet& other) const noexcept { return std::lexicographical_compare( - data_.begin(), data_.end(), other.data_.begin(), other.data_.end(), std::less{}); + _data.begin(), _data.end(), other._data.begin(), other._data.end(), std::less{}); } constexpr bool operator<=(const BitSet& other) const noexcept { return std::lexicographical_compare( - data_.begin(), data_.end(), other.data_.begin(), other.data_.end(), std::less_equal{}); + _data.begin(), _data.end(), other._data.begin(), other._data.end(), std::less_equal{}); } constexpr bool operator>(const BitSet& other) const noexcept { return std::lexicographical_compare( - data_.begin(), data_.end(), other.data_.begin(), other.data_.end(), std::greater{}); + _data.begin(), _data.end(), other._data.begin(), other._data.end(), std::greater{}); } constexpr bool operator>=(const BitSet& other) const noexcept { return std::lexicographical_compare( - data_.begin(), data_.end(), other.data_.begin(), other.data_.end(), std::greater_equal{}); + _data.begin(), _data.end(), other._data.begin(), other._data.end(), std::greater_equal{}); } private: - static constexpr size_t compute_block_index(size_t idx) noexcept + static constexpr size_t ComputeBlockIndex(size_t idx) noexcept { - if constexpr (block_count == 1) + if constexpr (BlockCount == 1) { return 0; } else { - return idx / block_type_bit_size; + return idx / BlockBitSize; } } - static constexpr size_t compute_block_offset(size_t idx) noexcept + static constexpr size_t ComputeBlockOffset(size_t idx) noexcept { - if constexpr (block_count == 1) + if constexpr (BlockCount == 1) { return idx; } else { - return idx % block_type_bit_size; + return idx % BlockBitSize; } } // Some operations require to trim of the excess. - constexpr void trim() noexcept + constexpr void Trim() noexcept { - const auto byteIdx = TBitSize / block_type_bit_size; - const auto bitIdx = TBitSize % block_type_bit_size; + const auto byteIdx = TBitSize / BlockBitSize; + const auto bitIdx = TBitSize % BlockBitSize; if constexpr (bitIdx == 0) return; - auto trimMask = block_mask_value; - trimMask <<= (block_type_bit_size - bitIdx); - trimMask >>= (block_type_bit_size - bitIdx); + auto trimMask = BlockValueMask; + trimMask <<= (BlockBitSize - bitIdx); + trimMask >>= (BlockBitSize - bitIdx); - data_[byteIdx] &= trimMask; + _data[byteIdx] &= trimMask; } }; diff --git a/src/openrct2/entity/Guest.h b/src/openrct2/entity/Guest.h index fd1f48f26c..c4e3e531df 100644 --- a/src/openrct2/entity/Guest.h +++ b/src/openrct2/entity/Guest.h @@ -32,8 +32,6 @@ #define PEEP_MAX_NAUSEA 255 #define PEEP_MAX_THIRST 255 -using namespace OpenRCT2; - enum class PeepThoughtType : uint8_t { CantAffordRide = 0, // "I can't afford" @@ -420,7 +418,7 @@ private: void MakePassingPeepsSick(Guest* passingPeep); void GivePassingPeepsIceCream(Guest* passingPeep); Ride* FindBestRideToGoOn(); - BitSet FindRidesToGoOn(); + OpenRCT2::BitSet FindRidesToGoOn(); bool FindVehicleToEnter(Ride* ride, std::vector& car_array); void GoToRideEntrance(Ride* ride); }; diff --git a/src/openrct2/peep/GuestPathfinding.cpp b/src/openrct2/peep/GuestPathfinding.cpp index bce65d31f5..b1e61e23ce 100644 --- a/src/openrct2/peep/GuestPathfinding.cpp +++ b/src/openrct2/peep/GuestPathfinding.cpp @@ -23,6 +23,8 @@ #include #include +using namespace OpenRCT2; + static bool _peepPathFindIsStaff; static int8_t _peepPathFindNumJunctions; static int8_t _peepPathFindMaxJunctions; diff --git a/src/openrct2/rct2/S6Importer.cpp b/src/openrct2/rct2/S6Importer.cpp index 480b0e6c37..4a5005b924 100644 --- a/src/openrct2/rct2/S6Importer.cpp +++ b/src/openrct2/rct2/S6Importer.cpp @@ -73,7 +73,6 @@ #include "../world/TilePointerIndex.hpp" #include -#include namespace RCT2 { @@ -91,7 +90,7 @@ namespace RCT2 S6Data _s6{}; uint8_t _gameVersion = 0; bool _isSV7 = false; - BitSet _isFlatRide{}; + OpenRCT2::BitSet _isFlatRide{}; ObjectEntryIndex _pathToSurfaceMap[16]; ObjectEntryIndex _pathToQueueSurfaceMap[16]; ObjectEntryIndex _pathToRailingMap[16]; diff --git a/test/tests/BitSetTests.cpp b/test/tests/BitSetTests.cpp index bae044f642..8e3f2efaeb 100644 --- a/test/tests/BitSetTests.cpp +++ b/test/tests/BitSetTests.cpp @@ -16,9 +16,9 @@ TEST(BitTest, test_index_construction) { BitSet<64u> bits({ 0u, 2u, 4u, 6u, 8u, 10u }); #ifdef _M_X64 - static_assert(std::is_same_v); + static_assert(std::is_same_v); #else - static_assert(std::is_same_v); + static_assert(std::is_same_v); #endif constexpr auto size = sizeof(bits); static_assert(size == 8u); @@ -30,7 +30,7 @@ TEST(BitTest, test_index_construction) TEST(BitTest, test_basic) { BitSet<8u> bits; - static_assert(std::is_same_v); + static_assert(std::is_same_v); constexpr auto size = sizeof(bits); static_assert(size == sizeof(uint8_t)); @@ -49,7 +49,7 @@ TEST(BitTest, test_basic) TEST(BitTest, test_flip) { BitSet<8u> bits; - static_assert(std::is_same_v); + static_assert(std::is_same_v); constexpr auto size = sizeof(bits); static_assert(size == sizeof(uint8_t)); @@ -62,7 +62,7 @@ TEST(BitTest, test_flip) TEST(BitTest, test_trim8) { BitSet<5u> bits; - static_assert(std::is_same_v); + static_assert(std::is_same_v); constexpr auto size = sizeof(bits); static_assert(size == sizeof(uint8_t)); @@ -76,7 +76,7 @@ TEST(BitTest, test_trim8) TEST(BitTest, test_trim16) { BitSet<14u> bits; - static_assert(std::is_same_v); + static_assert(std::is_same_v); constexpr auto size = sizeof(bits); static_assert(size == sizeof(uint16_t)); @@ -95,14 +95,14 @@ TEST(BitTest, test_big) bits.flip(); #ifdef _M_X64 - static_assert(std::is_same_v); + static_assert(std::is_same_v); static_assert(bits.data().size() == 4); ASSERT_EQ(bits.data()[0], ~0ULL); ASSERT_EQ(bits.data()[1], ~0ULL); ASSERT_EQ(bits.data()[2], ~0ULL); ASSERT_EQ(bits.data()[3], ~0ULL); #else - static_assert(std::is_same_v); + static_assert(std::is_same_v); static_assert(bits.data().size() == 8); ASSERT_EQ(bits.data()[0], ~0UL); ASSERT_EQ(bits.data()[1], ~0UL);