48 lines
829 B
TypeScript
48 lines
829 B
TypeScript
![]() |
import { TreeWalker } from './TreeWalker';
|
||
|
|
||
|
export abstract class ArrayChildrenTreeWalker<T> extends TreeWalker<T>
|
||
|
{
|
||
|
_parentMap = new Map<T,T>();
|
||
|
|
||
|
abstract getChildren( t:T ):T[];
|
||
|
|
||
|
clearParentMap()
|
||
|
{
|
||
|
this._parentMap.clear();
|
||
|
}
|
||
|
|
||
|
updateParentMap( root:T, ...otherRoots:T[] )
|
||
|
{
|
||
|
let stack = [ root ].concat( otherRoots );
|
||
|
|
||
|
while ( stack.length > 0 )
|
||
|
{
|
||
|
let parent = stack.shift();
|
||
|
let children = this.getChildren( parent );
|
||
|
|
||
|
for ( let child of children )
|
||
|
{
|
||
|
this._parentMap.set( child, parent );
|
||
|
stack.push( child );
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
childAt( t:T, index:number )
|
||
|
{
|
||
|
return this.getChildren( t )[ index ];
|
||
|
}
|
||
|
|
||
|
parent( t:T ):T
|
||
|
{
|
||
|
return this._parentMap.get( t );
|
||
|
}
|
||
|
|
||
|
numChildren( t:T )
|
||
|
{
|
||
|
return this.getChildren( t ).length;
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|