@rawify/unionfind
Version:
The RAW union find (disjoint set) library
11 lines (9 loc) • 1.23 kB
JavaScript
/*
UnionFind.js v0.0.2 8/16/2025
https://github.com/rawify/UnionFind.js
Copyright (c) 2025, Robert Eisele (https://raw.org/)
Licensed under the MIT license.
*/
;(function(f){function d(a){if(a>>>0!==a)throw new RangeError("n must be a non-negative integer");const b=new Int32Array(a);for(let c=0;c<a;c++)b[c]=-1;this._=b;this._count=a}d.prototype={constructor:d,find:function(a){const b=this._;if(a>b.length)return null;for(;0<=b[a];){const c=b[a],e=b[c];0<=e&&(b[a]=e);a=c}return a},union:function(a,b){a=this.find(a);b=this.find(b);if(a===b)return!1;const c=this._;if(c[a]>c[b]){const e=a;a=b;b=e}c[a]+=c[b];c[b]=a;this._count--;return!0},connected:function(a,
b){return this.find(a)===this.find(b)},sizeOf:function(a){a=this.find(a);return-this._[a]},count:function(){return this._count},reset:function(){const a=this._;for(let b=0;b<a.length;b++)a[b]=-1;this._count=a.length}};Object.defineProperty(d.prototype,"length",{configurable:!0,enumerable:!1,get:function(){return this._.length}});"function"===typeof define&&define.amd?define([],function(){return d}):"object"===typeof exports?(Object.defineProperty(d,"__esModule",{value:!0}),d["default"]=d,d.UnionFind=
d,module.exports=d):f.UnionFind=d})(this);