Socket
Socket
Sign inDemoInstall

set-ds

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    set-ds

Set data structure


Version published
Weekly downloads
0
Maintainers
1
Install size
16.0 kB
Created
Weekly downloads
 

Readme

Source

npm version

Set Data Structure

Description

This is a javascript implementation of a set data structure.

A set data structure is an implementation of the mathematical concept of a finite set. A set is a collection of unique values (no duplicates) that have no particular order. A set is more concerned whether or not values are contained in the set than the order in which they are added.

This implementation of a set inlcudes the common set operations of union, intersection, difference, and subset.

For specific examples and documentation, see the below sections


Basic Usage

Install with npm :

npm install set-ds --save

Basic usage example below. Note: it does not cover all the available methods, rather just highlights the main functionality to get up and running with this data structure. For a description of all the methods, see the API section.

var SetDS = require('set-ds');
var setA = new SetDS();

setA.isEmpty();
// --> true
setA.add(1);
setA.add(2);
setA.add(3);
setA.add(4);
setA.add(5);

setA.values();
// --> [1, 2, 3, 4, 5]

setA.has(4);
// --> true

// or instantiate with an array or args
var setB = new SetDS([7, 6, 5, 8, 4]);
setB.isEmpty();
// --> false

setB.values();
// --> [4, 5, 6, 7, 8]

var setC = setA.union(setB);
setC.values();
// --> [1, 2, 3, 4, 5, 6, 7, 8]

var setD = setA.intersection(setB);
setD.values();
// --> [4, 5]

var setE = setA.difference(setB);
setE.values();
// --> [1, 2, 3]

setA.subset(new SetDS(1, 7, 3, 4, 6, 5, 2));
// --> true

setA.clear();
setA.isEmpty();
// --> true

API

Available methods for a Set instance:

  • isEmpty()

    Determines if the set is empty

  • size()

    Returns the size, or number of items in the set

  • clear()

    Clears all the items from the set

  • add(value)

    Adds an item to the set. If the set already contains the item, it is not added.

  • remove(value)

    Removes an item from the set.

  • has(value)

    Determines of the set contains, or has, the value

  • values()

    Returns an array containing all the items in the set

  • union(otherSet)

    Returns a Set that is the union of this set and the 'otherSet'. The returned set will contain all the elements from both sets, and by definition, will not contain any duplicates.

  • intersection(otherSet)

    Returns a Set that ts the intersection of this set and the 'otherSet', The returned set will have only those items that both sets have in common.

  • difference(otherSet)

    Returns a Set that ts the different of this and the 'otherSet', The returned set will have those items that are contained in this set, but NOT contained in the 'otherSet'.

  • subset(otherSet)

    Returns whether or not this set is a subset of the 'otherSet'. If all items of this set are contained in the otherSet, this function returns true; false otherwise.


License

MIT © Jason Jones

Keywords

FAQs

Last updated on 11 Apr 2021

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc