Compare commits
3 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
08f7b59aa7 | 5 months ago |
|
|
038cbb73f4 | 5 months ago |
|
|
0f93988fb6 | 5 months ago |
@ -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,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;
|
||||
};
|
||||
@ -0,0 +1,40 @@
|
||||
#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 : 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});
|
||||
}
|
||||
};
|
||||
@ -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});
|
||||
}
|
||||
};
|
||||
@ -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>>;
|
||||
Loading…
Reference in New Issue