Provisional multithreading framework
This commit is contained in:
+126
-100
@@ -25,12 +25,12 @@ namespace Seele
|
||||
//std::memset(_data, 0, sizeof(T) * DEFAULT_ALLOC_SIZE);
|
||||
refreshIterators();
|
||||
}
|
||||
Array(uint32 size, T value = T())
|
||||
Array(size_t size, T value = T())
|
||||
: allocated(size), arraySize(size)
|
||||
{
|
||||
_data = new T[size];
|
||||
assert(_data != nullptr);
|
||||
for (uint32 i = 0; i < size; ++i)
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
{
|
||||
assert(i < size);
|
||||
_data[i] = value;
|
||||
@@ -38,7 +38,7 @@ namespace Seele
|
||||
refreshIterators();
|
||||
}
|
||||
Array(std::initializer_list<T> init)
|
||||
: allocated((uint32)init.size()), arraySize((uint32)init.size())
|
||||
: allocated(init.size()), arraySize(init.size())
|
||||
{
|
||||
_data = new T[init.size()];
|
||||
auto it = init.begin();
|
||||
@@ -194,6 +194,17 @@ namespace Seele
|
||||
}
|
||||
return endIt;
|
||||
}
|
||||
Iterator find(T&& item)
|
||||
{
|
||||
for (uint32 i = 0; i < arraySize; ++i)
|
||||
{
|
||||
if (_data[i] == item)
|
||||
{
|
||||
return Iterator(&_data[i]);
|
||||
}
|
||||
}
|
||||
return endIt;
|
||||
}
|
||||
Iterator begin() const
|
||||
{
|
||||
return beginIt;
|
||||
@@ -215,11 +226,11 @@ namespace Seele
|
||||
{
|
||||
if (arraySize == allocated)
|
||||
{
|
||||
uint32 newSize = arraySize + 1;
|
||||
size_t newSize = arraySize + 1;
|
||||
allocated = calculateGrowth(newSize);
|
||||
T *tempArray = new T[allocated];
|
||||
assert(tempArray != nullptr);
|
||||
for (uint32 i = 0; i < arraySize; ++i)
|
||||
for (size_t i = 0; i < arraySize; ++i)
|
||||
{
|
||||
tempArray[i] = _data[i];
|
||||
}
|
||||
@@ -230,6 +241,25 @@ namespace Seele
|
||||
refreshIterators();
|
||||
return _data[arraySize - 1];
|
||||
}
|
||||
T &add(T&& item)
|
||||
{
|
||||
if (arraySize == allocated)
|
||||
{
|
||||
size_t newSize = arraySize + 1;
|
||||
allocated = calculateGrowth(newSize);
|
||||
T *tempArray = new T[allocated];
|
||||
assert(tempArray != nullptr);
|
||||
for (size_t i = 0; i < arraySize; ++i)
|
||||
{
|
||||
tempArray[i] = std::move(_data[i]);
|
||||
}
|
||||
delete[] _data;
|
||||
_data = tempArray;
|
||||
}
|
||||
_data[arraySize++] = std::move(item);
|
||||
refreshIterators();
|
||||
return _data[arraySize - 1];
|
||||
}
|
||||
T &addUnique(const T &item = T())
|
||||
{
|
||||
Iterator it;
|
||||
@@ -240,22 +270,22 @@ namespace Seele
|
||||
return add(item);
|
||||
}
|
||||
template<typename... args>
|
||||
T &emplace(args...)
|
||||
T &emplace(args... arguments)
|
||||
{
|
||||
if (arraySize == allocated)
|
||||
{
|
||||
uint32 newSize = arraySize + 1;
|
||||
size_t newSize = arraySize + 1;
|
||||
allocated = calculateGrowth(newSize);
|
||||
T *tempArray = new T[allocated];
|
||||
assert(tempArray != nullptr);
|
||||
for (uint32 i = 0; i < arraySize; ++i)
|
||||
for (size_t i = 0; i < arraySize; ++i)
|
||||
{
|
||||
tempArray[i] = _data[i];
|
||||
}
|
||||
delete[] _data;
|
||||
_data = tempArray;
|
||||
}
|
||||
_data[arraySize++] = T(args...);
|
||||
_data[arraySize++] = T(arguments...);
|
||||
refreshIterators();
|
||||
return _data[arraySize - 1];
|
||||
}
|
||||
@@ -271,7 +301,7 @@ namespace Seele
|
||||
}
|
||||
else
|
||||
{
|
||||
_data[index] = _data[arraySize - 1];
|
||||
_data[index] = std::move(_data[arraySize - 1]);
|
||||
}
|
||||
arraySize--;
|
||||
}
|
||||
@@ -283,7 +313,7 @@ namespace Seele
|
||||
allocated = 0;
|
||||
refreshIterators();
|
||||
}
|
||||
void resize(uint32 newSize)
|
||||
void resize(size_t newSize)
|
||||
{
|
||||
if (newSize < allocated)
|
||||
{
|
||||
@@ -301,27 +331,31 @@ namespace Seele
|
||||
}
|
||||
refreshIterators();
|
||||
}
|
||||
inline uint32 indexOf(Iterator iterator)
|
||||
inline size_t indexOf(Iterator iterator)
|
||||
{
|
||||
return iterator - beginIt;
|
||||
}
|
||||
inline uint32 indexOf(ConstIterator iterator) const
|
||||
inline size_t indexOf(ConstIterator iterator) const
|
||||
{
|
||||
return iterator.p - beginIt.p;
|
||||
}
|
||||
inline uint32 indexOf(T& t)
|
||||
inline size_t indexOf(T& t)
|
||||
{
|
||||
return indexOf(find(t));
|
||||
}
|
||||
inline uint32 indexOf(const T& t) const
|
||||
inline size_t indexOf(const T& t) const
|
||||
{
|
||||
return indexOf(find(t));
|
||||
}
|
||||
inline uint32 size() const
|
||||
inline size_t size() const
|
||||
{
|
||||
return arraySize;
|
||||
}
|
||||
inline uint32 capacity() const
|
||||
inline size_t empty() const
|
||||
{
|
||||
return arraySize == 0;
|
||||
}
|
||||
inline size_t capacity() const
|
||||
{
|
||||
return allocated;
|
||||
}
|
||||
@@ -338,44 +372,27 @@ namespace Seele
|
||||
arraySize--;
|
||||
refreshIterators();
|
||||
}
|
||||
T &operator[](uint32 index)
|
||||
T &operator[](size_t index)
|
||||
{
|
||||
assert(index < arraySize);
|
||||
return _data[index];
|
||||
}
|
||||
inline T &operator[](size_t index)
|
||||
{
|
||||
return this->operator[]((uint32)index);
|
||||
}
|
||||
inline T &operator[](int32 index)
|
||||
{
|
||||
return this->operator[]((uint32)index);
|
||||
}
|
||||
const T &operator[](uint32 index) const
|
||||
const T &operator[](size_t index) const
|
||||
{
|
||||
assert(index < arraySize);
|
||||
return _data[index];
|
||||
}
|
||||
inline const T &operator[](size_t index) const
|
||||
{
|
||||
return this->operator[]((uint32)index);
|
||||
}
|
||||
inline const T &operator[](int32 index) const
|
||||
{
|
||||
return this->operator[]((uint32)index);
|
||||
}
|
||||
|
||||
private:
|
||||
uint32 calculateGrowth(uint32 newSize) const
|
||||
size_t calculateGrowth(size_t newSize) const
|
||||
{
|
||||
const uint32 oldCapacity = capacity();
|
||||
const size_t oldCapacity = capacity();
|
||||
|
||||
if (oldCapacity > UINT32_MAX - oldCapacity / 2)
|
||||
if (oldCapacity > SIZE_MAX - oldCapacity / 2)
|
||||
{
|
||||
return newSize; // geometric growth would overflow
|
||||
}
|
||||
|
||||
const uint32 geometric = oldCapacity + oldCapacity / 2;
|
||||
const size_t geometric = oldCapacity + oldCapacity / 2;
|
||||
|
||||
if (geometric < newSize)
|
||||
{
|
||||
@@ -395,71 +412,30 @@ namespace Seele
|
||||
{
|
||||
ar & arraySize;
|
||||
ar & allocated;
|
||||
for(uint32 i = 0; i < arraySize; ++i)
|
||||
for(size_t i = 0; i < arraySize; ++i)
|
||||
ar & _data[i];
|
||||
refreshIterators();
|
||||
}
|
||||
uint32 arraySize;
|
||||
uint32 allocated;
|
||||
size_t arraySize;
|
||||
size_t allocated;
|
||||
Iterator beginIt;
|
||||
Iterator endIt;
|
||||
T *_data;
|
||||
};
|
||||
|
||||
template <typename T, uint32 N>
|
||||
template <typename T, size_t N>
|
||||
struct StaticArray
|
||||
{
|
||||
public:
|
||||
StaticArray()
|
||||
{
|
||||
beginIt = Iterator(_data);
|
||||
endIt = Iterator(_data + N);
|
||||
}
|
||||
StaticArray(T value)
|
||||
{
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
_data[i] = value;
|
||||
}
|
||||
beginIt = Iterator(_data);
|
||||
endIt = Iterator(_data + N);
|
||||
}
|
||||
~StaticArray()
|
||||
{
|
||||
}
|
||||
|
||||
inline uint32 size() const
|
||||
{
|
||||
return N;
|
||||
}
|
||||
inline T *data()
|
||||
{
|
||||
return _data;
|
||||
}
|
||||
inline const T *data() const
|
||||
{
|
||||
return _data;
|
||||
}
|
||||
T &operator[](int index)
|
||||
{
|
||||
assert(index >= 0 && index < N);
|
||||
return _data[index];
|
||||
}
|
||||
const T &operator[](int index) const
|
||||
{
|
||||
assert(index >= 0 && index < N);
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
public:
|
||||
template <typename X>
|
||||
class IteratorBase
|
||||
{
|
||||
public:
|
||||
typedef std::forward_iterator_tag iterator_category;
|
||||
typedef X value_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
typedef X &reference;
|
||||
typedef X *pointer;
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using value_type = X;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using reference = X&;
|
||||
using pointer = X*;
|
||||
|
||||
IteratorBase(X *x = nullptr)
|
||||
: p(x)
|
||||
@@ -500,36 +476,86 @@ namespace Seele
|
||||
private:
|
||||
X *p;
|
||||
};
|
||||
typedef IteratorBase<T> Iterator;
|
||||
typedef IteratorBase<const T> ConstIterator;
|
||||
using value_type = T;
|
||||
using size_type = size_t;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = T*;
|
||||
using const_pointer = const T*;
|
||||
using reference = T&;
|
||||
using const_reference = const T&;
|
||||
|
||||
Iterator begin()
|
||||
using iterator = IteratorBase<T>;
|
||||
using const_iterator = IteratorBase<const T>;
|
||||
|
||||
using reverse_iterator = std::reverse_iterator<iterator>;
|
||||
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
||||
|
||||
StaticArray()
|
||||
{
|
||||
beginIt = iterator(_data);
|
||||
endIt = iterator(_data + N);
|
||||
}
|
||||
StaticArray(T value)
|
||||
{
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
_data[i] = value;
|
||||
}
|
||||
beginIt = iterator(_data);
|
||||
endIt = iterator(_data + N);
|
||||
}
|
||||
~StaticArray()
|
||||
{
|
||||
}
|
||||
|
||||
inline size_type size() const
|
||||
{
|
||||
return N;
|
||||
}
|
||||
inline pointer data()
|
||||
{
|
||||
return _data;
|
||||
}
|
||||
inline const_pointer data() const
|
||||
{
|
||||
return _data;
|
||||
}
|
||||
constexpr reference operator[](size_type index) noexcept
|
||||
{
|
||||
assert(index >= 0 && index < N);
|
||||
return _data[index];
|
||||
}
|
||||
constexpr const_reference operator[](size_type index) const noexcept
|
||||
{
|
||||
assert(index >= 0 && index < N);
|
||||
return _data[index];
|
||||
}
|
||||
iterator begin()
|
||||
{
|
||||
return beginIt;
|
||||
}
|
||||
Iterator end()
|
||||
iterator end()
|
||||
{
|
||||
return endIt;
|
||||
}
|
||||
ConstIterator begin() const
|
||||
const_iterator begin() const
|
||||
{
|
||||
return beginIt;
|
||||
}
|
||||
ConstIterator end() const
|
||||
const_iterator end() const
|
||||
{
|
||||
return beginIt;
|
||||
}
|
||||
private:
|
||||
T _data[N];
|
||||
Iterator beginIt;
|
||||
Iterator endIt;
|
||||
iterator beginIt;
|
||||
iterator endIt;
|
||||
friend class boost::serialization::access;
|
||||
template<class Archive>
|
||||
void serialize(Archive& ar, const unsigned int version)
|
||||
{
|
||||
ar & N;
|
||||
ar & _data;
|
||||
refreshIterators();
|
||||
}
|
||||
};
|
||||
} // namespace Seele
|
||||
@@ -250,6 +250,14 @@ public:
|
||||
refreshIterators();
|
||||
return Iterator(next);
|
||||
}
|
||||
void popBack()
|
||||
{
|
||||
remove(Iterator(tail->prev));
|
||||
}
|
||||
void popFront()
|
||||
{
|
||||
remove(Iterator(root));
|
||||
}
|
||||
Iterator insert(Iterator pos, const T &value)
|
||||
{
|
||||
_size++;
|
||||
|
||||
+56
-26
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <utility>
|
||||
#include "Array.h"
|
||||
|
||||
namespace Seele
|
||||
@@ -9,12 +10,15 @@ struct Pair
|
||||
public:
|
||||
Pair()
|
||||
: key(K()), value(V())
|
||||
{
|
||||
}
|
||||
Pair(K key, V value)
|
||||
: key(key), value(value)
|
||||
{
|
||||
}
|
||||
{}
|
||||
template<class KeyType>
|
||||
explicit Pair(KeyType&& key)
|
||||
: key(key), value(V())
|
||||
{}
|
||||
template<class KeyType, class ValueType>
|
||||
explicit Pair(KeyType&& key, ValueType&& value)
|
||||
: key(std::move(key)), value(std::move(value))
|
||||
{}
|
||||
K key;
|
||||
V value;
|
||||
};
|
||||
@@ -28,11 +32,12 @@ private:
|
||||
Node *leftChild;
|
||||
Node *rightChild;
|
||||
Node()
|
||||
: leftChild(nullptr), rightChild(nullptr), pair(K(), V())
|
||||
: leftChild(nullptr), rightChild(nullptr), pair()
|
||||
{
|
||||
}
|
||||
Node(const K &key)
|
||||
: leftChild(nullptr), rightChild(nullptr), pair(key, V())
|
||||
template<class KeyType, class ValueType>
|
||||
explicit Node(KeyType&& key, ValueType&& value)
|
||||
: leftChild(nullptr), rightChild(nullptr), pair(key, value)
|
||||
{
|
||||
}
|
||||
Node(const Node& other)
|
||||
@@ -242,7 +247,7 @@ public:
|
||||
Iterator operator--(int)
|
||||
{
|
||||
Iterator tmp(*this);
|
||||
++*this;
|
||||
--*this;
|
||||
return tmp;
|
||||
}
|
||||
Iterator operator++(int)
|
||||
@@ -256,8 +261,7 @@ public:
|
||||
Node *node;
|
||||
Array<Node *> traversal;
|
||||
};
|
||||
|
||||
V &operator[](const K &key)
|
||||
V &operator[](const K& key)
|
||||
{
|
||||
root = splay(root, key);
|
||||
if (root == nullptr || root->pair.key < key || key < root->pair.key)
|
||||
@@ -268,7 +272,18 @@ public:
|
||||
}
|
||||
return root->pair.value;
|
||||
}
|
||||
Iterator find(const K &key)
|
||||
V &operator[](K&& key)
|
||||
{
|
||||
root = splay(root, std::forward<K>(key));
|
||||
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);
|
||||
if (root == nullptr || root->pair.key != key)
|
||||
@@ -277,10 +292,25 @@ public:
|
||||
}
|
||||
return Iterator(root);
|
||||
}
|
||||
Iterator erase(const K &key)
|
||||
Iterator find(K&& key)
|
||||
{
|
||||
root = splay(root, std::move(key));
|
||||
if (root == nullptr || root->pair.key != key)
|
||||
{
|
||||
return endIt;
|
||||
}
|
||||
return Iterator(root);
|
||||
}
|
||||
Iterator erase(const K& key)
|
||||
{
|
||||
root = remove(root, key);
|
||||
refreshIterators();
|
||||
return Iterator(root);
|
||||
}
|
||||
Iterator erase(K&& key)
|
||||
{
|
||||
root = remove(root, std::move(key));
|
||||
refreshIterators();
|
||||
return Iterator(root);
|
||||
}
|
||||
void clear()
|
||||
@@ -290,9 +320,9 @@ public:
|
||||
_size = 0;
|
||||
refreshIterators();
|
||||
}
|
||||
bool exists(const K &key)
|
||||
bool exists(K&& key)
|
||||
{
|
||||
return find(key) != endIt;
|
||||
return find(std::forward<K>(key)) != endIt;
|
||||
}
|
||||
Iterator begin() const
|
||||
{
|
||||
@@ -365,21 +395,19 @@ private:
|
||||
y->leftChild = node;
|
||||
return y;
|
||||
}
|
||||
Node *makeNode(const K &key)
|
||||
{
|
||||
return new Node(key);
|
||||
}
|
||||
Node *insert(Node *r, const K &key)
|
||||
template<class KeyType>
|
||||
Node *insert(Node *r, KeyType&& key)
|
||||
{
|
||||
if (r == nullptr)
|
||||
return makeNode(key);
|
||||
|
||||
{
|
||||
return new Node(std::forward<KeyType>(key), V());
|
||||
}
|
||||
r = splay(r, key);
|
||||
|
||||
if (!(r->pair.key < key || key < r->pair.key))
|
||||
return r;
|
||||
|
||||
Node *newNode = makeNode(key);
|
||||
Node *newNode = new Node(std::forward<KeyType>(key), V());
|
||||
|
||||
if (key < r->pair.key)
|
||||
{
|
||||
@@ -395,7 +423,8 @@ private:
|
||||
}
|
||||
return newNode;
|
||||
}
|
||||
Node *remove(Node *r, const K &key)
|
||||
template<class KeyType>
|
||||
Node *remove(Node *r, KeyType&& key)
|
||||
{
|
||||
Node *temp;
|
||||
if (!r)
|
||||
@@ -424,7 +453,8 @@ private:
|
||||
delete temp;
|
||||
return r;
|
||||
}
|
||||
Node *splay(Node *r, const K &key)
|
||||
template<class KeyType>
|
||||
Node *splay(Node *r, KeyType&& key)
|
||||
{
|
||||
if (r == nullptr || !(r->pair.key < key || key < r->pair.key))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user