Trying ttf loading

This commit is contained in:
Dynamitos
2022-04-13 13:01:46 +02:00
parent bebfb94752
commit 3fec36295a
42 changed files with 563 additions and 549 deletions
+97 -41
View File
@@ -4,6 +4,7 @@
#include <iterator>
#include <assert.h>
#include <memory_resource>
#include <algorithm>
#include <boost/serialization/serialization.hpp>
#ifndef DEFAULT_ALLOC_SIZE
@@ -103,7 +104,7 @@ public:
markIteratorDirty();
}
constexpr explicit Array(const allocator_type& alloc)
constexpr explicit Array(const allocator_type& alloc) noexcept
: arraySize(0)
, allocated(DEFAULT_ALLOC_SIZE)
, allocator(alloc)
@@ -169,7 +170,7 @@ public:
other.arraySize = 0;
markIteratorDirty();
}
Array &operator=(const Array &other) noexcept
Array &operator=(const Array &other)
{
if (this != &other)
{
@@ -197,7 +198,8 @@ public:
}
return *this;
}
Array &operator=(Array &&other) noexcept
Array &operator=(Array &&other) noexcept(std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value
|| std::allocator_traits<Allocator>::is_always_equal::value)
{
if (this != &other)
{
@@ -217,24 +219,14 @@ public:
}
return *this;
}
~Array()
constexpr ~Array()
{
clear();
}
constexpr bool operator==(const Array &other)
[[nodiscard]]
constexpr iterator find(const value_type &item) noexcept
{
return _data == other._data;
}
constexpr bool operator!=(const Array &other)
{
return !(*this == other);
}
constexpr iterator find(const value_type &item)
{
for (uint32 i = 0; i < arraySize; ++i)
for (size_type i = 0; i < arraySize; ++i)
{
if (_data[i] == item)
{
@@ -243,9 +235,10 @@ public:
}
return endIt;
}
constexpr iterator find(value_type&& item)
[[nodiscard]]
constexpr iterator find(value_type&& item) noexcept
{
for (uint32 i = 0; i < arraySize; ++i)
for (size_type i = 0; i < arraySize; ++i)
{
if (_data[i] == item)
{
@@ -254,23 +247,36 @@ public:
}
return endIt;
}
constexpr allocator_type get_allocator() const
//template<class F, class... Args, std::predicate<F, Args...> Pred>
template<class Pred>
constexpr iterator find(Pred pred) const noexcept
{
for (size_type i = 0; i < arraySize; ++i)
{
if(pred(_data[i]))
{
return iterator(&_data[i]);
}
}
return endIt;
}
constexpr allocator_type get_allocator() const noexcept
{
return allocator;
}
constexpr iterator begin() const
constexpr iterator begin() const noexcept
{
return beginIt;
}
constexpr iterator end() const
constexpr iterator end() const noexcept
{
return endIt;
}
constexpr const_iterator cbegin() const
constexpr const_iterator cbegin() const noexcept
{
return beginIt;
}
constexpr const_iterator cend() const
constexpr const_iterator cend() const noexcept
{
return endIt;
}
@@ -316,6 +322,19 @@ public:
markIteratorDirty();
return _data[arraySize - 1];
}
template<std::predicate Pred>
constexpr void remove_if(Pred pred, bool keepOrder = true)
{
remove(find(pred), keepOrder);
}
constexpr void remove(const value_type& element, bool keepOrder = true)
{
remove(find(element), keepOrder);
}
constexpr void remove(value_type&& element, bool keepOrder = true)
{
remove(find(element), keepOrder);
}
constexpr void remove(iterator it, bool keepOrder = true)
{
remove(it - beginIt, keepOrder);
@@ -324,7 +343,7 @@ public:
{
if (keepOrder)
{
for(uint32 i = index; i < arraySize-1; ++i)
for(size_type i = index; i < arraySize-1; ++i)
{
_data[i] = std::move(_data[i+1]);
}
@@ -344,7 +363,7 @@ public:
{
resizeInternal(newSize, value);
}
constexpr void clear()
constexpr void clear() noexcept
{
if(_data == nullptr)
{
@@ -360,59 +379,71 @@ public:
allocated = 0;
markIteratorDirty();
}
inline size_type indexOf(iterator iterator)
constexpr size_type indexOf(iterator iterator)
{
return iterator - beginIt;
}
inline size_type indexOf(const_iterator iterator) const
constexpr size_type indexOf(const_iterator iterator) const
{
return iterator.p - beginIt.p;
}
inline size_type indexOf(T& t)
constexpr size_type indexOf(T& t)
{
return indexOf(find(t));
}
inline size_type indexOf(const T& t) const
constexpr size_type indexOf(const T& t) const
{
return indexOf(find(t));
}
inline size_type size() const
constexpr size_type size() const noexcept
{
return arraySize;
}
inline size_type empty() const
[[nodiscard]]
constexpr bool empty() const noexcept
{
return arraySize == 0;
}
inline size_type capacity() const
constexpr void reserve(size_type new_cap)
{
if(new_cap > allocated)
{
T* temp = allocateArray(new_cap);
std::uninitialized_move_n(beginIt, arraySize, temp);
_data = temp;
markIteratorDirty();
}
allocated = new_cap;
}
constexpr size_type capacity() const noexcept
{
return allocated;
}
inline pointer data() const
constexpr pointer data() const noexcept
{
return _data;
}
inline reference front() const
constexpr reference front() const
{
assert(arraySize > 0);
return _data[0];
}
inline reference back() const
constexpr reference back() const
{
assert(arraySize > 0);
return _data[arraySize - 1];
}
void pop()
constexpr void pop()
{
std::allocator_traits<allocator_type>::destroy(allocator, &_data[--arraySize]);
markIteratorDirty();
}
constexpr inline reference operator[](size_type index)
constexpr reference operator[](size_type index)
{
assert(index < arraySize);
return _data[index];
}
constexpr inline const reference operator[](size_type index) const
constexpr const reference operator[](size_type index) const
{
assert(index < arraySize);
return _data[index];
@@ -441,6 +472,7 @@ private:
beginIt = Iterator(_data);
endIt = Iterator(_data + arraySize);
}
[[nodiscard]]
T* allocateArray(size_type size)
{
T* result = allocator.allocate(size);
@@ -452,7 +484,7 @@ private:
allocator.deallocate(ptr, size);
}
template<typename Type>
T& addInternal(Type&& t)
T& addInternal(Type&& t) noexcept
{
if (arraySize == allocated)
{
@@ -471,7 +503,7 @@ private:
return _data[arraySize - 1];
}
template<typename Type>
void resizeInternal(size_type newSize, Type&& value)
void resizeInternal(size_type newSize, Type&& value) noexcept
{
if (newSize <= allocated)
{
@@ -534,6 +566,30 @@ private:
allocator_type allocator;
};
template<class Type, class Alloc>
constexpr bool operator==(const Array<Type, Alloc> &lhs, const Array<Type, Alloc>& rhs)
{
if(lhs.size() != rhs.size())
{
return false;
}
for(auto it1 = lhs.begin(), it2 = rhs.begin(); it1 != lhs.end() && it2 != rhs.end(); ++it1, ++it2)
{
if(*it1 != *it2)
{
return false;
}
}
return true;
}
template<class Type, class Alloc>
constexpr auto operator<=>(const Array<Type, Alloc>& lhs, const Array<Type, Alloc>& rhs)
{
return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
template <typename T, size_t N>
struct StaticArray
{
-414
View File
@@ -1,414 +0,0 @@
#pragma once
#include "MinimalEngine.h"
#include "Array.h"
namespace Seele
{
template<typename T, Allocator = std::pmr::polymorphic_allocator<T>>
class RingBuffer
{
public:
template <typename X>
class IteratorBase
{
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = X;
using difference_type = std::ptrdiff_t;
using reference = X&;
using pointer = X*;
IteratorBase(Array<T>& arr, size_t index)
: arr(arr)
, index(index)
{
}
reference operator*() const
{
return arr[index];
}
pointer operator->() const
{
return &arr[index];
}
inline bool operator!=(const IteratorBase &other) const
{
return arr != other.arr && index != other.index;
}
inline bool operator==(const IteratorBase &other) const
{
return arr == other.arr && index == other.index;
}
inline int operator-(const IteratorBase &other) const
{
return (int)(index - other.index);
}
IteratorBase &operator++()
{
index = (index + 1) % arr.size();
return *this;
}
IteratorBase &operator--()
{
index = (index + 1) % arr.size();
return *this;
}
IteratorBase operator++(int)
{
IteratorBase tmp(*this);
++*this;
return tmp;
}
IteratorBase operator--(int)
{
IteratorBase tmp(*this);
--*this;
return tmp;
}
private:
Array<T, Allocator>& arr;
size_t index;
};
using value_type = T;
using allocator_type = Allocator;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using const_pointer = const T*;
using reference = value_type&;
using const_reference = const value_type&;
using Iterator = IteratorBase<T>;
using ConstIterator = IteratorBase<const T>;
using iterator = Iterator;
using const_iterator = ConstIterator;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
constexpr RingBuffer() noexcept(noexcept(Allocator()))
{
refreshIterators();
}
constexpr explicit RingBuffer(const allocator_type& alloc)
: data(alloc)
{
refreshIterators();
}
constexpr RingBuffer(size_type size, const value_type& value, const allocator_type& alloc = allocator_type())
: data(size, value, alloc)
, end(size)
{
refreshIterators();
}
constexpr explicit RingBuffer(size_type size, const allocator_type& alloc = allocator_type())
: arraySize(size)
, allocated(size)
, allocator(alloc)
{
_data = allocateRingBuffer(size);
assert(_data != nullptr);
for (size_type i = 0; i < size; ++i)
{
std::allocator_traits<allocator_type>::construct(allocator, &_data[i]);
}
markIteratorDirty();
}
constexpr RingBuffer(std::initializer_list<T> init, const allocator_type& alloc = allocator_type())
: arraySize(init.size())
, allocated(init.size())
, allocator(alloc)
{
_data = allocateRingBuffer(init.size());
assert(_data != nullptr);
markIteratorDirty();
std::uninitialized_copy(init.begin(), init.end(), begin());
}
RingBuffer(const RingBuffer &other)
: arraySize(other.arraySize)
, allocated(other.allocated)
, allocator(std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.allocator))
{
_data = allocateRingBuffer(other.allocated);
assert(_data != nullptr);
markIteratorDirty();
std::uninitialized_copy(other.begin(), other.end(), begin());
}
RingBuffer(RingBuffer &&other) noexcept
: arraySize(std::move(other.arraySize))
, allocated(std::move(other.allocated))
, allocator(std::move(other.allocator))
{
_data = other._data;
other._data = nullptr;
other.allocated = 0;
other.arraySize = 0;
markIteratorDirty();
}
RingBuffer &operator=(const RingBuffer &other) noexcept
{
if (this != &other)
{
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value )
{
if (!std::allocator_traits<allocator_type>::is_always_equal::value
&& allocator != other.allocator)
{
clear();
}
allocator = other.allocator;
}
if (other.arraySize > allocated)
{
clear();
}
if(_data == nullptr)
{
_data = allocateRingBuffer(other.allocated);
allocated = other.allocated;
}
arraySize = other.arraySize;
markIteratorDirty();
std::uninitialized_copy(other.begin(), other.end(), begin());
}
return *this;
}
RingBuffer &operator=(RingBuffer &&other) noexcept
{
if (this != &other)
{
if constexpr (std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value)
{
allocator = std::move(other.allocator);
}
if (_data != nullptr)
{
clear();
}
allocated = std::move(other.allocated);
arraySize = std::move(other.arraySize);
_data = other._data;
other._data = nullptr;
markIteratorDirty();
}
return *this;
}
~RingBuffer()
{
clear();
}
constexpr bool operator==(const RingBuffer &other)
{
return _data == other._data;
}
constexpr bool operator!=(const RingBuffer &other)
{
return !(*this == other);
}
constexpr iterator find(const value_type &item)
{
for (uint32 i = 0; i < arraySize; ++i)
{
if (_data[i] == item)
{
return iterator(&_data[i]);
}
}
return endIt;
}
constexpr iterator find(value_type&& item)
{
for (uint32 i = 0; i < arraySize; ++i)
{
if (_data[i] == item)
{
return iterator(&_data[i]);
}
}
return endIt;
}
constexpr allocator_type get_allocator() const
{
return allocator;
}
constexpr iterator begin() const
{
return beginIt;
}
constexpr iterator end() const
{
return endIt;
}
constexpr const_iterator cbegin() const
{
return beginIt;
}
constexpr const_iterator cend() const
{
return endIt;
}
constexpr reference add(const value_type &item = value_type())
{
return addInternal(item);
}
constexpr reference add(value_type&& item)
{
return addInternal(std::forward<T>(item));
}
constexpr void addAll(RingBuffer other)
{
for(auto value : other)
{
addInternal(value);
}
}
constexpr reference addUnique(const value_type &item = value_type())
{
iterator it;
if((it = std::move(find(item))) != endIt)
{
return *it;
}
return addInternal(item);
}
template<typename... args>
constexpr reference emplace(args... arguments)
{
if (arraySize == allocated)
{
size_type newSize = arraySize + 1;
allocated = calculateGrowth(newSize);
T *tempRingBuffer = allocateRingBuffer(allocated);
assert(tempRingBuffer != nullptr);
std::uninitialized_move(begin(), end(), Iterator(tempRingBuffer));
deallocateRingBuffer(_data, arraySize);
_data = tempRingBuffer;
}
std::allocator_traits<allocator_type>::construct(allocator, &_data[arraySize++], arguments...);
markIteratorDirty();
return _data[arraySize - 1];
}
constexpr void remove(iterator it, bool keepOrder = true)
{
remove(it - beginIt, keepOrder);
}
constexpr void remove(size_type index, bool keepOrder = true)
{
if (keepOrder)
{
for(uint32 i = index; i < arraySize-1; ++i)
{
_data[i] = std::move(_data[i+1]);
}
}
else
{
_data[index] = std::move(_data[arraySize - 1]);
}
std::allocator_traits<allocator_type>::destroy(allocator, &_data[--arraySize]);
markIteratorDirty();
}
constexpr void resize(size_type newSize)
{
resizeInternal(newSize, std::move(T()));
}
constexpr void resize(size_type newSize, const value_type& value)
{
resizeInternal(newSize, value);
}
constexpr void clear()
{
if(_data == nullptr)
{
return;
}
for(size_type i = 0; i < arraySize; ++i)
{
std::allocator_traits<allocator_type>::destroy(allocator, &_data[i]);
}
deallocateRingBuffer(_data, allocated);
_data = nullptr;
arraySize = 0;
allocated = 0;
markIteratorDirty();
}
inline size_type indexOf(iterator iterator)
{
return iterator - beginIt;
}
inline size_type indexOf(const_iterator iterator) const
{
return iterator.p - beginIt.p;
}
inline size_type indexOf(T& t)
{
return indexOf(find(t));
}
inline size_type indexOf(const T& t) const
{
return indexOf(find(t));
}
inline size_type size() const
{
return arraySize;
}
inline size_type empty() const
{
return arraySize == 0;
}
inline size_type capacity() const
{
return allocated;
}
inline pointer data() const
{
return _data;
}
inline reference front() const
{
assert(arraySize > 0);
return _data[0];
}
inline reference back() const
{
assert(arraySize > 0);
return _data[arraySize - 1];
}
void pop()
{
std::allocator_traits<allocator_type>::destroy(allocator, &_data[--arraySize]);
markIteratorDirty();
}
constexpr inline reference operator[](size_type index)
{
assert(index < arraySize);
return _data[index];
}
constexpr inline const reference operator[](size_type index) const
{
assert(index < arraySize);
return _data[index];
}
private:
void refreshIterators()
{
beginIt = Iterator(data, begin);
endIt = Iterator(data, end);
}
size_type begin = 0;
size_type end = 0;
iterator beginIt;
iterator endIt;
Array<T, Allocator> data;
};
class StaticRingBuffer
{
};
} // namespace Seele