-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1,055 changed files
with
1,853 additions
and
1,468 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <cstdint> | ||
|
||
#include <RED4ext/Common.hpp> | ||
#include <RED4ext/Detail/Containers/ArrayIterator.hpp> | ||
|
||
namespace RED4ext | ||
{ | ||
template<typename T> | ||
struct Span | ||
{ | ||
using ValueType = T; | ||
using Reference = ValueType&; | ||
using ConstReference = const ValueType; | ||
using Pointer = ValueType*; | ||
using ConstPointer = const ValueType*; | ||
|
||
using SizeType = std::uint32_t; | ||
using DifferenceType = std::ptrdiff_t; | ||
|
||
using Iterator = Detail::ArrayIterator<ValueType, Span>; | ||
using ConstIterator = Detail::ArrayIterator<const ValueType, Span>; | ||
|
||
Span() | ||
: beginPtr(nullptr) | ||
, endPtr(nullptr) | ||
{ | ||
} | ||
|
||
Span(Pointer aBegin, Pointer aEnd) | ||
: beginPtr(aBegin) | ||
, endPtr(aEnd) | ||
{ | ||
} | ||
|
||
Span(Pointer aBegin, SizeType aCount) | ||
: beginPtr(aBegin) | ||
, endPtr(aBegin + aCount) | ||
{ | ||
} | ||
|
||
constexpr operator bool() const noexcept | ||
{ | ||
return IsEmpty(); | ||
} | ||
|
||
constexpr Reference operator[](SizeType aPos) const | ||
{ | ||
assert(aPos < Size()); | ||
return Data()[aPos]; | ||
} | ||
|
||
constexpr ConstReference operator[](SizeType aPos) const | ||
{ | ||
assert(aPos < Size()); | ||
return Data()[aPos]; | ||
} | ||
|
||
[[nodiscard]] constexpr Iterator Find(ConstReference aValue) const noexcept | ||
{ | ||
return std::find(cbegin(), cend(), aValue); | ||
} | ||
|
||
[[nodiscard]] constexpr bool Contains(ConstReference aValue) const noexcept | ||
{ | ||
return Find(aValue) != cend(); | ||
} | ||
|
||
#pragma region Iterator | ||
[[nodiscard]] constexpr Iterator begin() const noexcept | ||
{ | ||
return beginPtr; | ||
} | ||
|
||
[[nodiscard]] constexpr ConstIterator begin() const noexcept | ||
{ | ||
return beginPtr; | ||
} | ||
|
||
[[nodiscard]] constexpr ConstIterator cbegin() const noexcept | ||
{ | ||
return begin(); | ||
} | ||
|
||
[[nodiscard]] constexpr Iterator end() const noexcept | ||
{ | ||
return endPtr; | ||
} | ||
|
||
[[nodiscard]] constexpr ConstIterator end() const noexcept | ||
{ | ||
return endPtr; | ||
} | ||
|
||
[[nodiscard]] constexpr ConstIterator cend() const noexcept | ||
{ | ||
return end(); | ||
} | ||
#pragma endregion | ||
|
||
[[nodiscard]] constexpr bool IsEmpty() const | ||
{ | ||
return !Data(); | ||
} | ||
|
||
[[nodiscard]] constexpr Pointer Data() const noexcept | ||
{ | ||
return beginPtr; | ||
} | ||
|
||
[[nodiscard]] constexpr ConstPointer Data() const noexcept | ||
{ | ||
return beginPtr; | ||
} | ||
|
||
[[nodiscard]] constexpr DifferenceType Size() const | ||
{ | ||
return endPtr - beginPtr; | ||
} | ||
|
||
T* beginPtr; // 00 | ||
T* endPtr; // 08 | ||
}; | ||
RED4EXT_ASSERT_SIZE(Span<int32_t>, 0x10); | ||
RED4EXT_ASSERT_OFFSET(Span<int32_t>, beginPtr, 0x0); | ||
RED4EXT_ASSERT_OFFSET(Span<int32_t>, endPtr, 0x8); | ||
} // namespace RED4ext |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#pragma once | ||
|
||
#include <concepts> | ||
#include <iterator> | ||
|
||
namespace RED4ext::Detail | ||
{ | ||
template<typename T, typename Container> | ||
class ArrayIterator | ||
{ | ||
public: | ||
using iterator_concept = std::contiguous_iterator_tag; | ||
using iterator_category = std::random_access_iterator_tag; | ||
using value_type = T; | ||
using reference = value_type&; | ||
using pointer = value_type*; | ||
using difference_type = std::ptrdiff_t; | ||
|
||
constexpr ArrayIterator() noexcept | ||
: m_ptr(nullptr) | ||
{ | ||
} | ||
|
||
constexpr ArrayIterator(Container::Pointer aPtr) noexcept | ||
: m_ptr(aPtr) | ||
{ | ||
} | ||
|
||
// Allow conversion from Iterator to ConstIterator. | ||
template<typename U> | ||
requires std::same_as<U, typename Container::ValueType> | ||
constexpr ArrayIterator(const ArrayIterator<U, Container>& aOther) noexcept | ||
: m_ptr(aOther.Base()) | ||
{ | ||
} | ||
|
||
constexpr ArrayIterator(const ArrayIterator&) noexcept = default; | ||
constexpr ArrayIterator(ArrayIterator&&) noexcept = default; | ||
|
||
constexpr ArrayIterator& operator=(const ArrayIterator&) noexcept = default; | ||
constexpr ArrayIterator& operator=(ArrayIterator&&) noexcept = default; | ||
|
||
constexpr ~ArrayIterator() noexcept = default; | ||
|
||
[[nodiscard]] constexpr reference operator*() const noexcept | ||
{ | ||
return *m_ptr; | ||
} | ||
|
||
[[nodiscard]] constexpr pointer operator->() const noexcept | ||
{ | ||
return m_ptr; | ||
} | ||
|
||
constexpr ArrayIterator& operator++() noexcept | ||
{ | ||
++m_ptr; | ||
return *this; | ||
} | ||
|
||
constexpr ArrayIterator operator++(int) noexcept | ||
{ | ||
auto tmp = *this; | ||
++(*this); | ||
|
||
return tmp; | ||
} | ||
|
||
constexpr ArrayIterator& operator--() noexcept | ||
{ | ||
--m_ptr; | ||
return *this; | ||
} | ||
|
||
constexpr ArrayIterator operator--(int) noexcept | ||
{ | ||
auto tmp = *this; | ||
--(*this); | ||
|
||
return tmp; | ||
} | ||
|
||
[[nodiscard]] constexpr reference operator[](const difference_type aOffset) const noexcept | ||
{ | ||
return m_ptr[aOffset]; | ||
} | ||
|
||
constexpr ArrayIterator& operator+=(const difference_type aOffset) noexcept | ||
{ | ||
m_ptr += aOffset; | ||
return *this; | ||
} | ||
|
||
[[nodiscard]] constexpr ArrayIterator operator+(const difference_type aOffset) const noexcept | ||
{ | ||
return ArrayIterator(m_ptr + aOffset); | ||
} | ||
|
||
constexpr ArrayIterator& operator-=(const difference_type aOffset) noexcept | ||
{ | ||
m_ptr -= aOffset; | ||
return *this; | ||
} | ||
|
||
[[nodiscard]] constexpr ArrayIterator operator-(const difference_type aOffset) const noexcept | ||
{ | ||
return ArrayIterator(m_ptr - aOffset); | ||
} | ||
|
||
[[nodiscard]] constexpr difference_type operator-(const ArrayIterator& aRhs) const noexcept | ||
{ | ||
return m_ptr - aRhs.m_ptr; | ||
} | ||
|
||
[[nodiscard]] constexpr bool operator==(const ArrayIterator& aRhs) const noexcept | ||
{ | ||
return m_ptr == aRhs.m_ptr; | ||
} | ||
|
||
[[nodiscard]] constexpr auto operator<=>(const ArrayIterator& aRhs) const noexcept | ||
{ | ||
// TODO: _Unfancy | ||
return m_ptr <=> aRhs.m_ptr; | ||
} | ||
|
||
[[nodiscard]] constexpr Container::Pointer Base() const noexcept | ||
{ | ||
return m_ptr; | ||
} | ||
|
||
private: | ||
Container::Pointer m_ptr; | ||
}; | ||
} // namespace RED4ext::Detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.