mirror of
https://github.com/enjoy-digital/litex.git
synced 2025-01-04 09:52:26 -05:00
flow/network: replace NetworkX MultiDiGraph with simple implementation
This commit is contained in:
parent
8489604142
commit
4def6ec391
1 changed files with 67 additions and 4 deletions
|
@ -1,4 +1,4 @@
|
|||
from networkx import MultiDiGraph
|
||||
from collections import defaultdict
|
||||
|
||||
from migen.fhdl.std import *
|
||||
from migen.genlib.misc import optree
|
||||
|
@ -26,7 +26,70 @@ class AbstractActor:
|
|||
r += ">"
|
||||
return r
|
||||
|
||||
# TODO: rewrite this without networkx and without non-determinism
|
||||
class MultiDiGraph:
|
||||
def __init__(self):
|
||||
self.edges = defaultdict(list)
|
||||
self.incoming = defaultdict(set)
|
||||
self.outgoing = defaultdict(set)
|
||||
self.nodes = set()
|
||||
|
||||
def add_edge(self, a, b, **edge):
|
||||
self.edges[(a, b)].append(edge)
|
||||
self.incoming[b].add(a)
|
||||
self.outgoing[a].add(b)
|
||||
self.nodes |= {a, b}
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.nodes)
|
||||
|
||||
def __len__(self):
|
||||
return len(self.nodes)
|
||||
|
||||
def edges_iter(self, data=True):
|
||||
assert data
|
||||
for (a, b), edges in self.edges.items():
|
||||
for edge in edges:
|
||||
yield a, b, edge
|
||||
|
||||
def get_edge_data(self, a, b):
|
||||
return dict(enumerate(self.edges[(a, b)]))
|
||||
|
||||
def add_node(self, node):
|
||||
self.nodes.add(node)
|
||||
|
||||
def remove_node(self, node):
|
||||
for i in self.incoming.pop(node):
|
||||
del self.edges[(i, node)]
|
||||
self.outgoing[i].remove(node)
|
||||
for i in self.outgoing.pop(node):
|
||||
del self.edges[(node, i)]
|
||||
self.incoming[i].remove(node)
|
||||
self.nodes.remove(node)
|
||||
|
||||
def remove_edge(self, a, b, key):
|
||||
e = self.edges[(a, b)]
|
||||
del e[key]
|
||||
if not e:
|
||||
self.incoming[b].remove(a)
|
||||
self.outgoing[a].remove(b)
|
||||
|
||||
def in_edges(self, sink, data=True):
|
||||
assert data
|
||||
e = []
|
||||
for source in self.incoming[sink]:
|
||||
for edge in self.edges[(source, sink)]:
|
||||
e.append((source, sink, edge))
|
||||
return e
|
||||
|
||||
def out_edges(self, source, data=True):
|
||||
assert data
|
||||
e = []
|
||||
for sink in self.outgoing[source]:
|
||||
for edge in self.edges[(source, sink)]:
|
||||
e.append((source, sink, edge))
|
||||
return e
|
||||
|
||||
# TODO: rewrite this without non-determinism
|
||||
class DataFlowGraph(MultiDiGraph):
|
||||
def __init__(self):
|
||||
MultiDiGraph.__init__(self)
|
||||
|
|
Loading…
Reference in a new issue