Map changes

This commit is contained in:
2021-11-02 19:42:00 +01:00
parent 1b7ab9b1f1
commit ca8070ef71
12 changed files with 168 additions and 97 deletions
+20
View File
@@ -19,6 +19,26 @@
] ]
}, },
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30037/bin/Hostx64/x64/cl.exe" "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30037/bin/Hostx64/x64/cl.exe"
},
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/src/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.cmake-tools",
"cppStandard": "c++20",
"browse": {
"path": [
"external/**"
]
},
"compilerPath": "/usr/bin/g++"
} }
], ],
"version": 4 "version": 4
+11
View File
@@ -62,6 +62,17 @@
"traceResponse": true "traceResponse": true
} }
}, },
{
"name": "Test (Linux)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/bin/Debug/Seele_unit_tests",
"args": [],
"stopAtEntry": false,
"console": "internalConsole",
"cwd": "${workspaceRoot}/bin/Debug",
"environment": [],
},
{ {
"name": "Test", "name": "Test",
"type": "cppvsdbg", "type": "cppvsdbg",
+2 -7
View File
@@ -124,15 +124,10 @@
"-Wno-dev" "-Wno-dev"
], ],
"C_Cpp.default.cppStandard": "c++20", "C_Cpp.default.cppStandard": "c++20",
"C_Cpp.default.intelliSenseMode": "msvc-x64",
"C_Cpp.files.exclude": {
"**/.vscode": true,
"external/**": true
},
"files.watcherExclude": { "files.watcherExclude": {
"**/target": true "**/target": true
}, },
"C_Cpp.errorSquiggles": "Disabled",
"git.ignoreSubmodules": true, "git.ignoreSubmodules": true,
"cmake.configureOnOpen": true "cmake.configureOnOpen": true,
"C_Cpp.intelliSenseEngineFallback": "Enabled"
} }
+2
View File
@@ -5,6 +5,8 @@ set(CMAKE_CXX_STANDARD 20)
# Handle superbuild first # Handle superbuild first
option (USE_SUPERBUILD "Whether or not a superbuild should be invoked" ON) option (USE_SUPERBUILD "Whether or not a superbuild should be invoked" ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(ENGINE_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/src/Engine) set(ENGINE_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/src/Engine)
set(EXTERNAL_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/external) set(EXTERNAL_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/external)
set(ASSIMP_ROOT ${EXTERNAL_ROOT}/assimp) set(ASSIMP_ROOT ${EXTERNAL_ROOT}/assimp)
+1
View File
@@ -0,0 +1 @@
bin/Debug/compile_commands.json
+1 -1
View File
@@ -401,7 +401,7 @@ namespace Seele
} }
void pop() void pop()
{ {
arraySize--; _data[arraySize--].~T();
markIteratorDirty(); markIteratorDirty();
} }
constexpr inline reference operator[](size_type index) constexpr inline reference operator[](size_type index)
+29 -17
View File
@@ -189,7 +189,7 @@ public:
: root(nullptr) : root(nullptr)
, beginIt(nullptr) , beginIt(nullptr)
, endIt(nullptr) , endIt(nullptr)
, iteratorsDirty(false) , iteratorsDirty(true)
, _size(0) , _size(0)
, comp(Compare()) , comp(Compare())
{ {
@@ -200,7 +200,7 @@ public:
, root(nullptr) , root(nullptr)
, beginIt(nullptr) , beginIt(nullptr)
, endIt(nullptr) , endIt(nullptr)
, iteratorsDirty(false) , iteratorsDirty(true)
, _size(0) , _size(0)
, comp(comp) , comp(comp)
{ {
@@ -210,7 +210,7 @@ public:
, root(nullptr) , root(nullptr)
, beginIt(nullptr) , beginIt(nullptr)
, endIt(nullptr) , endIt(nullptr)
, iteratorsDirty(false) , iteratorsDirty(true)
, _size(0) , _size(0)
, comp(Compare()) , comp(Compare())
{ {
@@ -221,7 +221,7 @@ public:
, comp(other.comp) , comp(other.comp)
{ {
root = &nodeContainer[nodeContainer.indexOf(other.root)]; root = &nodeContainer[nodeContainer.indexOf(other.root)];
refreshIterators(); markIteratorDirty();
} }
Map(Map&& other) Map(Map&& other)
: nodeContainer(other.nodeContainer) : nodeContainer(other.nodeContainer)
@@ -229,7 +229,7 @@ public:
, comp(std::move(other.comp)) , comp(std::move(other.comp))
{ {
root = &nodeContainer[nodeContainer.indexOf(other.root)]; root = &nodeContainer[nodeContainer.indexOf(other.root)];
refreshIterators(); markIteratorDirty();
} }
~Map() ~Map()
{ {
@@ -242,7 +242,7 @@ public:
root = &nodeContainer[nodeContainer.indexOf(other.root)]; root = &nodeContainer[nodeContainer.indexOf(other.root)];
_size = other._size; _size = other._size;
comp = other.comp; comp = other.comp;
refreshIterators(); markIteratorDirty();
} }
return *this; return *this;
} }
@@ -254,14 +254,14 @@ public:
root = &nodeContainer[nodeContainer.indexOf(other.root)]; root = &nodeContainer[nodeContainer.indexOf(other.root)];
_size = std::move(other._size); _size = std::move(other._size);
comp = std::move(other.comp); comp = std::move(other.comp);
refreshIterators(); markIteratorDirty();
} }
return *this; return *this;
} }
inline mapped_type& operator[](const key_type& key) inline mapped_type& operator[](const key_type& key)
{ {
root = splay(root, key); root = splay(root, key);
refreshIterators(); markIteratorDirty();
if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key)) if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key))
{ {
root = insert(root, key); root = insert(root, key);
@@ -272,7 +272,7 @@ public:
inline mapped_type& operator[](key_type&& key) inline mapped_type& operator[](key_type&& key)
{ {
root = splay(root, std::move(key)); root = splay(root, std::move(key));
refreshIterators(); markIteratorDirty();
if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key)) if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key))
{ {
root = insert(root, std::move(key)); root = insert(root, std::move(key));
@@ -283,7 +283,7 @@ public:
iterator find(const key_type& key) iterator find(const key_type& key)
{ {
root = splay(root, key); root = splay(root, key);
refreshIterators(); markIteratorDirty();
if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key)) if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key))
{ {
return endIt; return endIt;
@@ -293,7 +293,7 @@ public:
iterator find(key_type&& key) iterator find(key_type&& key)
{ {
root = splay(root, std::move(key)); root = splay(root, std::move(key));
refreshIterators(); markIteratorDirty();
if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key)) if (root == nullptr || comp(root->pair.key, key) || comp(key, root->pair.key))
{ {
return endIt; return endIt;
@@ -303,13 +303,13 @@ public:
iterator erase(const key_type& key) iterator erase(const key_type& key)
{ {
root = remove(root, key); root = remove(root, key);
refreshIterators(); markIteratorDirty();
return iterator(root); return iterator(root);
} }
iterator erase(K&& key) iterator erase(K&& key)
{ {
root = remove(root, std::move(key)); root = remove(root, std::move(key));
refreshIterators(); markIteratorDirty();
return iterator(root); return iterator(root);
} }
void clear() void clear()
@@ -317,7 +317,7 @@ public:
nodeContainer.clear(); nodeContainer.clear();
root = nullptr; root = nullptr;
_size = 0; _size = 0;
refreshIterators(); markIteratorDirty();
} }
bool exists(key_type&& key) bool exists(key_type&& key)
{ {
@@ -486,10 +486,22 @@ private:
r = splay(r->leftChild, key); r = splay(r->leftChild, key);
r->rightChild = temp->rightChild; r->rightChild = temp->rightChild;
} }
temp->leftChild = nullptr; Node& lastNode = nodeContainer.back();
temp->rightChild = nullptr; size_t removedIndex = nodeContainer.indexOf(temp);
nodeContainer[removedIndex] = std::move(lastNode);
for(auto it : nodeContainer)
{
if(it.leftChild == &lastNode)
{
it.leftChild = &nodeContainer[removedIndex];
}
if(it.rightChild == &lastNode)
{
it.rightChild = &nodeContainer[removedIndex];
}
}
nodeContainer.pop();
_size--; _size--;
delete temp;
return r; return r;
} }
template<class KeyType> template<class KeyType>
+2 -1
View File
@@ -54,7 +54,8 @@ ShaderBuffer::ShaderBuffer(PGraphics graphics, uint32 size, VkBufferUsageFlags u
vkCreateBuffer(graphics->getDevice(), &info, nullptr, &buffers[i].buffer); vkCreateBuffer(graphics->getDevice(), &info, nullptr, &buffers[i].buffer);
bufferReqInfo.buffer = buffers[i].buffer; bufferReqInfo.buffer = buffers[i].buffer;
vkGetBufferMemoryRequirements2(graphics->getDevice(), &bufferReqInfo, &memRequirements); vkGetBufferMemoryRequirements2(graphics->getDevice(), &bufferReqInfo, &memRequirements);
buffers[i].allocation = graphics->getAllocator()->allocate(memRequirements, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, buffers[i].buffer); auto temp = graphics->getAllocator()->allocate(memRequirements, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, buffers[i].buffer);
buffers[i].allocation = temp;
vkBindBufferMemory(graphics->getDevice(), buffers[i].buffer, buffers[i].allocation->getHandle(), buffers[i].allocation->getOffset()); vkBindBufferMemory(graphics->getDevice(), buffers[i].buffer, buffers[i].allocation->getHandle(), buffers[i].allocation->getOffset());
} }
} }
+25 -22
View File
@@ -29,14 +29,16 @@ extern Seele::Map<void *, void *> registeredObjects;
extern std::mutex registeredObjectsLock; extern std::mutex registeredObjectsLock;
namespace Seele namespace Seele
{ {
template <typename T> template <typename T, typename Deleter>
class RefPtr; class RefPtr;
template <typename T> template <typename T, typename Deleter>
class RefObject class RefObject
{ {
public: public:
RefObject(T *ptr) RefObject(T *ptr, Deleter&& deleter)
: handle(ptr), refCount(1) : handle(ptr)
, deleter(std::move(deleter))
, refCount(1)
{ {
registeredObjects[ptr] = this; registeredObjects[ptr] = this;
} }
@@ -55,7 +57,7 @@ public:
registeredObjects.erase(handle); registeredObjects.erase(handle);
} }
// #pragma warning( disable: 4150) // #pragma warning( disable: 4150)
delete handle; //deleter(handle);
// #pragma warning( default: 4150) // #pragma warning( default: 4150)
} }
RefObject &operator=(const RefObject &rhs) RefObject &operator=(const RefObject &rhs)
@@ -108,11 +110,12 @@ public:
} }
private: private:
T *handle; T *handle;
Deleter deleter;
std::atomic_uint64_t refCount; std::atomic_uint64_t refCount;
friend class RefPtr<T>; friend class RefPtr<T, Deleter>;
}; };
template <typename T> template <typename T, typename Deleter = std::default_delete<T>>
class RefPtr class RefPtr
{ {
public: public:
@@ -124,7 +127,7 @@ public:
{ {
object = nullptr; object = nullptr;
} }
RefPtr(T *ptr) RefPtr(T *ptr, Deleter deleter = Deleter())
{ {
std::unique_lock l(registeredObjectsLock); std::unique_lock l(registeredObjectsLock);
auto registeredObj = registeredObjects.find(ptr); auto registeredObj = registeredObjects.find(ptr);
@@ -132,17 +135,17 @@ public:
auto registeredEnd = registeredObjects.end(); auto registeredEnd = registeredObjects.end();
if (registeredObj == registeredEnd) if (registeredObj == registeredEnd)
{ {
object = new RefObject<T>(ptr); object = new RefObject<T, Deleter>(ptr, std::move(deleter));
l.unlock(); l.unlock();
} }
else else
{ {
l.unlock(); l.unlock();
object = (RefObject<T> *)registeredObj->value; object = (RefObject<T, Deleter> *)registeredObj->value;
object->addRef(); object->addRef();
} }
} }
explicit RefPtr(RefObject<T> *other) explicit RefPtr(RefObject<T, Deleter> *other)
: object(other) : object(other)
{ {
object->addRef(); object->addRef();
@@ -166,12 +169,12 @@ public:
{ {
F *f = other.getObject()->getHandle(); F *f = other.getObject()->getHandle();
assert(static_cast<T *>(f)); assert(static_cast<T *>(f));
object = (RefObject<T> *)other.getObject(); object = (RefObject<T, Deleter> *)other.getObject();
object->addRef(); object->addRef();
} }
template <typename F> template <typename F, typename DeleterF = std::default_delete<F>>
RefPtr<F> cast() RefPtr<F, DeleterF> cast()
{ {
T *t = object->getHandle(); T *t = object->getHandle();
F *f = dynamic_cast<F *>(t); F *f = dynamic_cast<F *>(t);
@@ -179,12 +182,12 @@ public:
{ {
return nullptr; return nullptr;
} }
RefObject<F> *newObject = (RefObject<F> *)object; RefObject<F, DeleterF> *newObject = (RefObject<F, DeleterF> *)object;
return RefPtr<F>(newObject); return RefPtr<F, DeleterF>(newObject);
} }
template <typename F> template <typename F, typename DeleterF = std::default_delete<F>>
const RefPtr<F> cast() const const RefPtr<F, DeleterF> cast() const
{ {
T *t = object->getHandle(); T *t = object->getHandle();
F *f = dynamic_cast<F *>(t); F *f = dynamic_cast<F *>(t);
@@ -192,8 +195,8 @@ public:
{ {
return nullptr; return nullptr;
} }
RefObject<F> *newObject = (RefObject<F> *)object; RefObject<F, DeleterF> *newObject = (RefObject<F, DeleterF> *)object;
return RefPtr<F>(newObject); return RefPtr<F, DeleterF>(newObject);
} }
RefPtr &operator=(const RefPtr &other) RefPtr &operator=(const RefPtr &other)
@@ -254,7 +257,7 @@ public:
assert(object != nullptr); assert(object != nullptr);
return object->handle; return object->handle;
} }
RefObject<T> *getObject() const RefObject<T, Deleter> *getObject() const
{ {
return object; return object;
} }
@@ -268,7 +271,7 @@ public:
} }
private: private:
RefObject<T> *object; RefObject<T, Deleter> *object;
friend class boost::serialization::access; friend class boost::serialization::access;
template<class Archive> template<class Archive>
void serialize(Archive& ar, const unsigned int) void serialize(Archive& ar, const unsigned int)
-1
View File
@@ -3,7 +3,6 @@
#include "MinimalEngine.h" #include "MinimalEngine.h"
#include <time.h> #include <time.h>
#include <chrono> #include <chrono>
#define BOOST_TEST_MODULE SeeleEngine
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
using namespace Seele; using namespace Seele;
+14
View File
@@ -38,6 +38,20 @@ BOOST_AUTO_TEST_CASE(for_each)
BOOST_REQUIRE_EQUAL(count, 4); BOOST_REQUIRE_EQUAL(count, 4);
} }
BOOST_AUTO_TEST_CASE(remove_entry)
{
Map<int, int> map;
map[1] = 5;
map[4] = 1;
map[2] = 6;
map[3] = 4;
map.erase(2);
BOOST_REQUIRE_EQUAL(map.size(), 3);
BOOST_REQUIRE_EQUAL(map[1], 5);
BOOST_REQUIRE_EQUAL(map[4], 1);
BOOST_REQUIRE_EQUAL(map[3], 4);
}
BOOST_AUTO_TEST_CASE(key_exists) BOOST_AUTO_TEST_CASE(key_exists)
{ {
Map<int, int> map; Map<int, int> map;
+13
View File
@@ -1,5 +1,7 @@
#include "EngineTest.h" #include "EngineTest.h"
#include "MinimalEngine.h" #include "MinimalEngine.h"
#define BOOST_TEST_MODULE SeeleEngine
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
using namespace Seele; using namespace Seele;
@@ -18,6 +20,8 @@ struct TestStruct
} }
uint32 data; uint32 data;
}; };
struct DeclStruct;
BOOST_AUTO_TEST_CASE(basic_refcount) BOOST_AUTO_TEST_CASE(basic_refcount)
{ {
{ {
@@ -30,7 +34,16 @@ BOOST_AUTO_TEST_CASE(basic_refcount)
} }
BOOST_REQUIRE_EQUAL(ptr->data, 10); BOOST_REQUIRE_EQUAL(ptr->data, 10);
} }
std::shared_ptr<DeclStruct> test;
} }
struct DeclStruct
{
~DeclStruct()
{
data = 10;
}
uint32 data = 20;
};
struct DerivedStruct : public TestStruct struct DerivedStruct : public TestStruct
{ {