Vector class using arena
This commit is contained in:
@@ -1,9 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
#include <Core/Common/NonNullPtr.h>
|
||||
#include <Core/Memory/MemoryArena.h>
|
||||
#include <vector>
|
||||
#include <WinSock2.h>
|
||||
|
||||
namespace Juliet
|
||||
{
|
||||
template <typename Type>
|
||||
struct VectorArena
|
||||
{
|
||||
VectorArena()
|
||||
: First(nullptr)
|
||||
, Count(0)
|
||||
, Stride(sizeof(Type))
|
||||
{
|
||||
Arena = ArenaAllocate();
|
||||
}
|
||||
|
||||
~VectorArena() { ArenaRelease(Arena); }
|
||||
|
||||
void PushBack(Type&& value)
|
||||
{
|
||||
Type* entry = ArenaPushStruct<Type>(Arena);
|
||||
*entry = std::move(value);
|
||||
|
||||
if (Count == 0)
|
||||
{
|
||||
First = entry;
|
||||
}
|
||||
Last = entry;
|
||||
|
||||
++Count;
|
||||
}
|
||||
|
||||
void RemoveAtFast(index_t index)
|
||||
{
|
||||
Assert(index < Count);
|
||||
Assert(Count > 0);
|
||||
|
||||
Type* baseAdr = First;
|
||||
Type* elementAdr = First + index;
|
||||
|
||||
// Swap Last and element
|
||||
Swap(Last, elementAdr);
|
||||
--Count;
|
||||
}
|
||||
|
||||
void Clear()
|
||||
{
|
||||
ArenaClear(Arena);
|
||||
Count = 0;
|
||||
}
|
||||
|
||||
// C++ Accessors for loop supports and Index based access
|
||||
Type& operator[](size_t index) { return First[index]; }
|
||||
const Type& operator[](size_t index) const { return First[index]; }
|
||||
|
||||
Type* begin() { return First; }
|
||||
Type* end() { return First + Count; }
|
||||
|
||||
const Type* begin() const { return First; }
|
||||
const Type* end() const { return First + Count; }
|
||||
|
||||
size_t Size() const { return Count; }
|
||||
|
||||
private:
|
||||
Arena* Arena;
|
||||
Type* First;
|
||||
Type* Last;
|
||||
size_t Count;
|
||||
size_t Stride;
|
||||
};
|
||||
|
||||
// TODO : Create my own Vector class based on https://github.com/niklas-ourmachinery/bitsquid-foundation/blob/master/collection_types.h
|
||||
template <typename T>
|
||||
class Vector : public std::vector<T>
|
||||
|
||||
Reference in New Issue
Block a user