1.0.2 • Published 7 years ago

full-set v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

FullSet

Full-Set is a wrapper class around the native ES6 set object which adds the classical set operations.

For pedagogical purposes mostly, probably not production ready.

Install

npm i --save full-set

Requirements

This library relies on ES6 Sets, and so either a recent version of Node or the browsers will be required

Documentation

Methods

constructor

Creates and adds elements to a set.

Arguments

  • ...elements - Variadic, any number of elements as arguments

Example

    let A = new FullSet('1','2','3');

add

Adds elements to the set

Arguments

  • ...elements - Variadic, any number of elements as arguments
    let A = new FullSet('A','B');
    A.add('C','D','E');

clone

Creates a copy of a set

Example

    let A = new FullSet('TOAST','EGGS','BACON');
    let B = A.clone();

contains

Given an element will reply with a boolean if the set containts that element

Arguments

  • element - An element to query the set for

Example

    let A = new FullSet('A','B','C','D','E');
    A.contains('D') //true

equal

This will compare another set for equality, when both sets contain the same elements

Arguments

  • other_set - Another FullSet object to compare

Example

    let A = new FullSet('1','2');
    let B = new FullSet('1','2');
    let E = new FullSet('1','2','1','2');
    A.equal(B) //equal sets should equal
    A.equal(E) //duplicates don't matter

remove

Remove elements from a set

Arguments

  • ...elements - Variadic, any number of elements to remove from the set as arguments

Example

    let A = new FullSet('A','B','C','D','E');
    A.contains('D') //true
    A.contains('E') //true
    A.remove('D','E')
    A.contains('D') //false
    A.contains('E') //false

values

Returns an iterator of the elements in the set

Example

    let A = new FullSet('A','B','C','D','E');
    let iterator = A.values();
    iterator.next().value //A;

union

Create a new set which has all the elements of both sets.

Arguments

  • other_set - Another FullSet object

Example

    let A = new FullSet("A","B");
    let B = new FullSet("D","E");  
    let C = A.union(B); //A,B,D,E

intersect

Create a new set which only has elements this set and the other set contain.

Arguments

  • other_set - Another FullSet object

Example

     let A = new FullSet(1,2,3,4);
     let B = new FullSet(3,4,5,6);
     let inty = A.intersect(B); //3,4

complement

Given a universe set, it will return all of the elements not in this set.

Arguments

  • other_set - Another FullSet object

Example

    let universe = new FullSet(1,2,3,4,5);
    let A = new FullSet(1);
    let compA = A.complement(universe); //2,3,4,5

relativeComplement

Given another set, it will return a set of elements that are only in this set relative to the other set.

Arguments

  • other_set - Another FullSet object

Example

     let A = new FullSet(1,2,3,4);
     let B = new FullSet(3,4,5,6);
     let AminusB = A.relativeComplement(B); //1,2

symmetricDifference

Given another set, it will return a set of elements that are in this set and in the other set, but not the elements that are in both.

Arguments

  • other_set - Another FullSet object

Example

     let A = new FullSet(1,2,3,4);
     let B = new FullSet(3,4,5,6);
     let AtriangleB = A.symmetricDifference(B); // 1,2,5,6

crossProduct

Given another set, it will return a new set of ordered pairs (Arrays) of every combination of this set elements and the other sets elements.

Arguments

  • other_set - Another FullSet object

Example

     let A = new FullSet(1,2);
     let B = new FullSet(3,4);
     let AcrossB = A.crossProduct(B); //[1,3],[2,3],[1,4],[2,4]

powerSetIter

Iterator for all subsets of the set

Example

     let A = new FullSet(1,2,3);
     let powerIterator = A.powerSet();
     powerSet.next().value();

powerSet

Set of all subsets

warning the size of this set is exponential, 2 ^ this.size();

Example

     let A = new FullSet(1,2,3);
     let Apower = A.powerSet();

cardinality

Returns the number of elements in the set.

Example

     let A = new FullSet(1,2,3);
     A.cardinality() //3

size

Returns the number of elements in the set.

Example

     let A = new FullSet(1,2,3);
     A.size() //3