// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Platform/Platform.h"
#include "Engine/Core/Memory/Allocation.h"
#include "Engine/Core/Math/Math.h"
///
/// Template for dynamic array with variable capacity that stores the bit values.
///
template
API_CLASS(InBuild) class BitArray
{
friend BitArray;
public:
typedef uint64 ItemType;
typedef typename AllocationType::template Data AllocationData;
private:
int32 _count;
int32 _capacity;
AllocationData _allocation;
public:
///
/// Initializes a new instance of the class.
///
FORCE_INLINE BitArray()
: _count(0)
, _capacity(0)
{
}
///
/// Initializes a new instance of the class.
///
/// The initial capacity.
BitArray(int32 capacity)
: _count(0)
, _capacity(capacity)
{
if (capacity > 0)
_allocation.Allocate(Math::Max(capacity / sizeof(ItemType), 1));
}
///
/// Initializes a new instance of the class.
///
/// The other collection to copy.
BitArray(const BitArray& other)
{
_count = _capacity = other._count;
if (_capacity > 0)
_allocation.Allocate(Math::Max(_capacity / sizeof(ItemType), 1));
}
///
/// Initializes a new instance of the class.
///
/// The other collection to move.
FORCE_INLINE BitArray(BitArray&& other) noexcept
{
_count = other._count;
_capacity = other._capacity;
other._count = 0;
other._capacity = 0;
_allocation.Swap(other._allocation);
}
///
/// The assignment operator that deletes the current collection of items and the copies items from the other array.
///
/// The other collection to copy.
/// The reference to this.
BitArray& operator=(const BitArray& other) noexcept
{
if (this != &other)
{
if (_capacity < other._count)
{
_allocation.Free();
_capacity = other._count;
_allocation.Allocate(Math::Max(_capacity / sizeof(ItemType), 1));
}
_count = other._count;
}
return *this;
}
///
/// The move assignment operator that deletes the current collection of items and the moves items from the other array.
///
/// The other collection to move.
/// The reference to this.
BitArray& operator=(BitArray&& other) noexcept
{
if (this != &other)
{
_allocation.Free();
_count = other._count;
_capacity = other._capacity;
other._count = 0;
other._capacity = 0;
_allocation.Swap(other._allocation);
}
return *this;
}
///
/// Finalizes an instance of the class.
///
~BitArray()
{
}
public:
///
/// Gets the pointer to the bits storage data (linear allocation).
///
/// The data pointer.
FORCE_INLINE ItemType* Get()
{
return _allocation.Get();
}
///
/// Gets the pointer to the bits storage data (linear allocation).
///
/// The data pointer.
FORCE_INLINE const ItemType* Get() const
{
return _allocation.Get();
}
///
/// Gets the amount of the items in the collection.
///
/// The amount of items.
FORCE_INLINE int32 Count() const
{
return _count;
}
///
/// Gets the amount of the items that can be contained by collection without resizing.
///
/// The collection capacity.
FORCE_INLINE int32 Capacity() const
{
return _capacity;
}
///
/// Returns true if collection isn't empty.
///
/// True if collection isn't empty, otherwise false.
FORCE_INLINE bool HasItems() const
{
return _count != 0;
}
///
/// Returns true if collection is empty.
///
/// True if collection is empty, otherwise false.
FORCE_INLINE bool IsEmpty() const
{
return _count == 0;
}
///
/// Gets the item at the given index.
///
/// The index of the item.
/// The value of the item.
FORCE_INLINE bool operator[](int32 index) const
{
return Get(index);
}
///
/// Gets the item at the given index.
///
/// The index of the item.
/// The value of the item.
bool Get(int32 index) const
{
ASSERT(index >= 0 && index < _count);
const ItemType offset = index / sizeof(ItemType);
const ItemType bitMask = (ItemType)(int32)(1 << (index & ((int32)sizeof(ItemType) - 1)));
const ItemType item = ((ItemType*)_allocation.Get())[offset];
return (item & bitMask) != 0;
}
///
/// Sets the item at the given index.
///
/// The index of the item.
/// The value to set.
void Set(int32 index, bool value) const
{
ASSERT(index >= 0 && index < _count);
const ItemType offset = index / sizeof(ItemType);
const ItemType bitMask = (ItemType)(int32)(1 << (index & ((int32)sizeof(ItemType) - 1)));
ItemType& item = ((ItemType*)_allocation.Get())[offset];
if (value)
item |= bitMask;
else
item &= ~bitMask;
}
public:
///
/// Clear the collection without changing its capacity.
///
FORCE_INLINE void Clear()
{
_count = 0;
}
///
/// Changes the capacity of the collection.
///
/// The new capacity.
/// True if preserve collection data when changing its size, otherwise collection after resize will be empty.
void SetCapacity(const int32 capacity, bool preserveContents = true)
{
if (capacity == _capacity)
return;
ASSERT(capacity >= 0);
const int32 count = preserveContents ? (_count < capacity ? _count : capacity) : 0;
_allocation.Relocate(Math::Max(capacity / sizeof(ItemType), 1), _count / sizeof(ItemType), count / sizeof(ItemType));
_capacity = capacity;
_count = count;
}
///
/// Resizes the collection to the specified size. If the size is equal or less to the current capacity no additional memory reallocation in performed.
///
/// The new collection size.
/// True if preserve collection data when changing its size, otherwise collection after resize might not contain the previous data.
void Resize(int32 size, bool preserveContents = true)
{
if (_count <= size)
EnsureCapacity(size, preserveContents);
_count = size;
}
///
/// Ensures the collection has given capacity (or more).
///
/// The minimum capacity.
/// True if preserve collection data when changing its size, otherwise collection after resize will be empty.
void EnsureCapacity(int32 minCapacity, bool preserveContents = true)
{
if (_capacity < minCapacity)
{
const int32 capacity = _allocation.CalculateCapacityGrow(Math::Max(_capacity / sizeof(ItemType), 1), minCapacity);
SetCapacity(capacity, preserveContents);
}
}
///
/// Sets all items to the given value
///
/// The value to assign to all the collection items.
void SetAll(const bool value)
{
if (_count != 0)
Platform::MemorySet(_allocation.Get(), Math::Max(_count / sizeof(ItemType), 1), value ? MAX_int32 : 0);
}
///
/// Adds the specified item to the collection.
///
/// The item to add.
void Add(const bool item)
{
EnsureCapacity(_count + 1);
_count++;
Set(_count - 1, item);
}
///
/// Adds the specified item to the collection.
///
/// The items to add.
/// The items count.
void Add(const bool* items, int32 count)
{
EnsureCapacity(_count + count);
for (int32 i = 0; i < count; i++)
Add(items[i]);
}
///
/// Adds the other collection to the collection.
///
/// The other collection to add.
void Add(const BitArray& other)
{
EnsureCapacity(_count, other.Count());
for (int32 i = 0; i < other.Count(); i++)
Add(other[i]);
}
///
/// Swaps the contents of collection with the other object without copy operation. Performs fast internal data exchange.
///
/// The other collection.
void Swap(BitArray& other)
{
::Swap(_count, other._count);
::Swap(_capacity, other._capacity);
_allocation.Swap(other._allocation);
}
};