db12004df84e65305c8de0816454aa3ee714601f
[sbp.git] / src / edu / berkeley / sbp / util / IntPairMap.java
1 package edu.berkeley.sbp.util;
2 import java.util.*;
3
4 // FEATURE: make this faster (plenty of ways: quadradic probing hash table is one)
5 /** a sparse mapping from pairs of <tt>int</tt>'s to <tt>V</tt>'s */
6 public final class IntPairMap<V> {
7
8     private final HashMap<Long, V> hm = new HashMap<Long, V>();
9
10     private static long key(IntegerMappable k1, IntegerMappable k2) {
11         return (k1==null ? 0 : (((long)k1.toInt()) << 32)) | (k2==null ? 0 : k2.toInt());
12     }
13
14     public void put(IntegerMappable k1, IntegerMappable k2, V v) { hm.put(key(k1, k2), v); }
15     public V get(IntegerMappable k1, IntegerMappable k2) { return hm.get(key(k1, k2)); }
16     public Iterable<V> values() { return hm.values(); }
17     public int size() { return hm.size(); }
18     public void toArray(V[] v) { hm.values().toArray(v); }
19 }