2023-02-12 16:22:32 +08:00
|
|
|
#include "circuit.h"
|
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
void Circuit::cal_topo_index() {
|
|
|
|
int topo = 1;
|
|
|
|
std::queue<Gate*> q;
|
|
|
|
|
|
|
|
std::unordered_map<Gate*, int> ins;
|
|
|
|
for(Gate* gate : gates) {
|
|
|
|
ins[gate] = gate->inputs.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto in : PIs) {
|
|
|
|
in->topo = topo++;
|
|
|
|
q.push(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
while(!q.empty()) {
|
|
|
|
Gate* g = q.front(); q.pop();
|
|
|
|
for(Gate* out : g->outputs) {
|
|
|
|
ins[out]--;
|
|
|
|
if(ins[out] == 0) {
|
|
|
|
out->topo = topo++;
|
|
|
|
q.push(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|