|
@@ -2,6 +2,7 @@
|
|
|
#define ANKI_UTIL_DYNAMIC_ARRAY_H
|
|
#define ANKI_UTIL_DYNAMIC_ARRAY_H
|
|
|
|
|
|
|
|
#include "anki/util/Allocator.h"
|
|
#include "anki/util/Allocator.h"
|
|
|
|
|
+#include "anki/util/Memory.h"
|
|
|
|
|
|
|
|
namespace anki {
|
|
namespace anki {
|
|
|
|
|
|
|
@@ -23,57 +24,132 @@ public:
|
|
|
/// @name Constructors/destructor
|
|
/// @name Constructors/destructor
|
|
|
/// @{
|
|
/// @{
|
|
|
|
|
|
|
|
- /// XXX
|
|
|
|
|
|
|
+ /// Default
|
|
|
DynamicArray()
|
|
DynamicArray()
|
|
|
{}
|
|
{}
|
|
|
- /// XXX
|
|
|
|
|
- DynamicArray(const PtrSize n)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /// Copy is deleted
|
|
|
|
|
+ DynamicArray(const DynamicArray&) = delete;
|
|
|
|
|
+
|
|
|
|
|
+ /// Move
|
|
|
|
|
+ DynamicArray(DynamicArray&& other)
|
|
|
|
|
+ : b(other.e), e(other.e), allocator(other.allocator)
|
|
|
{
|
|
{
|
|
|
|
|
+ other.b = nullptr;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ /// Allocate the array
|
|
|
|
|
+ DynamicArray(const PtrSize n, const Alloc& alloc = Alloc())
|
|
|
|
|
+ : allocator(alloc)
|
|
|
|
|
+ {
|
|
|
|
|
+ resize(n);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ///
|
|
|
|
|
+ DynamicArray(const Alloc& alloc)
|
|
|
|
|
+ : allocator(alloc)
|
|
|
|
|
+ {}
|
|
|
|
|
+
|
|
|
|
|
+ /// Deallocate
|
|
|
|
|
+ ~DynamicArray()
|
|
|
|
|
+ {
|
|
|
|
|
+ clear();
|
|
|
}
|
|
}
|
|
|
/// @}
|
|
/// @}
|
|
|
|
|
|
|
|
Reference operator[](const PtrSize n)
|
|
Reference operator[](const PtrSize n)
|
|
|
{
|
|
{
|
|
|
|
|
+ ANKI_ASSERT(b != nullptr);
|
|
|
ANKI_ASSERT(n < getSize());
|
|
ANKI_ASSERT(n < getSize());
|
|
|
return *(begin + n);
|
|
return *(begin + n);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
ConstReference operator[](const PtrSize n) const
|
|
ConstReference operator[](const PtrSize n) const
|
|
|
{
|
|
{
|
|
|
|
|
+ ANKI_ASSERT(b != nullptr);
|
|
|
ANKI_ASSERT(n < getSize());
|
|
ANKI_ASSERT(n < getSize());
|
|
|
return *(begin + n);
|
|
return *(begin + n);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// Copy is not permited
|
|
|
|
|
+ DynamicArray& operator=(const Foo&) = delete;
|
|
|
|
|
+
|
|
|
|
|
+ /// Move
|
|
|
|
|
+ DynamicArray& operator=(DynamicArray&& other)
|
|
|
|
|
+ {
|
|
|
|
|
+ clear();
|
|
|
|
|
+
|
|
|
|
|
+ b = other.b;
|
|
|
|
|
+ e = other.e;
|
|
|
|
|
+ allocator = b.allocator;
|
|
|
|
|
+
|
|
|
|
|
+ other.b = nullptr;
|
|
|
|
|
+
|
|
|
|
|
+ return *this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/// Make it compatible with the C++11 range based for loop
|
|
/// Make it compatible with the C++11 range based for loop
|
|
|
Iterator begin()
|
|
Iterator begin()
|
|
|
{
|
|
{
|
|
|
|
|
+ ANKI_ASSERT(b != nullptr);
|
|
|
return b;
|
|
return b;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Make it compatible with the C++11 range based for loop
|
|
/// Make it compatible with the C++11 range based for loop
|
|
|
ConstIterator begin() const
|
|
ConstIterator begin() const
|
|
|
{
|
|
{
|
|
|
|
|
+ ANKI_ASSERT(b != nullptr);
|
|
|
return b;
|
|
return b;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Make it compatible with the C++11 range based for loop
|
|
/// Make it compatible with the C++11 range based for loop
|
|
|
Iterator end()
|
|
Iterator end()
|
|
|
{
|
|
{
|
|
|
|
|
+ ANKI_ASSERT(b != nullptr);
|
|
|
return e;
|
|
return e;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Make it compatible with the C++11 range based for loop
|
|
/// Make it compatible with the C++11 range based for loop
|
|
|
ConstIterator end() const
|
|
ConstIterator end() const
|
|
|
{
|
|
{
|
|
|
|
|
+ ANKI_ASSERT(b != nullptr);
|
|
|
return e;
|
|
return e;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// Get the elements count
|
|
|
PtrSize getSize() const
|
|
PtrSize getSize() const
|
|
|
{
|
|
{
|
|
|
return e - b;
|
|
return e - b;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// Resize the array
|
|
|
|
|
+ void resize(const PtrSize n)
|
|
|
|
|
+ {
|
|
|
|
|
+ ANKI_ASSERT(b == nullptr);
|
|
|
|
|
+ b = newObject<Value>(allocator, n);
|
|
|
|
|
+ e = b + n;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// Resize the array and call the constructors
|
|
|
|
|
+ template<typename... Args>
|
|
|
|
|
+ void resize(const PtrSize n, Args&&... args)
|
|
|
|
|
+ {
|
|
|
|
|
+ ANKI_ASSERT(b == nullptr);
|
|
|
|
|
+ b = newObject<Value>(allocator, n, std::forward<Args>(args)...);
|
|
|
|
|
+ e = b + n;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// Clear the array
|
|
|
|
|
+ void clear()
|
|
|
|
|
+ {
|
|
|
|
|
+ if(b)
|
|
|
|
|
+ {
|
|
|
|
|
+ deleteObjectArray<Value>(allocator, b);
|
|
|
|
|
+ b = nullptr;
|
|
|
|
|
+ e = nullptr;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private:
|
|
private:
|
|
|
Value* b = nullptr;
|
|
Value* b = nullptr;
|
|
|
Value* e = nullptr;
|
|
Value* e = nullptr;
|