This commit is contained in:
2025-08-01 18:57:08 +07:00
parent 0f93988fb6
commit 038cbb73f4
6 changed files with 128 additions and 37 deletions
-1
View File
@@ -5,7 +5,6 @@
template <typename T>
class IIterator {
public:
virtual T& operator*() const = 0;
virtual T* operator->() const = 0;
virtual operator const T*() const = 0;
+42
View File
@@ -0,0 +1,42 @@
#pragma once
#include <stack>
#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 : BaseIterator<T>::current->children())
q.push(it.get());
}
IIterator<T>& operator++() override {
advance();
return *this;
}
virtual size_t level() const {
return level_;
}
protected:
std::queue<T*> q;
size_t level_;
void advance() {
if (q.empty()) {
this->current = nullptr;
return;
}
this->current = q.front();
q.pop();
for (auto& it : this->current->children())
q.push(it.get());
}
};
+24 -4
View File
@@ -1,23 +1,43 @@
#pragma once
#include <tuple>
#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; }
struct Proxy {
T* e;
size_t level;
T& operator*() const { return *e; }
T* operator->() const { return e; }
};
Proxy& operator*() {
proxy = Proxy{ current, this->level() };
return proxy;
}
T* operator->() const override { return current; }
virtual operator const T*() const { 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;
private:
Proxy proxy;
};
+13 -7
View File
@@ -1,17 +1,18 @@
#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) {
explicit DFSIterator(T* ptr) : BaseIterator<T>(ptr), level_(0) {
if (!this->current)
return;
for (auto& it : BaseIterator<T>::current->children())
s.push(it.get());
for (auto it = BaseIterator<T>::current->children().rbegin(); it != BaseIterator<T>::current->children().rend(); ++it)
s.push({it->get(), 1});
}
IIterator<T>& operator++() override {
@@ -22,8 +23,9 @@ public:
virtual size_t level() const {
return level_;
}
protected:
std::stack<T*> s;
std::stack<std::pair<T*, size_t>> s;
size_t level_;
void advance() {
@@ -32,10 +34,14 @@ protected:
return;
}
this->current = s.top();
auto [node, lvl] = s.top();
s.pop();
for (auto& it : this->current->children())
s.push(it.get());
this->current = node;
level_ = lvl;
for (auto it = this->current->children().rbegin();
it != this->current->children().rend(); ++it)
s.push({it->get(), lvl + 1});
}
};
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include <queue>
#include "ifaces/INode.h"
#include "iterators/DFSIterator.h"
#include "iterators/BFSIterator.h"
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 TraversalDFS = Traversal<DFSIterator<INode>>;
using TraversalBFS = Traversal<BFSIterator<INode>>;