diff --git a/src/tools/CMakeLists.txt b/src/tools/CMakeLists.txt index f10c70d..4113960 100644 --- a/src/tools/CMakeLists.txt +++ b/src/tools/CMakeLists.txt @@ -25,6 +25,8 @@ add_mnode_dev_executable(mana_auto_replay) target_link_libraries(mana_auto_replay PRIVATE mana mnode::resources) add_mnode_dev_executable(mana_nng_client) target_link_libraries(mana_nng_client PRIVATE mana mnode::resources) +add_mnode_dev_executable(mana1) +target_link_libraries(mana1 PRIVATE mana mnode::resources) add_mnode_proto_dev_executable(mnode_proto_test1) add_mnode_proto_dev_executable(mnode_proto_ping_client) diff --git a/src/tools/mana1.cc b/src/tools/mana1.cc new file mode 100644 index 0000000..46421bd --- /dev/null +++ b/src/tools/mana1.cc @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +#include + +struct ArrayDescriptor +{ + std::string data_type; + size_t size; + size_t bits; + double lower_limit; + double upper_limit; + std::vector names; +}; + +namespace detail +{ +template struct Node: public std::enable_shared_from_this> +{ + std::string name; + std::weak_ptr> parent; + std::vector>> children; + T data; + + std::shared_ptr> add_child(const std::string &name) + { + auto child = std::make_shared>(); + child->name = name; + child->parent = this->shared_from_this(); + children.push_back(child); + return child; + } + + std::shared_ptr> get(const std::string &name) const + { + if (auto it = std::find_if(std::begin(children), std::end(children), + [&](const auto &child) { return child->name == name; }); + it != std::end(children)) + { + return *it; + } + + return {}; + } + + std::shared_ptr> get(const std::string &name) + { + if (auto ret = const_cast *>(this)->get(name)) + return ret; + return add_child(name); + } +}; +} // namespace detail + +using Node = detail::Node; + +void print_preorder(const std::shared_ptr &node, int depth) +{ + for (int i = 0; i < depth; ++i) + std::cout << " "; + std::cout << "name=" << node->name; + if (node->data.size > 0) + std::cout << ", size=" << node->data.size; + std::cout << std::endl; + + for (const auto &child: node->children) + print_preorder(child, depth + 1); +} + +void print_bfs(const std::shared_ptr &node) +{ + std::vector> queue; + queue.push_back(node); + + while (!queue.empty()) + { + auto current = queue.front(); + queue.erase(queue.begin()); + + std::cout << "name=" << current->name; + if (current->data.size > 0) + std::cout << ", size=" << current->data.size; + std::cout << std::endl; + + for (const auto &child: current->children) + queue.push_back(child); + } +} + +int main(int argc, char *argv[]) +{ + auto root = std::make_shared(); + root->name = "root"; + root->get("m0")->get("a00")->data.size = 16; + root->get("m0")->get("a01")->data.size = 16; + root->get("m1")->get("a10")->data.size = 32; + root->get("m1")->get("a10")->add_child("a100")->data.size = 32; + root->get("m1")->get("a10")->add_child("a101")->data.size = 4; + root->get("m1")->get("a11")->add_child("a110")->data.size = 32; + root->get("m1")->get("a12")->data.size = 8; + root->get("m1")->get("a12")->add_child("a120")->data.size = 16; + root->get("m1")->get("a12")->add_child("a121")->data.size = 16; + root->get("m1")->get("a12")->add_child("a122")->data.size = 16; + + std::cout << "preorder:\n"; + print_preorder(root, 0); + + std::cout << "bfs:\n"; + print_bfs(root); +}