You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
679 B
C++
43 lines
679 B
C++
#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 : BaseIterator<T>::current->children())
|
|
q.push(it.get());
|
|
}
|
|
|
|
IIterator<T>& operator++() override {
|
|
advance();
|
|
return *this;
|
|
}
|
|
|
|
virtual size_t level() const {
|
|
return level_;
|
|
}
|
|
|
|
protected:
|
|
std::queue<T*> q;
|
|
size_t level_;
|
|
|
|
void advance() {
|
|
if (q.empty()) {
|
|
this->current = nullptr;
|
|
return;
|
|
}
|
|
|
|
this->current = q.front();
|
|
q.pop();
|
|
|
|
for (auto& it : this->current->children())
|
|
q.push(it.get());
|
|
}
|
|
};
|