normalize members of iterator
This commit is contained in:
@@ -11,6 +11,4 @@ public:
|
||||
virtual bool operator!=(const IIterator& other) const = 0;
|
||||
|
||||
virtual IIterator& operator++() = 0;
|
||||
|
||||
virtual size_t level() const = 0;
|
||||
};
|
||||
|
||||
@@ -7,11 +7,11 @@ template <typename T>
|
||||
class BFSIterator : public BaseIterator<T> {
|
||||
public:
|
||||
explicit BFSIterator(T* ptr) : BaseIterator<T>(ptr) {
|
||||
if (!this->current)
|
||||
if (!this->current())
|
||||
return;
|
||||
|
||||
for (auto& it : BaseIterator<T>::current->children())
|
||||
q.push(it.get());
|
||||
for (auto& it : this->current()->children())
|
||||
q.push({it.get(), 1});
|
||||
}
|
||||
|
||||
IIterator<T>& operator++() override {
|
||||
@@ -19,24 +19,22 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual size_t level() const {
|
||||
return level_;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::queue<T*> q;
|
||||
size_t level_;
|
||||
std::queue<std::pair<T*, size_t>> q;
|
||||
|
||||
void advance() {
|
||||
if (q.empty()) {
|
||||
this->current = nullptr;
|
||||
this->set_current(nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
this->current = q.front();
|
||||
auto& [node, lvl] = q.front();
|
||||
q.pop();
|
||||
|
||||
for (auto& it : this->current->children())
|
||||
q.push(it.get());
|
||||
this->set_current(node);
|
||||
this->set_level(lvl);
|
||||
|
||||
for (auto& it : this->current()->children())
|
||||
q.push({it.get(), lvl + 1});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
template <typename T>
|
||||
class BaseIterator : public IIterator<T> {
|
||||
public:
|
||||
explicit BaseIterator(T* ptr) : current(ptr) {}
|
||||
explicit BaseIterator(T* ptr) : proxy{ptr, 0} {}
|
||||
|
||||
struct Proxy {
|
||||
T* e;
|
||||
@@ -20,24 +20,24 @@ public:
|
||||
T& operator*() const { return *e; }
|
||||
T* operator->() const { return e; }
|
||||
};
|
||||
Proxy& operator*() { return proxy; }
|
||||
|
||||
Proxy& operator*() {
|
||||
proxy = Proxy{ current, this->level() };
|
||||
return proxy;
|
||||
}
|
||||
|
||||
T* operator->() const override { return current; }
|
||||
operator const T*() const { return current; }
|
||||
operator T&() const { return *current; }
|
||||
operator T*() const { return current; }
|
||||
T* operator->() const override { return current(); }
|
||||
operator const T*() const { return current(); }
|
||||
operator T&() const { return *current(); }
|
||||
operator T*() const { return current(); }
|
||||
|
||||
bool operator!=(const IIterator<T>& other) const override {
|
||||
const T* other_current = other;
|
||||
return current != other_current;
|
||||
return current() != other_current;
|
||||
}
|
||||
|
||||
protected:
|
||||
T* current;
|
||||
T* current() const { return proxy.e; }
|
||||
void set_current(T* e) { proxy.e = e; }
|
||||
|
||||
size_t level() const { return proxy.level; }
|
||||
void set_level(size_t level) { proxy.level = level; }
|
||||
private:
|
||||
Proxy proxy;
|
||||
};
|
||||
|
||||
@@ -7,11 +7,12 @@
|
||||
template <typename T>
|
||||
class DFSIterator : public BaseIterator<T> {
|
||||
public:
|
||||
explicit DFSIterator(T* ptr) : BaseIterator<T>(ptr), level_(0) {
|
||||
if (!this->current)
|
||||
explicit DFSIterator(T* ptr) : BaseIterator<T>(ptr) {
|
||||
if (!this->current())
|
||||
return;
|
||||
|
||||
for (auto it = BaseIterator<T>::current->children().rbegin(); it != BaseIterator<T>::current->children().rend(); ++it)
|
||||
for (auto it = this->current()->children().rbegin();
|
||||
it != this->current()->children().rend(); ++it)
|
||||
s.push({it->get(), 1});
|
||||
}
|
||||
|
||||
@@ -20,28 +21,23 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual size_t level() const {
|
||||
return level_;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::stack<std::pair<T*, size_t>> s;
|
||||
size_t level_;
|
||||
|
||||
void advance() {
|
||||
if (s.empty()) {
|
||||
this->current = nullptr;
|
||||
this->set_current(nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
auto [node, lvl] = s.top();
|
||||
s.pop();
|
||||
|
||||
this->current = node;
|
||||
level_ = lvl;
|
||||
this->set_current(node);
|
||||
this->set_level(lvl);
|
||||
|
||||
for (auto it = this->current->children().rbegin();
|
||||
it != this->current->children().rend(); ++it)
|
||||
for (auto it = this->current()->children().rbegin();
|
||||
it != this->current()->children().rend(); ++it)
|
||||
s.push({it->get(), lvl + 1});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user