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 commit08f7b59aa7Author: GRayHook <s@marinkevich.ru> Date: Fri Aug 1 19:20:48 2025 +0700 normalize members of iterator commit038cbb73f4Author: GRayHook <s@marinkevich.ru> Date: Fri Aug 1 18:57:08 2025 +0700 bump commit0f93988fb6Author: GRayHook <s@marinkevich.ru> Date: Fri Aug 1 11:32:35 2025 +0700 tmp
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
#include <queue>
|
||||
|
||||
#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});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user