2013-10-02 03:15:20 -04:00
|
|
|
#ifndef COLLAPSETRANSFORMATION_H
|
|
|
|
|
#define COLLAPSETRANSFORMATION_H
|
|
|
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "NodeTransformation.h"
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
|
class CollapseTransformation: public NodeTransformation<T,T> {
|
|
|
|
|
public:
|
|
|
|
|
CollapseTransformation(T toCollapse);
|
|
|
|
|
~CollapseTransformation();
|
|
|
|
|
virtual NodeTree<T>* transform(NodeTree<T>* from);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
T toCollapse;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
|
CollapseTransformation<T>::CollapseTransformation(T toCollapse) {
|
|
|
|
|
this->toCollapse = toCollapse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
|
CollapseTransformation<T>::~CollapseTransformation() {
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
|
NodeTree<T>* CollapseTransformation<T>::transform(NodeTree<T>* from) {
|
|
|
|
|
std::queue<NodeTree<T>*> toProcess;
|
|
|
|
|
toProcess.push(from);
|
|
|
|
|
while(!toProcess.empty()) {
|
|
|
|
|
NodeTree<T>* node = toProcess.front();
|
|
|
|
|
toProcess.pop();
|
|
|
|
|
std::vector<NodeTree<T>*> children = node->getChildren();
|
|
|
|
|
for (int i = 0; i < children.size(); i++) {
|
|
|
|
|
if (children[i]->getData() == toCollapse) {
|
|
|
|
|
node->removeChild(children[i]);
|
|
|
|
|
std::vector<NodeTree<T>*> newChildren = children[i]->getChildren();
|
|
|
|
|
node->addChildren(newChildren);
|
2013-10-26 11:47:34 -04:00
|
|
|
toProcess.push(node); //Do this node again
|
|
|
|
|
// for (int j = 0; j < newChildren.size(); j++)
|
|
|
|
|
// toProcess.push(newChildren[j]);
|
2013-10-02 03:15:20 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
toProcess.push(children[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return from;
|
|
|
|
|
}
|