added more
[sbp.git] / src / edu / berkeley / sbp / util / IntPairMap.java
diff --git a/src/edu/berkeley/sbp/util/IntPairMap.java b/src/edu/berkeley/sbp/util/IntPairMap.java
new file mode 100644 (file)
index 0000000..c355488
--- /dev/null
@@ -0,0 +1,18 @@
+package edu.berkeley.sbp.util;
+import java.util.*;
+
+/** a mapping from keys of type <tt>K</tt> to <i>sets</i> of values of type <tt>T</tt> */
+public final class IntPairMap<V> {
+
+    private final HashMap<Long, V> hm = new HashMap<Long, V>();
+
+    private static long key(IntegerMappable k1, IntegerMappable k2) {
+        return (k1==null ? 0 : (((long)k1.toInt()) << 32)) | (k2==null ? 0 : k2.toInt());
+    }
+
+    public void put(IntegerMappable k1, IntegerMappable k2, V v) { hm.put(key(k1, k2), v); }
+    public V get(IntegerMappable k1, IntegerMappable k2) { return hm.get(key(k1, k2)); }
+    public Iterable<V> values() { return hm.values(); }
+    public int size() { return hm.size(); }
+    public void toArray(V[] v) { hm.values().toArray(v); }
+}