This commit is contained in:
2025-08-01 11:32:35 +07:00
parent 0f9d73366b
commit 0f93988fb6
7 changed files with 143 additions and 16 deletions
+17
View File
@@ -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;
};
+2
View File
@@ -14,5 +14,7 @@ public:
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;
//virtual operator ElemPtr() const = 0;
};
+23
View File
@@ -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;
}
};
+41
View File
@@ -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());
}
};
+7
View File
@@ -37,6 +37,13 @@ public:
getLink()->setParent(nullptr);
}
ElemPtr parent() override {
auto link = getLink();
ElemPtr parent = link->getParent();
return parent;
}
const std::vector<ElemPtr>& children() override {
auto link = getLink();