#include <cstddef>
#include <vector>
#include <map>
#include <utility>
template<class Vertex>
class DataClass {
public:
~DataClass() {
delete distance;
delete parents;
}
int count = 0;
std::map<Vertex, int> *distance = new std::map<Vertex, int>;
std::map<Vertex, Vertex> *parents = new std::map<Vertex, Vertex>;;
Vertex current_vertex = -1;
};
template<class Vertex>
class BfsVisitor {
public:
void ExamineVertex(const Vertex &vertex) {
data_class_->current_vertex = vertex;
}
void DiscoverVertex(const Vertex &vertex) {
if (data_class_->current_vertex == -1) {
data_class_->parents->insert(std::make_pair(vertex, vertex));
data_class_->distance->insert(std::make_pair(vertex, 0));
} else {
data_class_->parents->insert(std::make_pair
(vertex, data_class_->current_vertex));
data_class_->distance->insert(std::make_pair
(vertex, data_class_->distance->at
(data_class_->current_vertex) + 1));
}
}
size_t DistanceTo(const Vertex &target) const {
return data_class_->distance->at(target);
}
Vertex Parent(const Vertex &vertex) const {
return data_class_->parents->at(vertex);
}
~BfsVisitor() {
data_class_->count++;
if (data_class_->count == 2)
delete data_class_;
}
private:
# Skip checks if the class is small, where small means 25 lines or less.
# 25 lines seems like a good cutoff since that's the usual height of
# terminals, and any class that can't fit in one screen can't really
# be considered "small".
#
# Also skip checks if we are on the first line. This accounts for
# classes that look like
# class Foo { public: ... };
#
# If we didn't find the end of the class, last_line would be zero,
# and the check will be skipped by the first condition.
# Skip checks if the class is small, where small means 25 lines or less.
# 25 lines seems like a good cutoff since that's the usual height of
# terminals, and any class that can't fit in one screen can't really
# be considered "small".
#
# Also skip checks if we are on the first line. This accounts for
# classes that look like
# class Foo { public: ... };
#
# If we didn't find the end of the class, last_line would be zero,
# and the check will be skipped by the first condition.
DataClass<Vertex> *data_class_ = new DataClass<Vertex>();
};