implementing renderpass and vieport
This commit is contained in:
+404
-358
@@ -8,389 +8,435 @@
|
||||
#define DEFAULT_ALLOC_SIZE 16
|
||||
#endif
|
||||
|
||||
namespace Seele
|
||||
namespace Seele
|
||||
{
|
||||
template<typename T>
|
||||
struct Array
|
||||
template <typename T>
|
||||
struct Array
|
||||
{
|
||||
public:
|
||||
Array()
|
||||
: allocated(DEFAULT_ALLOC_SIZE), arraySize(0)
|
||||
{
|
||||
public:
|
||||
Array()
|
||||
: allocated(DEFAULT_ALLOC_SIZE)
|
||||
, arraySize(0)
|
||||
_data = new T[DEFAULT_ALLOC_SIZE];
|
||||
assert(_data != nullptr);
|
||||
memset(_data, 0, sizeof(T) * DEFAULT_ALLOC_SIZE);
|
||||
refreshIterators();
|
||||
}
|
||||
Array(uint32 size, T value = T())
|
||||
: allocated(size), arraySize(size)
|
||||
{
|
||||
_data = new T[size];
|
||||
assert(_data != nullptr);
|
||||
for (uint32 i = 0; i < size; ++i)
|
||||
{
|
||||
_data = (T*)malloc(DEFAULT_ALLOC_SIZE * sizeof(T));
|
||||
assert(_data != nullptr);
|
||||
memset(_data, 0, sizeof(T) * DEFAULT_ALLOC_SIZE);
|
||||
refreshIterators();
|
||||
assert(i < size);
|
||||
_data[i] = value;
|
||||
}
|
||||
Array(size_t size, T value = T())
|
||||
: allocated(size)
|
||||
, arraySize(size)
|
||||
refreshIterators();
|
||||
}
|
||||
Array(std::initializer_list<T> init)
|
||||
: allocated((uint32)init.size()), arraySize((uint32)init.size())
|
||||
{
|
||||
_data = new T[init.size()];
|
||||
auto it = init.begin();
|
||||
for (size_t i = 0; it != init.end(); i++, it++)
|
||||
{
|
||||
_data = (T*)malloc(size * sizeof(T));
|
||||
assert(_data != nullptr);
|
||||
for (int i = 0; i < size; ++i)
|
||||
assert(i < init.size());
|
||||
_data[i] = *it;
|
||||
}
|
||||
refreshIterators();
|
||||
}
|
||||
Array(const Array &other)
|
||||
: allocated(other.allocated), arraySize(other.arraySize)
|
||||
{
|
||||
_data = new T[other.allocated];
|
||||
assert(_data != nullptr);
|
||||
std::memcpy(_data, other._data, sizeof(T) * allocated);
|
||||
refreshIterators();
|
||||
}
|
||||
Array(Array &&other) noexcept
|
||||
: allocated(std::move(other.allocated)), arraySize(std::move(other.arraySize))
|
||||
{
|
||||
_data = other._data;
|
||||
other._data = nullptr;
|
||||
other.allocated = 0;
|
||||
other.arraySize = 0;
|
||||
refreshIterators();
|
||||
}
|
||||
Array &operator=(const Array &other) noexcept
|
||||
{
|
||||
if (*this != other)
|
||||
{
|
||||
if (_data != nullptr)
|
||||
{
|
||||
assert(i < size);
|
||||
_data[i] = value;
|
||||
delete[] _data;
|
||||
}
|
||||
refreshIterators();
|
||||
}
|
||||
Array(std::initializer_list<T> init)
|
||||
: allocated(init.size())
|
||||
, arraySize(init.size())
|
||||
{
|
||||
_data = (T*)malloc(init.size() * sizeof(T));
|
||||
auto it = init.begin();
|
||||
for (size_t i = 0; it != init.end(); i++, it++)
|
||||
{
|
||||
assert(i < init.size());
|
||||
_data[i] = *it;
|
||||
}
|
||||
refreshIterators();
|
||||
}
|
||||
Array(const Array& other)
|
||||
: allocated(other.allocated)
|
||||
, arraySize(other.arraySize)
|
||||
{
|
||||
_data = (T*)malloc(other.allocated * sizeof(T));
|
||||
assert(_data != nullptr);
|
||||
allocated = other.allocated;
|
||||
arraySize = other.arraySize;
|
||||
_data = new T[other.allocated];
|
||||
std::memcpy(_data, other._data, sizeof(T) * allocated);
|
||||
refreshIterators();
|
||||
}
|
||||
Array(Array&& other) noexcept
|
||||
: allocated(std::move(other.allocated))
|
||||
, arraySize(std::move(other.arraySize))
|
||||
return *this;
|
||||
}
|
||||
Array &operator=(Array &&other) noexcept
|
||||
{
|
||||
if (*this != other)
|
||||
{
|
||||
if (_data != nullptr)
|
||||
{
|
||||
delete[] _data;
|
||||
}
|
||||
allocated = std::move(other.allocated);
|
||||
arraySize = std::move(other.arraySize);
|
||||
_data = other._data;
|
||||
other._data = nullptr;
|
||||
other.allocated = 0;
|
||||
other.arraySize = 0;
|
||||
refreshIterators();
|
||||
}
|
||||
Array& operator=(const Array& other) noexcept
|
||||
return *this;
|
||||
}
|
||||
~Array()
|
||||
{
|
||||
if (_data)
|
||||
{
|
||||
if(*this != other)
|
||||
{
|
||||
if (_data != nullptr)
|
||||
{
|
||||
free(_data);
|
||||
}
|
||||
allocated = other.allocated;
|
||||
arraySize = other.arraySize;
|
||||
_data = (T*)malloc(other.allocated * sizeof(T));
|
||||
std::memcpy(_data, other._data, sizeof(T) * allocated);
|
||||
refreshIterators();
|
||||
}
|
||||
return *this;
|
||||
delete[] _data;
|
||||
_data = nullptr;
|
||||
}
|
||||
Array& operator=(Array&& other) noexcept
|
||||
{
|
||||
if(*this != other)
|
||||
{
|
||||
if (_data != nullptr)
|
||||
{
|
||||
free(_data);
|
||||
}
|
||||
allocated = std::move(other.allocated);
|
||||
arraySize = std::move(other.arraySize);
|
||||
_data = other._data;
|
||||
other._data = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
~Array()
|
||||
{
|
||||
if(_data)
|
||||
{
|
||||
free(_data);
|
||||
_data = nullptr;
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
IteratorBase(X* x = nullptr)
|
||||
: p(x)
|
||||
{}
|
||||
IteratorBase(const IteratorBase& i)
|
||||
: p(i.p)
|
||||
{}
|
||||
reference operator*() const
|
||||
{
|
||||
return *p;
|
||||
}
|
||||
pointer operator->() const
|
||||
{
|
||||
return p;
|
||||
}
|
||||
inline bool operator!=(const IteratorBase& other)
|
||||
{
|
||||
return p != other.p;
|
||||
}
|
||||
inline bool operator==(const IteratorBase& other)
|
||||
{
|
||||
return p == other.p;
|
||||
}
|
||||
inline int operator-(const IteratorBase& other)
|
||||
{
|
||||
return p - other.p;
|
||||
}
|
||||
IteratorBase& operator++() {
|
||||
p++;
|
||||
return *this;
|
||||
}
|
||||
IteratorBase& operator--() {
|
||||
p--;
|
||||
return *this;
|
||||
}
|
||||
IteratorBase operator++(int) {
|
||||
IteratorBase tmp(*this);
|
||||
++*this;
|
||||
return tmp;
|
||||
}
|
||||
IteratorBase operator--(int) {
|
||||
IteratorBase tmp(*this);
|
||||
--*this;
|
||||
return tmp;
|
||||
}
|
||||
private:
|
||||
X* p;
|
||||
};
|
||||
typedef IteratorBase<T> Iterator;
|
||||
typedef IteratorBase<const T> ConstIterator;
|
||||
|
||||
bool operator==(const Array& other)
|
||||
{
|
||||
return _data == other._data;
|
||||
}
|
||||
|
||||
bool operator!=(const Array& other)
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
Iterator find(const T& item)
|
||||
{
|
||||
for (int i = 0; i < arraySize; ++i)
|
||||
{
|
||||
if (_data[i] == item)
|
||||
{
|
||||
return Iterator(&_data[i]);
|
||||
}
|
||||
}
|
||||
return endIt;
|
||||
}
|
||||
Iterator begin()
|
||||
{
|
||||
return beginIt;
|
||||
}
|
||||
Iterator end()
|
||||
{
|
||||
return endIt;
|
||||
}
|
||||
|
||||
T& add(const T& item)
|
||||
{
|
||||
if (arraySize == allocated)
|
||||
{
|
||||
uint32 newSize = arraySize + 1;
|
||||
allocated = calculateGrowth(newSize);
|
||||
void* tempArray = malloc(sizeof(T) * allocated);
|
||||
assert(tempArray != nullptr);
|
||||
std::memset(tempArray, 0, sizeof(T) * allocated);
|
||||
std::memcpy(tempArray, _data, arraySize * sizeof(T));
|
||||
delete _data;
|
||||
_data = (T*)tempArray;
|
||||
}
|
||||
_data[arraySize++] = item;
|
||||
refreshIterators();
|
||||
return _data[arraySize - 1];
|
||||
}
|
||||
void remove(Iterator it, bool keepOrder = true)
|
||||
{
|
||||
remove(it - beginIt, keepOrder);
|
||||
}
|
||||
void remove(int index, bool keepOrder = true)
|
||||
{
|
||||
if (keepOrder)
|
||||
{
|
||||
std::memcpy(&_data[index], &_data[index + 1], sizeof(T) * (arraySize - index));
|
||||
}
|
||||
else
|
||||
{
|
||||
_data[index] = _data[arraySize - 1];
|
||||
}
|
||||
arraySize--;
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
arraySize = 0;
|
||||
allocated = 0;
|
||||
refreshIterators();
|
||||
}
|
||||
void resize(uint32 newSize)
|
||||
{
|
||||
if (newSize < allocated)
|
||||
{
|
||||
arraySize = newSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
T* newData = (T*)malloc(newSize * sizeof(T));
|
||||
assert(newData != nullptr);
|
||||
allocated = newSize;
|
||||
std::memcpy(newData, _data, sizeof(T) * arraySize);
|
||||
arraySize = newSize;
|
||||
delete _data;
|
||||
_data = newData;
|
||||
}
|
||||
refreshIterators();
|
||||
}
|
||||
inline uint32 size() const
|
||||
{
|
||||
return arraySize;
|
||||
}
|
||||
inline uint32 capacity() const
|
||||
{
|
||||
return allocated;
|
||||
}
|
||||
inline T* data() const
|
||||
{
|
||||
return _data;
|
||||
}
|
||||
T& back() const
|
||||
{
|
||||
return _data[arraySize - 1];
|
||||
}
|
||||
void pop()
|
||||
{
|
||||
arraySize--;
|
||||
}
|
||||
T& operator[](int index) const
|
||||
{
|
||||
assert(index >= 0 && index < arraySize);
|
||||
return _data[index];
|
||||
}
|
||||
private:
|
||||
uint32 calculateGrowth(uint32 newSize) const
|
||||
{
|
||||
const uint32 oldCapacity = capacity();
|
||||
|
||||
if (oldCapacity > UINT32_MAX - oldCapacity / 2) {
|
||||
return newSize; // geometric growth would overflow
|
||||
}
|
||||
|
||||
const uint32 geometric = oldCapacity + oldCapacity / 2;
|
||||
|
||||
if (geometric < newSize) {
|
||||
return newSize; // geometric growth would be insufficient
|
||||
}
|
||||
|
||||
return geometric; // geometric growth is sufficient
|
||||
}
|
||||
void refreshIterators()
|
||||
{
|
||||
beginIt = Iterator(_data);
|
||||
endIt = Iterator(_data + arraySize);
|
||||
}
|
||||
uint32 arraySize;
|
||||
uint32 allocated;
|
||||
Iterator beginIt;
|
||||
Iterator endIt;
|
||||
T* _data;
|
||||
};
|
||||
|
||||
template<typename T, uint32 N>
|
||||
struct StaticArray
|
||||
}
|
||||
template <typename X>
|
||||
class IteratorBase
|
||||
{
|
||||
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()
|
||||
{}
|
||||
typedef std::forward_iterator_tag iterator_category;
|
||||
typedef X value_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
typedef X &reference;
|
||||
typedef X *pointer;
|
||||
|
||||
inline uint32 size() const
|
||||
IteratorBase(X *x = nullptr)
|
||||
: p(x)
|
||||
{
|
||||
return N;
|
||||
}
|
||||
inline T* data() const
|
||||
IteratorBase(const IteratorBase &i)
|
||||
: p(i.p)
|
||||
{
|
||||
return _data;
|
||||
}
|
||||
T& operator[](int index)
|
||||
reference operator*() const
|
||||
{
|
||||
assert(index >= 0 && index < N);
|
||||
return _data[index];
|
||||
return *p;
|
||||
}
|
||||
pointer operator->() const
|
||||
{
|
||||
return p;
|
||||
}
|
||||
inline bool operator!=(const IteratorBase &other)
|
||||
{
|
||||
return p != other.p;
|
||||
}
|
||||
inline bool operator==(const IteratorBase &other)
|
||||
{
|
||||
return p == other.p;
|
||||
}
|
||||
inline int operator-(const IteratorBase &other)
|
||||
{
|
||||
return (int)(p - other.p);
|
||||
}
|
||||
IteratorBase &operator++()
|
||||
{
|
||||
p++;
|
||||
return *this;
|
||||
}
|
||||
IteratorBase &operator--()
|
||||
{
|
||||
p--;
|
||||
return *this;
|
||||
}
|
||||
IteratorBase operator++(int)
|
||||
{
|
||||
IteratorBase tmp(*this);
|
||||
++*this;
|
||||
return tmp;
|
||||
}
|
||||
IteratorBase operator--(int)
|
||||
{
|
||||
IteratorBase tmp(*this);
|
||||
--*this;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
IteratorBase(X* x = nullptr)
|
||||
: p(x)
|
||||
{}
|
||||
IteratorBase(const IteratorBase& i)
|
||||
: p(i.p)
|
||||
{}
|
||||
reference operator*() const
|
||||
{
|
||||
return *p;
|
||||
}
|
||||
pointer operator->() const
|
||||
{
|
||||
return p;
|
||||
}
|
||||
inline bool operator!=(const IteratorBase& other)
|
||||
{
|
||||
return p != other.p;
|
||||
}
|
||||
inline bool operator==(const IteratorBase& other)
|
||||
{
|
||||
return p == other.p;
|
||||
}
|
||||
IteratorBase& operator++() {
|
||||
p++;
|
||||
return *this;
|
||||
}
|
||||
IteratorBase operator++(int) {
|
||||
IteratorBase tmp(*this);
|
||||
++*this;
|
||||
return tmp;
|
||||
}
|
||||
private:
|
||||
X* p;
|
||||
};
|
||||
typedef IteratorBase<T> Iterator;
|
||||
typedef IteratorBase<const T> ConstIterator;
|
||||
|
||||
private:
|
||||
T _data[N];
|
||||
Iterator beginIt;
|
||||
Iterator endIt;
|
||||
X *p;
|
||||
};
|
||||
}
|
||||
typedef IteratorBase<T> Iterator;
|
||||
typedef IteratorBase<const T> ConstIterator;
|
||||
|
||||
bool operator==(const Array &other)
|
||||
{
|
||||
return _data == other._data;
|
||||
}
|
||||
|
||||
bool operator!=(const Array &other)
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
Iterator find(const T &item)
|
||||
{
|
||||
for (uint32 i = 0; i < arraySize; ++i)
|
||||
{
|
||||
if (_data[i] == item)
|
||||
{
|
||||
return Iterator(&_data[i]);
|
||||
}
|
||||
}
|
||||
return endIt;
|
||||
}
|
||||
Iterator begin()
|
||||
{
|
||||
return beginIt;
|
||||
}
|
||||
Iterator begin() const
|
||||
{
|
||||
return beginIt;
|
||||
}
|
||||
Iterator end()
|
||||
{
|
||||
return endIt;
|
||||
}
|
||||
Iterator end() const
|
||||
{
|
||||
return endIt;
|
||||
}
|
||||
|
||||
T &add(const T &item = T())
|
||||
{
|
||||
if (arraySize == allocated)
|
||||
{
|
||||
uint32 newSize = arraySize + 1;
|
||||
allocated = calculateGrowth(newSize);
|
||||
T *tempArray = new T[allocated];
|
||||
assert(tempArray != nullptr);
|
||||
for (uint32 i = 0; i < arraySize; ++i)
|
||||
{
|
||||
tempArray[i] = _data[i];
|
||||
}
|
||||
delete[] _data;
|
||||
_data = tempArray;
|
||||
}
|
||||
_data[arraySize++] = item;
|
||||
refreshIterators();
|
||||
return _data[arraySize - 1];
|
||||
}
|
||||
void remove(Iterator it, bool keepOrder = true)
|
||||
{
|
||||
remove(it - beginIt, keepOrder);
|
||||
}
|
||||
void remove(int index, bool keepOrder = true)
|
||||
{
|
||||
if (keepOrder)
|
||||
{
|
||||
std::memcpy(&_data[index], &_data[index + 1], sizeof(T) * (arraySize - index));
|
||||
}
|
||||
else
|
||||
{
|
||||
_data[index] = _data[arraySize - 1];
|
||||
}
|
||||
arraySize--;
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
delete[] _data;
|
||||
_data = nullptr;
|
||||
arraySize = 0;
|
||||
allocated = 0;
|
||||
refreshIterators();
|
||||
}
|
||||
void resize(uint32 newSize)
|
||||
{
|
||||
if (newSize < allocated)
|
||||
{
|
||||
arraySize = newSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
T *newData = new T[newSize];
|
||||
assert(newData != nullptr);
|
||||
allocated = newSize;
|
||||
std::memcpy(newData, _data, sizeof(T) * arraySize);
|
||||
arraySize = newSize;
|
||||
delete _data;
|
||||
_data = newData;
|
||||
}
|
||||
refreshIterators();
|
||||
}
|
||||
inline uint32 size() const
|
||||
{
|
||||
return arraySize;
|
||||
}
|
||||
inline uint32 capacity() const
|
||||
{
|
||||
return allocated;
|
||||
}
|
||||
inline T *data() const
|
||||
{
|
||||
return _data;
|
||||
}
|
||||
T &back() const
|
||||
{
|
||||
return _data[arraySize - 1];
|
||||
}
|
||||
void pop()
|
||||
{
|
||||
arraySize--;
|
||||
}
|
||||
T &operator[](uint32 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
|
||||
{
|
||||
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
|
||||
{
|
||||
const uint32 oldCapacity = capacity();
|
||||
|
||||
if (oldCapacity > UINT32_MAX - oldCapacity / 2)
|
||||
{
|
||||
return newSize; // geometric growth would overflow
|
||||
}
|
||||
|
||||
const uint32 geometric = oldCapacity + oldCapacity / 2;
|
||||
|
||||
if (geometric < newSize)
|
||||
{
|
||||
return newSize; // geometric growth would be insufficient
|
||||
}
|
||||
|
||||
return geometric; // geometric growth is sufficient
|
||||
}
|
||||
void refreshIterators()
|
||||
{
|
||||
beginIt = Iterator(_data);
|
||||
endIt = Iterator(_data + arraySize);
|
||||
}
|
||||
uint32 arraySize;
|
||||
uint32 allocated;
|
||||
Iterator beginIt;
|
||||
Iterator endIt;
|
||||
T *_data;
|
||||
};
|
||||
|
||||
template <typename T, uint32 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() const
|
||||
{
|
||||
return _data;
|
||||
}
|
||||
T &operator[](int index)
|
||||
{
|
||||
assert(index >= 0 && index < N);
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
IteratorBase(X *x = nullptr)
|
||||
: p(x)
|
||||
{
|
||||
}
|
||||
IteratorBase(const IteratorBase &i)
|
||||
: p(i.p)
|
||||
{
|
||||
}
|
||||
reference operator*() const
|
||||
{
|
||||
return *p;
|
||||
}
|
||||
pointer operator->() const
|
||||
{
|
||||
return p;
|
||||
}
|
||||
inline bool operator!=(const IteratorBase &other)
|
||||
{
|
||||
return p != other.p;
|
||||
}
|
||||
inline bool operator==(const IteratorBase &other)
|
||||
{
|
||||
return p == other.p;
|
||||
}
|
||||
IteratorBase &operator++()
|
||||
{
|
||||
p++;
|
||||
return *this;
|
||||
}
|
||||
IteratorBase operator++(int)
|
||||
{
|
||||
IteratorBase tmp(*this);
|
||||
++*this;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
private:
|
||||
X *p;
|
||||
};
|
||||
typedef IteratorBase<T> Iterator;
|
||||
typedef IteratorBase<const T> ConstIterator;
|
||||
|
||||
private:
|
||||
T _data[N];
|
||||
Iterator beginIt;
|
||||
Iterator endIt;
|
||||
};
|
||||
} // namespace Seele
|
||||
+196
-187
@@ -2,207 +2,216 @@
|
||||
#include "MinimalEngine.h"
|
||||
namespace Seele
|
||||
{
|
||||
template<typename T>
|
||||
class List
|
||||
template <typename T>
|
||||
class List
|
||||
{
|
||||
private:
|
||||
struct Node
|
||||
{
|
||||
Node *prev;
|
||||
Node *next;
|
||||
T data;
|
||||
};
|
||||
|
||||
public:
|
||||
List()
|
||||
{
|
||||
root = nullptr;
|
||||
tail = nullptr;
|
||||
beginIt = Iterator(root);
|
||||
endIt = Iterator(tail);
|
||||
size = 0;
|
||||
}
|
||||
~List()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
template <typename X>
|
||||
class IteratorBase
|
||||
{
|
||||
private:
|
||||
struct Node
|
||||
{
|
||||
Node* prev;
|
||||
Node* next;
|
||||
T data;
|
||||
};
|
||||
public:
|
||||
List()
|
||||
{
|
||||
root = nullptr;
|
||||
tail = nullptr;
|
||||
beginIt = Iterator(root);
|
||||
endIt = Iterator(tail);
|
||||
size = 0;
|
||||
}
|
||||
~List()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
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;
|
||||
typedef std::forward_iterator_tag iterator_category;
|
||||
typedef X value_type;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
typedef X &reference;
|
||||
typedef X *pointer;
|
||||
|
||||
IteratorBase(Node* x = nullptr)
|
||||
: node(x)
|
||||
{}
|
||||
IteratorBase(const IteratorBase& i)
|
||||
: node(i.node)
|
||||
{}
|
||||
reference operator*() const
|
||||
{
|
||||
return node->data;
|
||||
}
|
||||
pointer operator->() const
|
||||
{
|
||||
return &node->data;
|
||||
}
|
||||
inline bool operator!=(const IteratorBase& other)
|
||||
{
|
||||
return node != other.node;
|
||||
}
|
||||
inline bool operator==(const IteratorBase& other)
|
||||
{
|
||||
return node == other.node;
|
||||
}
|
||||
IteratorBase& operator--() {
|
||||
node = node->prev;
|
||||
return *this;
|
||||
}
|
||||
IteratorBase operator--(int) {
|
||||
IteratorBase tmp(*this);
|
||||
--* this;
|
||||
return tmp;
|
||||
}
|
||||
IteratorBase& operator++() {
|
||||
node = node->next;
|
||||
return *this;
|
||||
}
|
||||
IteratorBase operator++(int) {
|
||||
IteratorBase tmp(*this);
|
||||
++* this;
|
||||
return tmp;
|
||||
}
|
||||
private:
|
||||
Node* node;
|
||||
friend class List<T>;
|
||||
};
|
||||
typedef IteratorBase<T> Iterator;
|
||||
typedef IteratorBase<const T> ConstIterator;
|
||||
IteratorBase(Node *x = nullptr)
|
||||
: node(x)
|
||||
{
|
||||
}
|
||||
IteratorBase(const IteratorBase &i)
|
||||
: node(i.node)
|
||||
{
|
||||
}
|
||||
reference operator*() const
|
||||
{
|
||||
return node->data;
|
||||
}
|
||||
pointer operator->() const
|
||||
{
|
||||
return &node->data;
|
||||
}
|
||||
inline bool operator!=(const IteratorBase &other)
|
||||
{
|
||||
return node != other.node;
|
||||
}
|
||||
inline bool operator==(const IteratorBase &other)
|
||||
{
|
||||
return node == other.node;
|
||||
}
|
||||
IteratorBase &operator--()
|
||||
{
|
||||
node = node->prev;
|
||||
return *this;
|
||||
}
|
||||
IteratorBase operator--(int)
|
||||
{
|
||||
IteratorBase tmp(*this);
|
||||
--*this;
|
||||
return tmp;
|
||||
}
|
||||
IteratorBase &operator++()
|
||||
{
|
||||
node = node->next;
|
||||
return *this;
|
||||
}
|
||||
IteratorBase operator++(int)
|
||||
{
|
||||
IteratorBase tmp(*this);
|
||||
++*this;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
T& front()
|
||||
private:
|
||||
Node *node;
|
||||
friend class List<T>;
|
||||
};
|
||||
typedef IteratorBase<T> Iterator;
|
||||
typedef IteratorBase<const T> ConstIterator;
|
||||
|
||||
T &front()
|
||||
{
|
||||
return root->data;
|
||||
}
|
||||
T &back()
|
||||
{
|
||||
return tail->prev->data;
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
if (empty())
|
||||
{
|
||||
return root->data;
|
||||
return;
|
||||
}
|
||||
T& back()
|
||||
for (Node *tmp = root; tmp != tail;)
|
||||
{
|
||||
return tail->prev->data;
|
||||
tmp = tmp->next;
|
||||
delete tmp->prev;
|
||||
}
|
||||
void clear()
|
||||
delete tail;
|
||||
tail = nullptr;
|
||||
root = nullptr;
|
||||
}
|
||||
//Insert at the end
|
||||
Iterator add(const T &value)
|
||||
{
|
||||
if (root == nullptr)
|
||||
{
|
||||
if (empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
for (Node* tmp = root; tmp != tail;)
|
||||
{
|
||||
tmp = tmp->next;
|
||||
delete tmp->prev;
|
||||
}
|
||||
delete tail;
|
||||
tail = nullptr;
|
||||
root = nullptr;
|
||||
root = new Node();
|
||||
tail = root;
|
||||
}
|
||||
//Insert at the end
|
||||
Iterator add(const T& value)
|
||||
tail->data = value;
|
||||
Node *newTail = new Node();
|
||||
newTail->prev = tail;
|
||||
newTail->next = nullptr;
|
||||
tail->next = newTail;
|
||||
Iterator insertedElement(tail);
|
||||
tail = newTail;
|
||||
refreshIterators();
|
||||
size++;
|
||||
return insertedElement;
|
||||
}
|
||||
Iterator remove(Iterator pos)
|
||||
{
|
||||
size--;
|
||||
Node *prev = pos.node->prev;
|
||||
Node *next = pos.node->next;
|
||||
if (prev == nullptr)
|
||||
{
|
||||
if (root == nullptr)
|
||||
{
|
||||
root = new Node();
|
||||
tail = root;
|
||||
}
|
||||
tail->data = value;
|
||||
Node* newTail = new Node();
|
||||
newTail->prev = tail;
|
||||
newTail->next = nullptr;
|
||||
tail->next = newTail;
|
||||
Iterator insertedElement(tail);
|
||||
tail = newTail;
|
||||
root = next;
|
||||
}
|
||||
else
|
||||
{
|
||||
prev->next = next;
|
||||
}
|
||||
next->prev = prev;
|
||||
delete pos.node;
|
||||
refreshIterators();
|
||||
return Iterator(next);
|
||||
}
|
||||
Iterator insert(Iterator pos, const T &value)
|
||||
{
|
||||
size++;
|
||||
if (root == nullptr)
|
||||
{
|
||||
root = new Node();
|
||||
root->data = value;
|
||||
tail = new Node();
|
||||
root->next = tail;
|
||||
root->prev = nullptr;
|
||||
tail->prev = root;
|
||||
tail->next = nullptr;
|
||||
refreshIterators();
|
||||
size++;
|
||||
return insertedElement;
|
||||
}
|
||||
Iterator remove(Iterator pos)
|
||||
{
|
||||
size--;
|
||||
Node* prev = pos.node->prev;
|
||||
Node* next = pos.node->next;
|
||||
if (prev == nullptr)
|
||||
{
|
||||
root = next;
|
||||
}
|
||||
else
|
||||
{
|
||||
prev->next = next;
|
||||
}
|
||||
next->prev = prev;
|
||||
delete pos.node;
|
||||
refreshIterators();
|
||||
return Iterator(next);
|
||||
}
|
||||
Iterator insert(Iterator pos, const T& value)
|
||||
{
|
||||
size++;
|
||||
if (root == nullptr)
|
||||
{
|
||||
root = new Node();
|
||||
root->data = value;
|
||||
tail = new Node();
|
||||
root->next = tail;
|
||||
root->prev = nullptr;
|
||||
tail->prev = root;
|
||||
tail->next = nullptr;
|
||||
refreshIterators();
|
||||
return beginIt;
|
||||
}
|
||||
Node* tmp = pos.node->prev;
|
||||
Node* newNode = new Node();
|
||||
newNode->data = value;
|
||||
tmp->next = newNode;
|
||||
newNode->prev = tmp;
|
||||
newNode->next = pos.node;
|
||||
pos.node->prev = newNode;
|
||||
return Iterator(newNode);
|
||||
}
|
||||
Iterator find(const T& value)
|
||||
{
|
||||
for (Node* i = root; i != tail; i = i->next)
|
||||
{
|
||||
if (!(i->data < value) && !(value < i->data))
|
||||
{
|
||||
return Iterator(i);
|
||||
}
|
||||
}
|
||||
return endIt;
|
||||
}
|
||||
bool empty()
|
||||
{
|
||||
return size == 0;
|
||||
}
|
||||
uint32 length()
|
||||
{
|
||||
return size;
|
||||
}
|
||||
Iterator begin()
|
||||
{
|
||||
return beginIt;
|
||||
}
|
||||
Iterator end()
|
||||
Node *tmp = pos.node->prev;
|
||||
Node *newNode = new Node();
|
||||
newNode->data = value;
|
||||
tmp->next = newNode;
|
||||
newNode->prev = tmp;
|
||||
newNode->next = pos.node;
|
||||
pos.node->prev = newNode;
|
||||
return Iterator(newNode);
|
||||
}
|
||||
Iterator find(const T &value)
|
||||
{
|
||||
for (Node *i = root; i != tail; i = i->next)
|
||||
{
|
||||
return endIt;
|
||||
if (!(i->data < value) && !(value < i->data))
|
||||
{
|
||||
return Iterator(i);
|
||||
}
|
||||
}
|
||||
return endIt;
|
||||
}
|
||||
bool empty()
|
||||
{
|
||||
return size == 0;
|
||||
}
|
||||
uint32 length()
|
||||
{
|
||||
return size;
|
||||
}
|
||||
Iterator begin()
|
||||
{
|
||||
return beginIt;
|
||||
}
|
||||
Iterator end()
|
||||
{
|
||||
return endIt;
|
||||
}
|
||||
|
||||
private:
|
||||
void refreshIterators()
|
||||
{
|
||||
beginIt = Iterator(root);
|
||||
endIt = Iterator(tail);
|
||||
}
|
||||
Node* root;
|
||||
Node* tail;
|
||||
Iterator beginIt;
|
||||
Iterator endIt;
|
||||
uint32 size;
|
||||
};
|
||||
}
|
||||
private:
|
||||
void refreshIterators()
|
||||
{
|
||||
beginIt = Iterator(root);
|
||||
endIt = Iterator(tail);
|
||||
}
|
||||
Node *root;
|
||||
Node *tail;
|
||||
Iterator beginIt;
|
||||
Iterator endIt;
|
||||
uint32 size;
|
||||
};
|
||||
} // namespace Seele
|
||||
+312
-285
@@ -3,334 +3,361 @@
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
template<typename K, typename V>
|
||||
struct Pair
|
||||
template <typename K, typename V>
|
||||
struct Pair
|
||||
{
|
||||
public:
|
||||
Pair()
|
||||
: key(K()), value(V())
|
||||
{
|
||||
public:
|
||||
Pair()
|
||||
: key(K())
|
||||
, value(V())
|
||||
{}
|
||||
Pair(K key, V value)
|
||||
: key(key)
|
||||
, value(value)
|
||||
{}
|
||||
K key;
|
||||
V value;
|
||||
}
|
||||
Pair(K key, V value)
|
||||
: key(key), value(value)
|
||||
{
|
||||
}
|
||||
K key;
|
||||
V value;
|
||||
};
|
||||
template <typename K, typename V>
|
||||
struct Map
|
||||
{
|
||||
private:
|
||||
struct Node
|
||||
{
|
||||
Pair<K, V> pair;
|
||||
Node *leftChild;
|
||||
Node *rightChild;
|
||||
Node(const K &key)
|
||||
: leftChild(nullptr), rightChild(nullptr), pair(key, V())
|
||||
{
|
||||
}
|
||||
Node()
|
||||
: leftChild(nullptr), rightChild(nullptr), pair(K(), V())
|
||||
{
|
||||
}
|
||||
~Node()
|
||||
{
|
||||
if (leftChild != nullptr)
|
||||
{
|
||||
delete leftChild;
|
||||
}
|
||||
if (rightChild != nullptr)
|
||||
{
|
||||
delete rightChild;
|
||||
}
|
||||
}
|
||||
};
|
||||
template<typename K, typename V>
|
||||
struct Map
|
||||
|
||||
public:
|
||||
Map()
|
||||
: root(nullptr), _size(0)
|
||||
{
|
||||
}
|
||||
~Map()
|
||||
{
|
||||
delete root;
|
||||
}
|
||||
class Iterator
|
||||
{
|
||||
private:
|
||||
struct Node
|
||||
{
|
||||
Pair<K, V> pair;
|
||||
Node* leftChild;
|
||||
Node* rightChild;
|
||||
Node(const K& key)
|
||||
: leftChild(nullptr)
|
||||
, rightChild(nullptr)
|
||||
, pair(key, V())
|
||||
{}
|
||||
Node()
|
||||
: leftChild(nullptr)
|
||||
, rightChild(nullptr)
|
||||
, pair(K(), V())
|
||||
{}
|
||||
~Node()
|
||||
{
|
||||
if(leftChild != nullptr)
|
||||
{
|
||||
delete leftChild;
|
||||
}
|
||||
if(rightChild != nullptr)
|
||||
{
|
||||
delete rightChild;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
Map()
|
||||
: root(nullptr)
|
||||
{}
|
||||
~Map()
|
||||
{
|
||||
delete root;
|
||||
}
|
||||
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;
|
||||
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;
|
||||
|
||||
Iterator(Node* x = nullptr)
|
||||
: node(x)
|
||||
{}
|
||||
Iterator(Node* x, Array<Node*>&& beginIt)
|
||||
: node(x)
|
||||
, traversal(std::move(beginIt))
|
||||
{}
|
||||
Iterator(const Iterator& i)
|
||||
: node(i.node)
|
||||
, traversal(i.traversal)
|
||||
{}
|
||||
reference operator*() const
|
||||
Iterator(Node *x = nullptr)
|
||||
: node(x)
|
||||
{
|
||||
}
|
||||
Iterator(Node *x, Array<Node *> &&beginIt)
|
||||
: node(x), traversal(std::move(beginIt))
|
||||
{
|
||||
}
|
||||
Iterator(const Iterator &i)
|
||||
: node(i.node), traversal(i.traversal)
|
||||
{
|
||||
}
|
||||
reference operator*() const
|
||||
{
|
||||
return node->pair;
|
||||
}
|
||||
pointer operator->() const
|
||||
{
|
||||
return &node->pair;
|
||||
}
|
||||
inline bool operator!=(const Iterator &other)
|
||||
{
|
||||
return node != other.node;
|
||||
}
|
||||
inline bool operator==(const Iterator &other)
|
||||
{
|
||||
return node == other.node;
|
||||
}
|
||||
Iterator &operator++()
|
||||
{
|
||||
node = node->rightChild;
|
||||
while (node != nullptr && node->leftChild != nullptr)
|
||||
{
|
||||
return node->pair;
|
||||
}
|
||||
pointer operator->() const
|
||||
{
|
||||
return &node->pair;
|
||||
}
|
||||
inline bool operator!=(const Iterator& other)
|
||||
{
|
||||
return node != other.node;
|
||||
}
|
||||
inline bool operator==(const Iterator& other)
|
||||
{
|
||||
return node == other.node;
|
||||
}
|
||||
Iterator& operator++() {
|
||||
node = node->rightChild;
|
||||
while(node != nullptr && node->leftChild != nullptr)
|
||||
{
|
||||
traversal.add(node);
|
||||
node = node->leftChild;
|
||||
}
|
||||
if(node == nullptr && traversal.size() > 0)
|
||||
{
|
||||
node = traversal.back();
|
||||
traversal.pop();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
Iterator& operator--() {
|
||||
traversal.add(node);
|
||||
node = node->leftChild;
|
||||
while(node != nullptr && node->rightchild != nullptr)
|
||||
{
|
||||
traversal.add(node);
|
||||
node = node->rightChild;
|
||||
}
|
||||
if(node == nullptr && traversal.size() > 0)
|
||||
{
|
||||
node = traversal.back();
|
||||
traversal.pop();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
Iterator operator--(int) {
|
||||
Iterator tmp(*this);
|
||||
++* this;
|
||||
return tmp;
|
||||
}
|
||||
Iterator operator++(int) {
|
||||
Iterator tmp(*this);
|
||||
++* this;
|
||||
return tmp;
|
||||
}
|
||||
private:
|
||||
Node* node;
|
||||
Array<Node*> traversal;
|
||||
};
|
||||
V& operator[](const K& key)
|
||||
{
|
||||
root = splay(root, key);
|
||||
if (root == nullptr || root->pair.key < key || key < root->pair.key)
|
||||
if (node == nullptr && traversal.size() > 0)
|
||||
{
|
||||
root = insert(root, key);
|
||||
node = traversal.back();
|
||||
traversal.pop();
|
||||
}
|
||||
refreshIterators();
|
||||
return root->pair.value;
|
||||
return *this;
|
||||
}
|
||||
Iterator find(const K& key)
|
||||
Iterator &operator--()
|
||||
{
|
||||
root = splay(root, key);
|
||||
if (root == nullptr || root->pair.key != key)
|
||||
node = node->leftChild;
|
||||
while (node != nullptr && node->rightchild != nullptr)
|
||||
{
|
||||
return endIt;
|
||||
traversal.add(node);
|
||||
node = node->rightChild;
|
||||
}
|
||||
return Iterator(root);
|
||||
if (node == nullptr && traversal.size() > 0)
|
||||
{
|
||||
node = traversal.back();
|
||||
traversal.pop();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
Iterator erase(const K& key)
|
||||
Iterator operator--(int)
|
||||
{
|
||||
root = remove(root, key);
|
||||
refreshIterators();
|
||||
return Iterator(root);
|
||||
Iterator tmp(*this);
|
||||
++*this;
|
||||
return tmp;
|
||||
}
|
||||
bool exists(const K& key)
|
||||
Iterator operator++(int)
|
||||
{
|
||||
return find(key) != endIt;
|
||||
Iterator tmp(*this);
|
||||
++*this;
|
||||
return tmp;
|
||||
}
|
||||
Iterator begin() const
|
||||
|
||||
private:
|
||||
Node *node;
|
||||
Array<Node *> traversal;
|
||||
};
|
||||
V &operator[](const K &key)
|
||||
{
|
||||
root = splay(root, key);
|
||||
if (root == nullptr || root->pair.key < key || key < root->pair.key)
|
||||
{
|
||||
return beginIt;
|
||||
root = insert(root, key);
|
||||
_size++;
|
||||
}
|
||||
Iterator end() const
|
||||
refreshIterators();
|
||||
return root->pair.value;
|
||||
}
|
||||
Iterator find(const K &key)
|
||||
{
|
||||
root = splay(root, key);
|
||||
if (root == nullptr || root->pair.key != key)
|
||||
{
|
||||
return endIt;
|
||||
}
|
||||
private:
|
||||
void refreshIterators()
|
||||
return Iterator(root);
|
||||
}
|
||||
Iterator erase(const K &key)
|
||||
{
|
||||
root = remove(root, key);
|
||||
refreshIterators();
|
||||
return Iterator(root);
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
delete root;
|
||||
root = nullptr;
|
||||
_size = 0;
|
||||
refreshIterators();
|
||||
}
|
||||
bool exists(const K &key)
|
||||
{
|
||||
return find(key) != endIt;
|
||||
}
|
||||
Iterator begin() const
|
||||
{
|
||||
return beginIt;
|
||||
}
|
||||
Iterator end() const
|
||||
{
|
||||
return endIt;
|
||||
}
|
||||
bool empty() const
|
||||
{
|
||||
return root == nullptr;
|
||||
}
|
||||
uint32 size() const
|
||||
{
|
||||
return _size;
|
||||
}
|
||||
|
||||
private:
|
||||
void refreshIterators()
|
||||
{
|
||||
Node *beginNode = root;
|
||||
if (root == nullptr)
|
||||
{
|
||||
Node* beginNode = root;
|
||||
if(root == nullptr)
|
||||
beginIt = Iterator(nullptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
Array<Node *> beginTraversal;
|
||||
while (beginNode != nullptr)
|
||||
{
|
||||
beginIt = Iterator(nullptr);
|
||||
beginTraversal.add(beginNode);
|
||||
beginNode = beginNode->leftChild;
|
||||
}
|
||||
else
|
||||
beginNode = beginTraversal.back();
|
||||
beginTraversal.pop();
|
||||
beginIt = Iterator(beginNode, std::move(beginTraversal));
|
||||
}
|
||||
Node *endNode = root;
|
||||
if (root == nullptr)
|
||||
{
|
||||
endIt = Iterator(nullptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
Array<Node *> endTraversal;
|
||||
while (endNode != nullptr)
|
||||
{
|
||||
Array<Node*> beginTraversal;
|
||||
while(beginNode != nullptr)
|
||||
{
|
||||
beginTraversal.add(beginNode);
|
||||
beginNode = beginNode->leftChild;
|
||||
}
|
||||
beginNode = beginTraversal.back();
|
||||
beginTraversal.pop();
|
||||
beginIt = Iterator(beginNode, std::move(beginTraversal));
|
||||
endTraversal.add(endNode);
|
||||
endNode = endNode->rightChild;
|
||||
}
|
||||
Node* endNode = root;
|
||||
if(root == nullptr)
|
||||
endIt = Iterator(endNode, std::move(endTraversal));
|
||||
}
|
||||
}
|
||||
Node *root;
|
||||
Iterator beginIt;
|
||||
Iterator endIt;
|
||||
uint32 _size;
|
||||
Node *rotateRight(Node *node)
|
||||
{
|
||||
Node *y = node->leftChild;
|
||||
node->leftChild = y->rightChild;
|
||||
y->rightChild = node;
|
||||
return y;
|
||||
}
|
||||
Node *rotateLeft(Node *node)
|
||||
{
|
||||
Node *y = node->rightChild;
|
||||
node->rightChild = y->leftChild;
|
||||
y->leftChild = node;
|
||||
return y;
|
||||
}
|
||||
Node *makeNode(const K &key)
|
||||
{
|
||||
return new Node(key);
|
||||
}
|
||||
Node *insert(Node *r, const K &key)
|
||||
{
|
||||
if (r == nullptr)
|
||||
return makeNode(key);
|
||||
|
||||
r = splay(r, key);
|
||||
|
||||
if (!(r->pair.key < key || key < r->pair.key))
|
||||
return r;
|
||||
|
||||
Node *newNode = makeNode(key);
|
||||
|
||||
if (key < r->pair.key)
|
||||
{
|
||||
newNode->rightChild = r;
|
||||
newNode->leftChild = r->leftChild;
|
||||
r->leftChild = nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
newNode->leftChild = r;
|
||||
newNode->rightChild = r->rightChild;
|
||||
r->rightChild = nullptr;
|
||||
}
|
||||
return newNode;
|
||||
}
|
||||
Node *remove(Node *r, const K &key)
|
||||
{
|
||||
Node *temp;
|
||||
if (!r)
|
||||
return nullptr;
|
||||
|
||||
r = splay(r, key);
|
||||
|
||||
if (r->pair.key < key || key < r->pair.key)
|
||||
return r;
|
||||
|
||||
if (!r->leftChild)
|
||||
{
|
||||
temp = r;
|
||||
r = r->rightChild;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp = r;
|
||||
|
||||
r = splay(r->leftChild, key);
|
||||
r->rightChild = temp->rightChild;
|
||||
}
|
||||
temp->leftChild = nullptr;
|
||||
temp->rightChild = nullptr;
|
||||
_size--;
|
||||
delete temp;
|
||||
return r;
|
||||
}
|
||||
Node *splay(Node *r, const K &key)
|
||||
{
|
||||
if (r == nullptr || !(r->pair.key < key || key < r->pair.key))
|
||||
{
|
||||
return r;
|
||||
}
|
||||
|
||||
if (key < r->pair.key)
|
||||
{
|
||||
if (r->leftChild == nullptr)
|
||||
return r;
|
||||
|
||||
if (key < r->leftChild->pair.key)
|
||||
{
|
||||
endIt = Iterator(nullptr);
|
||||
r->leftChild->leftChild = splay(r->leftChild->leftChild, key);
|
||||
|
||||
r = rotateRight(r);
|
||||
}
|
||||
else
|
||||
else if (r->leftChild->pair.key < key)
|
||||
{
|
||||
Array<Node*> endTraversal;
|
||||
while(endNode != nullptr)
|
||||
r->leftChild->rightChild = splay(r->leftChild->rightChild, key);
|
||||
|
||||
if (r->leftChild->rightChild != nullptr)
|
||||
{
|
||||
endTraversal.add(endNode);
|
||||
endNode = endNode->rightChild;
|
||||
r->leftChild = rotateLeft(r->leftChild);
|
||||
}
|
||||
endIt = Iterator(endNode, std::move(endTraversal));
|
||||
}
|
||||
return (r->leftChild == nullptr) ? r : rotateRight(r);
|
||||
}
|
||||
Node* root;
|
||||
Iterator beginIt;
|
||||
Iterator endIt;
|
||||
Node* rotateRight(Node* node)
|
||||
else
|
||||
{
|
||||
Node* y = node->leftChild;
|
||||
node->leftChild = y->rightChild;
|
||||
y->rightChild = node;
|
||||
return y;
|
||||
}
|
||||
Node* rotateLeft(Node* node)
|
||||
{
|
||||
Node* y = node->rightChild;
|
||||
node->rightChild = y->leftChild;
|
||||
y->leftChild = node;
|
||||
return y;
|
||||
}
|
||||
Node* makeNode(const K& key)
|
||||
{
|
||||
return new Node(key);
|
||||
}
|
||||
Node* insert(Node* root, const K& key)
|
||||
{
|
||||
if (root == nullptr) return makeNode(key);
|
||||
if (r->rightChild == nullptr)
|
||||
return r;
|
||||
|
||||
root = splay(root, key);
|
||||
|
||||
if (!(root->pair.key < key || key < root->pair.key)) return root;
|
||||
|
||||
Node* newNode = makeNode(key);
|
||||
|
||||
if (key < root->pair.key)
|
||||
if (key < r->rightChild->pair.key)
|
||||
{
|
||||
newNode->rightChild = root;
|
||||
newNode->leftChild = root->leftChild;
|
||||
root->leftChild = nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
newNode->leftChild = root;
|
||||
newNode->rightChild = root->rightChild;
|
||||
root->rightChild = nullptr;
|
||||
}
|
||||
return newNode;
|
||||
}
|
||||
Node* remove(Node* root, const K& key)
|
||||
{
|
||||
Node* temp;
|
||||
if (!root)
|
||||
return nullptr;
|
||||
r->rightChild->leftChild = splay(r->rightChild->leftChild, key);
|
||||
|
||||
root = splay(root, key);
|
||||
|
||||
if (root->pair.key < key || key < root->pair.key)
|
||||
return root;
|
||||
|
||||
if (!root->leftChild)
|
||||
{
|
||||
temp = root;
|
||||
root = root->rightChild;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp = root;
|
||||
|
||||
root = splay(root->leftChild, key);
|
||||
root->rightChild = temp->rightChild;
|
||||
}
|
||||
temp->leftChild = nullptr;
|
||||
temp->rightChild = nullptr;
|
||||
delete temp;
|
||||
return root;
|
||||
}
|
||||
Node* splay(Node* root, const K& key)
|
||||
{
|
||||
if (root == nullptr || !(root->pair.key < key || key < root->pair.key))
|
||||
{
|
||||
return root;
|
||||
}
|
||||
|
||||
if (key < root->pair.key)
|
||||
{
|
||||
if (root->leftChild == nullptr)
|
||||
return root;
|
||||
|
||||
if (key < root->leftChild->pair.key)
|
||||
if (r->rightChild->leftChild != nullptr)
|
||||
{
|
||||
root->leftChild->leftChild = splay(root->leftChild->leftChild, key);
|
||||
|
||||
root = rotateRight(root);
|
||||
r->rightChild = rotateRight(r->rightChild);
|
||||
}
|
||||
else if (root->leftChild->pair.key < key)
|
||||
{
|
||||
root->leftChild->rightChild = splay(root->leftChild->rightChild, key);
|
||||
|
||||
if (root->leftChild->rightChild != nullptr)
|
||||
{
|
||||
root->leftChild = rotateLeft(root->leftChild);
|
||||
}
|
||||
}
|
||||
return (root->leftChild == nullptr) ? root : rotateRight(root);
|
||||
}
|
||||
else
|
||||
else if (r->rightChild->pair.key < key)
|
||||
{
|
||||
if (root->rightChild == nullptr)
|
||||
return root;
|
||||
|
||||
if (key < root->rightChild->pair.key)
|
||||
{
|
||||
root->rightChild->leftChild = splay(root->rightChild->leftChild, key);
|
||||
|
||||
if (root->rightChild->leftChild != nullptr)
|
||||
{
|
||||
root->rightChild = rotateRight(root->rightChild);
|
||||
}
|
||||
}
|
||||
else if (root->rightChild->pair.key < key)
|
||||
{
|
||||
root->rightChild->rightChild = splay(root->rightChild->rightChild, key);
|
||||
root = rotateLeft(root);
|
||||
}
|
||||
return (root->rightChild == nullptr) ? root : rotateLeft(root);
|
||||
r->rightChild->rightChild = splay(r->rightChild->rightChild, key);
|
||||
r = rotateLeft(r);
|
||||
}
|
||||
return (r->rightChild == nullptr) ? r : rotateLeft(r);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace Seele
|
||||
Reference in New Issue
Block a user