GithubHelp home page GithubHelp logo

yuukinajima / sortedset Goto Github PK

View Code? Open in Web Editor NEW

This project forked from danneu/sortedset

0.0 2.0 0.0 32 KB

a simple sorted set implementation that allows a custom compare function

JavaScript 100.00%

sortedset's Introduction

sortedset NPM version Build Status

A fast, simple sorted set implementation for javascript (browser and node) that allows a custom compare function.

Similar API to javascript's Set.

npm install sortedset

Compare Function

The compare function is the same thing that works with Array#sort().

  • If compare(a, b) < 0, a comes before b.
  • If compare(a, b) === 0, a and b are considered identical in the set.
  • If compare(a, b) > 0, b comes before a.

If no compare function is provided, a default ascending comparator is used.

const SortedSet = require('sortedset')

const set = new SortedSet([2, 1, 3])

console.log(set.toArray()) // [1, 2, 3]

Or pass in a custom comparator.

const SortedSet = require('sortedset')

function reverse (a, b) {
  if (a === b) return 0
  return a < b ? 1 : -1
}

const set = new SortedSet([2, 1, 3], reverse)

console.log(set.toArray()) // [3, 2, 1]

API

Constructor:

  • new SortedSet()
  • new SortedSet([2, 1, 3])
  • new SortedSet(customCompare)
  • new SortedSet([2, 1, 3], customCompare)

Properties:

  • SortedSet#size

Methods:

  • SortedSet#add(item)
  • SortedSet#delete(item) Note: Returns the deleted item if it existed, else undefined
  • SortedSet#clear()
  • SortedSet#has(item)
  • SortedSet#toArray()

sortedset's People

Contributors

danneu avatar

Watchers

yuuki najima avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.