dancing-links
Version:
Fastest JS solver for exact cover problems using Dancing Links
86 lines (85 loc) • 2.79 kB
TypeScript
/**
* Struct-of-Arrays (SoA) data structures for Dancing Links
*
* Implementation using typed arrays for better memory efficiency.
*
* ARCHITECTURE:
* - NodeStore: Contains all node data in separate Int32Array fields
* - ColumnStore: Contains all column data in separate Int32Array fields
* - Index-based references: Uses array indices instead of object pointers (NULL_INDEX = -1)
* - Pre-allocated storage: Fixed-size arrays determined by constraint matrix analysis
*
* DESIGN CONSIDERATIONS:
* - Setup cost: Requires capacity estimation during initialization
* - Memory pattern: Fixed allocation vs dynamic growth
* - Access pattern: Array indexing with bounds checking
* - Code style: Index-based operations throughout algorithms
*/
declare const NULL_INDEX = -1;
export declare class NodeStore<T> {
private capacity;
private _size;
readonly left: Int32Array;
readonly right: Int32Array;
readonly up: Int32Array;
readonly down: Int32Array;
readonly col: Int32Array;
readonly rowIndex: Int32Array;
readonly data: Array<T | null>;
constructor(maxNodes: number);
/**
* Allocate a new node and return its index
*/
allocateNode(): number;
/**
* Initialize a node with self-referencing links (circular)
*/
initializeNode(nodeIndex: number, colIndex?: number, rowIdx?: number, nodeData?: T | null): void;
/**
* Link two nodes horizontally (left-right)
*/
linkHorizontal(leftIndex: number, rightIndex: number): void;
/**
* Link two nodes vertically (up-down)
*/
linkVertical(upIndex: number, downIndex: number): void;
get size(): number;
}
export declare class ColumnStore {
private capacity;
private _size;
readonly head: Int32Array;
readonly len: Int32Array;
readonly prev: Int32Array;
readonly next: Int32Array;
constructor(maxColumns: number);
/**
* Allocate a new column and return its index
*/
allocateColumn(): number;
/**
* Initialize a column with given head node
*/
initializeColumn(colIndex: number, headNodeIndex: number): void;
/**
* Link two columns horizontally
*/
linkColumns(leftIndex: number, rightIndex: number): void;
get size(): number;
}
/**
* Internal constraint interface for capacity calculation
* Unifies ConstraintRow<T> and sparse constraint formats
*/
interface ConstraintWithColumns {
coveredColumns: number[];
}
/**
* Calculate required capacity for stores based on constraints
* Uses for...of loops for performance and readability
*/
export declare function calculateCapacity(numPrimary: number, numSecondary: number, constraints: Array<ConstraintWithColumns>): {
numNodes: number;
numColumns: number;
};
export { NULL_INDEX };