View Javadoc

1   package types;
2   
3   import gnu.trove.TDoubleArrayList;
4   import gnu.trove.TIntArrayList;
5   
6   /***
7    * a sparse vector implementation. Not necessarily the most efficient.
8    * 
9    * @author kuzman
10   * 
11   */
12  public class SparseVector {
13  	TIntArrayList indices;
14  	TDoubleArrayList values;
15  
16  	public SparseVector() {
17  		indices = new TIntArrayList();
18  		values = new TDoubleArrayList();
19  	}
20  
21  	public void add(int index, double value) {
22  		indices.add(index);
23  		values.add(value);
24  	}
25  
26  	public int getIndexAt(int i) {
27  		return indices.get(i);
28  	}
29  
30  	public double getValueAt(int i) {
31  		return values.get(i);
32  	}
33  
34  	public int numEntries() {
35  		return indices.size();
36  	}
37  
38  	@Override
39  	public String toString() {
40  		StringBuffer sb = new StringBuffer();
41  		for (int i : indices.toNativeArray())
42  			sb.append(i + " ");
43  		return sb.toString();
44  	}
45  
46  }