Files
poc-links/include/iterators/DFSIterator.h
T
grayhook f46e1a6d0b add iterators
New iterators can be used to iterate through Nodes.

Squashed commit of the following:

commit 602ed679631647dd1c8874b0b0145fcb09458341
Author: GRayHook <s@marinkevich.ru>
Date:   Fri Aug 1 19:36:46 2025 +0700

    fup after CR

commit 08f7b59aa7
Author: GRayHook <s@marinkevich.ru>
Date:   Fri Aug 1 19:20:48 2025 +0700

    normalize members of iterator

commit 038cbb73f4
Author: GRayHook <s@marinkevich.ru>
Date:   Fri Aug 1 18:57:08 2025 +0700

    bump

commit 0f93988fb6
Author: GRayHook <s@marinkevich.ru>
Date:   Fri Aug 1 11:32:35 2025 +0700

    tmp
2025-08-01 19:40:48 +07:00

44 lines
844 B
C++

#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});
}
};