Friday, May 18, 2012

Performance test of find cell


public void testFindCell_ICell() throws OutOfBoundsException {
       System.out.println("findCell");
        int testSize = 1000000;
        UniqueRandomNumber urn = new UniqueRandomNumber(testSize);
       
        ArrayListCellColl instance = new ArrayListCellColl();
        AliveCell ac;
     
     
     
        while (urn.hasNext()){
             ac = new AliveCell();
            int x = urn.getNext();
           
            ac.setX(x);
            ac.setY(x);
            instance.addCell(ac);
        }
         TimerClass timer = new TimerClass();
          timer.startTimer();
          int counter = 0;
          for (int i = 0; i<testSize;i++){
              Random gen = new Random();
              int z = gen.nextInt(testSize*2);
               ac = new AliveCell();
               ac.setX(z);
            ac.setY(z);
            if (instance.findCell(ac))counter++;
           
           
          }
          System.out.println("number found = " + counter);
         timer.stopTimer();
        System.out.println(timer.duration());
   
     
    }

Saturday, April 28, 2012

Unique Random Number Generator



package utilities;

import java.util.ArrayList;
import java.util.Collections;

/**
 *
 * @author noel
 */
public class UniqueRandomNumber {

    public static class OutOfBoundsException extends Exception {

        public OutOfBoundsException() {
            super("Generator past bounds");
        }
    }
    private int size;
    private int counter = 0;
 
    private boolean hasNext = false;
    private ArrayList <Integer> listOfValues = new ArrayList<Integer>();
    public UniqueRandomNumber(int size){
        this.size = size;
        if (size > 0 ) hasNext = true;
        for (int i = 0; i < size; i++){
            listOfValues.add(i);
        }
        Collections.shuffle(listOfValues);
     
    }
    public int getNext() throws OutOfBoundsException{
        if (counter >= size) throw new OutOfBoundsException();
        int returnVal = listOfValues.get(counter);
        counter++;
        if (counter >= size) hasNext = false;
        return returnVal;
     
    }
         

    /**
     * @return the hasNext
     */
    public boolean hasNext() {
        return hasNext;
    }
 
 
}

Wednesday, April 25, 2012

Timer Class for Performance Testing


package gameoflifeiversion;

/**
 *
 * @author noel
 */
public class TimerClass {
    private long startTime;
    private long currentTime;
    private long endTime;
 
    public TimerClass(){
     
    }
    public void startTimer(){
        startTime = System.currentTimeMillis();
    }
    public String lapTime(){
        currentTime = System.currentTimeMillis();
       return (currentTime - startTime) + " milliseconds";

   }
    public void stopTimer(){
         endTime = System.currentTimeMillis();
    }
     public String duration(){
     
       return (endTime - startTime) + " milliseconds";

   }
}

More Game of life code




package gameoflifeiversion;


import java.util.Collection;


/**
 *
 * @author noel
 */
public interface ICellColl {
    public ICell findCell(int x, int y);
    public ICell findCell (ICell cell);
    public Collection<ICell> getNeighbours(ICell cellToFind);   
    public Collection<ICell> getNeighbours(int x, int y);   
    public void addCell(ICell cell);
    
    public static class Extent{
        private int leastx = 1;
        private int greatestx = 1;
        private int leasty = 1;
        private int greatesty = 1;


        /**
         * @return the leastx
         */
        public int getLeastx() {
            return leastx;
        }


        /**
         * @param leastx the leastx to set
         */
        public void setLeastx(int in) {
            leastx = in;
        }


        /**
         * @return the greatestx
         */
        public  int getGreatestx() {
            return greatestx;
        }


        /**
         * @param greatestx the greatestx to set
         */
        public void setGreatestx(int in) {
            greatestx = in;
        }


        /**
         * @return the leasty
         */
        public  int getLeasty() {
            return leasty;
        }


        /**
         * @param leasty the leasty to set
         */
        public void setLeasty(int in) {
            leasty = in;
        }


        /**
         * @return the greatesty
         */
        public int getGreatesty() {
            return greatesty;
        }


        /**
         * @param greatesty the greatesty to set
         */
        public void setGreatesty(int in) {
            greatesty = in;
        }
    }
}






/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameoflifeiversion;


/**
 *
 * @author noel
 */
public abstract class AbstractICellColl  implements ICellColl {


    public AbstractICellColl() {
    }
    private ICellColl.Extent extent = new ICellColl.Extent(); 
    @Override
    public void addCell(ICell cell){
       
        if (cell.getX() < extent.getLeastx()) {
            extent.setLeastx(cell.getX());
        }
        if (cell.getX() > extent.getGreatestx()) {
            extent.setGreatestx(cell.getX());
        }
        if (cell.getY() < extent.getLeasty()) {
            extent.setLeasty(cell.getY());
        }
        if (cell.getY() > extent.getGreatesty()) {
            extent.setGreatesty(cell.getY());
        }
    }


    /**
     * @return the extent
     */
    public ICellColl.Extent getExtent() {
        return extent;
    }


    
    
}


package gameoflifeiversion;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

