Skip to content

Commit

Permalink
refactor(adjacency): update DisjointSet, add defDisjointSet()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 19, 2021
1 parent 7bb045b commit cfe3ed5
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/adjacency/src/disjoint-set.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fillRange } from "@thi.ng/arrays";

/**
* Typed array based Disjoint Set implementation with quick union and
* path compression, after Sedgewick & Wayne.
Expand All @@ -17,12 +19,9 @@ export class DisjointSet {
* @param n - initial capacity, ID range [0..n)
*/
constructor(n: number) {
const roots = (this.roots = new Uint32Array(n));
this.ranks = new Uint8Array(n).fill(0);
this.roots = fillRange(new Uint32Array(n));
this.ranks = new Uint8Array(n);
this.count = n;
for (let i = 0; i < n; ++i) {
roots[i] = i;
}
}

/**
Expand Down Expand Up @@ -99,3 +98,10 @@ export class DisjointSet {
return sets;
}
}

/**
* Creates a new {@link DisjointSet} with capacity `n`.
*
* @param n
*/
export const defDisjointSet = (n: number) => new DisjointSet(n);

0 comments on commit cfe3ed5

Please sign in to comment.