Skip to content

Commit 552b4c7

Browse files
mvcisbackmasinag
andcommitted
perf: Switch to using ids for hashing and eq check.
closes: #135 BREAKING_CHANGE: Structually equal circuits will not necessarily be equal now. co-authored-by: Gabriele Masina <[email protected]>
1 parent f501715 commit 552b4c7

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

aiger/aig.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def children(self):
5050
pass
5151

5252

53-
@attr.frozen(cache_hash=True)
53+
@attr.frozen(auto_detect=True)
5454
class AndGate(Node):
5555
left: Node
5656
right: Node
@@ -59,15 +59,27 @@ class AndGate(Node):
5959
def children(self):
6060
return (self.left, self.right)
6161

62+
def __eq__(self, other) -> bool:
63+
return id(self) == id(other) # Allow for duplication.
6264

63-
@attr.frozen
65+
def __hash__(self) -> int:
66+
return hash(id(self))
67+
68+
69+
@attr.frozen(auto_detect=True)
6470
class Inverter(Node):
6571
input: Node
6672

6773
@property
6874
def children(self):
6975
return (self.input, )
7076

77+
def __eq__(self, other) -> bool:
78+
return id(self) == id(other) # Allow for duplication.
79+
80+
def __hash__(self) -> int:
81+
return hash(id(self))
82+
7183

7284
@attr.frozen
7385
class Input(Node):
@@ -77,6 +89,12 @@ class Input(Node):
7789
def children(self):
7890
return ()
7991

92+
def __eq__(self, other) -> bool:
93+
return isinstance(other, Input) and (self.name == other.name)
94+
95+
def __hash__(self) -> int:
96+
return hash(("input", self.name))
97+
8098

8199
@attr.frozen
82100
class LatchIn(Node):
@@ -86,6 +104,12 @@ class LatchIn(Node):
86104
def children(self):
87105
return ()
88106

107+
def __eq__(self, other) -> bool:
108+
return isinstance(other, LatchIn) and (self.name == other.name)
109+
110+
def __hash__(self) -> int:
111+
return hash(("latch", self.name))
112+
89113

90114
@attr.frozen
91115
class ConstFalse(Node):

tests/test_expr.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,3 @@ def test_with_output():
8080
x, y, z = map(atom, 'xyz')
8181
expr = ite(x, y, z).with_output('xyz')
8282
assert expr.with_output('xyz')
83-
84-
85-
def test_hash_stability():
86-
x = atom('x')
87-
y = atom('y')
88-
89-
expr1 = x | y
90-
expr2 = (~~expr1).with_output(expr1.output)
91-
92-
assert hash(expr2) == hash(expr1)

0 commit comments

Comments
 (0)