@antv/x6
Version:
JavaScript diagramming library that uses SVG and HTML for rendering
70 lines (69 loc) • 2.05 kB
TypeScript
interface DataItem<T> {
priority: number;
value: T;
id?: string;
}
type Data<T> = DataItem<T>[];
export interface PriorityQueueOptions<T> {
comparator?: Comparator;
data?: Data<T>;
}
type Comparator = (a: number, b: number) => number;
/**
* An implementation of the Priority Queue abstract data type.
*
* @see: http://en.wikipedia.org/wiki/Priority_queue
*
* It is like a normal stack or queue, but where each item has assigned a
* priority (a number). Items with higher priority are served before items
* with lower priority. This implementation uses binary heap as an internal
* representation of the queue. The time complexity of all the methods is as
* follows:
*
* - create: `O(n)`
* - insert: `O(log n)`
* - remove: `O(log n)`
* - peek: `O(1)`
* - isEmpty: `O(1)`
* - peekPriority: `O(1)`
*/
export declare class PriorityQueue<T> {
protected readonly comparator: Comparator;
protected index: {
[key: string]: number;
};
protected data: Data<T>;
constructor(options?: PriorityQueueOptions<T>);
/**
* Returns `true` if the priority queue is empty, `false` otherwise.
*/
isEmpty(): boolean;
/**
* Inserts a value with priority to the queue. Optionally pass a unique
* id of this item. Passing unique IDs for each item you insert allows
* you to use the `updatePriority()` operation.
* @param priority
* @param value
* @param id
*/
insert(priority: number, value: T, id?: string): this;
/**
* Returns the value of an item with the highest priority.
*/
peek(): T;
/**
* Returns the highest priority in the queue.
*/
peekPriority(): number;
updatePriority(id: string, priority: number): void;
/**
* Removes the item with the highest priority from the queue
*
* @returns The value of the removed item.
*/
remove(): T;
protected heapify(): void;
protected bubbleUp(index: number): void;
protected bubbleDown(index: number): void;
}
export {};