Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor : typescript integration src/simulator/src/contention.ts #434

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 0 additions & 76 deletions src/simulator/src/contention.js

This file was deleted.

123 changes: 123 additions & 0 deletions src/simulator/src/contention.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* Represents a node with bitWidth and value properties
*/
interface Node {
bitWidth: number;
value: number | undefined;
}

/**
* @class ContentionPendingData
*
* Data structure to store pending contentions in the circuit.
*/
export default class ContentionPendingData {
private contentionPendingMap: Map<Node, Set<Node>>;
private totalContentions: number;

constructor() {
this.contentionPendingMap = new Map<Node, Set<Node>>();
this.totalContentions = 0;
}

/**
* Adds a contention between two nodes
* @param ourNode The source node
* @param theirNode The target node
*/
add(ourNode: Node, theirNode: Node): void {
if (this.contentionPendingMap.has(ourNode)) {
const existingSet = this.contentionPendingMap.get(ourNode)!;
if (!existingSet.has(theirNode)) this.totalContentions++;
existingSet.add(theirNode);
return;
}

this.totalContentions++;
this.contentionPendingMap.set(ourNode, new Set<Node>([theirNode]));
}

/**
* Checks if a node has any pending contentions
* @param ourNode The node to check
* @returns Whether the node has contentions
*/
has(ourNode: Node): boolean {
return this.contentionPendingMap.has(ourNode);
}

/**
* Removes a specific contention entry
* @param ourNode The source node
* @param theirNode The target node
*/
remove(ourNode: Node, theirNode: Node): void {
if (!this.contentionPendingMap.has(ourNode) ||
!this.contentionPendingMap.get(ourNode)!.has(theirNode)) return;

this.contentionPendingMap.get(ourNode)!.delete(theirNode);
if (this.contentionPendingMap.get(ourNode)!.size === 0) {
this.contentionPendingMap.delete(ourNode);
}
this.totalContentions--;
}

/**
* Removes all contentions for a specific node
* @param ourNode The node to remove contentions for
*/
removeAllContentionsForNode(ourNode: Node): void {
if (!this.contentionPendingMap.has(ourNode)) return;

const contentionsForOurNode = this.contentionPendingMap.get(ourNode)!;
for (const theirNode of contentionsForOurNode) {
this.remove(ourNode, theirNode);
}
}

/**
* Removes a contention if the nodes are resolved
* @param ourNode The source node
* @param theirNode The target node
*/
removeIfResolved(ourNode: Node, theirNode: Node): void {
if (ourNode.bitWidth === theirNode.bitWidth &&
(ourNode.value === theirNode.value || ourNode.value === undefined)) {
this.remove(ourNode, theirNode);
}
}

/**
* Removes resolved contentions for a specific node
* @param ourNode The node to check for resolved contentions
*/
removeIfResolvedAllContentionsForNode(ourNode: Node): void {
if (!this.contentionPendingMap.has(ourNode)) return;

const contentionsForOurNode = this.contentionPendingMap.get(ourNode)!;
for (const theirNode of contentionsForOurNode) {
this.removeIfResolved(ourNode, theirNode);
}
}

/**
* @returns Total number of contentions
*/
size(): number {
return this.totalContentions;
}

/**
* @returns List of all contention pairs
*/
nodes(): [Node, Node][] {
const items: [Node, Node][] = [];
for (const [ourNode, contentionSet] of this.contentionPendingMap) {
for (const theirNode of contentionSet) {
items.push([ourNode, theirNode]);
}
}

return items;
}
}
Loading