-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Performance improvements with immutable rules
- Loading branch information
Showing
12 changed files
with
231 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,27 @@ | ||
import { Queue } from '../queue'; | ||
|
||
describe('Queue', () => { | ||
test('enqueue / dequeue / size', () => { | ||
const queue = new Queue<string>(); | ||
|
||
expect(queue.size).toBe(0); | ||
|
||
queue.enqueue(3, 'data1'); | ||
queue.enqueue(1, 'data2'); | ||
queue.enqueue(2, 'data3'); | ||
|
||
expect(queue.size).toBe(3); | ||
|
||
expect(queue.dequeue()).toBe('data1'); | ||
expect(queue.size).toBe(2); | ||
|
||
expect(queue.dequeue()).toBe('data3'); | ||
expect(queue.size).toBe(1); | ||
|
||
expect(queue.dequeue()).toBe('data2'); | ||
expect(queue.size).toBe(0); | ||
|
||
expect(queue.dequeue()).toBeNull(); | ||
expect(queue.size).toBe(0); | ||
}); | ||
}); |
This file contains 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 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 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,97 @@ | ||
/** | ||
* Priority Queue (using pairing heap algorithm) | ||
*/ | ||
type QueueNode<T> = { | ||
priority: number; | ||
data: T; | ||
next: QueueNode<T> | null; | ||
head: QueueNode<T> | null; | ||
}; | ||
|
||
const merge = <T>(i: QueueNode<T> | null, j: QueueNode<T> | null) => { | ||
if (i == null) { | ||
return j; | ||
} | ||
|
||
if (j == null) { | ||
return i; | ||
} | ||
|
||
let tmp: QueueNode<T> | null = null; | ||
|
||
if (i.priority < j.priority) { | ||
tmp = i; | ||
i = j; | ||
j = tmp; | ||
} | ||
|
||
j.next = i.head; | ||
i.head = j; | ||
|
||
return i; | ||
}; | ||
|
||
const mergeList = <T>(s: QueueNode<T> | null) => { | ||
let n: QueueNode<T> | null = null; | ||
let a: QueueNode<T> | null = null; | ||
let b: QueueNode<T> | null = null; | ||
let j: QueueNode<T> | null = null; | ||
|
||
while (s != null) { | ||
a = s; | ||
b = null; | ||
s = s.next; | ||
a.next = null; | ||
|
||
if (s != null) { | ||
b = s; | ||
s = s.next; | ||
b.next = null; | ||
} | ||
|
||
a = merge(a, b); | ||
a!.next = n; | ||
n = a; | ||
} | ||
|
||
while (n != null) { | ||
j = n; | ||
n = n.next; | ||
s = merge(j, s); | ||
} | ||
|
||
return s; | ||
}; | ||
|
||
export class Queue<T> { | ||
private _root: QueueNode<T> | null = null; | ||
private _size = 0; | ||
|
||
public enqueue(priority: number, data: T): void { | ||
this._root = merge(this._root, { | ||
priority, | ||
data, | ||
next: null, | ||
head: null, | ||
}); | ||
|
||
this._size += 1; | ||
} | ||
|
||
public dequeue(): T | null { | ||
if (this._root == null) { | ||
return null; | ||
} | ||
|
||
const result = this._root.data; | ||
|
||
this._root = mergeList(this._root.head); | ||
this._size -= 1; | ||
|
||
return result; | ||
} | ||
|
||
public get size(): number { | ||
return this._size; | ||
} | ||
} |
Oops, something went wrong.