/**
 *
 * @author noel
 */
public class ArrayListCellColl extends AbstractICellColl{

    private ArrayList<ICell> coll = new ArrayList<ICell>();
    private boolean hasNewCell = false; 
    
    @Override
    public ICell findCell(int x, int y) {
        AliveCell ac = new AliveCell();
        ac.setX(x);
        ac.setY(y);
        return findCell(ac);
        
    }

    @Override
    public ICell findCell(ICell cell) {
        if (hasNewCell){
           Collections.sort(coll);
           hasNewCell = false;
        }
        if (cell == null) return null;
        int index = Collections.binarySearch(this.coll, cell);
        if (index < 0 )return null;
        return coll.get(index);
        
        
    }

    @Override
    public Collection<ICell> getNeighbours(ICell cellToFind) {
         ArrayList<ICell> neighbours = new ArrayList<ICell>();
        ICell addCell = findCell(cellToFind.getX() - 1, cellToFind.getY() - 1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX(), cellToFind.getY() - 1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() + 1, cellToFind.getY() - 1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() - 1, cellToFind.getY());
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() + 1, cellToFind.getY());
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() - 1, cellToFind.getY() +1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX(), cellToFind.getY() +1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() + 1, cellToFind.getY() +1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        return neighbours;

    }

    @Override
    public Collection<ICell> getNeighbours(int x, int y) {
        AliveCell ac = new AliveCell();
        ac.setX(x);
        ac.setY(y);
        return getNeighbours(ac);
    }

    @Override
    public void addCell(ICell cell) {
        hasNewCell = true;
        coll.add(cell);
       super.addCell(cell);
    }
    
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameoflifeiversion;


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;


/**
 *
 * @author noel
 */
public class HashMapCellColl extends AbstractICellColl {
private HashMap<ICell, ICell> coll = new HashMap<ICell,ICell>();
    
    @Override
    public ICell findCell(int x, int y) {
       AliveCell ac = new AliveCell();
        ac.setX(x);
        ac.setY(y);
        return findCell(ac);
    }


    @Override
    public ICell findCell(ICell cell) {
       return coll.get(cell);
    }


   
    @Override
    public Collection<ICell> getNeighbours(ICell cellToFind) {
         ArrayList<ICell> neighbours = new ArrayList<ICell>();
        ICell addCell = findCell(cellToFind.getX() - 1, cellToFind.getY() - 1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX(), cellToFind.getY() - 1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() + 1, cellToFind.getY() - 1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() - 1, cellToFind.getY());
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() + 1, cellToFind.getY());
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() - 1, cellToFind.getY() +1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX(), cellToFind.getY() +1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() + 1, cellToFind.getY() +1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        return neighbours;


    }


    @Override
    public Collection<ICell> getNeighbours(int x, int y) {
        AliveCell ac = new AliveCell();
        ac.setX(x);
        ac.setY(y);
        return getNeighbours(ac);
    }


    @Override
    public void addCell(ICell cell){
        if (cell==null) return;
        super.addCell(cell);
        coll.put(cell, cell);
    }
    
}




The Following blog posts the code for The Game of life
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameoflifeiversion;

/**
 *
 * @author noel
 */
public interface ICell extends Comparable{
    @Override
    public int hashCode();
    @Override
    public String toString(); 
    public void setX(int x);
    public void setY(int y);
    public int getX();
    public int getY();
    
    @Override
    public boolean equals(Object c);
    
    @Override
    public int compareTo(ICell cell);
    
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameoflifeiversion;

/**
 *
 * @author noel
 */
public abstract class AbstractCell implements ICell{
  private int x;
  private int y;
@Override
    public int compareTo(ICell comparison) {
        
        if (this.getY()comparison.getY()) return 1;
        if (this.getY() == comparison.getY()
                && this.getX() < comparison.getX()) return -1;
        if (this.getY() == comparison.getY()
                && this.getX() > comparison.getX()) return 1;
        return 0;
 
    }
 @Override
 public boolean equals(Object obj){
     if (!(obj instanceof AbstractCell)) return false;
    AbstractCell ac = (AbstractCell)obj;
    return ac.getX() == this.getX() && ac.getY() == this.getY();

 }
  
  
  
  
    @Override
  public int hashCode() {
      int returnValue = 0;
        if (x != 0){
            returnValue = x * 31;
            
        }
        if (y!=0){
         returnValue *= y;   
        }
        return returnValue;
        }
    
    /**
     * @return the x
     */
    public int getX() {
        return x;
    }

    /**
     * @param x the x to set
     */
    public void setX(int x) {
        this.x = x;
    }

    /**
     * @return the y
     */
    public int getY() {
        return y;
    }

    /**
     * @param y the y to set
     */
    public void setY(int y) {
        this.y = y;
    }
    public String toCellString(){
        return getX() + "_" + getY();
    }
    
    
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameoflifeiversion;

/**
 *
 * @author noel
 */
public class AliveCell extends AbstractCell{
    @Override
    public String toCellString(){
        return "A_"+super.toCellString();
    }

    
    
}