add iterators
New iterators can be used to iterate through Nodes. Squashed commit of the following: commit 602ed679631647dd1c8874b0b0145fcb09458341 Author: GRayHook <s@marinkevich.ru> Date: Fri Aug 1 19:36:46 2025 +0700 fup after CR commit08f7b59aa7Author: GRayHook <s@marinkevich.ru> Date: Fri Aug 1 19:20:48 2025 +0700 normalize members of iterator commit038cbb73f4Author: GRayHook <s@marinkevich.ru> Date: Fri Aug 1 18:57:08 2025 +0700 bump commit0f93988fb6Author: GRayHook <s@marinkevich.ru> Date: Fri Aug 1 11:32:35 2025 +0700 tmp
This commit is contained in:
+34
-16
@@ -1,19 +1,29 @@
|
||||
#include "nodes/SimpleNode.h"
|
||||
#include "nodes/ComplexNode.h"
|
||||
#include <iostream>
|
||||
|
||||
#include "Logger.h"
|
||||
|
||||
void printTree(const NodePtr& node, int indent = 0) {
|
||||
if (!node) {
|
||||
Logger::get("MAIN").err("No node");
|
||||
return;
|
||||
}
|
||||
#include "nodes/SimpleNode.h"
|
||||
#include "nodes/ComplexNode.h"
|
||||
#include "iterators/Traversal.h"
|
||||
|
||||
for (int i = 0; i < indent; ++i) std::cout << " ";
|
||||
std::cout << node->name() << "\n";
|
||||
|
||||
for (const auto& child : node->children())
|
||||
printTree(child, indent + 1);
|
||||
void printNode(INode& node, size_t level) {
|
||||
for (size_t i = level; i > 0; i--)
|
||||
std::cout << " ";
|
||||
std::cout << node.name() << "\n";
|
||||
}
|
||||
|
||||
void printTreeBFS(INode& node) {
|
||||
for (auto& child : traversal::BFS(&node))
|
||||
printNode(*child, 0);
|
||||
}
|
||||
void printTreeList(INode& node) {
|
||||
for (auto& child : traversal::DFS(&node))
|
||||
printNode(*child, 0);
|
||||
}
|
||||
void printTreeLadder(INode& node) {
|
||||
for (auto& [child, level] : traversal::DFS(&node))
|
||||
printNode(*child, level);
|
||||
}
|
||||
|
||||
int main() {
|
||||
@@ -31,7 +41,7 @@ int main() {
|
||||
root->linkChild(child1);
|
||||
|
||||
std::cout << "\nInit tree:\n";
|
||||
printTree(root);
|
||||
printTreeLadder(*root);
|
||||
std::cout << "\n";
|
||||
|
||||
{
|
||||
@@ -83,21 +93,29 @@ int main() {
|
||||
}
|
||||
|
||||
std::cout << "\nAnother tree:\n";
|
||||
printTree(root);
|
||||
printTreeLadder(*root);
|
||||
std::cout << "\n";
|
||||
|
||||
std::cout << "\nList:\n";
|
||||
printTreeList(*root);
|
||||
std::cout << "\n";
|
||||
|
||||
std::cout << "\nBFS:\n";
|
||||
printTreeBFS(*root);
|
||||
std::cout << "\n";
|
||||
|
||||
logger.info("Unlinking ComplexChild2...\n");
|
||||
child2->unlinkParent();
|
||||
|
||||
std::cout << "\nTree after unlink:\n";
|
||||
printTree(root);
|
||||
printTreeLadder(*root);
|
||||
std::cout << "\n";
|
||||
|
||||
logger.info("Put refs of ComplexChild2, ComplexChild3 and its children");
|
||||
}
|
||||
|
||||
std::cout << "\nTree after scope out:\n";
|
||||
printTree(root);
|
||||
printTreeLadder(*root);
|
||||
std::cout << "\n";
|
||||
|
||||
logger.info("Unlinking ComplexChild1 and ComplexChild3...\n");
|
||||
@@ -114,7 +132,7 @@ int main() {
|
||||
root->linkChild(child4);
|
||||
|
||||
std::cout << "\nTree flush and link SimpleChild4:\n";
|
||||
printTree(root);
|
||||
printTreeLadder(*root);
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user