From ea5f7825775b274ea06730328ebb527363e721a8 Mon Sep 17 00:00:00 2001 From: Revone Date: Wed, 30 Aug 2023 10:49:49 +0800 Subject: [PATCH] rename the priority to priorityExtractor --- src/data-structures/heap/heap.ts | 25 ++++++++++++++++--------- src/data-structures/types/heap.ts | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts index 6e9c896..49dc797 100644 --- a/src/data-structures/heap/heap.ts +++ b/src/data-structures/heap/heap.ts @@ -10,6 +10,13 @@ import type {HeapOptions} from '../types'; export class HeapItem { + /** + * The constructor function initializes an instance of a class with a priority and a value. + * @param {number} priority - The `priority` parameter is a number that represents the priority of the value. It is + * optional and has a default value of `NaN`. + * @param {T | null} [val=null] - The `val` parameter is of type `T | null`, which means it can accept a value of type + * `T` or `null`. + */ constructor(priority: number = NaN, val: T | null = null) { this._val = val; this._priority = priority; @@ -44,13 +51,13 @@ export abstract class Heap { */ protected constructor(options?: HeapOptions) { if (options) { - const {priority} = options; - if (priority !== undefined && typeof priority !== 'function') { + const {priorityExtractor} = options; + if (priorityExtractor !== undefined && typeof priorityExtractor !== 'function') { throw new Error('.constructor expects a valid priority function'); } - this._priorityCb = priority || ((el) => +el); + this._priorityExtractor = priorityExtractor || ((el) => +el); } else { - this._priorityCb = (el) => +el; + this._priorityExtractor = (el) => +el; } } @@ -60,9 +67,9 @@ export abstract class Heap { return this._pq; } - protected _priorityCb: (val: T) => number; - get priorityCb() { - return this._priorityCb; + protected _priorityExtractor: (val: T) => number; + get priorityExtractor() { + return this._priorityExtractor; } /** @@ -120,14 +127,14 @@ export abstract class Heap { throw new Error('.add expects a numeric priority'); } - if (Number.isNaN(+priority) && Number.isNaN(this._priorityCb(val))) { + if (Number.isNaN(+priority) && Number.isNaN(this._priorityExtractor(val))) { throw new Error( '.add expects a numeric priority ' + 'or a constructor callback that returns a number' ); } - const _priority = !Number.isNaN(+priority) ? priority : this._priorityCb(val); + const _priority = !Number.isNaN(+priority) ? priority : this._priorityExtractor(val); this._pq.add(new HeapItem(_priority, val)); return this; } diff --git a/src/data-structures/types/heap.ts b/src/data-structures/types/heap.ts index 389697a..02dc2d4 100644 --- a/src/data-structures/types/heap.ts +++ b/src/data-structures/types/heap.ts @@ -1,5 +1,5 @@ export type HeapOptions = { - priority?: (element: T) => number; + priorityExtractor?: (element: T) => number; // TODO there is an idea that support chaining which is for conveniently using the data structure // isChaining? : boolean } \ No newline at end of file