Commit 6bf9d5c5 authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

core: improving hierarchy source code

parent ceb9c064
Pipeline #39693 passed with stage
in 1 minute and 18 seconds
......@@ -661,7 +661,8 @@ public:
constexpr small_vector(small_vector&& other) noexcept = delete;
constexpr small_vector& operator=(small_vector&& other) noexcept = delete;
constexpr void clear() noexcept;
constexpr status init(i32 default_size) noexcept;
constexpr void clear() noexcept;
constexpr reference front() noexcept;
constexpr const_reference front() const noexcept;
......@@ -719,7 +720,7 @@ public:
constexpr vector(vector&& other) noexcept = delete;
constexpr vector& operator=(vector&& other) noexcept = delete;
status init(sz capacity) noexcept;
status init(sz capacity, int default_size = 0) noexcept;
void destroy() noexcept;
constexpr void clear() noexcept;
......@@ -7177,6 +7178,20 @@ small_vector<T, length>::operator=(
return *this;
}
template<typename T, sz length>
constexpr status
small_vector<T, length>::init(i32 default_size) noexcept
{
irt_return_if_fail(default_size > 0 &&
default_size < static_cast<i32>(length),
status::vector_init_capacity_error);
for (i32 i = 0; i < default_size; ++i)
new (&(m_buffer[i])) T{};
m_size = default_size;
}
template<typename T, sz length>
constexpr T*
small_vector<T, length>::data() noexcept
......@@ -7369,21 +7384,32 @@ inline vector<T>::~vector() noexcept
template<typename T>
inline status
vector<T>::init(sz capacity) noexcept
vector<T>::init(sz capacity, int default_size) noexcept
{
irt_return_if_fail(capacity > 0u &&
capacity < std::numeric_limits<i32>::max(),
status::vector_init_capacity_error);
irt_return_if_fail(default_size <= static_cast<i32>(capacity),
status::vector_init_capacity_error);
destroy();
m_data = reinterpret_cast<T*>(g_alloc_fn(capacity * sizeof(T)));
if (!m_data)
return status::vector_not_enough_memory;
m_size = 0;
m_capacity = static_cast<i32>(capacity);
if (default_size > 0) {
for (i32 i = 0; i < default_size; ++i)
new (&(m_data[i])) T{};
m_size = default_size;
} else {
m_size = 0;
}
return status::success;
}
......
......@@ -14,18 +14,24 @@ namespace irt {
template<typename T>
class hierarchy
{
public:
using value_type = T;
static_assert(std::is_enum<T>::value,
"T must be a enumeration: enum class id : unsigned {};");
private:
hierarchy* m_parent = nullptr;
hierarchy* m_sibling = nullptr;
hierarchy* m_child = nullptr;
T m_owner = undefined<T>();
T m_id = undefined<T>();
public:
hierarchy() noexcept = default;
~hierarchy() noexcept;
void set_owner(T object) noexcept;
T owner() const noexcept;
void set_id(T object) noexcept;
T id() const noexcept;
void parent_to(hierarchy& node) noexcept;
void make_sibling_after(hierarchy& node) noexcept;
......@@ -45,23 +51,23 @@ private:
};
template<typename T>
inline hierarchy<T>::~hierarchy() noexcept
hierarchy<T>::~hierarchy() noexcept
{
remove_from_hierarchy();
}
template<typename T>
T
hierarchy<T>::owner() const noexcept
hierarchy<T>::id() const noexcept
{
return m_owner;
return m_id;
}
template<typename T>
void
hierarchy<T>::set_owner(T id) noexcept
hierarchy<T>::set_id(T id) noexcept
{
m_owner = id;
m_id = id;
}
template<typename T>
......@@ -83,8 +89,8 @@ hierarchy<T>::parent_to(hierarchy& node) noexcept
{
remove_from_parent();
m_parent = &node;
m_sibling = node.m_child;
m_parent = &node;
m_sibling = node.m_child;
node.m_child = this;
}
......@@ -103,7 +109,6 @@ template<typename T>
void
hierarchy<T>::remove_from_parent() noexcept
{
if (m_parent) {
hierarchy<T>* prev = get_prior_sibling_node();
......@@ -143,21 +148,21 @@ template<typename T>
T
hierarchy<T>::get_parent() const noexcept
{
return m_parent ? m_parent->m_owner : undefined<T>();
return m_parent ? m_parent->m_id : undefined<T>();
}
template<typename T>
T
hierarchy<T>::get_child() const noexcept
{
return m_child ? m_child->m_owner : undefined<T>();
return m_child ? m_child->m_id : undefined<T>();
}
template<typename T>
T
hierarchy<T>::get_sibling() const noexcept
{
return m_sibling ? m_sibling->m_owner : undefined<T>();
return m_sibling ? m_sibling->m_id : undefined<T>();
}
template<typename T>
......@@ -189,7 +194,7 @@ hierarchy<T>::get_prior_sibling() const noexcept
{
hierarchy<T>* prior = get_prior_sibling_node();
return prior ? prior->m_owner : undefined<T>();
return prior ? prior->m_id : undefined<T>();
}
template<typename T>
......@@ -197,13 +202,13 @@ T
hierarchy<T>::get_next() const noexcept
{
if (m_child)
return m_child->m_owner;
return m_child->m_id;
const hierarchy<T>* node = this;
while (node && node->m_sibling == nullptr)
node = node->m_parent;
return node ? node->m_sibling->m_owner : undefined<T>();
return node ? node->m_sibling->m_id : undefined<T>();
}
template<typename T>
......@@ -215,7 +220,7 @@ hierarchy<T>::get_next_leaf() const noexcept
while (node->m_child)
node = node->m_child;
return node->m_owner;
return node->m_id;
}
const hierarchy<T>* node = this;
......@@ -227,7 +232,7 @@ hierarchy<T>::get_next_leaf() const noexcept
while (node->m_child) {
node = node->m_child;
}
return node->m_owner;
return node->m_id;
}
return nullptr;
......
......@@ -1044,24 +1044,23 @@ main()
"hierarchy-simple"_test = [] {
enum class int_id : irt::u64;
irt::data_array<int, int_id> data;
irt::data_array<int, int_id> data;
irt::vector<irt::hierarchy<int_id>> hier;
data.init(256);
hier.init(256);
expect((irt::is_success(data.init(256))) >> fatal);
expect((irt::is_success(hier.init(256, 256))) >> fatal);
auto& d1 = data.alloc(0);
auto d1_id = data.get_id(d1);
auto& head = data.alloc(0);
auto head_id = data.get_id(head);
auto& gd1 = hier.emplace_back();
gd1.set_owner(d1_id);
hier[irt::get_index(head_id)].set_id(head_id);
for (int i = 0; i < 15; ++i) {
auto& d = data.alloc(i + 1);
auto& d = data.alloc(i + 1);
auto d_id = data.get_id(d);
auto new_hier = hier.emplace_back();
new_hier.set_owner(d_id);
new_hier.parent_to(gd1);
hier[irt::get_index(head_id)].set_id(d_id);
hier[irt::get_index(head_id)].parent_to(
hier[irt::get_index(head_id)]);
}
};
......
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