/* 0440949 Andreas van Cranenburgh <andreas@unstable.nl>
 *
 * Data structures, assignment 4
 * Benchmark of populating and depopulating different hashtables
 * */

import java.util.*;

public final class Assignment4 {
	final static int ITERATIONS = 100000;
	final static int N = 100;

	public static final void main(String[] args) {
		HashMap<Integer, Integer> t1 = null;
		ChainingArrayHashTable<Integer, Integer> t2 = null;
		ProbingArrayHashTable<Integer, Integer> t3 = null;

		System.out.printf("# %d iteraties, %d experimenten\n", ITERATIONS, N);

		runbenchmarks("referentie", t1);
		runbenchmarks("ketting", t2);
		runbenchmarks("aftastend", t3);
		System.out.printf("summary(referentie)\n");
		System.out.printf("summary(ketting)\n");
		System.out.printf("summary(aftastend)\n");
		System.out.printf("wilcox.test(ketting, aftastend)\n\n");

	}

	private static void runbenchmarks(String name, ChainingArrayHashTable<Integer, Integer> table) {
		System.out.printf("%s <- c(", name);
		for (int a = 0; a < N; a++) {
			table = new ChainingArrayHashTable<Integer, Integer>(1023);
			benchmark(ITERATIONS, table);
		}
		System.out.printf("\b)\n");
	}

	private static void runbenchmarks(String name, ProbingArrayHashTable<Integer, Integer> table) {
		System.out.printf("%s <- c(", name);
		for (int a = 0; a < N; a++) {
			table = new ProbingArrayHashTable<Integer, Integer>(1023);
			benchmark(ITERATIONS, table);
		}
		System.out.printf("\b)\n");
	}

	private static void runbenchmarks(String name, HashMap<Integer, Integer> table) {
		System.out.printf("%s <- c(", name);
		for (int a = 0; a < N; a++) {
			table = new HashMap<Integer, Integer>(1023);
			benchmark(ITERATIONS, table);
		}
		System.out.printf("\b)\n");
	}

	private static void benchmark(int iterations, MyHashTable<Integer, Integer> table) {
		Random rand = new Random();
		long begin = System.currentTimeMillis();
		Integer test;
		int r1, r2 = rand.nextInt() % iterations, r3 = rand.nextInt() % iterations;
		rand.setSeed(0);
		table.put(r2, r2);
		table.put(r3, r3);

		for (int i = 0; i < iterations; i++) {
			r1 = r2;
			r2 = r3;
			r3 = rand.nextInt() % iterations;
			table.put(r3, r3);

			test = table.get(r2);
			assert(test.equals(new Integer(r2)));

			if (rand.nextDouble() > 0.7)
				table.remove(r1);
		}
		System.out.printf(" %d,", System.currentTimeMillis() - begin);
	}

	private static void benchmark(int iterations, HashMap<Integer, Integer> table) {
		Random rand = new Random();
		long begin = System.currentTimeMillis();
		Integer test;
		int r1;
		int r2 = rand.nextInt() % iterations;
		int r3 = rand.nextInt() % iterations;
		rand.setSeed(0);
		table.put(r2, r2);
		table.put(r3, r3);

		for (int i = 0; i < iterations; i++) {
			r1 = r2;
			r2 = r3;
			r3 = rand.nextInt() % iterations;
			table.put(r3, r3);
			test = table.get(r2);
			assert(test.equals(new Integer(r2)));
			if (rand.nextDouble() > 0.7) 
				table.remove(r1);
		}
		System.out.printf(" %d,", System.currentTimeMillis() - begin);
	}
}
