我正在编写一个python程序,它对组合电路执行一些操作,比如比较与其他电路的相等性、合并门、计数门、计数连接、查找扇出门等等
现在,我用以下方式表示组合电路:
(我还添加了平等性测试)
class Circuit:
def __init__(self):
self.gates = {} # key = the gates number, value = the gate
def __eq__(self, other):
if set(self.gates.keys()) != set(other.gates.keys()):
return False
for key in self.gates.keys():
if self.gates[key] != other.gates[key]:
return False
return True
class Gate:
def __init__(self, gate_type, number):
self.gate_type = gate_type # and, or, nand, nor, xor, xnor
self.number = number
self.incoming_gates = []
self.outgoing_gates = []
def __eq__(self, other):
# i know this is not correct, but in my case correct enough
return (
self.gate_type == other.gate_type
and self.number == other.number
and len(self.incoming) == len(other.incoming)
and len(self.outgoing) == len(other.outgoing)
)
我在代码中的表示对我来说似乎非常费力,所以我正在寻找更好的方法来实现这一点。我已经搜索了这方面的最佳实践,但没有找到任何东西
您可以通过只存储入站门引用来避免门类中的冗余,但这会使代码的其余部分实现起来更加复杂。我相信冗余和易用性的权衡应该有利于易用性
我不知道如何实现门之间的连接,但如果在self.incoming_gates/self.outing_gates中保存对象引用,则可能仅基于传入链接定义它们,并使用self自动更新源的传出_gate列表(可能在构造函数本身中)
您希望实现一个有向图,其中某些数据存储在顶点中Wikipedia has a discussion of various ways to represent a graph和{a2}讨论更一般的问题
对于快速修改图形的拓扑,以及进行(合并门等)操作,像您这样的邻接列表通常很有用
总的来说,我认为架构的测试是在你真正开始实现它的时候。我怀疑一旦你开始使用它,你会很快熟悉你的设计的好处和坏处,并且能够根据需要调整或构建助手功能
相关问题 更多 >
编程相关推荐