From 2b7bf3ce95f43d1299ee964c25cfce5177230b24 Mon Sep 17 00:00:00 2001 From: Revone Date: Thu, 16 Nov 2023 21:08:57 +0800 Subject: [PATCH] feat: The performance report generator supports parameter filtering commands, which can be used to run specific tests individually. It generates individual performance comparison reports. Enrich the COMMANDS.md document. --- src/data-structures/heap/heap.ts | 224 ++++++++++---------- test/unit/data-structures/heap/heap.test.ts | 4 +- 2 files changed, 114 insertions(+), 114 deletions(-) diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts index 0bc2df6..3dabf98 100644 --- a/src/data-structures/heap/heap.ts +++ b/src/data-structures/heap/heap.ts @@ -786,115 +786,115 @@ export class FibonacciHeap { } -export class CHeap { - - protected _length = 0; - private readonly _priorityQueue: T[] = []; - private readonly _cmp: (x: T, y: T) => number; - - constructor( - cmp: (x: T, y: T) => number = - function (x: T, y: T) { - if (x > y) return -1; - if (x < y) return 1; - return 0; - }, - copy = true - ) { - - this._cmp = cmp; - - } - - clear() { - this._length = 0; - this._priorityQueue.length = 0; - } - - push(item: T) { - this._priorityQueue.push(item); - this._pushUp(this._length); - this._length += 1; - } - - pop() { - if (this._length === 0) return; - const value = this._priorityQueue[0]; - const last = this._priorityQueue.pop()!; - this._length -= 1; - if (this._length) { - this._priorityQueue[0] = last; - this._pushDown(0, this._length >> 1); - } - return value; - } - - top(): T | undefined { - return this._priorityQueue[0]; - } - - find(item: T) { - return this._priorityQueue.indexOf(item) >= 0; - } - - remove(item: T) { - const index = this._priorityQueue.indexOf(item); - if (index < 0) return false; - if (index === 0) { - this.pop(); - } else if (index === this._length - 1) { - this._priorityQueue.pop(); - this._length -= 1; - } else { - this._priorityQueue.splice(index, 1, this._priorityQueue.pop()!); - this._length -= 1; - this._pushUp(index); - this._pushDown(index, this._length >> 1); - } - return true; - } - - updateItem(item: T) { - const index = this._priorityQueue.indexOf(item); - if (index < 0) return false; - this._pushUp(index); - this._pushDown(index, this._length >> 1); - return true; - } - - toArray() { - return [...this._priorityQueue]; - } - - private _pushUp(pos: number) { - const item = this._priorityQueue[pos]; - while (pos > 0) { - const parent = (pos - 1) >> 1; - const parentItem = this._priorityQueue[parent]; - if (this._cmp(parentItem, item) <= 0) break; - this._priorityQueue[pos] = parentItem; - pos = parent; - } - this._priorityQueue[pos] = item; - } - - private _pushDown(pos: number, halfLength: number) { - const item = this._priorityQueue[pos]; - while (pos < halfLength) { - let left = pos << 1 | 1; - const right = left + 1; - let minItem = this._priorityQueue[left]; - if ( - right < this._length && - this._cmp(minItem, this._priorityQueue[right]) > 0 - ) { - left = right; - minItem = this._priorityQueue[right]; - } - if (this._cmp(minItem, item) >= 0) break; - this._priorityQueue[pos] = minItem; - pos = left; - } - this._priorityQueue[pos] = item; - } -} +// export class CHeap { +// +// protected _length = 0; +// private readonly _priorityQueue: T[] = []; +// private readonly _cmp: (x: T, y: T) => number; +// +// constructor( +// cmp: (x: T, y: T) => number = +// function (x: T, y: T) { +// if (x > y) return -1; +// if (x < y) return 1; +// return 0; +// }, +// copy = true +// ) { +// +// this._cmp = cmp; +// +// } +// +// clear() { +// this._length = 0; +// this._priorityQueue.length = 0; +// } +// +// push(item: T) { +// this._priorityQueue.push(item); +// this._pushUp(this._length); +// this._length += 1; +// } +// +// pop() { +// if (this._length === 0) return; +// const value = this._priorityQueue[0]; +// const last = this._priorityQueue.pop()!; +// this._length -= 1; +// if (this._length) { +// this._priorityQueue[0] = last; +// this._pushDown(0, this._length >> 1); +// } +// return value; +// } +// +// top(): T | undefined { +// return this._priorityQueue[0]; +// } +// +// find(item: T) { +// return this._priorityQueue.indexOf(item) >= 0; +// } +// +// remove(item: T) { +// const index = this._priorityQueue.indexOf(item); +// if (index < 0) return false; +// if (index === 0) { +// this.pop(); +// } else if (index === this._length - 1) { +// this._priorityQueue.pop(); +// this._length -= 1; +// } else { +// this._priorityQueue.splice(index, 1, this._priorityQueue.pop()!); +// this._length -= 1; +// this._pushUp(index); +// this._pushDown(index, this._length >> 1); +// } +// return true; +// } +// +// updateItem(item: T) { +// const index = this._priorityQueue.indexOf(item); +// if (index < 0) return false; +// this._pushUp(index); +// this._pushDown(index, this._length >> 1); +// return true; +// } +// +// toArray() { +// return [...this._priorityQueue]; +// } +// +// private _pushUp(pos: number) { +// const item = this._priorityQueue[pos]; +// while (pos > 0) { +// const parent = (pos - 1) >> 1; +// const parentItem = this._priorityQueue[parent]; +// if (this._cmp(parentItem, item) <= 0) break; +// this._priorityQueue[pos] = parentItem; +// pos = parent; +// } +// this._priorityQueue[pos] = item; +// } +// +// private _pushDown(pos: number, halfLength: number) { +// const item = this._priorityQueue[pos]; +// while (pos < halfLength) { +// let left = pos << 1 | 1; +// const right = left + 1; +// let minItem = this._priorityQueue[left]; +// if ( +// right < this._length && +// this._cmp(minItem, this._priorityQueue[right]) > 0 +// ) { +// left = right; +// minItem = this._priorityQueue[right]; +// } +// if (this._cmp(minItem, item) >= 0) break; +// this._priorityQueue[pos] = minItem; +// pos = left; +// } +// this._priorityQueue[pos] = item; +// } +// } diff --git a/test/unit/data-structures/heap/heap.test.ts b/test/unit/data-structures/heap/heap.test.ts index 8e625f2..081d4e0 100644 --- a/test/unit/data-structures/heap/heap.test.ts +++ b/test/unit/data-structures/heap/heap.test.ts @@ -1,5 +1,5 @@ -import { CHeap, FibonacciHeap, MaxHeap, MinHeap } from '../../../../src'; -import { calcRunTime, logBigOMetricsWrap } from '../../../utils'; +import { FibonacciHeap, MaxHeap, MinHeap } from '../../../../src'; +import { logBigOMetricsWrap } from '../../../utils'; describe('Heap Operation Test', () => { it('should numeric heap work well', function () {