sudoku-wasm-engine
Version:
A WebAssembly-powered Sudoku solver with multithreaded batch processing capabilities
188 lines (187 loc) • 6.5 kB
TypeScript
/**
* SudokuEngine is a class that encapsulates all WebAssembly interactions
* and business logic for solving Sudoku puzzles.
*/
export declare class SudokuEngine {
private readonly wasmPath;
private wasmLoaded;
private wasmError;
private solveSudoku;
private solveBatch;
private allocateInputBuffer;
private allocateOutputBuffer;
private allocateSolvedFlags;
private freeAllBuffers;
private setPuzzle;
private getSolution;
private wasSolved;
private getCompletedThreadCount;
private requestStop;
private resetStopFlag;
private puzzles;
private solutions;
private batchSize;
private currentIndex;
private solvedCount;
private totalSolveTime;
private startTime;
private stopRequested;
private isProcessing;
private onProgressUpdate;
private onStatusChange;
/**
* Creates a new instance of SudokuEngine / support for path for fallback
*/
constructor(path?: string);
/**
* Loads the WebAssembly module and initializes the solver
* @returns Promise that resolves when the module is loaded and initialized
* @throws Error if the WebAssembly module fails to load or initialize
*/
initialize(): Promise<void>;
/**
* Initialize function wrappers from loaded WebAssembly module
* @private
* @throws Error if WebAssembly module does not have expected functions
*/
private initializeWasmFunctions;
/**
* Load puzzles from a file or string content
* @param content String content with one puzzle per line (81 chars each)
* @returns Number of valid puzzles loaded
* @remarks Each puzzle should be exactly 81 characters (9x9 grid), with digits 0-9 (0 for empty cells)
*/
loadPuzzles(content: string): number;
/**
* Solve a single puzzle
* @param puzzleString The puzzle as a string (81 chars, 0 for empty cells)
* @returns SolveResult object with solution string, duration, and solved status
* @throws Error if WebAssembly module is not loaded
*/
solvePuzzle(puzzleString: string): SolveResult;
/**
* Start bulk solving all loaded puzzles using multithreaded WebAssembly
* @param batchSize Optional number of puzzles to process in each batch
* @returns Promise that resolves when all puzzles have been processed
* @throws Error if WebAssembly module is not loaded, no puzzles are loaded, or processing is already in progress
*/
startBulkSolve(batchSize?: number): Promise<void>;
/**
* Process puzzles in batches to avoid blocking the UI
* @private
* @throws Error if WebAssembly functions are not available or batch processing fails
*/
private processBatches;
/**
* Stops the current batch processing after the current batch completes
*/
stopProcessing(): void;
/**
* Gets all valid solutions as a string with one solution per line
* @returns String containing all valid solutions (one per line)
*/
getSolutionsText(): string;
/**
* Gets a puzzle at the specified index
* @param index The index of the puzzle to retrieve
* @returns Puzzle string or null if index is out of bounds
*/
getPuzzle(index: number): string | null;
/**
* Gets a solution at the specified index
* @param index The index of the solution to retrieve
* @returns Solution string or null if index is out of bounds or no solution exists
*/
getSolutionByIndex(index: number): string | null;
/**
* Sets a callback function for progress updates
* @param callback Function to call with progress information
*/
setProgressCallback(callback: (progress: SudokuProgress) => void): void;
/**
* Sets a callback function for status updates
* @param callback Function to call with status messages
*/
setStatusCallback(callback: (status: string) => void): void;
/**
* Updates progress information and calls the progress callback if set
* @private
*/
private updateProgress;
/**
* Notifies of status changes by calling the status callback if set
* @param status Status message
* @private
*/
private notifyStatusChange;
/**
* Releases WebAssembly resources and clears all callbacks
*/
dispose(): void;
/**
* Checks if the WebAssembly module is loaded
* @returns True if WebAssembly module is loaded and ready to use
*/
isLoaded(): boolean;
/**
* Gets the last error that occurred during WebAssembly loading or initialization
* @returns Error object or null if no error occurred
*/
getError(): Error | null;
/**
* Gets the total number of puzzles loaded
* @returns Number of puzzles
*/
getPuzzleCount(): number;
/**
* Gets the number of successfully solved puzzles
* @returns Number of solved puzzles
*/
getSolvedCount(): number;
/**
* Gets the current processing index
* @returns Current puzzle index being processed
*/
getCurrentIndex(): number;
/**
* Checks if puzzle processing is currently running
* @returns True if batch processing is in progress
*/
isRunning(): boolean;
}
/**
* Result of a single Sudoku puzzle solve operation
*/
export interface SolveResult {
/** Solution string or null if no solution found */
solution: string | null;
/** Time taken to solve in milliseconds */
duration: number;
/** Boolean indicating whether puzzle was solved */
solved: boolean;
}
/**
* Progress information for bulk Sudoku solving operations
*/
export interface SudokuProgress {
/** Current puzzle index being processed */
currentIndex: number;
/** Total number of puzzles to solve */
totalPuzzles: number;
/** Number of successfully solved puzzles */
solvedCount: number;
/** Progress percentage (0-100) */
progressPercent: number;
/** Total time spent solving puzzles (ms) */
totalSolveTime: number;
/** Total elapsed time including overhead (ms) */
elapsedTime: number;
/** Whether processing is currently active */
isProcessing: boolean;
/** Average time to solve each puzzle (ms) */
averageSolveTime: number;
/** Estimated time remaining in seconds, or null if unknown */
timeRemaining: number | null;
/** Estimated completion date, or null if unknown */
estimatedCompletion: Date | null;
}