import java.io.FileReader;
import java.lang.Integer;
class HashMap {
    static int[] values={83, 52, 37, 52, 44, 91, 82, 95, 63, 12, 54, 41, 19, 1, 88, 15, 01,17, 8};
    private int[] table;
    private int n, collision;

    public HashMap(int n) {
        this.n=n;
        collisions=0;
        table=new int[n];
        for(int i=0;i<n;++i)
            table[i]=-1;
    }
    public void Insert(int x) {
        int v=0, s=0, pos=0;

        while(v!=-1 && v!=-2) {
            pos=h(x,s);
            v=table[pos];
            ++s;
        }
        collisions+=(s-1);
        table[pos]=x;
    }
    public void Delete(int x) {
        int v=0, s=0, pos=0;
        while(v!=-1 && v!=x && s<n) {
            pos=h(x,s);
            v=table[pos];
            ++s;
        }
        if(v==x) table[pos]=-2;
        else System.out.println("Trying to delete non-existing value from table");
    }
    public boolean Search(int x){
        int v=0, s=0, pos=0;
        while(v!=-1 && v!=x && s<n) {
            pos=h(x,s);
            v=table[pos];
            ++s;
        }
        if(v==x) return true;
        else return false;
    }
    private int h(int x, int s) {
        return (x%n+s)%n;
    }
    void print() {
        System.out.println("Collisions: "+collisions);
        System.out.print("Position :");
        for(int i=0;i<table.length;++i) System.out.print(i+"\t");
        System.out.println();
        System.out.print("Value     : ");
        for(int i=0;i<table.length;++i) System.out.print(table[i]+"\t");
        System.out.println();
    }
    private static int getInt(FileReader fr) throws java.io.IOException {
        int ci=fr.read();
        char c;
        String s="";
        while(ci!=32 && ci!=-1 && ci!=(int)'\n') {
            c=(char)ci;
            s=s+c;
            ci=fr.read();
        }
        return (new Integer(s)).intValue();
    }

    public static void main(String args[]) {

        if(args.length>0) { //Datei einlesen oder fest kodierte Testdaten?
            System.out.println("Reading data from file "+args[0]+".");
            try {
                HashMap h=new HashMap(30000);
                FileReader fr=new FileReader(args[0]);
                while(fr.ready()) {
                    int n=getInt(fr);
                    h.Insert(n);
                }
                fr.close();
                h.print();
            } catch(Exception e) {
                System.out.println("Error while reading file\n"+e);
            }
        } else {
            HashMap h=new HashMap(values.length);
            h.print();
            for(int i=0;i<values.length;++i) {
                h.Insert(values[i]);
            }
            h.print();
        }
    }
};
