2 package org.forester.evoinference.distance;
4 import java.text.DecimalFormat;
5 import java.util.ArrayList;
6 import java.util.HashSet;
8 import java.util.Map.Entry;
10 import java.util.SortedMap;
11 import java.util.TreeMap;
13 import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
15 public final class Sset {
17 public final static int FACTOR = 1000000;
18 private final static boolean DEBUG = true;
19 private final List<SortedMap<Integer, Set<Integer>>> _data;
22 _data = new ArrayList<SortedMap<Integer, Set<Integer>>>();
25 final public void addPairing( final double key, final int value, final int j ) {
26 addPairing( ( int ) ( FACTOR * key ), value, getS( j ) );
29 final public void addPairing( final int key, final int value, final int j ) {
30 addPairing( key, value, getS( j ) );
33 final public SortedMap<Integer, Set<Integer>> getS( final int j ) {
34 return _data.get( j );
37 final public Set<Integer> getValues( final int key, final int j ) {
38 return getS( j ).get( key );
41 final public void initialize( final BasicSymmetricalDistanceMatrix d ) {
42 for( int j = 0; j < d.getSize(); ++j ) {
43 final TreeMap<Integer, Set<Integer>> map = new TreeMap<Integer, Set<Integer>>();
45 for( int i = 0; i < j; ++i ) {
46 addPairing( ( int ) ( FACTOR * d.getValues()[ i ][ j ] ), i, map );
49 //System.out.println( toString() );
52 final public void initialize( final int size ) {
53 for( int j = 0; j < size; ++j ) {
54 final TreeMap<Integer, Set<Integer>> map = new TreeMap<Integer, Set<Integer>>();
59 final public void removePairing( final double key, final int value, final int j ) {
60 removePairing( ( int ) ( key * FACTOR ), value, j );
63 final public void removePairing( final int key, final int value, final int j ) {
64 final SortedMap<Integer, Set<Integer>> m = _data.get( j );
65 final Set<Integer> x = m.get( key );
70 .println( "________________________________________________________________________________________" );
71 System.out.println( toString() );
72 throw new IllegalArgumentException( "key " + key + " (->" + value + ") does not exist for row " + j );
75 if ( x.size() == 1 ) {
77 if ( !x.contains( value ) ) {
80 .println( "________________________________________________________________________________________" );
81 System.out.println( toString() );
82 throw new IllegalArgumentException( "pairing " + key + "->" + value + " does not exist for row "
88 else if ( x.size() > 1 ) {
90 if ( !x.remove( value ) ) {
91 throw new IllegalArgumentException( "pairing " + key + "->" + value
92 + " does not exist (could not be removed) for row " + j );
100 throw new IllegalStateException();
104 final public int size() {
108 // Slow, only for testing
109 @SuppressWarnings( "unchecked")
110 final public Set<Integer>[] toArray( final int j ) {
111 return _data.get( j ).values().toArray( new Set[ _data.get( j ).size() ] );
115 final public String toString() {
116 final DecimalFormat df = new DecimalFormat( "0.000000" );
117 final StringBuilder sb = new StringBuilder();
118 for( int j = 0; j < size(); ++j ) {
121 for( final Entry<Integer, Set<Integer>> entry : getSentrySet( j ) ) {
122 final double key = entry.getKey();
123 final Set<Integer> values = entry.getValue();
124 sb.append( df.format( key / FACTOR ) + "->" );
125 boolean first = true;
126 for( final int v : values ) {
137 return sb.toString();
140 final Set<Entry<Integer, Set<Integer>>> getSentrySet( final int j ) {
141 return getS( j ).entrySet();
144 final private static void addPairing( final int key, final int value, final SortedMap<Integer, Set<Integer>> m ) {
145 if ( !m.containsKey( key ) ) {
146 final HashSet<Integer> x = new HashSet<Integer>();
152 if ( m.get( key ).contains( value ) ) {
153 throw new IllegalArgumentException( "pairing " + key + "->" + value + " already exists" );
156 m.get( key ).add( value );