101 lines
3.4 KiB
C++
101 lines
3.4 KiB
C++
#pragma once
|
|
#include "includes.h"
|
|
#include "dynarray.hpp"
|
|
#include <set>
|
|
#include <string>
|
|
#include <map>
|
|
|
|
struct dependency_tree
|
|
{
|
|
typedef uint32_t ref_t;
|
|
typedef ref_t state_t;
|
|
typedef ref_t state_cmp_t;
|
|
|
|
enum STATE : state_t
|
|
{
|
|
FULFILLED = 0,
|
|
FAILED = 1,
|
|
WAITING = 2,
|
|
WORKING = 3
|
|
};
|
|
enum STATE_CMP : state_cmp_t
|
|
{
|
|
CMP_FULFILED = 1,
|
|
CMP_FAILED = 2,
|
|
CMP_WAITING = 4,
|
|
CMP_WORKING = 8
|
|
};
|
|
|
|
struct node
|
|
{
|
|
node(const node&) = delete;
|
|
node& operator =(const node&) = delete;
|
|
node(node&& other) noexcept;
|
|
node& operator =(node&& other) noexcept;
|
|
node();
|
|
// What depends on this node
|
|
std::set<ref_t> m_dependents = {};
|
|
// Name of the node
|
|
const char* m_name = nullptr;
|
|
// How many other nodes does this node depend on
|
|
ref_t m_num_pending_dependencies = 0;
|
|
// A user-controlled u30
|
|
ref_t m_userdata : (sizeof(ref_t)-2) = -1;
|
|
// State of the node
|
|
state_t m_state : 2;
|
|
|
|
~node();
|
|
};
|
|
|
|
~dependency_tree();
|
|
|
|
// A list that maps a nodes name to its index
|
|
std::map<std::string, ref_t> m_node_name_mapping;
|
|
// A list of all nodes in the dependency tree
|
|
dynarray<node> m_nodes;
|
|
|
|
node& operator[](const ref_t idx) noexcept;
|
|
const node& operator[](const ref_t idx) const noexcept;
|
|
|
|
dynarray<ref_t> get_leaf_nodes(const bool ignore_fulfilled=false) const;
|
|
dynarray<const char*> get_leaf_node_names(const bool ignore_fulfilled=false) const;
|
|
dynarray<ref_t> filter_nodes(const state_cmp_t state_cmp, const ref_t num_dependencies_gt, const bool invert_num_deps) const;
|
|
void set_node_state(const ref_t node, const state_t new_state);
|
|
void set_node_state(const char* node, const state_t new_state);
|
|
|
|
ref_t add_node(const char* node_name, const dynarray<ref_t>& dependents);
|
|
ref_t add_node(const char* node_name, const dynarray<const char*>& dependents);
|
|
ref_t add_node(const char* node_name, const std::initializer_list<ref_t>& dependents);
|
|
ref_t add_node(const char* node_name, const std::initializer_list<const char*>& dependents);
|
|
ref_t add_node(const char* node_name);
|
|
|
|
void add_node_dependency(const ref_t node, const ref_t dependency);
|
|
void add_node_dependency(const ref_t node, const char* dependency);
|
|
void add_node_dependency(const char* node, const ref_t dependency);
|
|
void add_node_dependency(const char* node, const char* dependency);
|
|
|
|
void add_node_dependencies(const ref_t node, const std::initializer_list<ref_t>& dependencies);
|
|
void add_node_dependencies(const ref_t node, const dynarray<ref_t>& dependencies);
|
|
void add_node_dependencies(const ref_t node, const std::initializer_list<const char*>& dependencies);
|
|
void add_node_dependencies(const ref_t node, const dynarray<const char*>& dependencies);
|
|
};
|
|
|
|
|
|
int luaopen_dependency_tree(lua_State* L);
|
|
|
|
int lua_dependency_create(lua_State* L);
|
|
int lua_dependency_destroy(lua_State* L);
|
|
int lua_dependency_add_node(lua_State* L);
|
|
int lua_dependency_add_dependency(lua_State* L);
|
|
int lua_dependency_remove_dependency(lua_State* L);
|
|
int lua_dependency_add_dependencies(lua_State* L);
|
|
int lua_dependency_remove_dependencies(lua_State* L);
|
|
int lua_dependency_get_leafs(lua_State* L);
|
|
int lua_dependency_match_nodes(lua_State* L);
|
|
int lua_dependency_get_node_state(lua_State* L);
|
|
int lua_dependency_set_node_state(lua_State* L);
|
|
int lua_dependency_get_node_userval(lua_State* L);
|
|
int lua_dependency_set_node_userval(lua_State* L);
|
|
int lua_dependency_get_node_name(lua_State* L);
|
|
int lua_dependency_get_node(lua_State* L);
|
|
int lua_dependency_index(lua_State* L); |