// 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) 
package intmap;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

// Map implementation not allocating objects in put and not requiring boxing of int key
// not good code - very limited functionality and very old style
public class IntMap<T> {
    private int maxsize;
    private int[] keys;
    private Object[] values;
    private int[] next;
    private int scale;
    private int free;
    private int count;
    private void add(int key, Object value, int ix) {
        keys[ix] = key;
        values[ix] = value;
        count++;
    }
    private void move(int target, int source) {
        if(source < scale) {
            keys[target] = 0;
            values[target] = null;
            next[target] = 0;
        } else {
            keys[target] = keys[source];
            values[target] = values[source];
            move(source, next[source]);
        }
    }
    public void dump() {
        for(int i = 0; i < free; i++) {
            if(keys[i] != 0) {
                System.err.printf("%d : %d %s %d\n", i, keys[i], values[i].toString(), next[i]);
            }
        }
    }
    public IntMap(int maxsize) {
        this.maxsize = maxsize;
        keys = new int[maxsize];
        values = new Object[maxsize];
        next = new int[maxsize];
        scale = maxsize / 2;
        free = scale;
        count = 0;
    }
    public int size() {
        return count;
    }
    public boolean isEmpty() {
        return (count == 0);
    }
    public boolean containsKey(int key) {
        return (get(key) != null);
    }
    public boolean containsValue(Object value) {
        for(int i = 0; i < free; i++) {
            if(keys[i] != 0) {
                if(values[i].equals(value)) {
                    return true;
                }
            }
        }
        return false;
    }
    @SuppressWarnings("unchecked")
    public T get(int key) {
        if(key == 0) {
            throw new RuntimeException("Key zero not allowed");
        }
        int ix = key % scale;
        if(keys[ix] == 0) {
            return null;
        } else {
            do {
                if(keys[ix] == key) {
                    return (T)values[ix];
                } else {
                    ix = next[ix];
                }
            } while(ix >= scale);
            return null;
        }
    }
    @SuppressWarnings("unchecked")
    public T put(int key, T value) {
        if(key == 0) {
            throw new RuntimeException("Key zero not allowed");
        }
        int ix = key % scale;
        int oldix;
        if(keys[ix] == 0) {
            add(key, value, ix);
            return null;
        } else {
            do {
                if(keys[ix] == key) {
                    Object res = values[ix];
                    values[ix] = value;
                    return (T)res;
                } else {
                    oldix = ix;
                    ix = next[ix];
                }
            } while(ix >= scale);
            if(free >= maxsize) {
                throw new RuntimeException("Maximum size exceeded");
            }
            add(key, value, free);
            next[oldix] = free;
            free++;
            return null;
        }
    }
    @SuppressWarnings("unchecked")
    public T remove(int key) {
        if(key == 0l) {
            throw new RuntimeException("Key zero not allowed");
        }
        int ix = key % scale;
        if(keys[ix] == 0) {
            return null;
        } else {
            do {
                if(keys[ix] == key) {
                    Object res = values[ix];
                    move(ix, next[ix]);
                    count--;
                    while(free > scale && keys[free-1] == 0) {
                        free--;
                    }
                    return (T)res;
                } else {
                    ix = next[ix];
                }
            } while(ix >= scale);
            return null;
        }
    }
    public void putAll(IntMap<? extends T> m) {
        Iterator<? extends Map.Entry<Integer, ? extends T>> it = m.entrySet().iterator();
        while(it.hasNext()) {
            Map.Entry<Integer, ? extends T> ent = it.next();
            put(ent.getKey(), ent.getValue());
        }
    }
    public void clear() {
        for(int i = 0; i < maxsize; i++) {
            keys[i] = 0;
            values[i] = null;
            next[i] = 0;
        }
        free = scale;
        count = 0;
    }
    public Set<Integer> keySet() {
        Set<Integer> res = new HashSet<>();
        for(int i = 0; i < free; i++) {
            if(keys[i] != 0) {
                res.add(keys[i]);
            }
        }
        return res;
    }
    @SuppressWarnings("unchecked")
    public Collection<T> values() {
        List<T> res = new ArrayList<>();
        for(int i = 0; i < free; i++) {
            if(keys[i] != 0) {
                res.add((T)values[i]);
            }
        }
        return res;
    }
    @SuppressWarnings("unchecked")
    public Set<Map.Entry<Integer,T>> entrySet() {
        Set<Map.Entry<Integer,T>> res = new HashSet<>();
        for(int i = 0; i < free; i++) {
            if(keys[i] != 0) {
                res.add(new AbstractMap.SimpleEntry<Integer,T>(keys[i], (T)values[i]));
            }
        }
        return res;
    }
}
