New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@algorithm.ts/queue

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@algorithm.ts/queue

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

  • 3.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
27
decreased by-25%
Maintainers
1
Weekly downloads
 
Created
Source

@algorithm.ts/queue


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 Collection interface.

    MemberReturnDescription
    init(elements?: T[], startPos?: number, endPos?: number)voidInitialize priority queue with initial elements.
    enqueue(val: T)voidDrop a element into the priority queue.
    enqueues(elements: T[], startIndex?: number, endIndex?: number)voidDrop multiple elements into the priority queue.
    dequeue(newElement?: T)`Tundefined`
    splice(filter, newElements?: T[], startIndex?: number, endIndex?: number)voidRemove existed elements which is not passed the filter, then enqueues the additional elements.
    front()`Tundefined`
    sizenumberGet the number of elements.
  • 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 Collection interface.

    SignatureDescription
    init(elements?: T[], startPos?: number, endPos?: number): voidInitialize circular queue with initial elements.
    enqueue(val: T): voidDrop a element into the circular queue.
    enqueues(elements: T[], startIndex?: number, endIndex?: number): voidDrop multiple elements into the circular queue.
    `dequeue(newElement?: T): Tundefined`
    splice(filter, newElements?: T[], startIndex?: number, endIndex?: number): voidRemove existed elements which is not passed the filter, then enqueues the additional elements.
    `front(): Tundefined`
    `pop(): Tundefined`
    `back(): Tundefined`
    `unshift(): Tundefined`
    resize(MAX_SIZE: number): voidResize the max-size of queue with the given size.
    rearrange(): voidRearrange elements, that is, put the first element in place 0-index.
  • 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()!
    }
    

Keywords

FAQs

Package last updated on 10 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc