UNPKG

dancing-links

Version:

Fastest JS solver for exact cover problems using Dancing Links

57 lines (56 loc) 1.94 kB
/** * Problem Builder - Constructs Dancing Links matrix from constraint rows * * Converts constraint data into optimized Struct-of-Arrays format for algorithm execution. * Pre-allocates storage based on matrix analysis to avoid dynamic resizing during search. */ import { ConstraintRow } from '../types/interfaces.js'; import { NodeStore, ColumnStore } from './data-structures.js'; /** * Search context for resumable Dancing Links algorithm execution * * Captures the algorithm state needed to pause and resume search operations. * This enables generator-style iteration without modifying the core algorithm * to use generators directly. */ export interface SearchContext<T> { /** Current depth in the search tree */ level: number; /** Stack of node indices chosen at each search level */ choice: number[]; /** Column selected for covering at current level */ bestColIndex: number; /** Current node being tried in the selected column */ currentNodeIndex: number; /** Whether this context has been used for search before */ hasStarted: boolean; /** Constraint matrix nodes with their current link state */ nodes: NodeStore<T>; /** Column headers with their current lengths and links */ columns: ColumnStore; } /** * Configuration for building a problem structure */ export interface ProblemConfig<T> { numPrimary: number; numSecondary: number; rows: ConstraintRow<T>[]; } /** * Builds Dancing Links data structures from constraint rows */ export declare class ProblemBuilder { /** * Build SearchContext for resumable search from constraint configuration */ static buildContext<T>(config: ProblemConfig<T>): SearchContext<T>; /** * Create column headers and linking structure */ private static buildColumns; /** * Create row nodes and link them into the column structure */ private static buildRows; }