Initial
This commit is contained in:
parent
d4c558a59a
commit
db05d963be
74 changed files with 4473 additions and 3231 deletions
22
Containers/CMakeLists.txt
Normal file
22
Containers/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
project(Containers)
|
||||
|
||||
### ---------------------- Static Library --------------------- ###
|
||||
file(GLOB SOURCES "./private/*.cpp")
|
||||
file(GLOB HEADERS "./public/*.hpp")
|
||||
add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS})
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC ./public/)
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC BaseModule)
|
||||
|
||||
### -------------------------- Tests -------------------------- ###
|
||||
enable_testing()
|
||||
file(GLOB TEST_SOURCES "./tests/*.cpp")
|
||||
add_executable(${PROJECT_NAME}Tests ${TEST_SOURCES})
|
||||
target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME} Utils)
|
||||
add_test(NAME ${PROJECT_NAME}Tests COMMAND ${PROJECT_NAME}Tests)
|
||||
|
||||
install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}/lib)
|
||||
18
Containers/private/Containers.cpp
Normal file
18
Containers/private/Containers.cpp
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
|
||||
#include "ContainersCommon.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
namespace tp {
|
||||
|
||||
static ModuleManifest* sModuleDependencies[] = { &gModuleBase,nullptr };
|
||||
ModuleManifest gModuleContainers = ModuleManifest("Containers", nullptr, nullptr, sModuleDependencies);
|
||||
|
||||
void* DefaultAllocator::allocate(ualni size) {
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
void DefaultAllocator::deallocate(void* p) {
|
||||
free(p);
|
||||
}
|
||||
}
|
||||
360
Containers/public/AvlTree.hpp
Normal file
360
Containers/public/AvlTree.hpp
Normal file
|
|
@ -0,0 +1,360 @@
|
|||
#pragma once
|
||||
|
||||
#include "ContainersCommon.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
template <typename NumericType>
|
||||
struct AvlNumericKey {
|
||||
|
||||
NumericType val;
|
||||
|
||||
AvlNumericKey() = default;
|
||||
AvlNumericKey(NumericType val) : val(val) {}
|
||||
|
||||
inline bool descentRight(AvlNumericKey in) const { return in.val > val; }
|
||||
inline bool descentLeft(AvlNumericKey in) const { return in.val < val; }
|
||||
inline bool exactNode(AvlNumericKey in) const { return in.val == val; }
|
||||
|
||||
inline AvlNumericKey getFindKey(/**/) const { return val; }
|
||||
inline AvlNumericKey keyInRightSubtree(AvlNumericKey in) const { return in.val; }
|
||||
inline AvlNumericKey keyInLeftSubtree(AvlNumericKey in) const { return in.val; }
|
||||
|
||||
inline void updateTreeCacheCallBack() {}
|
||||
};
|
||||
|
||||
template <typename Key, typename Data, class Allocator = DefaultAllocator>
|
||||
class AvlTree {
|
||||
typedef SelCopyArg<Key> KeyArg;
|
||||
typedef SelCopyArg<Data> DataArg;
|
||||
|
||||
public:
|
||||
class Node {
|
||||
friend AvlTree;
|
||||
|
||||
private:
|
||||
Node(KeyArg aKey, DataArg aData) : key(aKey), data(aData) {}
|
||||
|
||||
public:
|
||||
Data data;
|
||||
Key key;
|
||||
|
||||
private:
|
||||
Node* mLeft = nullptr;
|
||||
Node* mRight = nullptr;
|
||||
Node* mParent = nullptr;
|
||||
ualni mHeight = 0;
|
||||
|
||||
private:
|
||||
inline bool descentRight(KeyArg aKey) const { return key.descentRight(aKey); }
|
||||
inline bool descentLeft(KeyArg aKey) const { return key.descentRight(aKey); }
|
||||
inline bool exactNode(KeyArg aKey) const { return key.exactNode(aKey); }
|
||||
|
||||
inline KeyArg getFindKey(const Node* node = nullptr) const { return key.getFindKey(/*node*/); }
|
||||
inline KeyArg keyInRightSubtree(KeyArg aKey) const { return key.keyInRightSubtree(aKey); }
|
||||
inline KeyArg keyInLeftSubtree(KeyArg aKey) const { return key.keyInLeftSubtree(aKey); }
|
||||
|
||||
inline void updateTreeCacheCallBack() { key.updateTreeCacheCallBack(); }
|
||||
};
|
||||
|
||||
private:
|
||||
Node* mRoot = nullptr;
|
||||
ualni mSize = 0;
|
||||
Allocator mAlloc;
|
||||
|
||||
private:
|
||||
|
||||
inline void deleteNode(Node* node) {
|
||||
node->~Node();
|
||||
mAlloc.deallocate(node);
|
||||
}
|
||||
|
||||
inline Node* newNode(KeyArg key, DataArg data) {
|
||||
return new (mAlloc.allocate(sizeof(Node))) Node(key, data);
|
||||
}
|
||||
|
||||
inline ualni getNodeHeight(const Node* node) const {
|
||||
return node ? node->mHeight : -1;
|
||||
}
|
||||
|
||||
// returns new head
|
||||
Node* rotateLeft(Node* pivot) {
|
||||
DEBUG_ASSERT(pivot);
|
||||
|
||||
Node* const head = pivot;
|
||||
Node* const right = pivot->mRight;
|
||||
Node* const right_left = right->mLeft;
|
||||
Node* const parent = pivot->mParent;
|
||||
|
||||
// parents
|
||||
if (right_left) right_left->mParent = head;
|
||||
head->mParent = right;
|
||||
right->mParent = parent;
|
||||
|
||||
// children
|
||||
head->mRight = right_left;
|
||||
right->mLeft = head;
|
||||
|
||||
// heights
|
||||
head->mHeight = 1 + max(getNodeHeight(head->mLeft), getNodeHeight(head->mRight));
|
||||
right->mHeight = 1 + max(getNodeHeight(right->mLeft), getNodeHeight(right->mRight));
|
||||
|
||||
// cache
|
||||
head->updateTreeCacheCallBack();
|
||||
right->updateTreeCacheCallBack();
|
||||
|
||||
return right;
|
||||
}
|
||||
|
||||
Node* rotateRight(Node* pivot) {
|
||||
DEBUG_ASSERT(pivot);
|
||||
|
||||
Node* const head = pivot;
|
||||
Node* const left = pivot->mLeft;
|
||||
Node* const left_right = left->mRight;
|
||||
Node* const parent = pivot->mParent;
|
||||
|
||||
// parents
|
||||
if (left_right) left_right->mParent = head;
|
||||
head->mParent = left;
|
||||
left->mParent = parent;
|
||||
|
||||
// children
|
||||
head->mLeft = left_right;
|
||||
left->mRight = head;
|
||||
|
||||
// heights
|
||||
head->mHeight = 1 + max(getNodeHeight(head->mLeft), getNodeHeight(head->mRight));
|
||||
left->mHeight = 1 + max(getNodeHeight(left->mLeft), getNodeHeight(left->mRight));
|
||||
|
||||
// cache
|
||||
head->updateTreeCacheCallBack();
|
||||
left->updateTreeCacheCallBack();
|
||||
|
||||
return left;
|
||||
}
|
||||
|
||||
// recursively returns valid left or right child or root
|
||||
Node* insertUtil(Node* head, KeyArg key, DataArg data) {
|
||||
|
||||
Node* insertedNode;
|
||||
|
||||
if (head == nullptr) {
|
||||
mSize++;
|
||||
Node* out = newNode(key, data);
|
||||
out->updateTreeCacheCallBack();
|
||||
return out;
|
||||
}
|
||||
else if (head->exactNode(key)) {
|
||||
return head;
|
||||
}
|
||||
else if (head->descentRight(key)) {
|
||||
insertedNode = insertUtil(head->mRight, head->keyInRightSubtree(key), data);
|
||||
head->mRight = insertedNode;
|
||||
insertedNode->mParent = head;
|
||||
}
|
||||
else {
|
||||
insertedNode = insertUtil(head->mLeft, head->keyInLeftSubtree(key), data);
|
||||
head->mLeft = insertedNode;
|
||||
insertedNode->mParent = head;
|
||||
}
|
||||
|
||||
// update height
|
||||
head->mHeight = 1 + max(getNodeHeight(head->mRight), getNodeHeight(head->mLeft));
|
||||
|
||||
alni balance = alni(getNodeHeight(head->mRight) - getNodeHeight(head->mLeft));
|
||||
|
||||
if (balance > 1) {
|
||||
if (head->mRight->descentRight(head->keyInRightSubtree(key))) {
|
||||
return rotateLeft(head);
|
||||
}
|
||||
else {
|
||||
head->mRight = rotateRight(head->mRight);
|
||||
return rotateLeft(head);
|
||||
}
|
||||
}
|
||||
else if (balance < -1) {
|
||||
if (head->mLeft->descentLeft(head->keyInLeftSubtree(key))) {
|
||||
return rotateRight(head);
|
||||
}
|
||||
else {
|
||||
head->mLeft = rotateLeft(head->mLeft);
|
||||
return rotateRight(head);
|
||||
}
|
||||
}
|
||||
|
||||
head->updateTreeCacheCallBack();
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
Node* removeUtil(Node* head, KeyArg key) {
|
||||
if (head == nullptr) return head;
|
||||
|
||||
if (head->exactNode(key)) {
|
||||
if (head->mRight && head->mLeft) {
|
||||
Node* min = minNode(head->mRight);
|
||||
head->data = min->data;
|
||||
head->mRight = removeUtil(head->mRight, min->getFindKey(head->mRight));
|
||||
}
|
||||
else if (head->mRight) {
|
||||
head->data = head->mRight->data;
|
||||
deleteNode(head->mRight);
|
||||
head->mRight = nullptr;
|
||||
mSize--;
|
||||
}
|
||||
else if (head->mLeft) {
|
||||
head->data = head->mLeft->data;
|
||||
deleteNode(head->mLeft);
|
||||
head->mLeft = nullptr;
|
||||
mSize--;
|
||||
}
|
||||
else {
|
||||
deleteNode(head);
|
||||
mSize--;
|
||||
head = nullptr;
|
||||
}
|
||||
}
|
||||
else if (head->descentRight(key)) {
|
||||
head->mRight = removeUtil(head->mRight, head->keyInRightSubtree(key));
|
||||
}
|
||||
else if (head->descentLeft(key)) {
|
||||
head->mLeft = removeUtil(head->mLeft, head->keyInLeftSubtree(key));
|
||||
}
|
||||
|
||||
if (head == nullptr) return head;
|
||||
|
||||
head->mHeight = 1 + max(getNodeHeight(head->mRight), getNodeHeight(head->mLeft));
|
||||
alni balance = getNodeHeight(head->mRight) - getNodeHeight(head->mLeft);
|
||||
|
||||
if (balance < -1) {
|
||||
if (getNodeHeight(head->mLeft->mLeft) >= getNodeHeight(head->mLeft->mRight)) {
|
||||
return rotateRight(head);
|
||||
}
|
||||
else {
|
||||
head->mLeft = rotateLeft(head->mLeft);
|
||||
return rotateRight(head);
|
||||
}
|
||||
}
|
||||
else if (balance > 1) {
|
||||
if (getNodeHeight(head->mRight->mRight) >= getNodeHeight(head->mRight->mLeft)) {
|
||||
return rotateLeft(head);
|
||||
}
|
||||
else {
|
||||
head->mRight = rotateRight(head->mRight);
|
||||
return rotateLeft(head);
|
||||
}
|
||||
}
|
||||
|
||||
head->updateTreeCacheCallBack();
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
AvlTree() {
|
||||
MODULE_SANITY_CHECK(gModuleContainers)
|
||||
}
|
||||
|
||||
[[nodiscard]] ualni size() const {
|
||||
return mSize;
|
||||
}
|
||||
|
||||
Node* head() const {
|
||||
return this->mRoot;
|
||||
}
|
||||
|
||||
void insert(KeyArg key, DataArg data) {
|
||||
mRoot = insertUtil(mRoot, key, data);
|
||||
mRoot->mParent = nullptr;
|
||||
}
|
||||
|
||||
void remove(KeyArg key) {
|
||||
mRoot = removeUtil(mRoot, key);
|
||||
if (mRoot) mRoot->mParent = nullptr;
|
||||
}
|
||||
|
||||
Node* maxNode(Node* head) const {
|
||||
if (!head) return nullptr;
|
||||
while (head->mRight != nullptr) {
|
||||
head = head->mRight;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
Node* minNode(Node* head) const {
|
||||
if (!head) return nullptr;
|
||||
while (head->mLeft != nullptr) {
|
||||
head = head->mLeft;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
Node* find(KeyArg key) const {
|
||||
Node* iter = mRoot;
|
||||
while (true) {
|
||||
if (!iter) return nullptr;
|
||||
if (iter->exactNode(key)) return iter;
|
||||
if (iter->descentLeft(key)) {
|
||||
key = iter->keyInLeftSubtree(key);
|
||||
iter = iter->mLeft;
|
||||
} else {
|
||||
key = iter->keyInRightSubtree(key);
|
||||
iter = iter->mRight;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Node* findLessOrEq(KeyArg key) const {
|
||||
Node* iter = mRoot;
|
||||
while (true) {
|
||||
if (!iter) return nullptr;
|
||||
if (iter->exactNode(key)) return iter;
|
||||
if (iter->descentLeft(key)) {
|
||||
if (iter->mLeft) {
|
||||
key = iter->keyInLeftSubtree(key);
|
||||
iter = iter->mLeft;
|
||||
} else {
|
||||
return iter;
|
||||
}
|
||||
} else {
|
||||
if (iter->mRight) {
|
||||
key = iter->keyInRightSubtree(key);
|
||||
iter = iter->mRight;
|
||||
} else {
|
||||
return iter;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// returns first invalid node
|
||||
const Node* findInvalidNode(const Node* head) const {
|
||||
if (head == nullptr) return nullptr;
|
||||
|
||||
if (head->mLeft) {
|
||||
// TODO: incomplete test
|
||||
if (!head->descentLeft(head->mLeft->getFindKey(head))) return head;
|
||||
if (head->mLeft->mParent != head) return head;
|
||||
}
|
||||
|
||||
if (head->mRight) {
|
||||
if (!head->descentRight(head->mRight->getFindKey(head))) return head;
|
||||
if (head->mRight->mParent != head) return head;
|
||||
}
|
||||
|
||||
int balance = getNodeHeight(head->mRight) - getNodeHeight(head->mLeft);
|
||||
|
||||
if (balance > 1 || balance < -1) return head;
|
||||
|
||||
const Node* ret = findInvalidNode(head->mRight);
|
||||
|
||||
if (ret) return ret;
|
||||
|
||||
return findInvalidNode(head->mLeft);
|
||||
}
|
||||
|
||||
bool isValid() { return findInvalidNode(head()) == nullptr; }
|
||||
};
|
||||
}
|
||||
26
Containers/public/ContainersCommon.hpp
Normal file
26
Containers/public/ContainersCommon.hpp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "BaseModule.hpp"
|
||||
|
||||
namespace tp {
|
||||
extern ModuleManifest gModuleContainers;
|
||||
|
||||
class DefaultAllocator {
|
||||
public:
|
||||
DefaultAllocator() = default;
|
||||
static void *allocate(ualni);
|
||||
static void deallocate(void *);
|
||||
};
|
||||
|
||||
class DefaultSaverLoader {
|
||||
public:
|
||||
DefaultSaverLoader() = default;
|
||||
|
||||
template<typename Type>
|
||||
static void write(const Type&) {}
|
||||
|
||||
template<typename Type>
|
||||
static void read(Type&) {}
|
||||
};
|
||||
}
|
||||
322
Containers/public/List.hpp
Normal file
322
Containers/public/List.hpp
Normal file
|
|
@ -0,0 +1,322 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "ContainersCommon.hpp"
|
||||
|
||||
#include "TypeInfo.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
template <typename Type, class Allocator = DefaultAllocator>
|
||||
class List {
|
||||
|
||||
typedef SelCopyArg<Type> TypeArg;
|
||||
typedef ualni Index;
|
||||
|
||||
public:
|
||||
|
||||
struct Node {
|
||||
Type data;
|
||||
Node* next = nullptr;
|
||||
Node* prev = nullptr;
|
||||
Node() = default;
|
||||
explicit Node(TypeArg p_data) : data(p_data) {}
|
||||
Type& operator->() { return data; }
|
||||
};
|
||||
|
||||
class IteratorPointer {
|
||||
protected:
|
||||
Node* mIter;
|
||||
public:
|
||||
IteratorPointer() = default;
|
||||
Type& operator->() { return (mIter->data); }
|
||||
const Type& operator->() const { return (mIter->data); }
|
||||
};
|
||||
|
||||
class IteratorReference {
|
||||
protected:
|
||||
Node* mIter;
|
||||
public:
|
||||
IteratorReference() = default;
|
||||
Type* operator->() { return &(mIter->data); }
|
||||
const Type* operator->() const { return &(mIter->data); }
|
||||
};
|
||||
|
||||
class Iterator : public TypeSelect<TypeTraits<Type>::isPointer, IteratorPointer, IteratorReference>::Result {
|
||||
public:
|
||||
|
||||
explicit Iterator(Node* iter) { this->mIter = iter; }
|
||||
|
||||
Node* node() { return this->mIter; }
|
||||
Type& data() { return this->mIter->data; }
|
||||
const Node* node() const { return this->mIter; }
|
||||
const Type& data() const { return this->mIter->data; }
|
||||
|
||||
const Iterator& operator*() const { return *this; }
|
||||
|
||||
Iterator& operator++() {
|
||||
this->mIter = this->mIter->next;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const Iterator& left) const { return left.mIter == this->mIter; }
|
||||
bool operator!=(const Iterator& left) const { return left.mIter != this->mIter; }
|
||||
};
|
||||
|
||||
private:
|
||||
Node* mFirst = nullptr;
|
||||
Node* mLast = nullptr;
|
||||
Index mLength = 0;
|
||||
Allocator mAlloc;
|
||||
|
||||
public:
|
||||
|
||||
List() = default;
|
||||
|
||||
List(const init_list<Type>& list) { operator=(list); }
|
||||
|
||||
[[nodiscard]] inline Node* first() const { return mFirst; }
|
||||
[[nodiscard]] inline Node* last() const { return mLast; }
|
||||
[[nodiscard]] inline Index length() const { return mLength; }
|
||||
|
||||
[[nodiscard]] Node* newNode() { return new (mAlloc.allocate(sizeof(Node))) Node(); }
|
||||
[[nodiscard]] Node* newNode(TypeArg arg) { return new (mAlloc.allocate(sizeof(Node))) Node(arg); }
|
||||
[[nodiscard]] Node* newNodeNotConstructed() {
|
||||
auto node = (Node*) mAlloc.allocate(sizeof(Node));
|
||||
node->next = node->prev = nullptr;
|
||||
return node;
|
||||
}
|
||||
|
||||
[[nodiscard]] const Allocator& getAllocator() const { return mAlloc; }
|
||||
|
||||
void deleteNode(Node* node) {
|
||||
node->~Node();
|
||||
mAlloc.deallocate(node);
|
||||
}
|
||||
|
||||
Node* addNodeBack() {
|
||||
auto const out = newNode();
|
||||
pushBack(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
Node* addNodeFront() {
|
||||
auto const out = newNode();
|
||||
pushFront(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
void attach(Node* node, Node* node_to) {
|
||||
if (node_to) {
|
||||
if (node_to->next) {
|
||||
node->next = node_to->next;
|
||||
node->next->prev = node;
|
||||
}
|
||||
node_to->next = node;
|
||||
node->prev = node_to;
|
||||
if (node_to == mLast) {
|
||||
mLast = node;
|
||||
}
|
||||
} else {
|
||||
if (mFirst) {
|
||||
mFirst->prev = node;
|
||||
node->next = mFirst;
|
||||
mFirst = node;
|
||||
} else {
|
||||
mFirst = mLast = node;
|
||||
}
|
||||
}
|
||||
mLength++;
|
||||
}
|
||||
|
||||
void detach(Node* node) {
|
||||
if (node->next) {
|
||||
node->next->prev = node->prev;
|
||||
}
|
||||
if (node->prev) {
|
||||
node->prev->next = node->next;
|
||||
}
|
||||
if (node == mLast) {
|
||||
mLast = mLast->prev;
|
||||
}
|
||||
if (node == mFirst) {
|
||||
mFirst = mFirst->next;
|
||||
}
|
||||
mLength--;
|
||||
}
|
||||
|
||||
[[nodiscard]] Node* findIdx(Index idx) const {
|
||||
DEBUG_ASSERT(!mFirst || idx > mLength - 1)
|
||||
Node* found = mFirst;
|
||||
for (int i = 0; i != idx; i++) {
|
||||
found = found->next;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
[[nodiscard]] Node* find(const TypeArg data) const {
|
||||
Node* found = mFirst;
|
||||
for (alni i = 0; data != found->data; i++) {
|
||||
if (!found->next) {
|
||||
return nullptr;
|
||||
}
|
||||
found = found->next;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline const Type& operator[](Index idx) const {
|
||||
DEBUG_ASSERT(idx < mLength)
|
||||
return find(idx)->data;
|
||||
}
|
||||
|
||||
void pushBack(Node* new_node) { attach(new_node, mLast); }
|
||||
void pushFront(Node* new_node) { attach(new_node, nullptr); }
|
||||
void pushBack(TypeArg data) { pushBack(newNode(data)); }
|
||||
void pushFront(TypeArg data) { pushFront(newNode(data)); }
|
||||
|
||||
void popBack() {
|
||||
DEBUG_ASSERT(mLast)
|
||||
detach(mLast);
|
||||
deleteNode(mLast);
|
||||
}
|
||||
|
||||
void popFront() {
|
||||
DEBUG_ASSERT(mFirst)
|
||||
auto temp = mFirst;
|
||||
detach(mFirst);
|
||||
deleteNode(temp);
|
||||
}
|
||||
|
||||
void insert(Node* node, Index idx) {
|
||||
if (!mLength) {
|
||||
attach(node, mLast);
|
||||
} else if (idx >= mLength) {
|
||||
attach(node, nullptr);
|
||||
} else {
|
||||
attach(node, find(idx)->prev);
|
||||
}
|
||||
}
|
||||
|
||||
void insert(TypeArg data, Index idx) {
|
||||
insert(newNode(data), idx);
|
||||
}
|
||||
|
||||
void removeNode(Node* node) {
|
||||
detach(node);
|
||||
deleteNode(node);
|
||||
}
|
||||
|
||||
void removeAll() {
|
||||
while (mFirst) {
|
||||
popFront();
|
||||
}
|
||||
}
|
||||
|
||||
// copies data
|
||||
List& operator+=(const List& in) {
|
||||
for (auto node : in) {
|
||||
pushBack(node.data());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& operator+=(const init_list<Type>& list) {
|
||||
for (auto item : list) {
|
||||
pushBack(item);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& operator=(const List& in) {
|
||||
if (this == &in) { return *this; }
|
||||
removeAll();
|
||||
(*this) += in;
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& operator=(const init_list<Type>& list) {
|
||||
removeAll();
|
||||
*this += list;
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool operator==(const List& in) const {
|
||||
if (in == *this) { return true; }
|
||||
if (in.length() != length()) {
|
||||
return false;
|
||||
}
|
||||
Node* left = in.first();
|
||||
Node* right = first();
|
||||
while (left && right) {
|
||||
if (left->data != right->data) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (left != right) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename compare_val>
|
||||
[[nodiscard]] Node* find(bool (*found)(Node* node, compare_val val), compare_val value) const {
|
||||
for (Node* node = mFirst; node; node = node->next) {
|
||||
if (found(node, value)) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] Iterator begin() const {
|
||||
Iterator out(mFirst);
|
||||
return out;
|
||||
}
|
||||
|
||||
[[nodiscard]] Iterator end() const {
|
||||
return Iterator(nullptr);
|
||||
}
|
||||
|
||||
void invert() {
|
||||
Node* iter = mFirst;
|
||||
Node* tmp;
|
||||
while (iter) {
|
||||
tmp = iter;
|
||||
iter = iter->next;
|
||||
swap(tmp->next, tmp->prev);
|
||||
}
|
||||
swap(mFirst, mLast);
|
||||
}
|
||||
|
||||
void detachAll() {
|
||||
while (mFirst) {
|
||||
detach(mFirst);
|
||||
}
|
||||
}
|
||||
|
||||
template<class Saver>
|
||||
void write(Saver& file) const {
|
||||
file.write(mLength);
|
||||
for (auto item : *this) {
|
||||
file.write(item.data());
|
||||
}
|
||||
}
|
||||
|
||||
template<class Loader>
|
||||
void read(Loader& file) {
|
||||
removeAll();
|
||||
ualni len;
|
||||
file.read(len);
|
||||
for (auto i = len; i; i--) {
|
||||
auto node = newNodeNotConstructed();
|
||||
file.read(node->data);
|
||||
pushBack(node);
|
||||
}
|
||||
}
|
||||
|
||||
~List() {
|
||||
removeAll();
|
||||
}
|
||||
};
|
||||
}
|
||||
397
Containers/public/Map.hpp
Normal file
397
Containers/public/Map.hpp
Normal file
|
|
@ -0,0 +1,397 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "ContainersCommon.hpp"
|
||||
#include "Common.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
template<typename Key>
|
||||
ualni DefaultHashFunc(SelCopyArg<Key> key) {
|
||||
return hash(key);
|
||||
}
|
||||
|
||||
template<
|
||||
typename tKey, typename tVal,
|
||||
class tAllocator = DefaultAllocator,
|
||||
ualni(*tHashFunc)(SelCopyArg<tKey>) = DefaultHashFunc<tKey>,
|
||||
int tTableInitialSize = 4
|
||||
>
|
||||
class Map {
|
||||
|
||||
enum {
|
||||
MAP_PERTURB_SHIFT = 5,
|
||||
MAP_MIN_SIZE = 4,
|
||||
MAP_MAX_LOAD_PERCENTAGE = 66,
|
||||
};
|
||||
|
||||
typedef SelCopyArg<tKey> KeyArg;
|
||||
typedef SelCopyArg<tVal> ValArg;
|
||||
|
||||
public:
|
||||
|
||||
class Node {
|
||||
friend Map;
|
||||
Node(KeyArg aKey, ValArg aVal) : key(aKey), val(aVal) {}
|
||||
public:
|
||||
tKey key;
|
||||
tVal val;
|
||||
};
|
||||
|
||||
struct Idx {
|
||||
alni idx = -1;
|
||||
operator bool() { return idx != -1; }
|
||||
};
|
||||
|
||||
private:
|
||||
tAllocator mAlloc;
|
||||
Node** mTable;
|
||||
ualni mNSlots = 0;
|
||||
ualni mNEntries = 0;
|
||||
|
||||
private:
|
||||
|
||||
constexpr halnf maxLoadFactor() { return halnf(MAP_MAX_LOAD_PERCENTAGE) / 100.f; }
|
||||
|
||||
inline Node** newTable(const ualni len) {
|
||||
return new(mAlloc.allocate(sizeof(Node*) * len)) Node*[len]();
|
||||
}
|
||||
|
||||
inline Node* newNode(KeyArg key, ValArg val) {
|
||||
return new(mAlloc.allocate(sizeof(Node))) Node(key, val);
|
||||
}
|
||||
|
||||
inline Node* newNodeNotConstructed() {
|
||||
return (Node*) mAlloc.allocate(sizeof(Node));
|
||||
}
|
||||
|
||||
inline void deleteTable(Node** table) {
|
||||
mAlloc.deallocate(table);
|
||||
}
|
||||
|
||||
inline void deleteNode(Node* p) {
|
||||
p->~Node();
|
||||
mAlloc.deallocate(p);
|
||||
}
|
||||
|
||||
void markDeletedSlot(ualni idx) const {
|
||||
mTable[idx] = (Node*)-1;
|
||||
}
|
||||
|
||||
static bool isDeletedNode(Node* node) {
|
||||
return node == (Node*)-1;
|
||||
}
|
||||
|
||||
void rehash() {
|
||||
alni nSlotsOld = mNSlots;
|
||||
Node** tableOld = mTable;
|
||||
|
||||
mNSlots = next2pow((uhalni)((1.f / (maxLoadFactor())) * mNEntries + 1));
|
||||
mTable = newTable(mNSlots);
|
||||
mNEntries = 0;
|
||||
|
||||
for (alni i = 0; i < nSlotsOld; i++) {
|
||||
if (!tableOld[i] || isDeletedNode(tableOld[i])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
alni idx = findSlotWrite(tableOld[i]->key);
|
||||
mTable[idx] = tableOld[i];
|
||||
mNEntries++;
|
||||
}
|
||||
|
||||
deleteTable(tableOld);
|
||||
}
|
||||
|
||||
alni findSlotRead(KeyArg key) const {
|
||||
ualni const hashed_key = tHashFunc(key);
|
||||
ualni const mask = mNSlots - 1;
|
||||
ualni const shift = (hashed_key >> MAP_PERTURB_SHIFT) & ~1;
|
||||
alni idx = hashed_key & mask;
|
||||
NEXT:
|
||||
if (isDeletedNode(mTable[idx])) {
|
||||
goto SKIP;
|
||||
}
|
||||
if (!mTable[idx]) {
|
||||
return -1;
|
||||
}
|
||||
if (mTable[idx]->key == key) {
|
||||
return idx;
|
||||
}
|
||||
SKIP:
|
||||
idx = ((5 * idx) + 1 + shift) & mask;
|
||||
goto NEXT;
|
||||
}
|
||||
|
||||
// compares keys only when collisions occur
|
||||
alni findSlotReadExisting(KeyArg key) const {
|
||||
ualni const hashed_key = tHashFunc(key);
|
||||
ualni const mask = mNSlots - 1;
|
||||
ualni const shift = (hashed_key >> MAP_PERTURB_SHIFT) & ~1;
|
||||
alni idx = hashed_key & mask;
|
||||
NEXT:
|
||||
if (isDeletedNode(mTable[idx])) {
|
||||
goto SKIP;
|
||||
}
|
||||
if (!mTable[idx]) {
|
||||
return -1;
|
||||
}
|
||||
if (mTable[((5 * idx) + 1 + shift) & mask] == nullptr) {
|
||||
return idx;
|
||||
}
|
||||
if (mTable[idx]->key == key) {
|
||||
return idx;
|
||||
}
|
||||
SKIP:
|
||||
idx = ((5 * idx) + 1 + shift) & mask;
|
||||
goto NEXT;
|
||||
}
|
||||
|
||||
ualni findSlotWrite(KeyArg key) const {
|
||||
ualni const hashed_key = tHashFunc(key);
|
||||
ualni const mask = mNSlots - 1;
|
||||
ualni const shift = (hashed_key >> MAP_PERTURB_SHIFT) & ~1;
|
||||
ualni idx = hashed_key & mask;
|
||||
NEXT:
|
||||
if (isDeletedNode(mTable[idx]) || !mTable[idx]) {
|
||||
return idx;
|
||||
}
|
||||
if (mTable[idx]->key == key) {
|
||||
return idx;
|
||||
}
|
||||
idx = ((5 * idx) + 1 + shift) & mask;
|
||||
goto NEXT;
|
||||
}
|
||||
|
||||
void put(Node* node) {
|
||||
const ualni idx = findSlotWrite(node->key);
|
||||
|
||||
if (!mTable[idx] || isDeletedNode(mTable[idx])) {
|
||||
mNEntries++;
|
||||
}
|
||||
|
||||
mTable[idx] = node;
|
||||
|
||||
if ((halnf)mNEntries / mNSlots > maxLoadFactor()) {
|
||||
rehash();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
Map() {
|
||||
MODULE_SANITY_CHECK(gModuleContainers)
|
||||
mNSlots = next2pow(uhalni(tTableInitialSize - 1));
|
||||
mTable = newTable(mNSlots);
|
||||
}
|
||||
|
||||
Node** buff() const {
|
||||
return mTable;
|
||||
}
|
||||
|
||||
[[nodiscard]] ualni size() const {
|
||||
return mNEntries;
|
||||
}
|
||||
|
||||
[[nodiscard]] ualni slotsSize() const {
|
||||
return mNEntries;
|
||||
}
|
||||
|
||||
[[nodiscard]] const tAllocator& getAllocator() const {
|
||||
return mAlloc;
|
||||
}
|
||||
|
||||
void put(KeyArg key, ValArg val) {
|
||||
const ualni idx = findSlotWrite(key);
|
||||
if (!mTable[idx] || isDeletedNode(mTable[idx])) {
|
||||
mTable[idx] = newNode(key, val);
|
||||
mNEntries++;
|
||||
}
|
||||
mTable[idx]->val = val;
|
||||
if ((halnf) mNEntries / mNSlots > maxLoadFactor()) {
|
||||
rehash();
|
||||
}
|
||||
}
|
||||
|
||||
// undefined behavior if item is not presents
|
||||
tVal& get(KeyArg key) {
|
||||
DEBUG_ASSERT(findSlotRead(key) != -1 && "Key Error")
|
||||
return mTable[findSlotReadExisting(key)]->val;
|
||||
}
|
||||
|
||||
const tVal& get(KeyArg key) const {
|
||||
DEBUG_ASSERT(findSlotRead(key) != -1 && "Key Error")
|
||||
return mTable[findSlotReadExisting(key)]->val;
|
||||
}
|
||||
|
||||
[[nodiscard]] Idx presents(KeyArg key) const { return { findSlotRead(key) }; }
|
||||
|
||||
void remove(KeyArg key) {
|
||||
DEBUG_ASSERT(findSlotRead(key) != -1 && "Key Error")
|
||||
auto idx = findSlotReadExisting(key);
|
||||
|
||||
deleteNode(mTable[idx]);
|
||||
|
||||
markDeletedSlot(idx);
|
||||
|
||||
mNEntries--;
|
||||
if (halnf(mNEntries / mNSlots) < 1.f - maxLoadFactor()) {
|
||||
rehash();
|
||||
}
|
||||
}
|
||||
|
||||
const tVal& getSlotVal(ualni slot) const {
|
||||
DEBUG_ASSERT(slot < mNSlots && (mTable[slot] && !isDeletedNode(mTable[slot])) && "Key Error")
|
||||
return mTable[slot]->val;
|
||||
}
|
||||
|
||||
tVal& getSlotVal(ualni slot) {
|
||||
DEBUG_ASSERT(slot < mNSlots && (mTable[slot] && !isDeletedNode(mTable[slot])) && "Key Error")
|
||||
return mTable[slot]->val;
|
||||
}
|
||||
|
||||
const tVal& getSlotVal(Idx slot) const {
|
||||
DEBUG_ASSERT(slot.idx < mNSlots && (mTable[slot.idx] && !isDeletedNode(mTable[slot.idx])) && "Key Error")
|
||||
return mTable[slot]->val;
|
||||
}
|
||||
|
||||
tVal& getSlotVal(Idx slot) {
|
||||
DEBUG_ASSERT(slot.idx < mNSlots && (mTable[slot.idx] && !isDeletedNode(mTable[slot.idx])) && "Key Error")
|
||||
return mTable[slot.idx]->val;
|
||||
}
|
||||
|
||||
Map& operator=(const Map& in) {
|
||||
if (this == &in) {
|
||||
return *this;
|
||||
}
|
||||
removeAll();
|
||||
mNSlots = in.mNSlots;
|
||||
mTable = newTable(mNSlots);
|
||||
for (alni i = 0; i < mNSlots; i++) {
|
||||
if (in.mTable[i] && !isDeletedNode(in.mTable[i])) {
|
||||
put(in.mTable[i]->key, in.mTable[i]->val);
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool operator==(const Map& in) const {
|
||||
if (this == &in) {
|
||||
return true;
|
||||
}
|
||||
if (in.mNEntries != mNEntries) {
|
||||
return false;
|
||||
}
|
||||
for (auto i : in) {
|
||||
if (!presents(i->key) || get(i->key) != i->val) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void removeAll() {
|
||||
for (ualni i = 0; i < mNSlots; i++) {
|
||||
if (mTable[i] && !isDeletedNode(mTable[i])) {
|
||||
deleteNode(mTable[i]);
|
||||
}
|
||||
}
|
||||
deleteTable(mTable);
|
||||
mTable = newTable(tTableInitialSize);
|
||||
mNSlots = tTableInitialSize;
|
||||
mNEntries = 0;
|
||||
}
|
||||
|
||||
[[nodiscard]] alni slotIdx(alni entry_idx_in) const {
|
||||
alni entry_idx = -1;
|
||||
for (alni slot_idx = 0; slot_idx < mNSlots; slot_idx++) {
|
||||
if (mTable[slot_idx]) {
|
||||
entry_idx++;
|
||||
}
|
||||
if (entry_idx == entry_idx_in) {
|
||||
return slot_idx;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
Node* GetEntry(ualni idx) {
|
||||
auto slot = slotIdx(idx);
|
||||
DEBUG_ASSERT(slot != -1 && "Key error")
|
||||
return mTable[slot];
|
||||
}
|
||||
|
||||
const Node* GetEntry(ualni idx) const {
|
||||
auto slot = slotIdx(idx);
|
||||
DEBUG_ASSERT(slot != -1 && "Key error")
|
||||
return mTable[slot];
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
class Iterator {
|
||||
const Map* map;
|
||||
Node* mIter;
|
||||
alni mSlot;
|
||||
alni mEntry;
|
||||
|
||||
friend Map;
|
||||
explicit Iterator(const Map* _map) {
|
||||
mSlot = -1;
|
||||
mEntry = -1;
|
||||
map = _map;
|
||||
this->operator++();
|
||||
}
|
||||
|
||||
public:
|
||||
Node* operator->() { return mIter; }
|
||||
const Node* operator->() const { return mIter; }
|
||||
const Iterator& operator*() const { return *this; }
|
||||
|
||||
bool operator!=(ualni idx) const { return mSlot != idx; }
|
||||
|
||||
void operator++() {
|
||||
mSlot++;
|
||||
while ((map->isDeletedNode(map->mTable[mSlot]) || !map->mTable[mSlot]) && (mSlot != map->mNSlots)) {
|
||||
mSlot++;
|
||||
}
|
||||
if (mSlot != map->mNSlots) {
|
||||
mIter = map->mTable[mSlot];
|
||||
mEntry++;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
[[nodiscard]] Iterator begin() const {
|
||||
return Iterator(this);
|
||||
}
|
||||
|
||||
[[nodiscard]] ualni end() const {
|
||||
return mNSlots;
|
||||
}
|
||||
|
||||
template<class Saver>
|
||||
void write(Saver& file) {
|
||||
file.write(mNEntries);
|
||||
for (auto item : *this) {
|
||||
file.write(item->val);
|
||||
file.write(item->key);
|
||||
}
|
||||
}
|
||||
|
||||
template<class Loader>
|
||||
void read(Loader& file) {
|
||||
removeAll();
|
||||
ualni len;
|
||||
file.read(len);
|
||||
for (auto i = len; i; i--) {
|
||||
auto node = newNodeNotConstructed();
|
||||
file.read(node->val);
|
||||
file.read(node->key);
|
||||
put(node);
|
||||
}
|
||||
}
|
||||
|
||||
~Map() { removeAll(); }
|
||||
};
|
||||
}
|
||||
31
Containers/tests/AvlTest.cpp
Normal file
31
Containers/tests/AvlTest.cpp
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
|
||||
#include "Tests.hpp"
|
||||
|
||||
#include "AvlTree.hpp"
|
||||
|
||||
#include "Testing.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace tp;
|
||||
|
||||
TEST_DEF_STATIC(Simple) {
|
||||
AvlTree<AvlNumericKey<alni>, TestClass, TestAllocator> tree;
|
||||
|
||||
TEST(tree.size() == 0);
|
||||
TEST(tree.head() == nullptr);
|
||||
|
||||
tree.insert(6, TestClass(6));
|
||||
TEST(tree.isValid());
|
||||
TEST(tree.size() == 1);
|
||||
TEST(tree.head()->data == TestClass(6));
|
||||
|
||||
tree.remove(6);
|
||||
TEST(tree.isValid());
|
||||
TEST(tree.size() == 0);
|
||||
TEST(tree.head() == nullptr);
|
||||
}
|
||||
|
||||
TEST_DEF(Avl) {
|
||||
testSimple();
|
||||
}
|
||||
88
Containers/tests/ListTest.cpp
Normal file
88
Containers/tests/ListTest.cpp
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
|
||||
#include "Tests.hpp"
|
||||
#include "Testing.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace tp;
|
||||
|
||||
TEST_DEF_STATIC(SimpleReference) {
|
||||
tp::List<TestClass, TestAllocator> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
|
||||
|
||||
list.pushBack(TestClass(5));
|
||||
list.pushFront(TestClass(0));
|
||||
|
||||
ualni i = -1;
|
||||
for (auto iter : list) {
|
||||
i++;
|
||||
TEST_EQUAL(iter->getVal(), i);
|
||||
}
|
||||
|
||||
TEST(i == 5);
|
||||
|
||||
list.removeAll();
|
||||
TEST(list.getAllocator().getAllocationsCount() == 0);
|
||||
}
|
||||
|
||||
TEST_DEF_STATIC(SimplePointer) {
|
||||
tp::List<TestClass*, TestAllocator> list = { new TestClass(1), new TestClass(2), new TestClass(3), new TestClass(4) };
|
||||
|
||||
list.pushBack(new TestClass(5));
|
||||
list.pushFront(new TestClass(0));
|
||||
|
||||
ualni i = -1;
|
||||
for (auto iter : list) {
|
||||
i++;
|
||||
TEST_EQUAL(iter->getVal(), i);
|
||||
}
|
||||
|
||||
TEST(i == 5);
|
||||
|
||||
list.removeAll();
|
||||
|
||||
TEST(list.getAllocator().getAllocationsCount() == 0);
|
||||
}
|
||||
|
||||
TEST_DEF_STATIC(Copy) {
|
||||
tp::List<TestClass, TestAllocator> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
|
||||
tp::List<TestClass, TestAllocator> list2 = list;
|
||||
|
||||
TEST_EQUAL(list, list2);
|
||||
|
||||
list.removeAll();
|
||||
list2.removeAll();
|
||||
|
||||
TEST(list.getAllocator().getAllocationsCount() == 0);
|
||||
TEST(list2.getAllocator().getAllocationsCount() == 0);
|
||||
}
|
||||
|
||||
TEST_DEF_STATIC(SaveLoad) {
|
||||
tp::List<TestClass, TestAllocator> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
|
||||
|
||||
TestFile file;
|
||||
|
||||
list.write(file);
|
||||
|
||||
list.removeAll();
|
||||
|
||||
file.setAddress(0);
|
||||
|
||||
list.read(file);
|
||||
|
||||
ualni i = 0;
|
||||
for (auto iter : list) {
|
||||
i++;
|
||||
TEST_EQUAL(iter->getVal(), i);
|
||||
}
|
||||
TEST(i == 4);
|
||||
|
||||
list.removeAll();
|
||||
|
||||
TEST(list.getAllocator().getAllocationsCount() == 0);
|
||||
}
|
||||
|
||||
TEST_DEF(List) {
|
||||
testSimplePointer();
|
||||
testSimpleReference();
|
||||
testSaveLoad();
|
||||
}
|
||||
137
Containers/tests/MapTest.cpp
Normal file
137
Containers/tests/MapTest.cpp
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
|
||||
#include "Tests.hpp"
|
||||
#include "Testing.hpp"
|
||||
|
||||
#include "Map.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace tp;
|
||||
|
||||
TEST_DEF_STATIC(SimpleReference) {
|
||||
tp::Map<tp::ualni, TestClass, TestAllocator> map;
|
||||
|
||||
for (auto i : Range(1000, 100000)) {
|
||||
map.put(i, TestClass(i));
|
||||
}
|
||||
|
||||
for (auto i : Range(1000, 100000)) {
|
||||
TEST(map.presents(i));
|
||||
TEST_EQUAL(map.get(i).getVal(), i);
|
||||
}
|
||||
|
||||
for (auto i : Range(1000, 100000)) {
|
||||
map.put(i, TestClass(i));
|
||||
}
|
||||
|
||||
for (auto i : Range(1000, 2000)) {
|
||||
TEST(map.presents(i));
|
||||
map.remove(i);
|
||||
TEST(!map.presents(i));
|
||||
}
|
||||
|
||||
for (auto i : Range(2000, 100000)) {
|
||||
TEST(map.presents(i));
|
||||
TEST_EQUAL(map.get(i).getVal(), i);
|
||||
}
|
||||
|
||||
for (auto i : map) {
|
||||
i->val.setVal(3);
|
||||
}
|
||||
|
||||
map.removeAll();
|
||||
|
||||
TEST(map.getAllocator().getAllocationsCount() == 1);
|
||||
}
|
||||
|
||||
TEST_DEF_STATIC(SimplePointer) {
|
||||
tp::Map<tp::ualni, TestClass*, TestAllocator> map;
|
||||
|
||||
for (auto i : Range(1000)) {
|
||||
map.put(i, new TestClass(i));
|
||||
}
|
||||
|
||||
for (auto i : Range(1000)) {
|
||||
TEST(map.presents(i));
|
||||
TEST_EQUAL(map.get(i)->getVal(), i);
|
||||
}
|
||||
|
||||
for (auto i : Range(1000)) {
|
||||
map.put(i, new TestClass(i));
|
||||
}
|
||||
|
||||
for (auto i : Range(900, 1000)) {
|
||||
TEST(map.presents(i));
|
||||
map.remove(i);
|
||||
TEST(!map.presents(i));
|
||||
}
|
||||
|
||||
for (auto i : Range(900)) {
|
||||
TEST(map.presents(i));
|
||||
TEST_EQUAL(map.get(i)->getVal(), i);
|
||||
}
|
||||
|
||||
for (auto i : map) {
|
||||
i->val->setVal(3);
|
||||
delete i->val;
|
||||
}
|
||||
|
||||
map.removeAll();
|
||||
|
||||
TEST(map.getAllocator().getAllocationsCount() == 1);
|
||||
}
|
||||
|
||||
TEST_DEF_STATIC(Copy) {
|
||||
tp::Map<tp::ualni, TestClass, TestAllocator> map;
|
||||
|
||||
for (auto i : Range(10)) {
|
||||
map.put(i, TestClass(i));
|
||||
}
|
||||
|
||||
tp::Map<tp::ualni, TestClass, TestAllocator> map2 = map;
|
||||
|
||||
TEST_EQUAL(map, map2);
|
||||
|
||||
map.removeAll();
|
||||
map2.removeAll();
|
||||
|
||||
TEST(map.getAllocator().getAllocationsCount() == 1);
|
||||
TEST(map2.getAllocator().getAllocationsCount() == 1);
|
||||
}
|
||||
|
||||
TEST_DEF_STATIC(SaveLoad) {
|
||||
tp::Map<tp::ualni, TestClass, TestAllocator> map;
|
||||
|
||||
for (auto i : Range(10)) {
|
||||
map.put(i, TestClass(i));
|
||||
}
|
||||
|
||||
TestFile file;
|
||||
|
||||
map.write(file);
|
||||
|
||||
map.removeAll();
|
||||
|
||||
TEST(map.getAllocator().getAllocationsCount() == 1);
|
||||
|
||||
file.setAddress(0);
|
||||
|
||||
map.read(file);
|
||||
|
||||
TEST(map.getAllocator().getAllocationsCount() == 11);
|
||||
|
||||
for (auto i : Range(10)) {
|
||||
TEST(map.presents(i));
|
||||
TEST_EQUAL(map.get(i).getVal(), i);
|
||||
}
|
||||
|
||||
map.removeAll();
|
||||
|
||||
TEST(map.getAllocator().getAllocationsCount() == 1);
|
||||
}
|
||||
|
||||
TEST_DEF(Map) {
|
||||
testSimplePointer();
|
||||
testSimpleReference();
|
||||
testSaveLoad();
|
||||
}
|
||||
41
Containers/tests/Tests.cpp
Normal file
41
Containers/tests/Tests.cpp
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
|
||||
#include "Tests.hpp"
|
||||
|
||||
#include "Testing.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
static bool init(const tp::ModuleManifest* self) {
|
||||
tp::gTesting.setRootName(self->getName());
|
||||
return true;
|
||||
}
|
||||
|
||||
void* TestAllocator::allocate(tp::ualni size) {
|
||||
nAllocations++;
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
void TestAllocator::deallocate(void* p) {
|
||||
nAllocations--;
|
||||
free(p);
|
||||
}
|
||||
|
||||
tp::ualni TestAllocator::getAllocationsCount() const {
|
||||
return nAllocations;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
tp::ModuleManifest* deps[] = { &tp::gModuleUtils, nullptr };
|
||||
tp::ModuleManifest testModule("ContainersTest", init, nullptr, deps);
|
||||
|
||||
if (!testModule.initialize()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
testList();
|
||||
testMap();
|
||||
testAvl();
|
||||
|
||||
testModule.deinitialize();
|
||||
}
|
||||
73
Containers/tests/Tests.hpp
Normal file
73
Containers/tests/Tests.hpp
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#pragma once
|
||||
|
||||
#include "Utils.hpp"
|
||||
|
||||
class TestClass {
|
||||
tp::ualni val2 = 0;
|
||||
tp::ualni val1;
|
||||
public:
|
||||
|
||||
explicit TestClass(tp::ualni val) : val1(val) {}
|
||||
|
||||
template<class Saver>
|
||||
void write(Saver& file) const {
|
||||
file.write(val1);
|
||||
}
|
||||
|
||||
template<class Loader>
|
||||
void read(Loader& file) {
|
||||
file.read(val1);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool operator==(const TestClass& in) const {
|
||||
return in.val1 == val1;
|
||||
}
|
||||
|
||||
[[nodiscard]] tp::ualni getVal() const { return val1; }
|
||||
void setVal(tp::ualni val) { val1 = val; }
|
||||
};
|
||||
|
||||
class TestAllocator {
|
||||
tp::ualni nAllocations = 0;
|
||||
public:
|
||||
TestAllocator() = default;
|
||||
void* allocate(tp::ualni size);
|
||||
void deallocate(void* p);
|
||||
[[nodiscard]] tp::ualni getAllocationsCount() const;
|
||||
};
|
||||
|
||||
class TestFile {
|
||||
tp::ualni mem[1024] = { 0 };
|
||||
tp::ualni address = 0;
|
||||
|
||||
public:
|
||||
TestFile() = default;
|
||||
|
||||
template<typename Type>
|
||||
void write(const Type& val) {
|
||||
val.write(*this);
|
||||
}
|
||||
|
||||
template<>
|
||||
void write<tp::ualni>(const tp::ualni& val) {
|
||||
mem[address] = val;
|
||||
address++;
|
||||
}
|
||||
|
||||
void setAddress(tp::ualni addr) { address = addr; }
|
||||
|
||||
template<typename Type>
|
||||
void read(Type& val) {
|
||||
val.read(*this);
|
||||
}
|
||||
|
||||
template<>
|
||||
void read<tp::ualni>(tp::ualni& val) {
|
||||
val = mem[address];
|
||||
address++;
|
||||
}
|
||||
};
|
||||
|
||||
void testList();
|
||||
void testMap();
|
||||
void testAvl();
|
||||
Loading…
Add table
Add a link
Reference in a new issue