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

simple-heap

v1.0.0

Published

A basic binary heap data structure

Downloads

8

Readme

simple-heap

A no-frills binary heap with finite double precision weights and unique integer items from a finite universe.

Memory for the heap is stored in a set of typed arrays and other than the constructor and resize(), none of the methods require any memory allocation or garbage collection.

Example

var createHeap = require('simple-heap')

//Create a new heap with a maximum capacity of 10 items
var heap = createHeap(10)

for(var i=0; i<10; ++i) {
  //put() adds an item to the heap or changes a weight
  heap.put(
    i,               // item
    Math.pow(i-5,2)) // weight
}

for(var i=0; i<10; ++i) {
  //You can access the top item in the heap with minItem()/minWeight()
  console.log(heap.top(), heap.weight())

  //pop() when called with no arguments removes the top item
  heap.pop()
}

//You can change the weights of an item by calling upsert again
for(var i=0; i<10; ++i) {
  heap.put(i, Math.random())
}
for(var i=9; i>=0; --i) {
  heap.put(i, i)
}

//pop(item) takes an item out of the heap
for(var i=0; i<10; i+=2) {
  heap.pop(i)
}

//Remove all remaining items from heap
while(heap.size > 0) {
  console.log(heap.pop())
}

//When you are all done with the heap you need to dispose it so its memory
//can be reused by typedarray-pool
heap.dispose()

Output:

5 0
4 1
6 1
7 4
3 4
2 9
8 9
9 16
1 16
0 25
1
5
7
3
9

Install

npm i simple-heap

API

Constructor

var heap = require('simple-heap')(capacity)

Creates a new simple heap.

  • capacity is the maximum number of items in the heap.

Returns A new heap data structure

Time complexity O(capacity)

Space complexity O(capacity)

Properties

heap.size

The number of elements in the heap

heap.capacity

The maximum capacity of the heap

Methods

heap.put(item, weight)

If item is not in the heap, then adds it to the heap with the given weight. Otherwise, if item is in the heap then the weight of item is changed to weight

  • item is an integer between 0 and heap.capacity representing the key of the item
  • weight is the weight to assign to item in the queue

Time complexity O(log(heap.size))

heap.pop([item])

Removes item from the heap, or if not specified removes the smallest item.

  • item is an optional argument, which if passed is the item to remove otherwise the top of the heap is removed.

Returns The item which was removed or -1 if the heap is empty

Time complexity O(log(heap.size))

heap.weight([item])

Find the weight of the item or else the weight of the top item

  • item is the item whose weight we are accessing. Defaults to heap.top()

Returns The weight of item in the heap, or if item is not in the heap returns NaN

Time complexity O(1)

heap.top()

Returns The top item or -1 if the heap is empty

Time complexity O(1)

heap.resize(ncapacity)

Resizes the heap to capacity

  • ncapacity is the new heap capacity, must be larger than heap.size. Takes `O(c)

Time complexity O(ncapacity)

heap.clear()

Removes all items from the heap. Takes O(size) time

heap.dispose()

Releases all resources associated with the heap.

Time complexity O(1)

License

(c) 2015 Mikola Lysenko. MIT License