Code cleanup

This commit is contained in:
Joseph Aquino 2025-07-04 20:49:27 -04:00
parent ec7a62536a
commit e35ddf2ce4
3 changed files with 152 additions and 133 deletions

View File

@ -1,7 +1,3 @@
#! /bin/bash #! /bin/bash
if [ -z "$1" ] || [ $# -eq 0 ]
then ninja -t clean
ninja debug -t clean && ninja release -t clean
else
ninja $1 -t clean
fi

View File

@ -6,16 +6,17 @@
namespace container namespace container
{ {
using CapacityType = EntityIndex;
using BoolArrayType = u8;
template<typename T, CapacityType capacity>
template<typename T, EntityIndex capacity>
class Array class Array
{ {
public: public:
using data_type = T; using data_type = T;
using pointer = data_type*; using pointer = data_type*;
using reference = data_type&; using reference = data_type&;
using const_pointer = const pointer; using const_pointer = const data_type*;
using const_reference = const data_type&; using const_reference = const data_type&;
using difference_type = std::ptrdiff_t; using difference_type = std::ptrdiff_t;
using iterator_category = std::forward_iterator_tag; using iterator_category = std::forward_iterator_tag;
@ -24,39 +25,24 @@ public:
class Iterator class Iterator
{ {
public: public:
Iterator(const_pointer input) Iterator(data_type* const input)
: m_ptr(input) : m_ptr(input)
{ } { }
Iterator& operator++() Iterator& operator++()
{ { m_ptr++; return *this; }
m_ptr++;
return *this;
}
Iterator operator++(int) Iterator operator++(int)
{ { auto it = *this; m_ptr++; return it; }
auto it = *this;
m_ptr++;
return it;
}
Iterator& operator--() Iterator& operator--()
{ { m_ptr--; return *this; }
m_ptr--;
return *this;
}
Iterator operator--(int) 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; } pointer operator->() const { return m_ptr; }
reference operator*() const { return *m_ptr; } reference operator*() const { return *m_ptr; }
@ -69,39 +55,24 @@ public:
class ConstIterator class ConstIterator
{ {
public: public:
ConstIterator(const_pointer input) ConstIterator(data_type* const input)
: m_ptr(input) : m_ptr(input)
{ } { }
ConstIterator& operator++() ConstIterator& operator++()
{ { m_ptr++; return *this; }
m_ptr++;
return *this;
}
ConstIterator operator++(int) ConstIterator operator++(int)
{ { auto it = *this; m_ptr++; return it; }
auto it = *this;
m_ptr++;
return it;
}
ConstIterator& operator--() ConstIterator& operator--()
{ { m_ptr--; return *this; }
m_ptr--;
return *this;
}
ConstIterator operator--(int) 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_pointer operator->() const { return m_ptr; }
const_reference operator*() const { return *m_ptr; } const_reference operator*() const { return *m_ptr; }
@ -122,6 +93,7 @@ public:
Array(Array&& other) Array(Array&& other)
: m_data(other.m_data) : m_data(other.m_data)
{ {
m_data = other.m_data;
other.m_data = nullptr; other.m_data = nullptr;
} }
@ -138,20 +110,29 @@ public:
return *this; return *this;
} }
pointer data() { return m_data; } 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 begin() { return Iterator(m_data); }
Iterator end() { return Iterator(m_data + capacity); } Iterator end() { return Iterator(m_data + capacity); }
ConstIterator cbegin() const { return ConstIterator(m_data); } ConstIterator begin() const { return ConstIterator(m_data); }
ConstIterator cend() const { return ConstIterator(m_data + capacity); } ConstIterator end() const { return ConstIterator(m_data + capacity); }
private: private:
pointer m_data; pointer m_data;
@ -159,13 +140,13 @@ private:
}; };
template <EntityIndex capacity> template <CapacityType capacity>
class Array<bool, capacity> class Array<bool, capacity>
{ {
public: public:
class reference; class reference;
class const_reference; class const_reference;
using data_type = u8; using data_type = BoolArrayType;
using pointer = reference; using pointer = reference;
using const_pointer = const_reference; using const_pointer = const_reference;
using difference_type = std::ptrdiff_t; using difference_type = std::ptrdiff_t;
@ -175,7 +156,7 @@ public:
class reference class reference
{ {
friend class Array; 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_ptr(ptr_in)
, m_mask(mask_in) , m_mask(mask_in)
{ {
@ -193,15 +174,16 @@ public:
return !!(*m_ptr & m_mask); 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; *m_ptr |= m_mask;
} break;
else case false:
{
*m_ptr &= m_mask; *m_ptr &= m_mask;
break;
} }
return *this; return *this;
} }
@ -217,14 +199,14 @@ public:
} }
private: private:
data_type* m_ptr; data_type* const m_ptr;
u8 m_mask; const data_type m_mask;
}; };
class const_reference class const_reference
{ {
friend class Array; 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_ptr(ptr_in)
, m_mask(mask_in) , m_mask(mask_in)
{ } { }
@ -241,159 +223,188 @@ public:
const_reference& operator=(const const_reference& other) = delete; const_reference& operator=(const const_reference& other) = delete;
const_reference& operator=(const reference& other) = delete; const_reference& operator=(const reference& other) = delete;
void flip() = delete;
private: private:
const data_type* m_ptr; const data_type* const m_ptr;
u8 m_mask; const data_type m_mask;
}; };
class Iterator class Iterator
{ {
public: public:
Iterator(data_type* input, u8 index) Iterator(data_type* const input, const data_type index)
: m_ptr(input) : m_ptr(input)
, m_currentClusterIndex(index) , m_bitIndex(index)
{ } { }
Iterator& operator++() Iterator& operator++()
{ {
if (m_currentClusterIndex++ == 7) switch (m_bitIndex++)
{ {
m_currentClusterIndex = 0; case 7:
m_bitIndex = 0;
m_ptr++; m_ptr++;
}
return *this; return *this;
break;
default:
return *this;
}
} }
Iterator operator++(int) Iterator operator++(int)
{ {
auto it = *this; auto it = *this;
if (m_currentClusterIndex++ == 7) switch (m_bitIndex++)
{ {
case 7:
m_bitIndex = 0;
m_ptr++; m_ptr++;
m_currentClusterIndex = 0;
}
return it; return it;
break;
default:
return it;
}
} }
Iterator& operator--() Iterator& operator--()
{ {
if (m_currentClusterIndex-- == 0) switch (m_bitIndex--)
{ {
case 0:
m_bitIndex = 7;
m_ptr--; m_ptr--;
m_currentClusterIndex = 7;
}
return *this; return *this;
break;
default:
return *this;
}
} }
Iterator operator--(int) Iterator operator--(int)
{ {
auto it = *this; auto it = *this;
if (m_currentClusterIndex-- == 0) switch (m_bitIndex--)
{ {
case 0:
m_bitIndex = 7;
m_ptr--; m_ptr--;
m_currentClusterIndex = 7;
}
return it; return it;
break;
default:
return it;
}
} }
bool operator[](const int) = delete; bool operator[](const int) = delete;
reference operator*() const reference operator*() const
{ {
return reference(m_ptr, 1 << m_currentClusterIndex); return reference(m_ptr, 1 << m_bitIndex);
} }
bool operator==(const Iterator& other) const 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 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: private:
data_type* m_ptr; data_type* m_ptr;
u8 m_currentClusterIndex{}; data_type m_bitIndex{};
}; };
class ConstIterator class ConstIterator
{ {
public: public:
ConstIterator(const data_type* input, u8 index) ConstIterator(const data_type* input, const data_type index)
: m_ptr(input) : m_ptr(input)
, m_currentClusterIndex(index) , m_bitIndex(index)
{ } { }
ConstIterator& operator++() ConstIterator& operator++()
{ {
if (m_currentClusterIndex++ == 7) switch (m_bitIndex++)
{ {
m_currentClusterIndex = 0; case 7:
m_bitIndex = 0;
m_ptr++; m_ptr++;
}
return *this; return *this;
break;
default:
return *this;
}
} }
ConstIterator operator++(int) ConstIterator operator++(int)
{ {
auto it = *this; auto it = *this;
if (m_currentClusterIndex++ == 7) switch (m_bitIndex++)
{ {
case 7:
m_bitIndex = 0;
m_ptr++; m_ptr++;
m_currentClusterIndex = 0;
}
return it; return it;
break;
default:
return it;
}
} }
ConstIterator& operator--() ConstIterator& operator--()
{ {
if (m_currentClusterIndex-- == 0) switch (m_bitIndex--)
{ {
case 0:
m_bitIndex = 7;
m_ptr--; m_ptr--;
m_currentClusterIndex = 7;
}
return *this; return *this;
break;
default:
return *this;
}
} }
ConstIterator operator--(int) ConstIterator operator--(int)
{ {
auto it = *this; auto it = *this;
if (m_currentClusterIndex-- == 0) switch (m_bitIndex--)
{ {
case 0:
m_bitIndex = 7;
m_ptr--; m_ptr--;
m_currentClusterIndex = 7;
}
return it; return it;
break;
default:
return it;
}
} }
bool operator[](const int) = delete; bool operator[](const int) = delete;
const_reference operator*() const const_reference operator*() const
{ { return const_reference(m_ptr, 1 << m_bitIndex); }
return const_reference(m_ptr, 1 << m_currentClusterIndex);
}
bool operator==(const ConstIterator& other) const 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 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: private:
const data_type* m_ptr; const data_type* m_ptr;
u8 m_currentClusterIndex{}; data_type m_bitIndex{};
}; };
public: public:
@ -406,6 +417,7 @@ public:
Array(Array&& other) Array(Array&& other)
: m_data(other.m_data) : m_data(other.m_data)
{ {
m_data = other.m_data;
other.m_data = nullptr; other.m_data = nullptr;
} }
@ -421,27 +433,37 @@ public:
return *this; return *this;
} }
data_type* data() { return m_data; }
//use readAt() instead //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)); switch ((int)input)
{
case true:
m_data[index / 8] |= (1 << (index % 8));
break;
case false:
m_data[index / 8] &= ~(1 << (index % 8));
break;
}
} }
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)); }
Iterator begin() { return Iterator(m_data, 0); } Iterator begin() { return Iterator(m_data, 0); }
Iterator end() { return Iterator(m_data + (capacity / 8), capacity % 8); } Iterator end() { return Iterator(m_data + (capacity / 8), capacity % 8); }
ConstIterator cbegin() const { return ConstIterator(m_data, 0); } ConstIterator begin() const { return ConstIterator(m_data, 0); }
ConstIterator cend() const { return ConstIterator(m_data + (capacity / 8), capacity % 8); } ConstIterator end() const { return ConstIterator(m_data + (capacity / 8), capacity % 8); }
private: private:
data_type* m_data; data_type* m_data;

View File

@ -31,6 +31,7 @@ project "2d-platformer"
language "C++" language "C++"
cppdialect "C++17" cppdialect "C++17"
systemversion "latest" systemversion "latest"
targetname "2d-platformer"
files files
{ {