aboutsummaryrefslogtreecommitdiff
path: root/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp
diff options
context:
space:
mode:
authorMax-001 <80035972+Max-001@users.noreply.github.com>2024-10-05 12:07:18 +0200
committerMax-001 <80035972+Max-001@users.noreply.github.com>2024-10-05 12:07:18 +0200
commitc767ef1e3d1d78e50eb1b6d9824b1c8961d68524 (patch)
tree13fe577b71a31dc82a3d31dc300a8e14d6d0fcb3 /mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp
parentc39adf3040c3f2f7cfcab5ce5b7e39b815e8131d (diff)
First setup
Diffstat (limited to 'mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp')
-rw-r--r--mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp b/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp
new file mode 100644
index 0000000..878a85f
--- /dev/null
+++ b/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp
@@ -0,0 +1,84 @@
+template<typename T>
+ContiguousContainer<T>::ContiguousContainer()
+ : mSize(0), mCapacity(10) {
+ // Allocate memory for 10 objects initially
+ mData = static_cast<T*>(malloc(mCapacity * sizeof(T)));
+ if (!mData) {
+ throw std::bad_alloc();
+ }
+}
+
+template<typename T>
+ContiguousContainer<T>::~ContiguousContainer() {
+ // Destroy all constructed objects
+ for (size_t i = 0; i < mSize; ++i) {
+ mData[i].~T();
+ }
+ // Free the allocated memory
+ free(mData);
+}
+
+template<typename T>
+template<typename... Args>
+void ContiguousContainer<T>::pushBack(Args&&... args) {
+ if (mSize == mCapacity) {
+ // Double the capacity if the container is full
+ resize(mCapacity * 2);
+ }
+ // Use placement new with perfect forwarding to construct the object in place
+ new (mData + mSize) T(std::forward<Args>(args)...);
+ ++mSize;
+}
+
+template<typename T>
+void ContiguousContainer<T>::popBack() {
+ if (mSize > 0) {
+ --mSize;
+ // Explicitly call the destructor
+ mData[mSize].~T();
+ }
+}
+
+template<typename T>
+T& ContiguousContainer<T>::operator[](size_t index) {
+ if (index >= mSize) {
+ throw std::out_of_range("Index out of range");
+ }
+ return mData[index];
+}
+
+template<typename T>
+size_t ContiguousContainer<T>::getSize() const {
+ return mSize;
+}
+
+// Function that returns a vector of references to all stored objects
+template<typename T>
+std::vector<std::reference_wrapper<T>> ContiguousContainer<T>::getAllReferences() {
+ std::vector<std::reference_wrapper<T>> references;
+ references.reserve(mSize); // Reserve space to avoid reallocation
+ for (size_t i = 0; i < mSize; ++i) {
+ references.push_back(std::ref(mData[i]));
+ }
+ return references;
+}
+
+template<typename T>
+void ContiguousContainer<T>::resize(size_t new_capacity) {
+ // Allocate new memory block with the updated capacity
+ T* new_data = static_cast<T*>(malloc(new_capacity * sizeof(T)));
+ if (!new_data) {
+ throw std::bad_alloc();
+ }
+
+ // Move or copy existing objects to the new memory block
+ for (size_t i = 0; i < mSize; ++i) {
+ new (new_data + i) T(std::move(mData[i])); // Move the objects
+ mData[i].~T(); // Call the destructor for the old object
+ }
+
+ // Free the old memory block
+ free(mData);
+ mData = new_data;
+ mCapacity = new_capacity;
+}