tmp
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;
|
||||
};
|
||||
@ -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());
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue