npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@algorithm.ts/queue

v4.0.0

Published

Typescript implementation of queues, such as priority queue and circular queue.

Downloads

243

Readme

A typescript implementation of Priority Queue (based on min-heap) and Circular Queue.

Install

  • npm

    npm install --save @algorithm.ts/queue
  • yarn

    yarn add @algorithm.ts/queue

Usage

PriorityQueue

Priority Queue is a special queue structure, the first element of the queue always returns to the minimum value in the queue, and the amortized time complexity of the enqueue and dequeue operations are both $O(\log N)$.

  • IPriorityQueue: PriorityQueue implements the IPriorityQueue interface.

    | Signature | Description | | :------------------------------------------------------------------------------: | :------------------------------------------------------------------------------- | | consuming(): IterableIterator<T> | Popup the elements from the queue by the dequeue order. | | count(filter: (element: T) => boolean): number | Count the elements in the queue which matched by the filter. | | dequeue(newElement?: T): T\|undefined | Popup the top element, and push the given newElement if it is not undefined. | | enqueue(val: T): void | Push a element into the priority queue. | | enqueues(elements: Iterable<T>): void | Push multiple elements into the priority queue. | | enqueues_advance(elements: ReadonlyArray<T>, start: number, end: number): void | Push multiple elements into the priority queue. | | exclude(filter: (element: T) => boolean): void | Remove elements matched the filter. | | destroy(): void | Destroy the queue and release the memory. | | front(): T\|undefined | Get the top element from the priority queue. | | init(initialElements?: Iterable<T>: void | Initialize priority queue with initial elements. | | readonly size: number | Get the number of elements. | | readonly destroyed: number | Indicate whether the priority queue destroyed. |

  • IPriorityQueueProps

    export interface IPriorityQueueProps<T> {
      /**
      * If the compare(x, y) < 0, then x has a higher precedence than y.
      */
      compare: ICompare<T>
    }
    • new PriorityQueue<number>({ compare: (x, y) => x - y }): The top element has a minimum value.
    • new PriorityQueue<number>({ compare: (x, y) => y - x }): The top element has a maximum value.

CircularQueue

Circular queue is a queue structure, the main purpose of its design is to reuse space as much as possible on the basis of ordinary queues. Circular queues usually need to specify the maximum volume C of the collector. If the number of elements in the queue exceeds C, only the most recent C elements are kept in the queue. Other operations are the same as ordinary queues.

  • ICircularQueue: CircularQueue implements the ICircularQueue interface.

    | Signature | Description | | :------------------------------------------------------------------------------: | :------------------------------------------------------------------------------- | | consuming(): IterableIterator<T> | Popup the elements from the queue by the dequeue order. | | count(filter: (element: T) => boolean): number | Count the elements in the queue which matched by the filter. | | dequeue(newElement?: T): T\|undefined | Popup the top element, and push the given newElement if it is not undefined. | | enqueue(val: T): void | Push a element into the circular queue. | | enqueues(elements: Iterable<T>): void | Push multiple elements into the circular queue. | | enqueues_advance(elements: ReadonlyArray<T>, start: number, end: number): void | Push multiple elements into the circular queue. | | exclude(filter: (element: T) => boolean): void | Remove elements matched the filter. | | destroy(): void | Destroy the queue and release the memory. | | front(): T\|undefined | Get the first enqueued element from the circular queue. | | back(): T\|undefined | Get the last enqueued element from the circular queue. | | init(initialElements?: Iterable<T>: void | Initialize circular queue with initial elements. | | resize(MAX_SIZE: number): void | Resize the max-size of queue with the given size. | | rearrange(): void | Rearrange elements, that is, put the first element in place 0-index. | | readonly size: number | Get the number of elements. | | readonly destroyed: number | Indicate whether the circular queue destroyed. |

  • ICircularQueueProps

    export interface ICircularQueueProps {
      /**
      * Initial capacity of the circular queue.
      */
      capacity?: number
      /**
      * Automatically extends the queue capacity when the queue is full.
      * @default true
      */
      autoResize?: boolean
      /**
      * @default 1.5
      */
      autoResizeExpansionRatio?: number
    }

Example

  • Basic -- PriorityQueue

    import { PriorityQueue } = '@algorithm.ts/queue'
    
    const Q = new PriorityQueue<number>({ compare: (x, y) => y - x })
    
    Q.enqueue(3)
    Q.enqueue(7)
    Q.enqueue(-5)
    Q.enqueue(1)
    Q.size        // => 4
    Array.from(Q) // => [7, 3, -5, 1] !!!NOTE: the result are not guaranteed to be ordered.
    
    Q.dequeue()   // => 7
    Q.dequeue()   // => 3
    Q.front()     // => 1
    Q.front()     // => 1
    Q.dequeue()   // => 1
    Q.front()     // => -5
    Q.dequeue()   // => -5
    Q.front()     // => undefined
    Q.dequeue()   // => undefined
  • Basic -- CircularQueue

    import { CircularQueue } from '@algorithm.ts/queue'
    
    const queue = new CircularQueue<{ name: string }>()
    
    // Initialize the circular-queue with the maximum number of elements it can
    // be managed.
    queue.init(100)
    
    // Append a element to the end of the queue.
    queue.enqueue({ name: 'alice' }) // => 0
    queue.enqueue({ name: 'bob' }) // => 1
    queue.size          // => 2
    
    // Get the front element of the queue.
    queue.front()       // => { name: 'alice' }
    
    // Get the last element of the queue.
    queue.back()        // => { name: 'bob' }
    
    // Take off the first element of the queue.
    queue.dequeue()     // => { name: 'alice' }
    queue.size          // => 1
  • A solution for 剑指 offer#63 https://www.nowcoder.com/practice/9be0172896bd43948f8a32fb954e1be1

    import { PriorityQueue } from '@algorithm.ts/queue'
    
    const lowerQ = new PriorityQueue<number>({ compare: (x, y) => x - y })
    const upperQ = new PriorityQueue<number>({ compare: (x, y) => y - x })
    
    export function Insert(num: number): void {
      if (lowerQ.size === upperQ.size) {
        upperQ.enqueue(num)
        lowerQ.enqueue(upperQ.dequeue()!)
      } else {
        lowerQ.enqueue(num)
        upperQ.enqueue(lowerQ.dequeue()!)
      }
    }
    
    export function GetMedian(): number {
      return lowerQ.size === upperQ.size
        ? (lowerQ.front()! + upperQ.front()!) / 2
        : lowerQ.front()!
    }

Related