2 package org.forester.evoinference.distance;
4 import java.text.DecimalFormat;
5 import java.util.ArrayList;
7 import java.util.Map.Entry;
9 import java.util.SortedMap;
10 import java.util.SortedSet;
11 import java.util.TreeMap;
12 import java.util.TreeSet;
14 import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
16 public final class S {
18 public final static int FACTOR = 1000000;
19 private final static boolean DEBUG = true;
20 private final List<SortedMap<Integer, SortedSet<Integer>>> _data;
23 _data = new ArrayList<SortedMap<Integer, SortedSet<Integer>>>();
26 final public void addPairing( final float key, final int value, final int j ) {
27 addPairing( ( int ) ( FACTOR * key ), value, getS( j ) );
30 final public void addPairing( final int key, final int value, final int j ) {
31 addPairing( key, value, getS( j ) );
34 final public SortedMap<Integer, SortedSet<Integer>> getS( final int j ) {
35 return _data.get( j );
38 final public SortedSet<Integer> getValues( final int key, final int j ) {
39 return getS( j ).get( key );
42 final public void initialize( final BasicSymmetricalDistanceMatrix d ) {
43 for( int j = 0; j < d.getSize(); ++j ) {
44 final TreeMap<Integer, SortedSet<Integer>> map = new TreeMap<Integer, SortedSet<Integer>>();
46 for( int i = 0; i < j; ++i ) {
47 addPairing( ( int ) ( FACTOR * d.getValues()[ i ][ j ] ), i, map );
50 System.out.println( toString() );
53 final public void initialize( final int size ) {
54 for( int j = 0; j < size; ++j ) {
55 final TreeMap<Integer, SortedSet<Integer>> map = new TreeMap<Integer, SortedSet<Integer>>();
60 final public void removePairing( final float key, final int value, final int j ) {
61 removePairing( ( int ) ( key * FACTOR ), value, j );
64 final public void removePairing( final int key, final int value, final int j ) {
65 final SortedMap<Integer, SortedSet<Integer>> m = _data.get( j );
66 final SortedSet<Integer> x = m.get( key );
71 .println( "________________________________________________________________________________________" );
72 System.out.println( toString() );
73 throw new IllegalArgumentException( "key " + key + " (->" + value + ") does not exist for row " + j );
76 if ( x.size() == 1 ) {
78 if ( !x.contains( value ) ) {
81 .println( "________________________________________________________________________________________" );
82 System.out.println( toString() );
83 throw new IllegalArgumentException( "pairing " + key + "->" + value + " does not exist for row "
89 else if ( x.size() > 1 ) {
91 if ( !x.remove( value ) ) {
92 throw new IllegalArgumentException( "pairing " + key + "->" + value
93 + " does not exist (could not be removed) for row " + j );
101 throw new IllegalStateException();
105 final public int size() {
109 // Slow, only for testing
110 @SuppressWarnings( "unchecked")
111 final public SortedSet<Integer>[] toArray( final int j ) {
112 return _data.get( j ).values().toArray( new SortedSet[ _data.get( j ).size() ] );
116 final public String toString() {
117 final DecimalFormat df = new DecimalFormat( "0.000000" );
118 final StringBuilder sb = new StringBuilder();
119 for( int j = 0; j < size(); ++j ) {
122 for( final Entry<Integer, SortedSet<Integer>> entry : getSentrySet( j ) ) {
123 final double key = entry.getKey();
124 final SortedSet<Integer> values = entry.getValue();
125 sb.append( df.format( key / FACTOR ) + "->" );
126 boolean first = true;
127 for( final int v : values ) {
138 return sb.toString();
141 final Set<Entry<Integer, SortedSet<Integer>>> getSentrySet( final int j ) {
142 return getS( j ).entrySet();
145 final private static void addPairing( final int key, final int value, final SortedMap<Integer, SortedSet<Integer>> m ) {
146 if ( !m.containsKey( key ) ) {
147 final TreeSet<Integer> x = new TreeSet<Integer>();
153 if ( m.get( key ).contains( value ) ) {
154 throw new IllegalArgumentException( "pairing " + key + "->" + value + " already exists" );
157 m.get( key ).add( value );