Commit f1a578b1 authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

flatlist

parent 5705590f
Pipeline #2183 failed with stage
in 43 seconds
......@@ -39,10 +39,10 @@
namespace bits {
using ID = std::int64_t;
using IDs = std::int32_t;
using ID = std::uint_least64_t;
using IDs = std::uint_least32_t;
constexpr int
constexpr ID
get_index(ID id) noexcept
{
return static_cast<int>(id & 0x00000000ffffffff);
......@@ -161,10 +161,13 @@ struct data_array
*/
void clear();
// alloc (memclear* and/or construct*, *optional) an item from freeList or
// items[max_used++], sets id to (next_key++ << 16) | index
/* alloc (memclear* and/or construct*, *optional) an item from
freeList or items[max_used++], sets id to (next_key++ << 16) | index */
T& alloc();
template <typename... Args>
T& alloc(Args&&... args);
// puts entry on free list (uses id to store next)
void free(T&);
......@@ -313,6 +316,34 @@ data_array<T, Identifier>::alloc()
new_index = max_used++;
}
new (&items[new_index].item) T(std::formard<Args>(args)...);
if (next_key == get_max_key<Identifier>())
next_key = 1;
items[new_index].id = (next_key++ << 16) | new_index;
++max_size;
return items[new_index].item;
}
template<typename T, typename Identifier>
template <typename... Args>
T&
data_array<T, Identifier>::alloc(Args&&... args)
{
int new_index;
if (free_head >= 0) {
new_index = free_head;
if (is_valid(items[free_head].id))
free_head = -1;
else
free_head = get_index(items[free_head].id);
} else {
new_index = max_used++;
}
Do_alloc<T, Identifier>(items[new_index].item, std::is_trivial<T>());
if (next_key == get_max_key<Identifier>())
......
template<typename T, typename Identifier>
class flat_list_iterator
{
flat_list<T, Identifier>& list;
Identifier current = 0;
public:
using iterator_category = std::forward_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
flat_list_iterator(flat_list<T, Identifier>& list_ )
: list(list_)
, current(0)
{}
flat_list_iterator(flat_list<T, Identifier>& list_, Identifier current_)
: list(list_)
, current(current_)
{}
flat_list_iterator operator++(int)
{
flat_list_iterator copy(*this);
auto& t = list.get(current);
copy.current = t.next;
return copy;
}
flat_list_iterator& operator++()
{
auto& t = list.get(current);
current = t.next;
returh *this;
}
T operator*() const
{
return list.get(current);
}
friend bool operator==(const flat_list_iterator& lhs, const flat_list_iterator& rhs)
{
return &lhs.list == &rhs.list && lhs.current == rhs.current;
}
friend bool operator!=(const flat_list_iterator& lhs, const flat_list_iterator& rhs)
{
return !(lhs == rhs.m_entry);
}
// friend bool operator<(const flat_list_iterator& lhs, const flat_list_iterator& rhs)
// {
// return lhs.m_entry < rhs.m_entry;
// }
};
template<typename T, typename Identifier>
struct flat_list
{
struct item
{
T id;
Identifier next;
};
Identifier head = 0;
flat_list() = default;
~flat_list() = default;
Identifier append(data_array<item, Identifier>& items, T id)
{
auto& t = item.alloc();
t.id = id;
t.next = head ? head : 0;
return get_id(t);
}
void clear(data_array<item, Identifier>& items)
{
while (head) {
auto to_delete = head;
auto& t = items.get(head);
head = t.next;
items.free(to_delete);
}
}
};
......@@ -37,6 +37,8 @@ struct Dynamics
std::string package;
std::string library;
std::string language;
// void* allocator;
};
struct View
......@@ -122,7 +124,7 @@ struct Values
struct Condition
{
std::string name;
Value value;
ID value_list;
};
struct Connection
......@@ -136,9 +138,11 @@ struct Connection
struct Model
{
std::string name;
float x, y;
float width, height;
IDs dynamics;
ID conditions_list;
ID observables_list;
ID connections_list;
ID parent;
enum class model_type
{
......@@ -149,12 +153,26 @@ struct Model
model_type type;
};
struct GuiModel
{
ID model = { 0u };
ImVec2 pos = { 0, 0 };
ImVec2 size = { 0, 0 };
bool selected = { false };
};
struct Class
{
std::string name;
ID model;
};
struct List_item
{
ID item; // Object ID in the container.
ID next; // Next ID
};
struct Vpz
{
std::string name;
......@@ -168,9 +186,11 @@ struct Vpz
bits::data_array<Connection, IDs> connections;
bits::data_array<Class, IDs> classes;
bits::matrix<IDs> model_dynamics;
bits::matrix<IDs> model_observables;
bits::matrix<IDs> model_conditions;
bits::data_array<bits::list_item, IDs> conditions_list;
bits::data_array<bits::list_item, IDs> connections_list;
bits::data_array<bits::list_item, IDs> observables_list
bits::data_array<bits::list_item, IDs> conditions_list;
bits::data_array<bits::list_item, IDs> value_list;
};
} // vle
......@@ -181,29 +201,24 @@ main(int /* argc */, char* /* argv */ [])
vle::Vpz vpz;
vpz.links.init(vpz.data_links, 100);
auto& model = vpw.model.alloc();
model.name = "model 1";
auto& cond = vpz.conditions.alloc();
cond.name = "condition 1";
float& v1 = vpz.values.real32.alloc();
float& v2 = vpz.values.real32.alloc();
v1 = 1.f;
float& v2 = vpz.values.real32.alloc();
v2 = 2.f;
cond.value.id = vpz.values.real32.get_id(v1);
cond.value.type = vle::Value::value_type::real32;
auto& mdl = vpz.models.alloc();
mdl.name = "name 1";
append_value(vpz, cond, 1.f);
append_value(vpz, cond, 2.f);
append_value(vpz, cond, 3.f);
append_value(vpz, cond, 4.f);
mdl.conditions =
vpz.links.emplace(vpz.conditions.get_id(cond), mdl.conditions);
mdl.conditions =
vpz.links.emplace(vpz.conditions.get_id(cond), mdl.conditions);
mdl.conditions =
vpz.links.emplace(vpz.conditions.get_id(cond), mdl.conditions);
mdl.conditions =
vpz.links.emplace(vpz.conditions.get_id(cond), mdl.conditions);
append_condition(vpz, model, cond);
remove_condition(vpz, model, cond);
return unit_test::report_errors();
clear_value_list(vpz, cond);
}
Supports Markdown
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