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

feat(gsoc'24): Change the implementation of the priority queue to use a heap data structure (#3916) #369

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
168 changes: 93 additions & 75 deletions src/simulator/src/eventQueue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Event Queue is simply a priority Queue, basic implementation O(n^2).
* Event Queue is simply a heap Queue, basic implementation O(logn).
* @category eventQueue
*/

Expand All @@ -12,112 +12,130 @@ interface QueueObject {
propagationDelay: number
}

export class EventQueue {
export default class EventQueue {
size: number
queue: Array<QueueObject>
frontIndex: number
backIndex: number
time: number
constructor(size: number) {
this.size = size
this.queue = new Array(size)
this.frontIndex = 0
this.time = 0
this.size = size;
this.queue = new Array(size);
this.backIndex = 0;
this.time = 0;
}

add(obj: QueueObject, delay: number) {
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
this.remove(obj); // Remove the existing element from the heap
}

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.backIndex === this.size) throw 'EventQueue size exceeded';
obj.queueProperties.time = this.time + (delay || obj.propagationDelay);
obj.queueProperties.index = this.backIndex;
this.queue[this.backIndex] = obj;
obj.queueProperties.inQueue = true;
this.heapifyUp(this.backIndex); // Heapify the element up to its correct position
this.backIndex++;
}

/**
* To add without any delay.
* @param {CircuitElement} obj - the object to be added
*/
* To add without any delay.
*/
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.
*/
* Function to remove an object from the queue
*/
remove(obj: QueueObject) {
if (!obj.queueProperties.inQueue) return;
const { index } = obj.queueProperties;
this.swap(index, this.backIndex - 1);
obj.queueProperties.inQueue = false;
this.backIndex--;
this.heapifyDown(index); // Heapify the swapped element down to its correct position
}

/**
* Function to swap two objects in the queue
*/
swap(v1: number, v2: number) {
const obj1 = this.queue[v1]
obj1.queueProperties.index = v2
const obj1 = this.queue[v1];
obj1.queueProperties.index = v2;

const obj2 = this.queue[v2]
obj2.queueProperties.index = v1
const obj2 = this.queue[v2];
obj2.queueProperties.index = v1;

this.queue[v1] = obj2
this.queue[v2] = obj1
this.queue[v1] = obj2;
this.queue[v2] = obj1;
}

/**
* function to pop element from queue.
*/
* Function to heapify the element up to its correct position.
*/
heapifyUp(startIndex: number) {
let index = startIndex;
while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2);
if (this.queue[index].queueProperties.time >= this.queue[parentIndex].queueProperties.time) break;
this.swap(index, parentIndex);
index = parentIndex;
}
}

/**
* Function to heapify the element down to its correct position.
*/
heapifyDown(indexStart: number) {
let index = indexStart;
while (true) {
const leftChildIndex = 2 * index + 1;
const rightChildIndex = 2 * index + 2;
let smallestChildIndex = index;

if (leftChildIndex < this.backIndex && this.queue[leftChildIndex].queueProperties.time < this.queue[smallestChildIndex].queueProperties.time) {
smallestChildIndex = leftChildIndex;
}

if (rightChildIndex < this.backIndex && this.queue[rightChildIndex].queueProperties.time < this.queue[smallestChildIndex].queueProperties.time) {
smallestChildIndex = rightChildIndex;
}

if (smallestChildIndex === index) break;

this.swap(index, smallestChildIndex);
index = smallestChildIndex;
}
}

/**
* function to pop element from the front of the queue
*/
pop() {
if (this.isEmpty()) throw 'Queue Empty'
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
const obj = this.queue[0];
this.time = obj.queueProperties.time;
this.remove(obj);
return obj;
}

/**
* function to reset queue.
* function to reset the queue
*/
reset() {
for (let i = 0; i < this.frontIndex; i++)
this.queue[i].queueProperties.inQueue = false
this.time = 0
this.frontIndex = 0
while (this.backIndex > 0) {
this.queue[this.backIndex - 1].queueProperties.inQueue = false;
this.backIndex--;
}
this.time = 0;
}

/**
* function to check if empty queue.
*/
* function to check if the queue is empty.
*/
isEmpty() {
return this.frontIndex == 0
return this.backIndex === 0;
}
}
}
2 changes: 1 addition & 1 deletion src/simulator/src/simulationArea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const simulationArea: SimulationArea = {
this.canvas = document.getElementById('simulationArea') as HTMLCanvasElement;
this.canvas.width = width;
this.canvas.height = height;
this.simulationQueue = new EventQueue(10000);
this.simulationQueue = new EventQueue(100000);
this.context = this.canvas.getContext('2d')!;
simulationArea.changeClockTime(simulationArea.timePeriod);
this.mouseDown = false;
Expand Down
Loading