3700 FPS here we go

This commit is contained in:
Dynamitos
2021-04-13 23:09:16 +02:00
parent d5f0fe644f
commit 312ae2af9a
36 changed files with 401 additions and 231 deletions
+43 -28
View File
@@ -12,10 +12,14 @@
namespace Seele
{
enum class Init_t {
NO_INIT
};
template <typename T>
struct Array
{
public:
Array()
: arraySize(0)
, allocated(DEFAULT_ALLOC_SIZE)
@@ -23,7 +27,15 @@ namespace Seele
_data = new T[DEFAULT_ALLOC_SIZE];
assert(_data != nullptr);
//std::memset(_data, 0, sizeof(T) * DEFAULT_ALLOC_SIZE);
refreshIterators();
markIteratorDirty();
}
Array(Init_t)
: arraySize(0)
, allocated(0)
, _data(nullptr)
, beginIt(Iterator(nullptr))
, endIt(Iterator(nullptr))
{
}
Array(size_t size, T value = T())
: arraySize(size)
@@ -36,7 +48,7 @@ namespace Seele
assert(i < size);
_data[i] = value;
}
refreshIterators();
markIteratorDirty();
}
Array(std::initializer_list<T> init)
: arraySize(init.size())
@@ -49,7 +61,7 @@ namespace Seele
assert(i < init.size());
_data[i] = *it;
}
refreshIterators();
markIteratorDirty();
}
Array(const Array &other)
: arraySize(other.arraySize)
@@ -57,7 +69,7 @@ namespace Seele
{
_data = new T[other.allocated];
assert(_data != nullptr);
refreshIterators();
markIteratorDirty();
std::copy(other.begin(), other.end(), beginIt);
}
Array(Array &&other) noexcept
@@ -68,7 +80,7 @@ namespace Seele
other._data = nullptr;
other.allocated = 0;
other.arraySize = 0;
refreshIterators();
markIteratorDirty();
}
Array &operator=(const Array &other) noexcept
{
@@ -76,12 +88,15 @@ namespace Seele
{
if (_data != nullptr)
{
delete[] _data;
if(other.arraySize > allocated)
{
delete[] _data;
_data = new T[other.allocated];
allocated = other.allocated;
}
}
allocated = other.allocated;
arraySize = other.arraySize;
_data = new T[other.allocated];
refreshIterators();
markIteratorDirty();
std::copy(other.begin(), other.end(), beginIt);
}
return *this;
@@ -93,12 +108,13 @@ namespace Seele
if (_data != nullptr)
{
delete[] _data;
_data = nullptr;
}
allocated = std::move(other.allocated);
arraySize = std::move(other.arraySize);
_data = other._data;
other._data = nullptr;
refreshIterators();
markIteratorDirty();
}
return *this;
}
@@ -132,15 +148,15 @@ namespace Seele
{
return p;
}
inline bool operator!=(const IteratorBase &other)
inline bool operator!=(const IteratorBase &other) const
{
return p != other.p;
}
inline bool operator==(const IteratorBase &other)
inline bool operator==(const IteratorBase &other) const
{
return p == other.p;
}
inline int operator-(const IteratorBase &other)
inline int operator-(const IteratorBase &other) const
{
return (int)(p - other.p);
}
@@ -238,7 +254,7 @@ namespace Seele
_data = tempArray;
}
_data[arraySize++] = item;
refreshIterators();
markIteratorDirty();
return _data[arraySize - 1];
}
T &add(T&& item)
@@ -257,7 +273,7 @@ namespace Seele
_data = tempArray;
}
_data[arraySize++] = std::move(item);
refreshIterators();
markIteratorDirty();
return _data[arraySize - 1];
}
T &addUnique(const T &item = T())
@@ -286,7 +302,7 @@ namespace Seele
_data = tempArray;
}
_data[arraySize++] = T(arguments...);
refreshIterators();
markIteratorDirty();
return _data[arraySize - 1];
}
void remove(Iterator it, bool keepOrder = true)
@@ -314,7 +330,7 @@ namespace Seele
_data = nullptr;
arraySize = 0;
allocated = 0;
refreshIterators();
markIteratorDirty();
}
void resize(size_t newSize)
{
@@ -335,7 +351,7 @@ namespace Seele
delete _data;
_data = newData;
}
refreshIterators();
markIteratorDirty();
}
inline size_t indexOf(Iterator iterator)
{
@@ -369,21 +385,21 @@ namespace Seele
{
return _data;
}
T &back() const
inline T &back() const
{
return _data[arraySize - 1];
}
void pop()
{
arraySize--;
refreshIterators();
markIteratorDirty();
}
T &operator[](size_t index)
constexpr inline T &operator[](size_t index)
{
assert(index < arraySize);
return _data[index];
}
const T &operator[](size_t index) const
constexpr inline const T &operator[](size_t index) const
{
assert(index < arraySize);
return _data[index];
@@ -407,7 +423,7 @@ namespace Seele
return geometric; // geometric growth is sufficient
}
void refreshIterators()
void markIteratorDirty()
{
beginIt = Iterator(_data);
endIt = Iterator(_data + arraySize);
@@ -416,18 +432,17 @@ namespace Seele
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & version;
ar & arraySize;
resize(arraySize);
for(size_t i = 0; i < arraySize; ++i)
ar & _data[i];
refreshIterators();
markIteratorDirty();
}
size_t arraySize;
size_t allocated;
size_t arraySize = 0;
size_t allocated = 0;
Iterator beginIt;
Iterator endIt;
T *_data;
T *_data = nullptr;
};
template <typename T, size_t N>
+5 -5
View File
@@ -203,7 +203,7 @@ public:
tail->next = newTail;
Iterator insertedElement(tail);
tail = newTail;
refreshIterators();
markIteratorDirty();
_size++;
return insertedElement;
}
@@ -221,7 +221,7 @@ public:
tail->next = newTail;
Iterator insertedElement(tail);
tail = newTail;
refreshIterators();
markIteratorDirty();
_size++;
return insertedElement;
}
@@ -247,7 +247,7 @@ public:
next->prev = prev;
}
delete pos.node;
refreshIterators();
markIteratorDirty();
return Iterator(next);
}
void popBack()
@@ -270,7 +270,7 @@ public:
root->prev = nullptr;
tail->prev = root;
tail->next = nullptr;
refreshIterators();
markIteratorDirty();
return beginIt;
}
Node *tmp = pos.node->prev;
@@ -319,7 +319,7 @@ public:
}
private:
void refreshIterators()
void markIteratorDirty()
{
beginIt = Iterator(root);
endIt = Iterator(tail);
+68 -22
View File
@@ -110,16 +110,22 @@ private:
public:
Map()
: root(nullptr), _size(0)
: root(nullptr)
, _size(0)
, iteratorsDirty(false)
, beginIt(nullptr)
, endIt(nullptr)
{
}
Map(const Map& other)
: root(new Node(*other.root)), _size(other._size)
: root(new Node(*other.root))
, _size(other._size)
{
refreshIterators();
}
Map(Map&& other)
: root(std::move(other.root)), _size(other._size)
: root(std::move(other.root))
, _size(other._size)
{
refreshIterators();
}
@@ -137,7 +143,7 @@ public:
}
root = new Node(*other.root);
_size = other.size;
refreshIterators();
markIteratorDirty();
}
return *this;
}
@@ -151,21 +157,21 @@ public:
}
root = new Node(std::move(*other.root));
_size = std::move(other._size);
refreshIterators();
markIteratorDirty();
}
return *this;
}
class Iterator
{
public:
typedef std::bidirectional_iterator_tag iterator_category;
typedef Pair<K, V> value_type;
typedef std::ptrdiff_t difference_type;
typedef Pair<K, V> &reference;
typedef Pair<K, V> *pointer;
using iterator_category = std::bidirectional_iterator_tag;
using value_type = Pair<K, V>;
using difference_type = std::ptrdiff_t;
using reference = Pair<K, V>&;
using pointer = Pair<K, V>*;
Iterator(Node *x = nullptr)
: node(x)
: node(x), traversal(Init_t::NO_INIT)
{
}
Iterator(Node *x, Array<Node *> &&beginIt)
@@ -261,31 +267,32 @@ public:
Node *node;
Array<Node *> traversal;
};
V &operator[](const K& key)
inline V &operator[](const K& key)
{
root = splay(root, key);
markIteratorDirty();
if (root == nullptr || root->pair.key < key || key < root->pair.key)
{
root = insert(root, key);
_size++;
refreshIterators();
}
return root->pair.value;
}
V &operator[](K&& key)
inline V &operator[](K&& key)
{
root = splay(root, std::forward<K>(key));
markIteratorDirty();
if (root == nullptr || root->pair.key < key || key < root->pair.key)
{
root = insert(root, std::forward<K>(key));
_size++;
refreshIterators();
}
return root->pair.value;
}
Iterator find(const K& key)
{
root = splay(root, key);
markIteratorDirty();
if (root == nullptr || root->pair.key != key)
{
return endIt;
@@ -295,6 +302,7 @@ public:
Iterator find(K&& key)
{
root = splay(root, std::move(key));
markIteratorDirty();
if (root == nullptr || root->pair.key != key)
{
return endIt;
@@ -304,13 +312,13 @@ public:
Iterator erase(const K& key)
{
root = remove(root, key);
refreshIterators();
markIteratorDirty();
return Iterator(root);
}
Iterator erase(K&& key)
{
root = remove(root, std::move(key));
refreshIterators();
markIteratorDirty();
return Iterator(root);
}
void clear()
@@ -318,18 +326,42 @@ public:
delete root;
root = nullptr;
_size = 0;
refreshIterators();
markIteratorDirty();
}
bool exists(K&& key)
{
return find(std::forward<K>(key)) != endIt;
}
Iterator begin()
{
if(iteratorsDirty)
{
refreshIterators();
}
return beginIt;
}
Iterator end()
{
if(iteratorsDirty)
{
refreshIterators();
}
return endIt;
}
Iterator begin() const
{
if(iteratorsDirty)
{
return calcBeginIterator();
}
return beginIt;
}
Iterator end() const
{
if(iteratorsDirty)
{
return calcEndIterator();
}
return endIt;
}
bool empty() const
@@ -342,12 +374,22 @@ public:
}
private:
void markIteratorDirty()
{
iteratorsDirty = true;
}
void refreshIterators()
{
beginIt = calcBeginIterator();
endIt = calcEndIterator();
iteratorsDirty = false;
}
inline Iterator calcBeginIterator() const
{
Node *beginNode = root;
if (root == nullptr)
{
beginIt = Iterator(nullptr);
return Iterator(nullptr);
}
else
{
@@ -359,12 +401,15 @@ private:
}
beginNode = beginTraversal.back();
beginTraversal.pop();
beginIt = Iterator(beginNode, std::move(beginTraversal));
return Iterator(beginNode, std::move(beginTraversal));
}
}
inline Iterator calcEndIterator() const
{
Node *endNode = root;
if (root == nullptr)
{
endIt = Iterator(nullptr);
return Iterator(nullptr);
}
else
{
@@ -374,12 +419,13 @@ private:
endTraversal.add(endNode);
endNode = endNode->rightChild;
}
endIt = Iterator(endNode, std::move(endTraversal));
return Iterator(endNode, std::move(endTraversal));
}
}
Node *root;
Iterator beginIt;
Iterator endIt;
bool iteratorsDirty;
uint32 _size;
Node *rotateRight(Node *node)
{