Collections const-correctness fix

This commit is contained in:
Mateusz Karbowiak
2024-10-30 21:06:16 +01:00
parent c0a1563402
commit 02db7d02f2
6 changed files with 26 additions and 26 deletions

View File

@@ -22,12 +22,12 @@ private:
int32 _capacity; int32 _capacity;
AllocationData _allocation; AllocationData _allocation;
FORCE_INLINE static int32 ToItemCount(int32 size) FORCE_INLINE static int32 ToItemCount(const int32 size)
{ {
return Math::DivideAndRoundUp<int32>(size, sizeof(ItemType)); return Math::DivideAndRoundUp<int32>(size, sizeof(ItemType));
} }
FORCE_INLINE static int32 ToItemCapacity(int32 size) FORCE_INLINE static int32 ToItemCapacity(const int32 size)
{ {
return Math::Max<int32>(Math::DivideAndRoundUp<int32>(size, sizeof(ItemType)), 1); return Math::Max<int32>(Math::DivideAndRoundUp<int32>(size, sizeof(ItemType)), 1);
} }
@@ -46,7 +46,7 @@ public:
/// Initializes a new instance of the <see cref="BitArray"/> class. /// Initializes a new instance of the <see cref="BitArray"/> class.
/// </summary> /// </summary>
/// <param name="capacity">The initial capacity.</param> /// <param name="capacity">The initial capacity.</param>
BitArray(int32 capacity) BitArray(const int32 capacity)
: _count(0) : _count(0)
, _capacity(capacity) , _capacity(capacity)
{ {
@@ -200,7 +200,7 @@ public:
/// </summary> /// </summary>
/// <param name="index">The index of the item.</param> /// <param name="index">The index of the item.</param>
/// <returns>The value of the item.</returns> /// <returns>The value of the item.</returns>
FORCE_INLINE bool operator[](int32 index) const FORCE_INLINE bool operator[](const int32 index) const
{ {
return Get(index); return Get(index);
} }
@@ -210,7 +210,7 @@ public:
/// </summary> /// </summary>
/// <param name="index">The index of the item.</param> /// <param name="index">The index of the item.</param>
/// <returns>The value of the item.</returns> /// <returns>The value of the item.</returns>
bool Get(int32 index) const bool Get(const int32 index) const
{ {
ASSERT(index >= 0 && index < _count); ASSERT(index >= 0 && index < _count);
const ItemType offset = index / sizeof(ItemType); const ItemType offset = index / sizeof(ItemType);
@@ -224,7 +224,7 @@ public:
/// </summary> /// </summary>
/// <param name="index">The index of the item.</param> /// <param name="index">The index of the item.</param>
/// <param name="value">The value to set.</param> /// <param name="value">The value to set.</param>
void Set(int32 index, bool value) void Set(const int32 index, const bool value)
{ {
ASSERT(index >= 0 && index < _count); ASSERT(index >= 0 && index < _count);
const ItemType offset = index / sizeof(ItemType); const ItemType offset = index / sizeof(ItemType);
@@ -250,7 +250,7 @@ public:
/// </summary> /// </summary>
/// <param name="capacity">The new capacity.</param> /// <param name="capacity">The new capacity.</param>
/// <param name="preserveContents">True if preserve collection data when changing its size, otherwise collection after resize will be empty.</param> /// <param name="preserveContents">True if preserve collection data when changing its size, otherwise collection after resize will be empty.</param>
void SetCapacity(const int32 capacity, bool preserveContents = true) void SetCapacity(const int32 capacity, const bool preserveContents = true)
{ {
if (capacity == _capacity) if (capacity == _capacity)
return; return;
@@ -266,7 +266,7 @@ public:
/// </summary> /// </summary>
/// <param name="size">The new collection size.</param> /// <param name="size">The new collection size.</param>
/// <param name="preserveContents">True if preserve collection data when changing its size, otherwise collection after resize might not contain the previous data.</param> /// <param name="preserveContents">True if preserve collection data when changing its size, otherwise collection after resize might not contain the previous data.</param>
void Resize(int32 size, bool preserveContents = true) void Resize(const int32 size, const bool preserveContents = true)
{ {
if (_count <= size) if (_count <= size)
EnsureCapacity(size, preserveContents); EnsureCapacity(size, preserveContents);
@@ -278,7 +278,7 @@ public:
/// </summary> /// </summary>
/// <param name="minCapacity">The minimum capacity.</param> /// <param name="minCapacity">The minimum capacity.</param>
/// <param name="preserveContents">True if preserve collection data when changing its size, otherwise collection after resize will be empty.</param> /// <param name="preserveContents">True if preserve collection data when changing its size, otherwise collection after resize will be empty.</param>
void EnsureCapacity(int32 minCapacity, bool preserveContents = true) void EnsureCapacity(int32 minCapacity, const bool preserveContents = true)
{ {
if (_capacity < minCapacity) if (_capacity < minCapacity)
{ {
@@ -313,7 +313,7 @@ public:
/// </summary> /// </summary>
/// <param name="items">The items to add.</param> /// <param name="items">The items to add.</param>
/// <param name="count">The items count.</param> /// <param name="count">The items count.</param>
void Add(const bool* items, int32 count) void Add(const bool* items, const int32 count)
{ {
EnsureCapacity(_count + count); EnsureCapacity(_count + count);
for (int32 i = 0; i < count; i++) for (int32 i = 0; i < count; i++)

View File

@@ -149,7 +149,7 @@ public:
/// Initializes a new instance of the <see cref="Dictionary"/> class. /// Initializes a new instance of the <see cref="Dictionary"/> class.
/// </summary> /// </summary>
/// <param name="capacity">The initial capacity.</param> /// <param name="capacity">The initial capacity.</param>
Dictionary(int32 capacity) Dictionary(const int32 capacity)
{ {
SetCapacity(capacity); SetCapacity(capacity);
} }
@@ -538,7 +538,7 @@ public:
/// </summary> /// </summary>
/// <param name="capacity">The new capacity.</param> /// <param name="capacity">The new capacity.</param>
/// <param name="preserveContents">Enables preserving collection contents during resizing.</param> /// <param name="preserveContents">Enables preserving collection contents during resizing.</param>
void SetCapacity(int32 capacity, bool preserveContents = true) void SetCapacity(int32 capacity, const bool preserveContents = true)
{ {
if (capacity == Capacity()) if (capacity == Capacity())
return; return;
@@ -598,7 +598,7 @@ public:
/// </summary> /// </summary>
/// <param name="minCapacity">The minimum required capacity.</param> /// <param name="minCapacity">The minimum required capacity.</param>
/// <param name="preserveContents">True if preserve collection data when changing its size, otherwise collection after resize will be empty.</param> /// <param name="preserveContents">True if preserve collection data when changing its size, otherwise collection after resize will be empty.</param>
void EnsureCapacity(int32 minCapacity, bool preserveContents = true) void EnsureCapacity(int32 minCapacity, const bool preserveContents = true)
{ {
if (_size >= minCapacity) if (_size >= minCapacity)
return; return;

View File

@@ -130,7 +130,7 @@ public:
/// Initializes a new instance of the <see cref="HashSet"/> class. /// Initializes a new instance of the <see cref="HashSet"/> class.
/// </summary> /// </summary>
/// <param name="capacity">The initial capacity.</param> /// <param name="capacity">The initial capacity.</param>
HashSet(int32 capacity) HashSet(const int32 capacity)
{ {
SetCapacity(capacity); SetCapacity(capacity);
} }
@@ -415,7 +415,7 @@ public:
/// </summary> /// </summary>
/// <param name="capacity">New capacity</param> /// <param name="capacity">New capacity</param>
/// <param name="preserveContents">Enable/disable preserving collection contents during resizing</param> /// <param name="preserveContents">Enable/disable preserving collection contents during resizing</param>
void SetCapacity(int32 capacity, bool preserveContents = true) void SetCapacity(int32 capacity, const bool preserveContents = true)
{ {
if (capacity == Capacity()) if (capacity == Capacity())
return; return;
@@ -474,7 +474,7 @@ public:
/// </summary> /// </summary>
/// <param name="minCapacity">The minimum required capacity.</param> /// <param name="minCapacity">The minimum required capacity.</param>
/// <param name="preserveContents">True if preserve collection data when changing its size, otherwise collection after resize will be empty.</param> /// <param name="preserveContents">True if preserve collection data when changing its size, otherwise collection after resize will be empty.</param>
void EnsureCapacity(int32 minCapacity, bool preserveContents = true) void EnsureCapacity(int32 minCapacity, const bool preserveContents = true)
{ {
if (_size >= minCapacity) if (_size >= minCapacity)
return; return;

View File

@@ -75,13 +75,13 @@ public:
return _allocation.Get()[_front]; return _allocation.Get()[_front];
} }
FORCE_INLINE T& operator[](int32 index) FORCE_INLINE T& operator[](const int32 index)
{ {
ASSERT(index >= 0 && index < _count); ASSERT(index >= 0 && index < _count);
return _allocation.Get()[(_front + index) % _capacity]; return _allocation.Get()[(_front + index) % _capacity];
} }
FORCE_INLINE const T& operator[](int32 index) const FORCE_INLINE const T& operator[](const int32 index) const
{ {
ASSERT(index >= 0 && index < _count); ASSERT(index >= 0 && index < _count);
return _allocation.Get()[(_front + index) % _capacity]; return _allocation.Get()[(_front + index) % _capacity];

View File

@@ -39,20 +39,20 @@ public:
return (T*)_data; return (T*)_data;
} }
FORCE_INLINE int32 CalculateCapacityGrow(int32 capacity, int32 minCapacity) const FORCE_INLINE int32 CalculateCapacityGrow(int32 capacity, const int32 minCapacity) const
{ {
ASSERT(minCapacity <= Capacity); ASSERT(minCapacity <= Capacity);
return Capacity; return Capacity;
} }
FORCE_INLINE void Allocate(int32 capacity) FORCE_INLINE void Allocate(const int32 capacity)
{ {
#if ENABLE_ASSERTION_LOW_LAYERS #if ENABLE_ASSERTION_LOW_LAYERS
ASSERT(capacity <= Capacity); ASSERT(capacity <= Capacity);
#endif #endif
} }
FORCE_INLINE void Relocate(int32 capacity, int32 oldCount, int32 newCount) FORCE_INLINE void Relocate(const int32 capacity, int32 oldCount, int32 newCount)
{ {
#if ENABLE_ASSERTION_LOW_LAYERS #if ENABLE_ASSERTION_LOW_LAYERS
ASSERT(capacity <= Capacity); ASSERT(capacity <= Capacity);
@@ -104,7 +104,7 @@ public:
return _data; return _data;
} }
FORCE_INLINE int32 CalculateCapacityGrow(int32 capacity, int32 minCapacity) const FORCE_INLINE int32 CalculateCapacityGrow(int32 capacity, const int32 minCapacity) const
{ {
if (capacity < minCapacity) if (capacity < minCapacity)
capacity = minCapacity; capacity = minCapacity;
@@ -129,7 +129,7 @@ public:
return capacity; return capacity;
} }
FORCE_INLINE void Allocate(int32 capacity) FORCE_INLINE void Allocate(const int32 capacity)
{ {
#if ENABLE_ASSERTION_LOW_LAYERS #if ENABLE_ASSERTION_LOW_LAYERS
ASSERT(!_data); ASSERT(!_data);
@@ -141,7 +141,7 @@ public:
#endif #endif
} }
FORCE_INLINE void Relocate(int32 capacity, int32 oldCount, int32 newCount) FORCE_INLINE void Relocate(const int32 capacity, int32 oldCount, int32 newCount)
{ {
T* newData = capacity != 0 ? (T*)Allocator::Allocate(capacity * sizeof(T)) : nullptr; T* newData = capacity != 0 ? (T*)Allocator::Allocate(capacity * sizeof(T)) : nullptr;
#if !BUILD_RELEASE #if !BUILD_RELEASE

View File

@@ -242,7 +242,7 @@ public:
/// </summary> /// </summary>
/// <param name="capacity">The new capacity.</param> /// <param name="capacity">The new capacity.</param>
/// <param name="preserveContents">True if preserve collection data when changing its size, otherwise collection after resize will be empty.</param> /// <param name="preserveContents">True if preserve collection data when changing its size, otherwise collection after resize will be empty.</param>
void SetCapacity(const int32 capacity, bool preserveContents = true) void SetCapacity(const int32 capacity, const bool preserveContents = true)
{ {
if (capacity == Capacity()) if (capacity == Capacity())
return; return;
@@ -365,7 +365,7 @@ private:
return (int32)Platform::InterlockedIncrement(&_count) - 1; return (int32)Platform::InterlockedIncrement(&_count) - 1;
} }
FORCE_INLINE static int32 GetMinCapacity(int32 count) FORCE_INLINE static int32 GetMinCapacity(const int32 count)
{ {
// Ensure there is a slack for others threads to reduce resize counts in highly multi-threaded environment // Ensure there is a slack for others threads to reduce resize counts in highly multi-threaded environment
constexpr int32 slack = PLATFORM_THREADS_LIMIT * 8; constexpr int32 slack = PLATFORM_THREADS_LIMIT * 8;