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

implement eventqueue using heap #180

Merged
merged 2 commits into from
May 27, 2020
Merged
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
115 changes: 115 additions & 0 deletions src/MinHeap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
export interface HeapWrapper<T> {
key: number,
value: T
}

export class MinHeap<T> {
private heap: HeapWrapper<T>[];
constructor() {
this.heap = [];
}
shift(v: number) {
this.heap = this.heap.map(({ key, value }) => ({ key: key + v, value }));
}
len() {
return this.heap.length;
}
push(v: HeapWrapper<T>) {
const loc = this.len();
this.heap.push(v);
this.updateUp(loc);
}
pop(): HeapWrapper<T> {
if (this.len() == 0) {
throw new Error("no element to pop");
}
const top = this.heap[0];
if (this.len() > 1) {
this.heap[0] = this.heap.pop() as HeapWrapper<T>;
this.updateDown(0);
} else {
this.heap.pop();
}
return top;
}
find(v: T): HeapWrapper<T> | null {
for (let i = 0; i < this.len(); i++) {
if (v == this.heap[i].value) {
return this.heap[i];
}
}
return null;
}
remove(v: T) {
let index = null;
for (let i = 0; i < this.len(); i++) {
if (v == this.heap[i].value) {
index = i;
}
}
if (index) {
if (this.len() > 1) {
this.heap[index] = this.heap.pop() as HeapWrapper<T>;
this.updateDown(index);
return true;
} else {
this.heap.pop();
return true;
}
}
return false;
}
private parentNode(x: number): number {
return Math.floor((x - 1) / 2);
}
private leftChildNode(x: number): number {
return 2 * x + 1;
}
private rightChildNode(x: number): number {
return 2 * x + 2;
}
private existNode(x: number): boolean {
return x >= 0 && x < this.heap.length;
}
private swap(x: number, y: number) {
const t = this.heap[x];
this.heap[x] = this.heap[y];
this.heap[y] = t;
}
private minNode(numbers: number[]) {
const validnumbers = numbers.filter(this.existNode.bind(this));
let minimal = validnumbers[0];
for (const i of validnumbers) {
if (this.heap[i].key < this.heap[minimal].key) {
minimal = i;
}
}
return minimal;
}
private updateUp(x: number) {
if (x == 0) {
return;
}
const parent = this.parentNode(x);
if (this.existNode(parent) && this.heap[x].key < this.heap[parent].key) {
this.swap(x, parent);
this.updateUp(parent);
}
}
private updateDown(x: number) {
const leftChild = this.leftChildNode(x);
const rightChild = this.rightChildNode(x);
if (!this.existNode(leftChild)) {
return;
}
const m = this.minNode([x, leftChild, rightChild]);
if (m != x) {
this.swap(x, m);
this.updateDown(m);
}
}
debugPrint() {
console.log(this.heap);
}

}
54 changes: 18 additions & 36 deletions src/eventqueue.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {MinHeap} from "./MinHeap";

export default class EventQueue<T = any> {
_time: number;
_eventTimes: number[];
_events: T[];
_events: MinHeap<T>;

/**
* @class Generic event queue: stores events and retrieves them based on their time
*/
constructor() {
this._time = 0;
this._events = [];
this._eventTimes = [];
this._events = new MinHeap();
}

/**
Expand All @@ -21,8 +21,7 @@ export default class EventQueue<T = any> {
* Clear all scheduled events
*/
clear() {
this._events = [];
this._eventTimes = [];
this._events = new MinHeap();
return this;
}

Expand All @@ -31,32 +30,23 @@ export default class EventQueue<T = any> {
* @param {number} time
*/
add(event: T, time: number) {
let index = this._events.length;
for (let i=0;i<this._eventTimes.length;i++) {
if (this._eventTimes[i] > time) {
index = i;
break;
}
}

this._events.splice(index, 0, event);
this._eventTimes.splice(index, 0, time);
this._events.push({ key: time, value: event });
}

/**
* Locates the nearest event, advances time if necessary. Returns that event and removes it from the queue.
* @returns {? || null} The event previously added by addEvent, null if no event available
*/
get() {
if (!this._events.length) { return null; }
if (!this._events.len()) { return null; }

let time = this._eventTimes.splice(0, 1)[0];
let { key: time, value: event } = this._events.pop();
if (time > 0) { /* advance */
this._time += time;
for (let i=0;i<this._eventTimes.length;i++) { this._eventTimes[i] -= time; }
this._events.shift(-time);
}

return this._events.splice(0, 1)[0];
return event;
}

/**
Expand All @@ -65,9 +55,12 @@ export default class EventQueue<T = any> {
* @returns {number} time
*/
getEventTime(event: T) {
let index = this._events.indexOf(event);
if (index == -1) { return undefined }
return this._eventTimes[index];
const r = this._events.find(event);
if (r) {
const { key } = r;
return key;
}
return undefined;
}

/**
Expand All @@ -76,18 +69,7 @@ export default class EventQueue<T = any> {
* @returns {bool} success?
*/
remove(event: T) {
let index = this._events.indexOf(event);
if (index == -1) { return false }
this._remove(index);
return true;
};

/**
* Remove an event from the queue
* @param {int} index
*/
_remove(index: number) {
this._events.splice(index, 1);
this._eventTimes.splice(index, 1);
this._events.remove(event);
};
}

Loading