1
+ /**
2
+ * Represents a node with bitWidth and value properties
3
+ */
4
+ interface Node {
5
+ bitWidth : number ;
6
+ value : number | undefined ;
7
+ }
8
+
9
+ /**
10
+ * @class ContentionPendingData
11
+ *
12
+ * Data structure to store pending contentions in the circuit.
13
+ */
14
+ export default class ContentionPendingData {
15
+ private contentionPendingMap : Map < Node , Set < Node > > ;
16
+ private totalContentions : number ;
17
+
18
+ constructor ( ) {
19
+ this . contentionPendingMap = new Map < Node , Set < Node > > ( ) ;
20
+ this . totalContentions = 0 ;
21
+ }
22
+
23
+ /**
24
+ * Adds a contention between two nodes
25
+ * @param ourNode The source node
26
+ * @param theirNode The target node
27
+ */
28
+ add ( ourNode : Node , theirNode : Node ) : void {
29
+ if ( this . contentionPendingMap . has ( ourNode ) ) {
30
+ const existingSet = this . contentionPendingMap . get ( ourNode ) ! ;
31
+ if ( ! existingSet . has ( theirNode ) ) this . totalContentions ++ ;
32
+ existingSet . add ( theirNode ) ;
33
+ return ;
34
+ }
35
+
36
+ this . totalContentions ++ ;
37
+ this . contentionPendingMap . set ( ourNode , new Set < Node > ( [ theirNode ] ) ) ;
38
+ }
39
+
40
+ /**
41
+ * Checks if a node has any pending contentions
42
+ * @param ourNode The node to check
43
+ * @returns Whether the node has contentions
44
+ */
45
+ has ( ourNode : Node ) : boolean {
46
+ return this . contentionPendingMap . has ( ourNode ) ;
47
+ }
48
+
49
+ /**
50
+ * Removes a specific contention entry
51
+ * @param ourNode The source node
52
+ * @param theirNode The target node
53
+ */
54
+ remove ( ourNode : Node , theirNode : Node ) : void {
55
+ if ( ! this . contentionPendingMap . has ( ourNode ) ||
56
+ ! this . contentionPendingMap . get ( ourNode ) ! . has ( theirNode ) ) return ;
57
+
58
+ this . contentionPendingMap . get ( ourNode ) ! . delete ( theirNode ) ;
59
+ if ( this . contentionPendingMap . get ( ourNode ) ! . size === 0 ) {
60
+ this . contentionPendingMap . delete ( ourNode ) ;
61
+ }
62
+ this . totalContentions -- ;
63
+ }
64
+
65
+ /**
66
+ * Removes all contentions for a specific node
67
+ * @param ourNode The node to remove contentions for
68
+ */
69
+ removeAllContentionsForNode ( ourNode : Node ) : void {
70
+ if ( ! this . contentionPendingMap . has ( ourNode ) ) return ;
71
+
72
+ const contentionsForOurNode = this . contentionPendingMap . get ( ourNode ) ! ;
73
+ for ( const theirNode of contentionsForOurNode ) {
74
+ this . remove ( ourNode , theirNode ) ;
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Removes a contention if the nodes are resolved
80
+ * @param ourNode The source node
81
+ * @param theirNode The target node
82
+ */
83
+ removeIfResolved ( ourNode : Node , theirNode : Node ) : void {
84
+ if ( ourNode . bitWidth === theirNode . bitWidth &&
85
+ ( ourNode . value === theirNode . value || ourNode . value === undefined ) ) {
86
+ this . remove ( ourNode , theirNode ) ;
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Removes resolved contentions for a specific node
92
+ * @param ourNode The node to check for resolved contentions
93
+ */
94
+ removeIfResolvedAllContentionsForNode ( ourNode : Node ) : void {
95
+ if ( ! this . contentionPendingMap . has ( ourNode ) ) return ;
96
+
97
+ const contentionsForOurNode = this . contentionPendingMap . get ( ourNode ) ! ;
98
+ for ( const theirNode of contentionsForOurNode ) {
99
+ this . removeIfResolved ( ourNode , theirNode ) ;
100
+ }
101
+ }
102
+
103
+ /**
104
+ * @returns Total number of contentions
105
+ */
106
+ size ( ) : number {
107
+ return this . totalContentions ;
108
+ }
109
+
110
+ /**
111
+ * @returns List of all contention pairs
112
+ */
113
+ nodes ( ) : [ Node , Node ] [ ] {
114
+ const items : [ Node , Node ] [ ] = [ ] ;
115
+ for ( const [ ourNode , contentionSet ] of this . contentionPendingMap ) {
116
+ for ( const theirNode of contentionSet ) {
117
+ items . push ( [ ourNode , theirNode ] ) ;
118
+ }
119
+ }
120
+
121
+ return items ;
122
+ }
123
+ }
0 commit comments