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
+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());
}
};