X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fevoinference%2Fdistance%2FS.java;h=842f59a392172aa86169c2cb2a8480d8ea0d62a6;hb=8a5da5060358c86632b0ed74bdc38a7449768cb6;hp=1ea6f9b8d30e03107f37a6410d4f0c0e77703afd;hpb=4c5715c2e8dd072a230cd3e493594a0f45655c55;p=jalview.git diff --git a/forester/java/src/org/forester/evoinference/distance/S.java b/forester/java/src/org/forester/evoinference/distance/S.java index 1ea6f9b..842f59a 100644 --- a/forester/java/src/org/forester/evoinference/distance/S.java +++ b/forester/java/src/org/forester/evoinference/distance/S.java @@ -1,6 +1,7 @@ package org.forester.evoinference.distance; +import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; import java.util.Map.Entry; @@ -12,44 +13,138 @@ import java.util.TreeSet; import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix; -public class S { +public final class S { - private final List>> _data; + public final static int FACTOR = 1000000; + private final static boolean DEBUG = true; + private final List>> _data; - S() { - _data = new ArrayList>>(); + public S() { + _data = new ArrayList>>(); } - void addValue( final double key, final int value, final int j ) { - final SortedMap> m = _data.get( j ); - addValue( key, value, m ); + final public void addPairing( final double key, final int value, final int j ) { + addPairing( ( int ) ( FACTOR * key ), value, getS( j ) ); } - SortedMap> getS( final int j ) { + final public void addPairing( final int key, final int value, final int j ) { + addPairing( key, value, getS( j ) ); + } + + final public SortedMap> getS( final int j ) { return _data.get( j ); } - Set>> getSentrySet( final int j ) { - return _data.get( j ).entrySet(); + final public SortedSet getValues( final int key, final int j ) { + return getS( j ).get( key ); } - void initialize( final BasicSymmetricalDistanceMatrix d ) { + final public void initialize( final BasicSymmetricalDistanceMatrix d ) { for( int j = 0; j < d.getSize(); ++j ) { - final TreeMap> map = new TreeMap>(); + final TreeMap> map = new TreeMap>(); _data.add( map ); for( int i = 0; i < j; ++i ) { - addValue( d.getValues()[ i ][ j ], i, map ); + addPairing( ( int ) ( FACTOR * d.getValues()[ i ][ j ] ), i, map ); } } + System.out.println( toString() ); + } + + final public void initialize( final int size ) { + for( int j = 0; j < size; ++j ) { + final TreeMap> map = new TreeMap>(); + _data.add( map ); + } + } + + final public void removePairing( final double key, final int value, final int j ) { + removePairing( ( int ) ( key * FACTOR ), value, j ); } - static void addValue( final double key, final int value, final SortedMap> m ) { + final public void removePairing( final int key, final int value, final int j ) { + final SortedMap> m = _data.get( j ); + final SortedSet x = m.get( key ); + if ( DEBUG ) { + if ( x == null ) { + System.out.println( toString() ); + throw new IllegalArgumentException( "key " + key + " (->" + value + ") does not exist for row " + j ); + } + } + if ( x.size() == 1 ) { + if ( DEBUG ) { + if ( !x.contains( value ) ) { + throw new IllegalArgumentException( "pairing " + key + "->" + value + " does not exist for row " + + j ); + } + } + m.remove( key ); + } + else if ( x.size() > 1 ) { + if ( DEBUG ) { + if ( !x.remove( value ) ) { + throw new IllegalArgumentException( "pairing " + key + "->" + value + + " does not exist (could not be removed) for row " + j ); + } + } + else { + x.remove( value ); + } + } + else if ( DEBUG ) { + throw new IllegalStateException(); + } + } + + final public int size() { + return _data.size(); + } + + // Slow, only for testing + @SuppressWarnings( "unchecked") + final public SortedSet[] toArray( final int j ) { + return _data.get( j ).values().toArray( new SortedSet[ _data.get( j ).size() ] ); + } + + @Override + final public String toString() { + final DecimalFormat df = new DecimalFormat( "0.00" ); + final StringBuilder sb = new StringBuilder(); + for( int j = 0; j < size(); ++j ) { + for( final Entry> entry : getSentrySet( j ) ) { + final double key = entry.getKey(); + final SortedSet values = entry.getValue(); + sb.append( df.format( key / FACTOR ) + "->" ); + boolean first = true; + for( final int v : values ) { + if ( !first ) { + sb.append( "," ); + } + first = false; + sb.append( v ); + } + sb.append( " " ); + } + sb.append( "\n" ); + } + return sb.toString(); + } + + final Set>> getSentrySet( final int j ) { + return getS( j ).entrySet(); + } + + final private static void addPairing( final int key, final int value, final SortedMap> m ) { if ( !m.containsKey( key ) ) { final TreeSet x = new TreeSet(); x.add( value ); m.put( key, x ); } else { + if ( DEBUG ) { + if ( m.get( key ).contains( value ) ) { + throw new IllegalArgumentException( "pairing " + key + "->" + value + " already exists" ); + } + } m.get( key ).add( value ); } }