Skip to content

Instantly share code, notes, and snippets.

@busti
Created June 13, 2016 23:12
Show Gist options
  • Save busti/9eb0bbddc397bc63c4734169588bb1d8 to your computer and use it in GitHub Desktop.
Save busti/9eb0bbddc397bc63c4734169588bb1d8 to your computer and use it in GitHub Desktop.
/**
* @file traverse.cpp
*/
#include "traverse.h"
#include <set>
#include <vector>
#include <stack>
#include <algorithm>
// TODO: Implement traverseBackwards in two overloads
void printFunction(std::ostream& os, const Graph& graph, bool printInfix)
{
std::function<void (const Cell &)> preOrderCallback = [&](const Cell& cell) {
if (cell.getCellType().getCalculator().isOutput()) {
os << cell.getName() << " = ";
} else if (cell.getCellType().getCalculator().isInput()) {
os << cell.getName();
} else {
os << cell.getCellType().getName() << "(";
}
};
std::function<void (const Cell &)> inOrderCallback = [&](const Cell& cell) {
//if (!cell.getCellType().getCalculator().isOutput() && !cell.getCellType().getCalculator().isOutput())
// os << ", ";
};
std::function<void (const Cell &)> postOrderCallback = [&](const Cell& cell) {
if (cell.getCellType().getCalculator().isOutput()) {
os << "\n";
} else if (!cell.getCellType().getCalculator().isInput()) {
os << ")";
} else {
os << ", ";
}
};
traverseBackwards(graph, preOrderCallback, inOrderCallback, postOrderCallback);
}
std::vector<std::set<size_t>> generateLayers(const Graph& g)
{
std::vector<std::set<size_t>> layers(g.getCells().size());
std::stack<size_t> currentDepth;
// TODO: Implement generateLayers
return layers;
}
void traverseBackwards(const Cell &cell, const Graph &graph,
const std::function<void (const Cell &)> &preOrderCallback,
const std::function<void (const Cell &)> &inOrderCallback,
const std::function<void (const Cell &)> &postOrderCallback) {
preOrderCallback(cell);
for (size_t id: cell.getInEdgeIds()) {
const Cell &cell2 = graph.getCell(graph.getEdge(id).getInCellId());
//Call this with the new or the old cell?..
inOrderCallback(cell);
traverseBackwards(cell2, graph, preOrderCallback, inOrderCallback, postOrderCallback);
}
postOrderCallback(cell);
}
void traverseBackwards(const Graph &graph,
const std::function<void (const Cell &)> &preOrderCallback,
const std::function<void (const Cell &)> &inOrderCallback,
const std::function<void (const Cell &)> &postOrderCallback) {
for(size_t id: graph.getOutCells()){
traverseBackwards(graph.getCell(id), graph, preOrderCallback, inOrderCallback, postOrderCallback);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment