// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class PrimitiveHashTable<K,V> {
    private static class PrimitiveEntry<K,V> {
        private K key;
        private V value;
        private int index;
        public PrimitiveEntry(K key, V value) {
            this.key = key;
            this.value = value;
            this.index = -1;
        }
        public K getKey() {
            return key;
        }
        public V getValue() {
            return value;
        }
        public int getIndex() {
            return index;
        }
        public void setIndex(int index) {
            this.index = index;
        }
    }
    private int capacity;
    private PrimitiveEntry<K,V> table[];
    private int nextfree;
    public PrimitiveHashTable(int capacity) {
        this.capacity = capacity;
        this.table = new PrimitiveEntry[2 * capacity];
        this.nextfree = capacity;
    }
    public void put(K key, V value) {
        int  ix = key.hashCode() % capacity;
        if(table[ix] == null) {
            table[ix] = new PrimitiveEntry<K,V>(key, value);
        } else {
            if(nextfree >= table.length) throw new RuntimeException("Table is full");
            while(table[ix].getIndex() >= 0) ix = table[ix].getIndex();
            table[nextfree] = new PrimitiveEntry<K,V>(key, value);
            table[ix].setIndex(nextfree);
            nextfree++;
        }
    }
    public V get(K key) {
        int  ix = key.hashCode() % capacity;
        if(table[ix] == null) {
            return null;
        } else {
            while(ix >= 0) {
                if(table[ix].getKey().equals(key)) {
                    return table[ix].getValue();
                }
                ix = table[ix].getIndex();
            }
        }
        return null;
    }
}
