Home / Class/ DisjointSet Class — react Architecture

DisjointSet Class — react Architecture

Architecture documentation for the DisjointSet class in DisjointSet.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  1765a682_3028_4441_b26f_c712ca2597d5["DisjointSet"]
  edec7689_7b1d_03c9_9cbb_bb9b0552bc30["DisjointSet.ts"]
  1765a682_3028_4441_b26f_c712ca2597d5 -->|defined in| edec7689_7b1d_03c9_9cbb_bb9b0552bc30
  a89a21a8_52fd_f4df_8805_31b9e3fb228e["union()"]
  1765a682_3028_4441_b26f_c712ca2597d5 -->|method| a89a21a8_52fd_f4df_8805_31b9e3fb228e
  74efde51_a311_d84c_0e23_ddafd072e338["find()"]
  1765a682_3028_4441_b26f_c712ca2597d5 -->|method| 74efde51_a311_d84c_0e23_ddafd072e338
  d17d77ab_7e43_9266_e176_75b6266f6340["has()"]
  1765a682_3028_4441_b26f_c712ca2597d5 -->|method| d17d77ab_7e43_9266_e176_75b6266f6340
  cc6dbdb8_321f_04a6_e222_5616c33d7c7c["canonicalize()"]
  1765a682_3028_4441_b26f_c712ca2597d5 -->|method| cc6dbdb8_321f_04a6_e222_5616c33d7c7c
  4c434fb2_75ab_6c2c_c237_b1d38a1b0141["forEach()"]
  1765a682_3028_4441_b26f_c712ca2597d5 -->|method| 4c434fb2_75ab_6c2c_c237_b1d38a1b0141
  631ac101_c6b9_8c55_7557_0bce62532665["buildSets()"]
  1765a682_3028_4441_b26f_c712ca2597d5 -->|method| 631ac101_c6b9_8c55_7557_0bce62532665
  69b545ed_1e0b_f2ec_f604_014c0332cb4a["size()"]
  1765a682_3028_4441_b26f_c712ca2597d5 -->|method| 69b545ed_1e0b_f2ec_f604_014c0332cb4a

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Utils/DisjointSet.ts lines 12–133

export default class DisjointSet<T> {
  #entries: Map<T, T> = new Map();

  /*
   * Updates the graph to reflect that the given @param items form a set,
   * linking any previous sets that the items were part of into a single
   * set.
   */
  union(items: Array<T>): void {
    const first = items.shift();
    CompilerError.invariant(first != null, {
      reason: 'Expected set to be non-empty',
      loc: GeneratedSource,
    });
    /*
     * determine an arbitrary "root" for this set: if the first
     * item already has a root then use that, otherwise the first item
     * will be the new root.
     */
    let root = this.find(first);
    if (root == null) {
      root = first;
      this.#entries.set(first, first);
    }
    // update remaining items (which may already be part of other sets)
    for (const item of items) {
      let itemParent = this.#entries.get(item);
      if (itemParent == null) {
        // new item, no existing set to update
        this.#entries.set(item, root);
        continue;
      } else if (itemParent === root) {
        continue;
      } else {
        let current = item;
        while (itemParent !== root) {
          this.#entries.set(current, root);
          current = itemParent;
          itemParent = this.#entries.get(current)!;
        }
      }
    }
  }

  /*
   * Finds the set to which the given @param item is associated, if @param item
   * is present in this set. If item is not present, returns null.
   *
   * Note that the returned value may be any item in the set to which the input
   * belongs: the only guarantee is that all items in a set will return the same
   * value in between calls to `union()`.
   */
  find(item: T): T | null {
    if (!this.#entries.has(item)) {
      return null;
    }
    const parent = this.#entries.get(item)!;
    if (parent === item) {
      // this is the root element
      return item;
    }
    // Recurse to find the root (caching all elements along the path to the root)
    const root = this.find(parent)!;
    // Cache the element itself
    this.#entries.set(item, root);
    return root;
  }

  has(item: T): boolean {
    return this.#entries.has(item);
  }

  /*
   * Forces the set into canonical form, ie with all items pointing directly to
   * their root, and returns a Map representing the mapping of items to their roots.
   */
  canonicalize(): Map<T, T> {
    const entries = new Map<T, T>();
    for (const item of this.#entries.keys()) {
      const root = this.find(item)!;
      entries.set(item, root);

Domain

Frequently Asked Questions

What is the DisjointSet class?
DisjointSet is a class in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Utils/DisjointSet.ts.
Where is DisjointSet defined?
DisjointSet is defined in compiler/packages/babel-plugin-react-compiler/src/Utils/DisjointSet.ts at line 12.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free