Skip to content

Instantly share code, notes, and snippets.

@kevinlynx
Created October 20, 2013 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinlynx/7069844 to your computer and use it in GitHub Desktop.
Save kevinlynx/7069844 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
class A
{
};
class B
{
};
class C : public A
{
};
class D : public A ,public B
{
};
class E : public B
{
};
class F : public D, public E ,public C
{
};
//*/
class ClassInfo
{
public:
std::string name;
std::vector<ClassInfo *> parents;
std::vector<ClassInfo *> children;
};
ClassInfo classes[6];
void Init() {
for(int i = 0;i < 6; ++i){
classes[i].name = 'A' + i;
}
classes[0].children.push_back(&classes[2]);
classes[0].children.push_back(&classes[3]);
classes[1].children.push_back(&classes[3]);
classes[1].children.push_back(&classes[4]);
classes[2].children.push_back(&classes[5]);
classes[3].children.push_back(&classes[5]);
classes[4].children.push_back(&classes[5]);
classes[2].parents.push_back(&classes[0]);
classes[3].parents.push_back(&classes[0]);
classes[3].parents.push_back(&classes[1]);
classes[4].parents.push_back(&classes[1]);
classes[5].parents.push_back(&classes[2]);
classes[5].parents.push_back(&classes[3]);
classes[5].parents.push_back(&classes[4]);
}
struct PathNode {
std::string name;
enum { ROOT, CHILD, PARENT };
int type;
PathNode(const std::string &s, int t) :
name(s), type(t) { }
};
typedef std::vector<PathNode> DumpPath;
void dumpPath(const DumpPath &path) {
for (auto it = path.begin(); it != path.end(); ++it) {
auto node = *it;
if (node.type == PathNode::CHILD)
std::cout << "->child->";
else if (node.type == PathNode::PARENT)
std::cout << "->parent->";
std::cout << node.name;
}
std::cout << std::endl;
}
#define EXIST(path, _n) \
std::find_if(path.begin(), path.end(), [_n](PathNode n) { return _n == n.name; }) !== path.end()
bool nodeExist(const DumpPath &path, const std::string &name) {
return std::find_if(path.begin(), path.end(), [name] (PathNode n) {
return n.name == name;
}) != path.end();
}
void dumpClassTree(const ClassInfo &CI, DumpPath path) {
for (auto it = CI.parents.begin(); it != CI.parents.end(); ++it) {
auto parent = *it;
DumpPath tpath = path;
tpath.push_back(PathNode(parent->name, PathNode::PARENT));
if (nodeExist(path, parent->name)) {
dumpPath(tpath);
continue;
}
dumpClassTree(*parent, tpath);
}
for (auto it = CI.children.begin(); it != CI.children.end(); ++it) {
auto child = *it;
DumpPath tpath = path;
tpath.push_back(PathNode(child->name, PathNode::CHILD));
if (nodeExist(path, child->name)) {
dumpPath(tpath);
continue;
}
dumpClassTree(*child, tpath);
}
}
int main(int, char **) {
Init();
DumpPath path;
path.push_back(PathNode(classes[1].name, PathNode::ROOT));
dumpClassTree(classes[1], path);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment