/* 0440949 Andreas van Cranenburgh <andreas@unstable.nl>
 * Mon Feb 18 16:35:00 CET 2008
 *
 * Data structures, assignment 1
 * Benchmark of populating and depopulating lists and queues
 * */

import java.util.*;

public final class Assignment1 {
    private ArrayList <List <Integer> > lists =
        new ArrayList <List <Integer> > ();
    private ArrayList <Queue <Integer> > queues =
        new ArrayList <Queue <Integer> > ();

    public Assignment1() {
        lists.add(new ArrayList<Integer>());
        lists.add(new LinkedList<Integer>());
        lists.add(new Stack<Integer>());
        lists.add(new Vector<Integer>());

        queues.add(new LinkedList<Integer>());
        queues.add(new PriorityQueue<Integer>());
    }

    public static final void main(String[] args) {
        Assignment1 bench = new Assignment1();
        long seed = 0;
        int mutations[] = new int[args.length];
        for (int a = 0; a < args.length; a++ ) {
            if (args[a].equals("-s")) {
                try {
                    seed = Long.parseLong(args[a+1]);
                } catch (NumberFormatException E) {
                    System.out.print("Non-fatal error: couldn't parse seed;");
                    System.out.println(" found: " + args[a+1]);
                    System.out.println("A default seed (0) will be used.");
                } finally {
                    // skip one token (parameter to -s):
                    a += 1;
                }
            } else {
		try {
		    mutations[a] = Integer.parseInt(args[a]);
		} catch (NumberFormatException E) {
		    errorExit("Fatal error: integer expected; found: " 
			+ args[a]);
		}
            }
        }

        if (args.length > 0)
           bench.benchmark(seed, mutations);
        else
           bench.benchmark();
    }

    public void benchmark() {

        for (List<Integer> list : lists) {
            ListTimer timer = new ListTimer(list);
            System.out.print(list.getClass().getSimpleName());
            System.out.print(":    ");
            System.out.println(timer.time());
        }

        for (Queue<Integer> queue : queues) {
            QueueTimer timer = new QueueTimer(queue);
            System.out.print(queue.getClass().getSimpleName());
            System.out.print(":    ");
            System.out.println(timer.time());
        }
    }

    public void benchmark(long elemGenSeed) {

        for (List<Integer> list : lists) {
            ListTimer timer = new ListTimer(list, elemGenSeed);
            System.out.print(list.getClass().getSimpleName());
            System.out.print(":    ");
            System.out.println(timer.time());
        }

        for (Queue<Integer> queue : queues) {
            QueueTimer timer = new QueueTimer(queue, elemGenSeed);
            System.out.print(queue.getClass().getSimpleName());
            System.out.print(":    ");
            System.out.println(timer.time());
        }
    }

    public void benchmark(long elemGenSeed, int[] mutations) {

        for (List<Integer> list : lists) {
            ListTimer timer = new ListTimer(list, elemGenSeed);
            System.out.print(list.getClass().getSimpleName());
            System.out.print(":    ");
            try {
                System.out.println(timer.time(mutations));
            } catch (RuntimeException E) {
                errorExit(E.toString());
            }
        }

        for (Queue<Integer> queue : queues) {
            QueueTimer timer = new QueueTimer(queue, elemGenSeed);
            System.out.print(queue.getClass().getSimpleName());
            System.out.print(":    ");
            try {
                System.out.println(timer.time(mutations));
            } catch (RuntimeException E) {
                errorExit(E.toString());
            }
        }
    }

    private static void errorExit(String msg) {
        System.out.println(msg);
        System.exit(1);
    }
}
