/** * Hierarchical Grouping - Optimize nested data structures * * Groups related data hierarchically to reduce repetition and improve * compression for tree-like and nested data structures. * * Features: * - Automatic hierarchy detection * - Parent-child relationship optimization * - Nested structure flattening * - Common ancestor extraction * * Example: * Original nested structure: * ``` * { * "company": "Acme Corp", * "departments": [ * { * "name": "Engineering", * "company": "Acme Corp", * "employees": [...] * }, * { * "name": "Sales", * "company": "Acme Corp", * "employees": [...] * } * ] * } * ``` * * Optimized with hierarchy: * ``` * @hierarchy company * company: Acme Corp * * @group departments * name,employees * Engineering,[...] * Sales,[...] * ``` * * Token Savings: 15-30% for deeply nested structures */ import type { HierarchicalOptions } from './types.js'; /** * Hierarchy node representing a level in the data structure */ export interface HierarchyNode { name: string; level: number; parent?: string; commonFields: Map; uniqueFields: string[]; children: HierarchyNode[]; } /** * Hierarchy analysis result */ export interface HierarchyAnalysis { depth: number; nodeCount: number; commonFieldCount: number; estimatedSavings: number; recommended: boolean; hierarchyTree: HierarchyNode | null; } /** * Hierarchical grouping manager */ export declare class HierarchicalGrouping { private options; constructor(options?: Partial); /** * Analyze data for hierarchical structure * * @param data - Data to analyze * @param maxDepth - Maximum depth to analyze * @returns Analysis result */ analyzeHierarchy(data: any, maxDepth?: number): HierarchyAnalysis; /** * Build hierarchy tree from data * * @param data - Data object * @param currentDepth - Current depth * @param maxDepth - Maximum depth * @param parentName - Parent node name * @returns Hierarchy node */ private buildHierarchyTree; /** * Find common fields across array elements * * @param array - Array of objects * @returns Map of common field names to values */ private findCommonFields; /** * Calculate depth of hierarchy tree * * @param node - Root node * @returns Maximum depth */ private calculateDepth; /** * Count total nodes in tree * * @param node - Root node * @returns Node count */ private countNodes; /** * Count total common fields in tree * * @param node - Root node * @returns Common field count */ private countCommonFields; /** * Generate hierarchy directive * * Format: @hierarchy parentField * * @param fieldName - Parent field name * @returns TONL directive */ generateHierarchyDirective(fieldName: string): string; /** * Generate group directive * * Format: @group groupName * * @param groupName - Group name * @returns TONL directive */ generateGroupDirective(groupName: string): string; /** * Parse hierarchy directive * * @param directive - Directive string * @returns Field name */ parseHierarchyDirective(directive: string): string; /** * Parse group directive * * @param directive - Directive string * @returns Group name */ parseGroupDirective(directive: string): string; /** * Extract common fields from nested structure * * @param data - Nested data * @returns Object with common fields extracted */ extractCommonFields(data: any): { common: Record; remaining: any; }; /** * Flatten nested arrays if beneficial * * @param data - Data with nested arrays * @returns Flattened structure */ flattenArrays(data: any): any; /** * Estimate savings from hierarchical grouping * * @param data - Original data * @returns Estimated byte savings */ estimateSavings(data: any): number; /** * Check if hierarchical grouping would be beneficial * * @param data - Data to check * @returns True if recommended */ shouldGroup(data: any): boolean; } //# sourceMappingURL=hierarchical.d.ts.map