mirror of
https://github.com/thunderbrewhq/squall.git
synced 2025-12-12 02:22:30 +00:00
feat(array): add fixed array template
This commit is contained in:
parent
5946abcb7d
commit
23df793b34
3 changed files with 75 additions and 0 deletions
|
|
@ -2,5 +2,6 @@
|
|||
#define STORM_ARRAY_HPP
|
||||
|
||||
#include "array/TSBaseArray.hpp"
|
||||
#include "array/TSFixedArray.hpp"
|
||||
|
||||
#endif
|
||||
|
|
|
|||
67
storm/array/TSFixedArray.hpp
Normal file
67
storm/array/TSFixedArray.hpp
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#ifndef STORM_ARRAY_TS_FIXED_ARRAY_HPP
|
||||
#define STORM_ARRAY_TS_FIXED_ARRAY_HPP
|
||||
|
||||
#include "array/TSBaseArray.hpp"
|
||||
#include "Memory.hpp"
|
||||
#include <cstdint>
|
||||
|
||||
template<class T>
|
||||
class TSFixedArray : public TSBaseArray<T> {
|
||||
public:
|
||||
void ReallocData(uint32_t count);
|
||||
void SetCount(uint32_t count);
|
||||
};
|
||||
|
||||
template<class T>
|
||||
void TSFixedArray<T>::ReallocData(uint32_t count) {
|
||||
T* oldData = this->m_data;
|
||||
|
||||
if (count < this->m_count) {
|
||||
for (int32_t i = count; i < this->m_count; i++) {
|
||||
T* element = &this->m_data[i];
|
||||
delete element;
|
||||
}
|
||||
}
|
||||
|
||||
this->m_alloc = count;
|
||||
|
||||
void* v6 = SMemReAlloc(oldData, sizeof(T) * count, nullptr, 0, 0x10);
|
||||
this->m_data = (T*)v6;
|
||||
|
||||
if (!v6) {
|
||||
this->m_data = (T*)SMemAlloc(sizeof(T) * count, nullptr, 0, 0x0);
|
||||
|
||||
if (oldData) {
|
||||
uint32_t smallestCount = count >= this->m_count ? this->m_count : count;
|
||||
|
||||
for (int32_t i = 0; i < smallestCount; i++) {
|
||||
T* v8 = &this->m_data[i];
|
||||
|
||||
if (v8) {
|
||||
*v8 = oldData[i];
|
||||
}
|
||||
}
|
||||
|
||||
SMemFree(oldData, nullptr, 0, 0x0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void TSFixedArray<T>::SetCount(uint32_t count) {
|
||||
if (count != this->m_count) {
|
||||
if (count) {
|
||||
this->ReallocData(count);
|
||||
|
||||
for (int32_t i = this->m_count; i < count; i++) {
|
||||
new (&this->m_data[i]) T();
|
||||
}
|
||||
|
||||
this->m_count = count;
|
||||
} else {
|
||||
this->Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -7,3 +7,10 @@ TEST_CASE("TSBaseArray", "[list]") {
|
|||
REQUIRE(array.Count() == 0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("TSFixedArray", "[list]") {
|
||||
SECTION("constructs correctly") {
|
||||
TSFixedArray<uint32_t> array;
|
||||
REQUIRE(array.Count() == 0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue