Commit 3e62d9c9 authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

core: update array container

parent 289a51fc
Pipeline #10269 passed with stage
in 1 minute and 7 seconds
...@@ -55,6 +55,10 @@ enum class status ...@@ -55,6 +55,10 @@ enum class status
data_array_archive_init_capacity_error, data_array_archive_init_capacity_error,
data_array_archive_not_enough_memory, data_array_archive_not_enough_memory,
array_init_capacity_zero,
array_init_capacity_too_big,
array_init_not_enough_memory,
vector_init_capacity_zero, vector_init_capacity_zero,
vector_init_capacity_too_big, vector_init_capacity_too_big,
vector_init_not_enough_memory, vector_init_not_enough_memory,
...@@ -1649,11 +1653,8 @@ public: ...@@ -1649,11 +1653,8 @@ public:
m_items = static_cast<value_type*>( m_items = static_cast<value_type*>(
std::malloc(new_capacity * sizeof(value_type))); std::malloc(new_capacity * sizeof(value_type)));
for (auto i = 0u; i != m_capacity; ++i)
new (&m_items[i]) T();
if (m_items == nullptr) if (m_items == nullptr)
return status::block_allocator_not_enough_memory; return status::vector_init_not_enough_memory;
} }
m_capacity = new_capacity; m_capacity = new_capacity;
...@@ -1796,87 +1797,115 @@ public: ...@@ -1796,87 +1797,115 @@ public:
using const_iterator = const T*; using const_iterator = const T*;
private: private:
value_type* items{ nullptr }; value_type* m_items{ nullptr };
size_type capacity{ 0 }; unsigned m_capacity{ 0 };
public: public:
array() = default; array() = default;
~array() noexcept ~array() noexcept
{ {
if constexpr (!std::is_trivial_v<T>) clear();
for (u32 i = 0; i != capacity; ++i)
items[i].~T();
if (items) if (m_items)
std::free(items); std::free(m_items);
} }
array(const array&) noexcept = delete;
array& operator=(const array&) noexcept = delete;
array(array&&) noexcept = delete;
array& operator=(array&&) noexcept = delete;
status init(std::size_t new_capacity) noexcept status init(std::size_t new_capacity) noexcept
{ {
if (new_capacity > std::numeric_limits<unsigned>::max())
return status::array_init_capacity_too_big;
if (new_capacity == 0) if (new_capacity == 0)
return status::block_allocator_bad_capacity; return status::array_init_capacity_zero;
if (new_capacity != capacity) { if (new_capacity != m_capacity) {
if (items) { if (m_items) {
if constexpr (!std::is_trivial_v<T>) if constexpr (!std::is_trivial_v<T>)
for (u32 i = 0; i != capacity; ++i) for (auto i = 0u; i != m_capacity; ++i)
items[i].~T(); m_items[i].~T();
if (items) if (m_items)
std::free(items); std::free(m_items);
} }
items = static_cast<value_type*>( m_items = static_cast<value_type*>(
std::malloc(new_capacity * sizeof(value_type))); std::malloc(new_capacity * sizeof(value_type)));
for (u32 i = 0; i != capacity; ++i) for (auto i = 0u; i != m_capacity; ++i)
new (&items[i]) T(); new (&m_items[i]) T();
if (items == nullptr) if (m_items == nullptr)
return status::block_allocator_not_enough_memory; return status::array_init_not_enough_memory;
} }
capacity = new_capacity; m_capacity = new_capacity;
return status::success; return status::success;
} }
void clear() noexcept
{
if constexpr (!std::is_trivial_v<T>)
for (auto i = 0u; i != m_capacity; ++i)
m_items[i].~T();
}
reference operator[](size_type i) noexcept reference operator[](size_type i) noexcept
{ {
assert(i < capacity); assert(i < m_capacity);
return items[i]; return m_items[i];
} }
const_reference operator[](size_type i) const noexcept const_reference operator[](size_type i) const noexcept
{ {
assert(i < capacity); assert(i < m_capacity);
return items[i]; return m_items[i];
} }
iterator begin() noexcept iterator begin() noexcept
{ {
return items; return m_items;
} }
iterator end() noexcept iterator end() noexcept
{ {
return items + capacity; return m_items + m_capacity;
} }
const_iterator begin() const noexcept const_iterator begin() const noexcept
{ {
return items; return m_items;
} }
const_iterator end() const noexcept const_iterator end() const noexcept
{ {
return items + capacity; return m_items + m_capacity;
}
const_iterator cbegin() const noexcept
{
return m_items;
}
const_iterator cend() const noexcept
{
return m_items + m_capacity;
} }
size_t size() const noexcept size_t size() const noexcept
{ {
return capacity; return m_capacity;
}
size_t capacity() const noexcept
{
return m_capacity;
} }
}; };
......
...@@ -348,6 +348,36 @@ main() ...@@ -348,6 +348,36 @@ main()
} }
}; };
"array_api"_test = [] {
struct position
{
position() noexcept = default;
position(int x_, int y_)
: x(x_)
, y(y_)
{}
int x = 0, y = 0;
};
irt::array<position> positions;
!expect(irt::status::success == positions.init(16u));
expect(positions.size() == 16_u);
expect(positions.capacity() == 16u);
for (int i = 0; i != 16; ++i) {
positions[i].x = i;
positions[i].y = i;
}
for (int i = 0; i != 16; ++i) {
expect(positions[i].x == i);
expect(positions[i].y == i);
}
};
"vector_api"_test = [] { "vector_api"_test = [] {
struct position struct position
{ {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment