-
Notifications
You must be signed in to change notification settings - Fork 173
refactor(simulator): clean up EventQueue implementation and add TS types #678
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
Open
SouparnaChatterjee
wants to merge
3
commits into
CircuitVerse:main
Choose a base branch
from
SouparnaChatterjee:event-queue-cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+120
−105
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
7b6a94a
refactor(simulator): clean up EventQueue implementation and add TS types
SouparnaChatterjee eec59c6
refactor(eventQueue): replace O(n²) queue with min-heap priority queu…
SouparnaChatterjee 3d42f7f
fix(build): remove wrongly named build.sh
SouparnaChatterjee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // build.js (Windows + cross-platform replacement for build.sh) | ||
|
|
||
| const { execSync } = require("child_process"); | ||
| const fs = require("fs"); | ||
|
|
||
| console.log("Reading version.json..."); | ||
|
|
||
| const versions = JSON.parse(fs.readFileSync("version.json", "utf8")) | ||
| .map((v) => v.version); | ||
|
|
||
| for (const version of versions) { | ||
| console.log(`\nBuilding for version: ${version}`); | ||
|
|
||
| try { | ||
| execSync(`npx vite build --config vite.config.${version}.ts`, { | ||
| stdio: "inherit", | ||
| }); | ||
| } catch (err) { | ||
| console.error(`❌ Build failed for version: ${version}`); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| console.log("\n✅ All builds completed successfully!"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,24 @@ | ||
| #!/bin/bash | ||
| // build.js (Windows + cross-platform equivalent of build.sh) | ||
|
|
||
| const { execSync } = require("child_process"); | ||
| const fs = require("fs"); | ||
|
|
||
| versions=($(jq -r '.[].version' version.json)) | ||
| console.log("Reading version.json..."); | ||
|
|
||
| const versions = JSON.parse(fs.readFileSync("version.json", "utf8")) | ||
| .map((v) => v.version); | ||
| for version in "${versions[@]}"; do | ||
| echo "Building for version: $version" | ||
|
|
||
| npx vite build --config vite.config."$version".ts | ||
|
|
||
| #Build status | ||
| if [ $? -ne 0 ]; then | ||
| echo "Build failed for version: $version" | ||
| exit 1 | ||
| fi | ||
| done | ||
| for (const version of versions) { | ||
| console.log(`\nBuilding for version: ${version}`); | ||
| echo "All builds completed successfully" | ||
| try { | ||
| execSync(`npx vite build --config vite.config.${version}.ts`, { | ||
| stdio: "inherit", | ||
| }); | ||
| } catch (err) { | ||
| console.error(`❌ Build failed for version: ${version}`); | ||
| process.exit(1); | ||
| } | ||
| } | ||
| console.log("\n✅ All builds completed successfully!"); | ||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,123 +1,133 @@ | ||
| /** | ||
| * Event Queue is simply a priority Queue, basic implementation O(n^2). | ||
| * EventQueue implements a priority queue where events are ordered by time. | ||
| * Uses a binary min-heap for O(log n) insertion and removal. | ||
| * @category eventQueue | ||
| */ | ||
|
|
||
| interface QueueObject { | ||
| export interface QueueObject { | ||
| queueProperties: { | ||
| inQueue: boolean | ||
| time: number | ||
| index: number | ||
| } | ||
| propagationDelay: number | ||
| inQueue: boolean; | ||
| time: number; | ||
| index: number; // heap index | ||
| }; | ||
| propagationDelay: number; | ||
| } | ||
|
|
||
| export class EventQueue { | ||
| size: number | ||
| queue: Array<QueueObject> | ||
| frontIndex: number | ||
| time: number | ||
| constructor(size: number) { | ||
| this.size = size | ||
| this.queue = new Array(size) | ||
| this.frontIndex = 0 | ||
| this.time = 0 | ||
| } | ||
| private heap: QueueObject[] = []; | ||
| time = 0; | ||
|
|
||
| constructor(private size: number) {} | ||
|
|
||
| /** | ||
| * Insert an object with delay. Maintains min-heap by event time. | ||
| */ | ||
| add(obj: QueueObject, delay: number) { | ||
| const eventTime = this.time + (delay ?? obj.propagationDelay); | ||
| obj.queueProperties.time = eventTime; | ||
|
|
||
| if (obj.queueProperties.inQueue) { | ||
| obj.queueProperties.time = | ||
| this.time + (delay || obj.propagationDelay) | ||
| let i = obj.queueProperties.index | ||
| while ( | ||
| i > 0 && | ||
| obj.queueProperties.time > | ||
| this.queue[i - 1].queueProperties.time | ||
| ) { | ||
| this.swap(i, i - 1) | ||
| i-- | ||
| } | ||
| i = obj.queueProperties.index | ||
| while ( | ||
| i < this.frontIndex - 1 && | ||
| obj.queueProperties.time < | ||
| this.queue[i + 1].queueProperties.time | ||
| ) { | ||
| this.swap(i, i + 1) | ||
| i++ | ||
| } | ||
| return | ||
| // Update time + reposition | ||
| this.heapifyUp(obj.queueProperties.index); | ||
| this.heapifyDown(obj.queueProperties.index); | ||
| return; | ||
| } | ||
|
|
||
| if (this.frontIndex == this.size) throw 'EventQueue size exceeded' | ||
| this.queue[this.frontIndex] = obj | ||
| obj.queueProperties.time = this.time + (delay || obj.propagationDelay) | ||
| obj.queueProperties.index = this.frontIndex | ||
| this.frontIndex++ | ||
| obj.queueProperties.inQueue = true | ||
| let i = obj.queueProperties.index | ||
| while ( | ||
| i > 0 && | ||
| obj.queueProperties.time > this.queue[i - 1].queueProperties.time | ||
| ) { | ||
| this.swap(i, i - 1) | ||
| i-- | ||
| if (this.heap.length === this.size) { | ||
| throw new Error("EventQueue size exceeded"); | ||
| } | ||
|
|
||
| obj.queueProperties.index = this.heap.length; | ||
| obj.queueProperties.inQueue = true; | ||
| this.heap.push(obj); | ||
| this.heapifyUp(obj.queueProperties.index); | ||
| } | ||
|
|
||
| /** | ||
| * To add without any delay. | ||
| * @param {CircuitElement} obj - the object to be added | ||
| * Insert object at current time. | ||
| */ | ||
| addImmediate(obj: QueueObject) { | ||
| this.queue[this.frontIndex] = obj | ||
| obj.queueProperties.time = this.time | ||
| obj.queueProperties.index = this.frontIndex | ||
| obj.queueProperties.inQueue = true | ||
| this.frontIndex++ | ||
| this.add(obj, 0); | ||
| } | ||
|
|
||
| /** | ||
| * Function to swap two objects in queue. | ||
| * Pop next event (minimum event time) | ||
| */ | ||
| swap(v1: number, v2: number) { | ||
| const obj1 = this.queue[v1] | ||
| obj1.queueProperties.index = v2 | ||
| pop() { | ||
| if (this.isEmpty()) throw new Error("Queue Empty"); | ||
|
|
||
| const obj2 = this.queue[v2] | ||
| obj2.queueProperties.index = v1 | ||
| const top = this.heap[0]; | ||
| const last = this.heap.pop()!; | ||
|
|
||
| if (this.heap.length > 0) { | ||
| this.heap[0] = last; | ||
| last.queueProperties.index = 0; | ||
| this.heapifyDown(0); | ||
| } | ||
|
|
||
| this.queue[v1] = obj2 | ||
| this.queue[v2] = obj1 | ||
| top.queueProperties.inQueue = false; | ||
| this.time = top.queueProperties.time; | ||
| return top; | ||
| } | ||
|
|
||
| /** | ||
| * function to pop element from queue. | ||
| * Whether queue contains zero events. | ||
| */ | ||
| pop() { | ||
| if (this.isEmpty()) throw 'Queue Empty' | ||
|
|
||
| this.frontIndex-- | ||
| const obj = this.queue[this.frontIndex] | ||
| this.time = obj.queueProperties.time | ||
| obj.queueProperties.inQueue = false | ||
| return obj | ||
| isEmpty() { | ||
| return this.heap.length === 0; | ||
| } | ||
|
|
||
| /** | ||
| * function to reset queue. | ||
| * Reset entire queue. | ||
| */ | ||
| reset() { | ||
| for (let i = 0; i < this.frontIndex; i++) | ||
| this.queue[i].queueProperties.inQueue = false | ||
| this.time = 0 | ||
| this.frontIndex = 0 | ||
| for (const obj of this.heap) obj.queueProperties.inQueue = false; | ||
| this.heap = []; | ||
| this.time = 0; | ||
| } | ||
|
|
||
| /** | ||
| * function to check if empty queue. | ||
| */ | ||
| isEmpty() { | ||
| return this.frontIndex == 0 | ||
| // ------------------------------- | ||
| // Heap Utility Functions | ||
| // ------------------------------- | ||
|
|
||
| private swap(i: number, j: number) { | ||
| const a = this.heap[i]; | ||
| const b = this.heap[j]; | ||
| this.heap[i] = b; | ||
| this.heap[j] = a; | ||
| a.queueProperties.index = j; | ||
| b.queueProperties.index = i; | ||
| } | ||
|
|
||
| private heapifyUp(i: number) { | ||
| while (i > 0) { | ||
| const parent = Math.floor((i - 1) / 2); | ||
| if (this.heap[i].queueProperties.time >= this.heap[parent].queueProperties.time) break; | ||
| this.swap(i, parent); | ||
| i = parent; | ||
| } | ||
| } | ||
|
|
||
| private heapifyDown(i: number) { | ||
| const n = this.heap.length; | ||
|
|
||
| while (true) { | ||
| let smallest = i; | ||
| const left = 2 * i + 1; | ||
| const right = 2 * i + 2; | ||
|
|
||
| if (left < n && this.heap[left].queueProperties.time < this.heap[smallest].queueProperties.time) { | ||
| smallest = left; | ||
| } | ||
|
|
||
| if (right < n && this.heap[right].queueProperties.time < this.heap[smallest].queueProperties.time) { | ||
| smallest = right; | ||
| } | ||
|
|
||
| if (smallest === i) break; | ||
|
|
||
| this.swap(i, smallest); | ||
| i = smallest; | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CRITICAL: File extension mismatch - JavaScript file named as shell script.
This file contains Node.js JavaScript code but is named
build.sh. The.shextension indicates a shell script, which will cause execution failures and confusion. The inline comment on Line 1 confirms this should bebuild.js, and the static analysis errors from shellcheck confirm it cannot parse this as shell script.Please rename this file from
build.shtobuild.jsto match its actual content and the build command referenced in package.json.🧰 Tools
🪛 Shellcheck (0.11.0)
[error] 1-1: Was this intended as a comment? Use # in sh.
(SC1127)
[error] 1-1: Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.
(SC2148)
[error] 1-1: '(' is invalid here. Did you forget to escape it?
(SC1036)
[error] 1-1: Parsing stopped here. Invalid use of parentheses?
(SC1088)
🤖 Prompt for AI Agents