athanclark.com

IntSet

Keywords:
JavaScript, Set Theory, Bit Masking, Container, BigInt
Development Date:
2023-11-21
Website:
https://www.npmjs.com/package/intset

Summary

IntSet is a container for storing bigints in a relatively efficient manner. It uses bit masking to store numbers more efficiently.

Functionality

You can view the code at the GitHub repository, but I’ll describe the functionality here.

As a Container

You can treat an IntSet as a general purpose container for bigints pretty intuitively, designed after the built-in Set:

const IntSet = require('intset');

let set = new IntSet();

// add a value
set.add(1n);

// remove a value
set.remove(1n);

// check if the set contains a value
set.contains(1n);

// see if the set is empty
set.isEmpty();

As a Set

The whole purpose behind this data structure is to allow for “efficient” unions, intersections, symmetric difference, and difference operations with the sets. Imagine you have two sets, set1 and set2. These operations can be done like so:

// returns a union of the two sets
const unionOfSet1And2 = set1.union(set2);

// returns an intersection
const unionOfSet1And2 = set1.intersection(set2);

// returns a symmetric difference
const symDiffOfSet1And2 = set1.symmetricDifference(set2);

// returns set1 without elements of set2
const set1Diff2 = set1.difference(set2);

Implementation

This datastructure is implemented in a very simple way – through bit masking. For a thorough explanation of bit masking, please see this project’s blog post.