Skip to content

Commit

Permalink
Replace Arrays by ArrayMatrix
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhahmann committed May 8, 2024
1 parent 5dc4a68 commit be8120c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ public class ZhangUnorderedTreeEditDistance< T >

private final TreeDetails[] deleteCosts;

private final NodeMapping< T >[][] treeMappings;
private final ArrayMatrix< NodeMapping< T > > treeMappings;

private final NodeMapping< T >[][] forestMappings;
private final ArrayMatrix< NodeMapping< T > > forestMappings;

private final List< Tree< T > > subtrees1;

Expand Down Expand Up @@ -231,8 +231,8 @@ private ZhangUnorderedTreeEditDistance( final Tree< T > tree1, final Tree< T > t
insertCosts = new EditCosts<>( tree2, costFunction, subtrees2.size(), this::getTreeIndex2 ).costs;
deleteCosts = new EditCosts<>( tree1, costFunction, subtrees1.size(), this::getTreeIndex1 ).costs;

treeMappings = new NodeMapping[ subtrees1.size() ][ subtrees2.size() ];
forestMappings = new NodeMapping[ subtrees1.size() ][ subtrees2.size() ];
treeMappings = new ArrayMatrix<>( subtrees1.size(), subtrees2.size() );
forestMappings = new ArrayMatrix<>( subtrees1.size(), subtrees2.size() );
}

private int getTreeIndex1( Tree< T > tree1 )
Expand Down Expand Up @@ -286,7 +286,7 @@ private void log()
logger.trace( "forest insertion[{}] = {}", subtree, insertCosts[ getTreeIndex2( subtree ) ].forestCost );
}

private void logDistances( String prefix, NodeMapping< T >[][] nodeMappings )
private void logDistances( String prefix, ArrayMatrix< NodeMapping< T > > nodeMappings )
{
if ( !logger.isTraceEnabled() )
return;
Expand All @@ -296,7 +296,7 @@ private void logDistances( String prefix, NodeMapping< T >[][] nodeMappings )
StringJoiner stringJoiner = new StringJoiner( ", ", "[", "]" );
for ( Tree< T > t2 : subtrees2 )
{
NodeMapping< T > editOperation = nodeMappings[ getTreeIndex1( t1 ) ][ getTreeIndex2( t2 ) ];
NodeMapping< T > editOperation = nodeMappings.get( getTreeIndex1( t1 ), getTreeIndex2( t2 ) );
stringJoiner.add( editOperation == null ? "-" : Double.toString( editOperation.getCost() ) );
}
logger.trace( "{} distance[{}] = {}", prefix, t1, stringJoiner );
Expand All @@ -309,11 +309,11 @@ private void logDistances( String prefix, NodeMapping< T >[][] nodeMappings )
*/
private NodeMapping< T > treeMapping( Tree< T > tree1, Tree< T > tree2 )
{
NodeMapping< T > operation = treeMappings[ getTreeIndex1( tree1 ) ][ getTreeIndex2( tree2 ) ];
NodeMapping< T > operation = treeMappings.get( getTreeIndex1( tree1 ), getTreeIndex2( tree2 ) );
if ( operation == null )
{
operation = computeTreeMapping( tree1, tree2 );
treeMappings[ getTreeIndex1( tree1 ) ][ getTreeIndex2( tree2 ) ] = operation;
treeMappings.set( getTreeIndex1( tree1 ), getTreeIndex2( tree2 ), operation );
}
return operation;
}
Expand Down Expand Up @@ -348,11 +348,11 @@ private NodeMapping< T > computeTreeMapping( Tree< T > tree1, Tree< T > tree2 )
*/
private NodeMapping< T > forestMapping( final Tree< T > forest1, final Tree< T > forest2 )
{
NodeMapping< T > operation = forestMappings[ getTreeIndex1( forest1 ) ][ getTreeIndex2( forest2 ) ];
NodeMapping< T > operation = forestMappings.get( getTreeIndex1( forest1 ), getTreeIndex2( forest2 ) );
if ( operation == null )
{
operation = computeForestMapping( forest1, forest2 );
forestMappings[ getTreeIndex1( forest1 ) ][ getTreeIndex2( forest2 ) ] = operation;
forestMappings.set( getTreeIndex1( forest1 ), getTreeIndex2( forest2 ), operation );
}
return operation;
}
Expand Down Expand Up @@ -682,4 +682,31 @@ private TreeDetails( final double treeCost, final double forestCost )
this.forestCost = forestCost;
}
}

private static class ArrayMatrix< T >
{
private final ArrayList< ArrayList< T > > matrix;

private ArrayMatrix( int rows, int columns )
{
matrix = new ArrayList<>( rows );
for ( int i = 0; i < rows; i++ )
{
ArrayList< T > row = new ArrayList<>( columns );
for ( int j = 0; j < columns; j++ )
row.add( null );
matrix.add( row );
}
}

private T get( int i, int j )
{
return matrix.get( i ).get( j );
}

private void set( int i, int j, T value )
{
matrix.get( i ).set( j, value );
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.mastodon.mamut.treesimilarity.util;

import java.util.List;

public class ArrayMatrix< T >
{
private final List< List< T > > matrix;

public ArrayMatrix( List< List< T > > matrix )
{
this.matrix = matrix;
}
}

0 comments on commit be8120c

Please sign in to comment.