iters
Sergey Marinkevich 6 months ago
parent 0f9d73366b
commit 0f93988fb6

@ -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;
}
};

@ -0,0 +1,17 @@
#pragma once
/// \brief Интерфейс для классов-итераторов.
/// \tparam T Тип итерируемого элемента.
template <typename T>
class IIterator {
public:
virtual T& operator*() const = 0;
virtual T* operator->() const = 0;
virtual operator const T*() const = 0;
virtual bool operator!=(const IIterator& other) const = 0;
virtual IIterator& operator++() = 0;
virtual size_t level() const = 0;
};

@ -14,5 +14,7 @@ public:
virtual void linkChild(const ElemPtr& child) = 0; virtual void linkChild(const ElemPtr& child) = 0;
virtual void unlinkParent() = 0; virtual void unlinkParent() = 0;
virtual const std::vector<ElemPtr>& children() = 0; virtual const std::vector<ElemPtr>& children() = 0;
virtual ElemPtr parent() = 0;
virtual LinkPtr getLink() = 0; virtual LinkPtr getLink() = 0;
//virtual operator ElemPtr() const = 0;
}; };

@ -0,0 +1,23 @@
#pragma once
#include "ifaces/IIterator.h"
/// \brief Базовый итератор.
/// \tparam T Тип итерируемого элемента.
/// Предоставляет базовую реализацию методов для работы с итерируемыми объектами.
template <typename T>
class BaseIterator : public IIterator<T> {
protected:
T* current;
public:
explicit BaseIterator(T* ptr) : current(ptr) {}
T& operator*() const override { return *current; }
T* operator->() const override { return current; }
virtual operator const T*() const { return current; }
bool operator!=(const IIterator<T>& other) const override {
const T* other_current = other;
return current != other_current;
}
};

@ -0,0 +1,41 @@
#pragma once
#include <stack>
#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 : BaseIterator<T>::current->children())
s.push(it.get());
}
IIterator<T>& operator++() override {
advance();
return *this;
}
virtual size_t level() const {
return level_;
}
protected:
std::stack<T*> s;
size_t level_;
void advance() {
if (s.empty()) {
this->current = nullptr;
return;
}
this->current = s.top();
s.pop();
for (auto& it : this->current->children())
s.push(it.get());
}
};

@ -37,6 +37,13 @@ public:
getLink()->setParent(nullptr); getLink()->setParent(nullptr);
} }
ElemPtr parent() override {
auto link = getLink();
ElemPtr parent = link->getParent();
return parent;
}
const std::vector<ElemPtr>& children() override { const std::vector<ElemPtr>& children() override {
auto link = getLink(); auto link = getLink();

@ -1,19 +1,35 @@
#include "nodes/SimpleNode.h"
#include "nodes/ComplexNode.h"
#include <iostream> #include <iostream>
#include "Logger.h" #include "Logger.h"
void printTree(const NodePtr& node, int indent = 0) { #include "nodes/SimpleNode.h"
if (!node) { #include "nodes/ComplexNode.h"
Logger::get("MAIN").err("No node"); #include "iterators/DFSIterator.h"
return;
}
for (int i = 0; i < indent; ++i) std::cout << " ";
std::cout << node->name() << "\n";
for (const auto& child : node->children()) template<typename Policy>
printTree(child, indent + 1); class Traversal {
INode* start;
public:
explicit Traversal(INode* start) : start(start) {}
Policy begin() const { return Policy(start); }
Policy end() const { return Policy(nullptr); }
};
void printNode(INode& node) {
NodePtr parent = node.parent();
while (parent) {
std::cout << " ";
parent = parent->parent();
}
std::cout << node.name() << "\n";
}
void printTree(INode& node) {
for (auto& child : Traversal<DFSIterator<INode>>(&node))
printNode(child);
} }
int main() { int main() {
@ -31,7 +47,7 @@ int main() {
root->linkChild(child1); root->linkChild(child1);
std::cout << "\nInit tree:\n"; std::cout << "\nInit tree:\n";
printTree(root); printTree(*root);
std::cout << "\n"; std::cout << "\n";
{ {
@ -83,21 +99,21 @@ int main() {
} }
std::cout << "\nAnother tree:\n"; std::cout << "\nAnother tree:\n";
printTree(root); printTree(*root);
std::cout << "\n"; std::cout << "\n";
logger.info("Unlinking ComplexChild2...\n"); logger.info("Unlinking ComplexChild2...\n");
child2->unlinkParent(); child2->unlinkParent();
std::cout << "\nTree after unlink:\n"; std::cout << "\nTree after unlink:\n";
printTree(root); printTree(*root);
std::cout << "\n"; std::cout << "\n";
logger.info("Put refs of ComplexChild2, ComplexChild3 and its children"); logger.info("Put refs of ComplexChild2, ComplexChild3 and its children");
} }
std::cout << "\nTree after scope out:\n"; std::cout << "\nTree after scope out:\n";
printTree(root); printTree(*root);
std::cout << "\n"; std::cout << "\n";
logger.info("Unlinking ComplexChild1 and ComplexChild3...\n"); logger.info("Unlinking ComplexChild1 and ComplexChild3...\n");
@ -114,7 +130,7 @@ int main() {
root->linkChild(child4); root->linkChild(child4);
std::cout << "\nTree flush and link SimpleChild4:\n"; std::cout << "\nTree flush and link SimpleChild4:\n";
printTree(root); printTree(*root);
std::cout << "\n"; std::cout << "\n";
} }

Loading…
Cancel
Save