es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
76 lines (75 loc) • 2.94 kB
TypeScript
/**
* Represents a priority queue data structure.
* @template T The type of elements in the priority queue.
* @example
* const pq = new PriorityQueue<string>();
* pq.enqueue('task1', 2).enqueue('task2', 1);
* console.log(pq.dequeue()); // 'task2'
*/
export declare class PriorityQueue<T> {
private items;
/**
* Adds an item to the priority queue with the specified priority.
* @param {T} item - The item to add to the priority queue.
* @param {number} priority - The priority of the item (lower numbers indicate higher priority). Default: 1
* @returns {PriorityQueue<T>} The priority queue instance for chaining.
*/
enqueue(item: T, priority?: number): PriorityQueue<T>;
private bubbleUp;
/**
* Removes and returns the item with the highest priority from the queue.
* @returns {T} The item with the highest priority, or undefined if the queue is empty.
* @throws {Error} If the priority queue is empty.
*/
dequeue(): T;
private bubbleDown;
/**
* Returns the number of items in the priority queue.
* @returns {number} The size of the priority queue.
*/
size(): number;
/**
* Checks if the priority queue is empty.
* @returns {boolean} True if the priority queue is empty, false otherwise.
*/
isEmpty(): boolean;
/**
* Returns the item with the highest priority without removing it.
* @returns {T | undefined} The item with the highest priority, or undefined if the queue is empty.
*/
peek(): T | undefined;
/**
* Checks if the priority queue contains a specific item.
* @param {T} item - The item to check for.
* @returns {boolean} True if the item is in the priority queue, false otherwise.
*/
contains(item: T): boolean;
/**
* Changes the priority of an existing item.
* @param {T} item - The item whose priority is to be changed.
* @param {number} newPriority - The new priority for the item.
* @returns {boolean} True if the priority was changed, false if the item was not found.
*/
changePriority(item: T, newPriority: number): boolean;
/**
* Removes a specific item from the priority queue.
* @param {T} item - The item to remove.
* @returns {T | boolean} The Value of the removed item if the item was removed, false otherwise.
*/
remove(item: T): T | boolean;
/**
* Merges another priority queue into this one.
* @param {PriorityQueue<T>} otherQueue - The other priority queue to merge.
*/
merge(otherQueue: PriorityQueue<T>): void;
/**
* Serializes the priority queue to a JSON string.
* @returns {string} The serialized priority queue.
*/
serialize(): string;
/**
* Deserializes a JSON string to populate the priority queue.
* @param {string} data - The JSON string to deserialize.
*/
deserialize(data: string): void;
}