Compare commits

...

2 Commits

Author SHA1 Message Date
Сергей Маринкевич d00bb90a38 blah 2025-08-05 19:30:21 +07:00
grayhook f46e1a6d0b 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

commit 08f7b59aa7
Author: GRayHook <s@marinkevich.ru>
Date:   Fri Aug 1 19:20:48 2025 +0700

    normalize members of iterator

commit 038cbb73f4
Author: GRayHook <s@marinkevich.ru>
Date:   Fri Aug 1 18:57:08 2025 +0700

    bump

commit 0f93988fb6
Author: GRayHook <s@marinkevich.ru>
Date:   Fri Aug 1 11:32:35 2025 +0700

    tmp
2025-08-01 19:40:48 +07:00
17 changed files with 325 additions and 59 deletions
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include "iterators/BaseIterator.h"
template <typename T>
struct IteratorForward : public BaseIterator<T> {
explicit IteratorForward(T* ptr) : Iterator<T>(first(ptr)) {}
static T* first(T* node) {
return node;
}
static T* next(T* node) {
return node ? node->get_next() : nullptr;
}
Iterator<T>& operator++() override {
this->current = next(this->current);
return *this;
}
};
+14
View File
@@ -0,0 +1,14 @@
#pragma once
/// \brief Интерфейс для классов-итераторов.
/// \tparam T Тип итерируемого элемента.
template <typename T>
class IIterator {
public:
virtual T* operator->() const = 0;
virtual operator const T*() const = 0;
virtual bool operator!=(const IIterator& other) const = 0;
virtual IIterator& operator++() = 0;
};
+3
View File
@@ -11,8 +11,11 @@ public:
using ElemPtr = std::shared_ptr<TElem>;
virtual ~ILinkMixin() = default;
virtual operator std::shared_ptr<TElem>() = 0;
virtual operator TElem&() = 0;
virtual void linkChild(const ElemPtr& child) = 0;
virtual void unlinkParent() = 0;
virtual const std::vector<ElemPtr>& children() = 0;
virtual ElemPtr parent() = 0;
virtual LinkPtr getLink() = 0;
};
+40
View File
@@ -0,0 +1,40 @@
#pragma once
#include <queue>
#include "iterators/BaseIterator.h"
template <typename T>
class BFSIterator : public BaseIterator<T> {
public:
explicit BFSIterator(T* ptr) : BaseIterator<T>(ptr) {
if (!this->current())
return;
for (auto& it : this->current()->children())
q.push({it.get(), 1});
}
IIterator<T>& operator++() override {
advance();
return *this;
}
protected:
std::queue<std::pair<T*, size_t>> q;
void advance() {
if (q.empty()) {
this->set_current(nullptr);
return;
}
auto& [node, lvl] = q.front();
q.pop();
this->set_current(node);
this->set_level(lvl);
for (auto& it : this->current()->children())
q.push({it.get(), lvl + 1});
}
};
+42
View File
@@ -0,0 +1,42 @@
#pragma once
#include "ifaces/IIterator.h"
/// \brief Базовый итератор.
/// \tparam T Тип итерируемого элемента.
/// Предоставляет базовую реализацию методов для работы с итерируемыми объектами.
/// Возвращает объект-прокси, мимикрирующий под итерируемый элемент. Позволяет использовать
/// распаковку структуры для получения уровня элемента в поддереве.
template <typename T>
class BaseIterator : public IIterator<T> {
public:
explicit BaseIterator(T* ptr) : proxy{ptr, 0} {}
struct Proxy {
T* e;
size_t level;
T& operator*() const { return *e; }
T* operator->() const { return e; }
};
Proxy& operator*() { return proxy; }
T* operator->() const override { return current(); }
operator const T*() const { return current(); }
operator T&() const { return *current(); }
operator T*() const { return current(); }
bool operator!=(const IIterator<T>& other) const override {
const T* other_current = other;
return current() != other_current;
}
protected:
T* current() const { return proxy.e; }
void set_current(T* e) { proxy.e = e; }
size_t level() const { return proxy.level; }
void set_level(size_t level) { proxy.level = level; }
private:
Proxy proxy;
};
+43
View File
@@ -0,0 +1,43 @@
#pragma once
#include <stack>
#include <iterator>
#include "iterators/BaseIterator.h"
template <typename T>
class DFSIterator : public BaseIterator<T> {
public:
explicit DFSIterator(T* ptr) : BaseIterator<T>(ptr) {
if (!this->current())
return;
for (auto it = this->current()->children().rbegin();
it != this->current()->children().rend(); ++it)
s.push({it->get(), 1});
}
IIterator<T>& operator++() override {
advance();
return *this;
}
protected:
std::stack<std::pair<T*, size_t>> s;
void advance() {
if (s.empty()) {
this->set_current(nullptr);
return;
}
auto [node, lvl] = s.top();
s.pop();
this->set_current(node);
this->set_level(lvl);
for (auto it = this->current()->children().rbegin();
it != this->current()->children().rend(); ++it)
s.push({it->get(), lvl + 1});
}
};
+27
View File
@@ -0,0 +1,27 @@
#pragma once
#include "ifaces/INode.h"
#include "iterators/DFSIterator.h"
#include "iterators/BFSIterator.h"
namespace traversal {
/// \brief Фабрика итераторов узлов дерева.
/// \tparam T Тип алгоритма для обхода дерева.
/// Конструирует итерируемый объект с заданным алгоритмом обхода. Например, в ширину
/// или глубину.
template<typename Policy>
class Traversal {
INode* start;
public:
explicit Traversal(INode* start) : start(start) {}
explicit Traversal(NodePtr start) : start(start.get()) {}
Policy begin() const { return Policy(start); }
Policy end() const { return Policy(nullptr); }
};
using DFS = Traversal<DFSIterator<INode>>;
using BFS = Traversal<BFSIterator<INode>>;
}
+1 -1
View File
@@ -34,7 +34,7 @@ public:
}
~BaseLink() override {
Logger::get("Link").dbg("--- Destructor called for: BaseLink");
Logger::get("ConDes").dbg("--- Destructor called for: BaseLink");
}
protected:
std::vector<ElemPtr> children_;
+1 -1
View File
@@ -20,7 +20,7 @@ public:
void removeChild(const ElemPtr& child) override { }
~NotImplementedLink() override {
Logger::get("Link").dbg("--- Destructor called for: NotImplementedLink");
Logger::get("ConDes").dbg("--- Destructor called for: NotImplementedLink");
}
private:
std::vector<ElemPtr> empty_;
+25 -2
View File
@@ -14,7 +14,15 @@ class BaseLinkMixin : public virtual ILinkMixin<INode>,
public:
~BaseLinkMixin() override {
Logger::get("Mixin").dbg("--- Destructor called for: BaseLinkMixin");
Logger::get("ConDes").dbg("--- Destructor called for: BaseLinkMixin");
}
operator std::shared_ptr<INode>() override {
return this->getNode();
}
operator INode&() override {
return static_cast<INode&>(*this);
}
void linkChild(const ElemPtr& child) override {
@@ -33,10 +41,25 @@ public:
auto parentLink = parent->getLink();
parentLink->removeChild(getNode());
/* NOTE:
*
* Keep a reference to the node we gonna to unlink.
* Otherwise, we'll disappear between `removeChild` and
* `setParent`. Do not rearrange these calls, because
* we want to modify the tree top down.
*/
auto node = getNode();
parentLink->removeChild(node);
getLink()->setParent(nullptr);
}
ElemPtr parent() override {
auto link = getLink();
ElemPtr parent = link->getParent();
return parent;
}
const std::vector<ElemPtr>& children() override {
auto link = getLink();
+11
View File
@@ -0,0 +1,11 @@
#pragma once
#include <memory>
template <typename T>
class FabricMixin {
public:
template <typename... Args>
static std::shared_ptr<T> create(Args&&... args) {
return std::shared_ptr<T>(new T(std::forward<Args>(args)...));
}
};
+3 -3
View File
@@ -16,7 +16,7 @@ class HierarchicalLinkMixin : public LazyLinkMixin<OneToOneLink<TElem>> {
public:
~HierarchicalLinkMixin() override {
Logger::get("Mixin").dbg("--- Destructor called for: HierarchicalLinkMixin");
Logger::get("ConDes").dbg("--- Destructor called for: HierarchicalLinkMixin");
}
void linkChild(const ElemPtr& child) override {
@@ -34,10 +34,10 @@ protected:
if (typeid(*child) == typeid(*this)) {
Logger::get("Mixin").dbg("--- Mutate to OneToMany");
newLink = std::make_shared<OneToManyLink<TElem>>(this->getNode());
newLink = std::make_shared<OneToManyLink<TElem>>(*this);
} else {
Logger::get("Mixin").dbg("--- Mutate to OneToOne");
newLink = std::make_shared<OneToOneLink<TElem>>(this->getNode());
newLink = std::make_shared<OneToOneLink<TElem>>(*this);
}
if (newLink && this->link_)
+2 -3
View File
@@ -25,14 +25,13 @@ public:
LazyLinkMixin() {}
~LazyLinkMixin() override {
Logger::get("Mixin").dbg("--- Destructor called for: LazyLinkMixin");
Logger::get("ConDes").info("--- Destructor called for: LazyLinkMixin");
}
protected:
void lazyInit() {
if (!link_) {
link_ = std::make_shared<TLink>(
BaseLinkMixin::getNode());
link_ = std::make_shared<TLink>(*this);
}
}
LinkPtr link_;
+2 -3
View File
@@ -1,5 +1,4 @@
#pragma once
#include <iostream>
#include "ifaces/INode.h"
#include "Logger.h"
@@ -9,12 +8,12 @@
class BaseNode : public virtual INode {
public:
BaseNode(std::string name) : name_(std::move(name)) {
Logger::get("Node").dbg(std::string("--- Base constructor called for: ") + name_);
Logger::get("ConDes").dbg(std::string("--- Base constructor called for: ") + name_);
}
const std::string& name() const override { return name_; }
const std::string& kind() const override { return kind_; }
~BaseNode() {
Logger::get("Node").dbg(std::string("--- Base destructor called for: ") + name_);
Logger::get("ConDes").info(std::string("--- Base destructor called for: ") + name_);
}
protected:
std::string name_;
+8 -5
View File
@@ -1,19 +1,22 @@
#pragma once
#include <iostream>
#include "nodes/BaseNode.h"
#include "mixins/HierarchicalLinkMixin.h"
#include "mixins/FabricMixin.h"
#include "Logger.h"
/// \brief Класс сложного (составного) узла дерева.
/// Может содержать несколько дочерних ComplexNode и один SimpleNode.
/// Может содержать несколько дочерних ComplexNode или один SimpleNode.
class ComplexNode : public BaseNode,
virtual public HierarchicalLinkMixin<INode> {
virtual public HierarchicalLinkMixin<INode>,
public FabricMixin<ComplexNode> {
public:
~ComplexNode() {
Logger::get("Node").dbg(std::string("--- Complex destructor called for: ") + name_);
Logger::get("ConDes").dbg(std::string("--- Complex destructor called for: ") + name_);
}
private:
friend class FabricMixin<ComplexNode>;
ComplexNode(std::string name) : BaseNode(std::move(name)) {
Logger::get("Node").dbg(std::string("--- Complex constructor called for: ") + name_);
Logger::get("ConDes").dbg(std::string("--- Complex constructor called for: ") + name_);
}
};
+7 -4
View File
@@ -1,20 +1,23 @@
#pragma once
#include <iostream>
#include "nodes/BaseNode.h"
#include "mixins/LazyLinkMixin.h"
#include "mixins/FabricMixin.h"
#include "links/OneToOneLink.h"
#include "Logger.h"
/// \brief Класс простого (листового) узла дерева.
/// Может содержать только одного дочернего ComplexNode.
class SimpleNode : public BaseNode,
virtual public LazyLinkMixin<OneToOneLink<INode>> {
virtual public LazyLinkMixin<OneToOneLink<INode>>,
public FabricMixin<SimpleNode> {
public:
~SimpleNode() {
Logger::get("Node").dbg(std::string("--- Simple destructor called for: ") + name_);
Logger::get("ConDes").dbg(std::string("--- Simple destructor called for: ") + name_);
}
private:
friend class FabricMixin<SimpleNode>;
SimpleNode(std::string name) : BaseNode(std::move(name)) {
Logger::get("Node").dbg(std::string("--- Simple constructor called for: ") + name_);
Logger::get("ConDes").dbg(std::string("--- Simple constructor called for: ") + name_);
}
};
+75 -37
View File
@@ -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() {
@@ -21,37 +31,39 @@ int main() {
Logger::suppressCategory("Node");
Logger::suppressCategory("Link");
Logger::suppressCategory("Mixin");
Logger::suppressCategory("ConDes");
Logger::setMinSeverity("ConDes", Logger::Severity::Info);
auto& logger = Logger::get("MAIN");
logger.info("Entering main scope...");
{
auto root = std::make_shared<ComplexNode>("ComplexRoot");
auto child1 = std::make_shared<ComplexNode>("ComplexChild1");
auto root = ComplexNode::create("ComplexRoot");
auto child1 = ComplexNode::create("ComplexChild1");
root->linkChild(child1);
std::cout << "\nInit tree:\n";
printTree(root);
printTreeLadder(*root);
std::cout << "\n";
{
auto child2 = std::make_shared<ComplexNode>("ComplexChild2");
auto child2 = ComplexNode::create("ComplexChild2");
root->linkChild(child2);
auto subchild2 = std::make_shared<SimpleNode>("SimpleSubChild2");
auto subchild2 = SimpleNode::create("SimpleSubChild2");
child2->linkChild(subchild2);
auto child3 = std::make_shared<ComplexNode>("ComplexChild3");
auto child3 = ComplexNode::create("ComplexChild3");
root->linkChild(child3);
auto subchild3 = std::make_shared<SimpleNode>("SimpleSubChild3");
child3->linkChild(subchild3);
child3->linkChild(SimpleNode::create("SimpleSubChild3"));
{
// Негативный сценарий 1: попытка добавить второй SimpleNode к ComplexNode
try {
auto anotherSimple = std::make_shared<SimpleNode>("ShouldFail");
child2->linkChild(anotherSimple);
child2->linkChild(SimpleNode::create("ShouldFail"));
logger.err("[ERROR] Не должно было получиться добавить второй SimpleNode к ComplexNode!");
} catch (const std::logic_error& e) {
logger.warn(std::string("[Ожидаемое исключение] ") + e.what());
@@ -59,14 +71,12 @@ int main() {
// Негативный сценарий 2: попытка добавить ComplexNode к SimpleNode
// Это допустимо: SimpleNode может иметь ComplexNode в качестве единственного ребёнка
auto goodComplex = std::make_shared<ComplexNode>("GoodComplex");
subchild2->linkChild(goodComplex);
subchild2->linkChild(ComplexNode::create("GoodComplex"));
logger.info("[OK] ComplexNode успешно добавлен к SimpleNode как единственный ребёнок.");
// Негативный сценарий: попытка добавить второго ребёнка к SimpleNode
try {
auto anotherSimple = std::make_shared<SimpleNode>("ShouldFail2");
subchild2->linkChild(anotherSimple);
subchild2->linkChild(SimpleNode::create("ShouldFail2"));
logger.err("[ERROR] Не должно было получиться добавить второго ребёнка к SimpleNode!");
} catch (const std::logic_error& e) {
logger.warn(std::string("[Ожидаемое исключение] ") + e.what());
@@ -74,8 +84,7 @@ int main() {
// Негативный сценарий: попытка добавить SimpleNode в ComplexNode, который уже содержит несколько ComplexNode-дочерних
try {
auto badSimple = std::make_shared<SimpleNode>("BadSimple");
root->linkChild(badSimple);
root->linkChild(SimpleNode::create("BadSimple"));
logger.err("[ERROR] Не должно было получиться добавить SimpleNode в ComplexNode с несколькими ComplexNode-дочерними!");
} catch (const std::logic_error& e) {
logger.warn(std::string("[Ожидаемое исключение] ") + e.what());
@@ -83,42 +92,71 @@ 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");
child1->unlinkParent();
for (auto child : root->getLink()->getChildren()) {
NodePtr childNode = std::dynamic_pointer_cast<INode>(child);
if (childNode->name() == "ComplexChild3") {
childNode->unlinkParent();
for (auto child : traversal::BFS(root)) {
if (child->name() == "ComplexChild3") {
/* Avoid of disconnecting a node of the tree
* we're traversing. Method holds reference
* to it's object itself. But iterator ignores
* smart pointers, feeling free to dereference
* the destroyed object.
*/
#if 1
/* Keep in mind to keep your reference until
* the cycle ends, if you want more than just
* remove subtree.
*/
NodePtr node = *child;
node->unlinkParent();
std::cout << "Unlinked: "
<< node->name() << std::endl;
break;
#else
/* Otherwise, just break immediately you
* unlinked a node. The current pointer is not
* a valid pointer anymore.
*/
child->unlinkParent();
break;
#endif
}
}
auto child4 = std::make_shared<SimpleNode>("SimpleChild4");
root->linkChild(child4);
root->linkChild(SimpleNode::create("SimpleChild4"));
std::cout << "\nTree flush and link SimpleChild4:\n";
printTree(root);
printTreeLadder(*root);
std::cout << "\n";
}
logger.info("Exited main scope. All smart pointers destroyed.");
logger.info("(It don't? Check ConDes logger)");
return 0;
}