From e35ddf2ce42d61b16962238ce104414de4291448 Mon Sep 17 00:00:00 2001 From: Joseph Aquino Date: Fri, 4 Jul 2025 20:49:27 -0400 Subject: [PATCH] Code cleanup --- clean-ninja.sh | 8 +- include/Containers/Array.h | 276 ++++++++++++++++++++----------------- premake5.lua | 1 + 3 files changed, 152 insertions(+), 133 deletions(-) diff --git a/clean-ninja.sh b/clean-ninja.sh index b5e1ed7..8897d95 100755 --- a/clean-ninja.sh +++ b/clean-ninja.sh @@ -1,7 +1,3 @@ #! /bin/bash -if [ -z "$1" ] || [ $# -eq 0 ] - then - ninja debug -t clean && ninja release -t clean - else - ninja $1 -t clean -fi \ No newline at end of file + +ninja -t clean \ No newline at end of file diff --git a/include/Containers/Array.h b/include/Containers/Array.h index 9b81a92..10a4f76 100644 --- a/include/Containers/Array.h +++ b/include/Containers/Array.h @@ -6,16 +6,17 @@ namespace container { +using CapacityType = EntityIndex; +using BoolArrayType = u8; - -template +template class Array { public: using data_type = T; using pointer = data_type*; using reference = data_type&; - using const_pointer = const pointer; + using const_pointer = const data_type*; using const_reference = const data_type&; using difference_type = std::ptrdiff_t; using iterator_category = std::forward_iterator_tag; @@ -24,39 +25,24 @@ public: class Iterator { public: - Iterator(const_pointer input) + Iterator(data_type* const input) : m_ptr(input) { } Iterator& operator++() - { - m_ptr++; - return *this; - } + { m_ptr++; return *this; } Iterator operator++(int) - { - auto it = *this; - m_ptr++; - return it; - } + { auto it = *this; m_ptr++; return it; } Iterator& operator--() - { - m_ptr--; - return *this; - } - + { m_ptr--; return *this; } Iterator operator--(int) - { - auto it = *this; - m_ptr++; - return it; - } + { auto it = *this; m_ptr++; return it; } - reference operator[](const int index) const { return m_ptr[index]; } + reference operator[](const CapacityType index) const { return m_ptr[index]; } pointer operator->() const { return m_ptr; } reference operator*() const { return *m_ptr; } @@ -69,39 +55,24 @@ public: class ConstIterator { public: - ConstIterator(const_pointer input) + ConstIterator(data_type* const input) : m_ptr(input) { } ConstIterator& operator++() - { - m_ptr++; - return *this; - } + { m_ptr++; return *this; } ConstIterator operator++(int) - { - auto it = *this; - m_ptr++; - return it; - } + { auto it = *this; m_ptr++; return it; } ConstIterator& operator--() - { - m_ptr--; - return *this; - } - + { m_ptr--; return *this; } ConstIterator operator--(int) - { - auto it = *this; - m_ptr++; - return it; - } + { auto it = *this; m_ptr++; return it; } - const_reference operator[](const int index) const { return m_ptr[index]; } + const_reference operator[](const CapacityType index) const { return m_ptr[index]; } const_pointer operator->() const { return m_ptr; } const_reference operator*() const { return *m_ptr; } @@ -121,8 +92,9 @@ public: Array(Array&& other) : m_data(other.m_data) - { - other.m_data = nullptr; + { + m_data = other.m_data; + other.m_data = nullptr; } @@ -138,20 +110,29 @@ public: return *this; } - pointer data() { return m_data; } - reference operator[](const int index) { return m_data[index]; } + const_pointer cosntData() const { return m_data; } - const_reference operator[](const int index) const { return m_data[index]; } + reference operator[](const CapacityType index) { return m_data[index]; } - inline constexpr EntityIndex size() { return capacity; } + const_reference operator[](const CapacityType index) const { return m_data[index]; } + + pointer operator->() { return m_data; } + + const_pointer operator->() const { return m_data; } + + reference operator*() { return *m_data; } + + const_reference operator*() const { return *m_data; } + + inline constexpr CapacityType size() const { return capacity; } Iterator begin() { return Iterator(m_data); } Iterator end() { return Iterator(m_data + capacity); } - ConstIterator cbegin() const { return ConstIterator(m_data); } - ConstIterator cend() const { return ConstIterator(m_data + capacity); } + ConstIterator begin() const { return ConstIterator(m_data); } + ConstIterator end() const { return ConstIterator(m_data + capacity); } private: pointer m_data; @@ -159,13 +140,13 @@ private: }; -template +template class Array { public: class reference; class const_reference; - using data_type = u8; + using data_type = BoolArrayType; using pointer = reference; using const_pointer = const_reference; using difference_type = std::ptrdiff_t; @@ -175,7 +156,7 @@ public: class reference { friend class Array; - reference(data_type* ptr_in, u8 mask_in) + reference(data_type* const ptr_in, const data_type mask_in) : m_ptr(ptr_in) , m_mask(mask_in) { @@ -193,15 +174,16 @@ public: return !!(*m_ptr & m_mask); } - reference& operator=(bool input) + reference& operator=(const bool input) { - if (input) + switch ((int)input) { + case true: *m_ptr |= m_mask; - } - else - { + break; + case false: *m_ptr &= m_mask; + break; } return *this; } @@ -217,14 +199,14 @@ public: } private: - data_type* m_ptr; - u8 m_mask; + data_type* const m_ptr; + const data_type m_mask; }; class const_reference { friend class Array; - const_reference(data_type* ptr_in, u8 mask_in) + const_reference(const data_type* ptr_in, const data_type mask_in) : m_ptr(ptr_in) , m_mask(mask_in) { } @@ -241,159 +223,188 @@ public: const_reference& operator=(const const_reference& other) = delete; const_reference& operator=(const reference& other) = delete; - void flip() = delete; private: - const data_type* m_ptr; - u8 m_mask; + const data_type* const m_ptr; + const data_type m_mask; }; class Iterator { public: - Iterator(data_type* input, u8 index) + Iterator(data_type* const input, const data_type index) : m_ptr(input) - , m_currentClusterIndex(index) + , m_bitIndex(index) { } Iterator& operator++() { - if (m_currentClusterIndex++ == 7) + switch (m_bitIndex++) { - m_currentClusterIndex = 0; + case 7: + m_bitIndex = 0; m_ptr++; + return *this; + break; + + default: + return *this; } - - return *this; } Iterator operator++(int) { auto it = *this; - if (m_currentClusterIndex++ == 7) + switch (m_bitIndex++) { + case 7: + m_bitIndex = 0; m_ptr++; - m_currentClusterIndex = 0; - } + return it; + break; - return it; + default: + return it; + } } Iterator& operator--() { - if (m_currentClusterIndex-- == 0) + switch (m_bitIndex--) { + case 0: + m_bitIndex = 7; m_ptr--; - m_currentClusterIndex = 7; - } + return *this; + break; - return *this; + default: + return *this; + } } Iterator operator--(int) { auto it = *this; - if (m_currentClusterIndex-- == 0) + switch (m_bitIndex--) { + case 0: + m_bitIndex = 7; m_ptr--; - m_currentClusterIndex = 7; - } + return it; + break; - return it; + default: + return it; + } } bool operator[](const int) = delete; reference operator*() const { - return reference(m_ptr, 1 << m_currentClusterIndex); + return reference(m_ptr, 1 << m_bitIndex); } bool operator==(const Iterator& other) const { - return (m_ptr == other.m_ptr) && (m_currentClusterIndex == other.m_currentClusterIndex); + return (m_ptr == other.m_ptr) && (m_bitIndex == other.m_bitIndex); } bool operator!=(const Iterator& other) const { - return (m_ptr != other.m_ptr) || (m_currentClusterIndex != other.m_currentClusterIndex); + return (m_ptr != other.m_ptr) || (m_bitIndex != other.m_bitIndex); } private: data_type* m_ptr; - u8 m_currentClusterIndex{}; + data_type m_bitIndex{}; }; class ConstIterator { public: - ConstIterator(const data_type* input, u8 index) + ConstIterator(const data_type* input, const data_type index) : m_ptr(input) - , m_currentClusterIndex(index) + , m_bitIndex(index) { } ConstIterator& operator++() { - if (m_currentClusterIndex++ == 7) + switch (m_bitIndex++) { - m_currentClusterIndex = 0; + case 7: + m_bitIndex = 0; m_ptr++; + return *this; + break; + + default: + return *this; } - - return *this; } ConstIterator operator++(int) { auto it = *this; - if (m_currentClusterIndex++ == 7) + switch (m_bitIndex++) { + case 7: + m_bitIndex = 0; m_ptr++; - m_currentClusterIndex = 0; - } + return it; + break; - return it; + default: + return it; + } } ConstIterator& operator--() { - if (m_currentClusterIndex-- == 0) + switch (m_bitIndex--) { + case 0: + m_bitIndex = 7; m_ptr--; - m_currentClusterIndex = 7; - } + return *this; + break; - return *this; + default: + return *this; + } } ConstIterator operator--(int) { auto it = *this; - if (m_currentClusterIndex-- == 0) + switch (m_bitIndex--) { + case 0: + m_bitIndex = 7; m_ptr--; - m_currentClusterIndex = 7; - } + return it; + break; - return it; + default: + return it; + } } bool operator[](const int) = delete; const_reference operator*() const - { - return const_reference(m_ptr, 1 << m_currentClusterIndex); - } + { return const_reference(m_ptr, 1 << m_bitIndex); } bool operator==(const ConstIterator& other) const - { return (m_ptr == other.m_ptr) && (m_currentClusterIndex == other.m_currentClusterIndex); } + { return (m_ptr == other.m_ptr) && (m_bitIndex == other.m_bitIndex); } bool operator!=(const ConstIterator& other) const - { return (m_ptr != other.m_ptr) || (m_currentClusterIndex != other.m_currentClusterIndex); } + { return (m_ptr != other.m_ptr) || (m_bitIndex != other.m_bitIndex); } private: const data_type* m_ptr; - u8 m_currentClusterIndex{}; + data_type m_bitIndex{}; }; public: @@ -406,6 +417,7 @@ public: Array(Array&& other) : m_data(other.m_data) { + m_data = other.m_data; other.m_data = nullptr; } @@ -421,27 +433,37 @@ public: return *this; } - data_type* data() { return m_data; } //use readAt() instead - reference operator[](int index) + reference operator[](const CapacityType index) + { return reference(m_data + (index / 8), 1 << (index % 8)); } + + + const_reference operator[](const CapacityType index) const + { return reference(m_data + (index / 8), 1 << (index % 8)); } + + inline constexpr CapacityType size() { return capacity; } + + bool readAt(const CapacityType index) const { return m_data[index / 8] & (1 << (index % 8)); } + + void changeAt(const CapacityType index, const bool input) { - return reference(m_data + (index / 8), 1 << (index % 8)); - } - - inline constexpr EntityIndex size() { return capacity; } - - bool readAt(const int index) const { return m_data[index / 8] & (1 << (index % 8)); } - - void setTrueAt(const int index) { m_data[index / 8] |= (1 << (index % 8)); } - - void setFalseAt(const int index) { m_data[index / 8] &= ~(1 << (index % 8)); } + switch ((int)input) + { + case true: + m_data[index / 8] |= (1 << (index % 8)); + break; + case false: + m_data[index / 8] &= ~(1 << (index % 8)); + break; + } + } Iterator begin() { return Iterator(m_data, 0); } Iterator end() { return Iterator(m_data + (capacity / 8), capacity % 8); } - ConstIterator cbegin() const { return ConstIterator(m_data, 0); } - ConstIterator cend() const { return ConstIterator(m_data + (capacity / 8), capacity % 8); } + ConstIterator begin() const { return ConstIterator(m_data, 0); } + ConstIterator end() const { return ConstIterator(m_data + (capacity / 8), capacity % 8); } private: data_type* m_data; diff --git a/premake5.lua b/premake5.lua index f66ed8e..1d30ca6 100644 --- a/premake5.lua +++ b/premake5.lua @@ -31,6 +31,7 @@ project "2d-platformer" language "C++" cppdialect "C++17" systemversion "latest" + targetname "2d-platformer" files {