optimizations to FastSet: make it final again, don't shift the contents of the entire...
[sbp.git] / src / edu / berkeley / sbp / util / IntPairMap.java
1 // Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp.util;
4 import java.util.*;
5
6 // FEATURE: make this faster (plenty of ways: quadradic probing hash table is one)
7 /** a sparse mapping from pairs of <tt>int</tt>'s to <tt>V</tt>'s */
8 public final class IntPairMap<V> implements Iterable<V> {
9
10     private final HashMap<Long, V> hm = new HashMap<Long, V>();
11
12     private static long key(IntegerMappable k1, IntegerMappable k2) {
13         return (k1==null ? 0 : (((long)k1.toInt()) << 32)) | (k2==null ? 0 : k2.toInt());
14     }
15
16     public void put(IntegerMappable k1, IntegerMappable k2, V v) { hm.put(key(k1, k2), v); }
17     public V get(IntegerMappable k1, IntegerMappable k2) { return hm.get(key(k1, k2)); }
18     public void remove(IntegerMappable k1, IntegerMappable k2) { hm.remove(key(k1, k2)); }
19     public Iterable<V> values() { return hm.values(); }
20     public int size() { return hm.size(); }
21     public void toArray(V[] v) { hm.values().toArray(v); }
22     public Iterator<V> iterator() { return hm.values().iterator(); }
23 }