6ad7df363b65db589a4494ec9e66e7af9f4eb880
[jalview.git] / forester / java / src / org / forester / evoinference / distance / Sset.java
1
2 package org.forester.evoinference.distance;
3
4 import java.text.DecimalFormat;
5 import java.util.ArrayList;
6 import java.util.HashSet;
7 import java.util.List;
8 import java.util.Map.Entry;
9 import java.util.Set;
10 import java.util.SortedMap;
11 import java.util.TreeMap;
12
13 import org.forester.evoinference.matrix.distance.BasicSymmetricalDistanceMatrix;
14
15 public final class Sset {
16
17     public final static int                              FACTOR = 1000000;
18     private final static boolean                         DEBUG  = true;
19     private final List<SortedMap<Integer, Set<Integer>>> _data;
20
21     public Sset() {
22         _data = new ArrayList<SortedMap<Integer, Set<Integer>>>();
23     }
24
25     final public void addPairing( final double key, final int value, final int j ) {
26         addPairing( ( int ) ( FACTOR * key ), value, getS( j ) );
27     }
28
29     final public void addPairing( final int key, final int value, final int j ) {
30         addPairing( key, value, getS( j ) );
31     }
32
33     final public SortedMap<Integer, Set<Integer>> getS( final int j ) {
34         return _data.get( j );
35     }
36
37     final public Set<Integer> getValues( final int key, final int j ) {
38         return getS( j ).get( key );
39     }
40
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>>();
44             _data.add( map );
45             for( int i = 0; i < j; ++i ) {
46                 addPairing( ( int ) ( FACTOR * d.getValues()[ i ][ j ] ), i, map );
47             }
48         }
49         //System.out.println( toString() );
50     }
51
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>>();
55             _data.add( map );
56         }
57     }
58
59     final public void removePairing( final double key, final int value, final int j ) {
60         removePairing( ( int ) ( key * FACTOR ), value, j );
61     }
62
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 );
66         if ( DEBUG ) {
67             if ( x == null ) {
68                 System.out.println();
69                 System.out
70                         .println( "________________________________________________________________________________________" );
71                 System.out.println( toString() );
72                 throw new IllegalArgumentException( "key " + key + " (->" + value + ") does not exist for row " + j );
73             }
74         }
75         if ( x.size() == 1 ) {
76             if ( DEBUG ) {
77                 if ( !x.contains( value ) ) {
78                     System.out.println();
79                     System.out
80                             .println( "________________________________________________________________________________________" );
81                     System.out.println( toString() );
82                     throw new IllegalArgumentException( "pairing " + key + "->" + value + " does not exist for row "
83                             + j );
84                 }
85             }
86             m.remove( key );
87         }
88         else if ( x.size() > 1 ) {
89             if ( DEBUG ) {
90                 if ( !x.remove( value ) ) {
91                     throw new IllegalArgumentException( "pairing " + key + "->" + value
92                             + " does not exist (could not be removed) for row " + j );
93                 }
94             }
95             else {
96                 x.remove( value );
97             }
98         }
99         else if ( DEBUG ) {
100             throw new IllegalStateException();
101         }
102     }
103
104     final public int size() {
105         return _data.size();
106     }
107
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() ] );
112     }
113
114     @Override
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 ) {
119             sb.append( j );
120             sb.append( ": " );
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 ) {
127                     if ( !first ) {
128                         sb.append( "," );
129                     }
130                     first = false;
131                     sb.append( v );
132                 }
133                 sb.append( "  " );
134             }
135             sb.append( "\n" );
136         }
137         return sb.toString();
138     }
139
140     final Set<Entry<Integer, Set<Integer>>> getSentrySet( final int j ) {
141         return getS( j ).entrySet();
142     }
143
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>();
147             x.add( value );
148             m.put( key, x );
149         }
150         else {
151             if ( DEBUG ) {
152                 if ( m.get( key ).contains( value ) ) {
153                     throw new IllegalArgumentException( "pairing " + key + "->" + value + " already exists" );
154                 }
155             }
156             m.get( key ).add( value );
157         }
158     }
159 }