Skip to content

Commit

Permalink
improve performance by Hashmap and polynomial function
Browse files Browse the repository at this point in the history
  • Loading branch information
gnozil committed May 28, 2012
1 parent b7ff901 commit 39b368f
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 29 deletions.
86 changes: 78 additions & 8 deletions src/com/github/permatrix/CompactImage.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.permatrix;


import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
Expand All @@ -16,11 +15,15 @@

import javax.imageio.ImageIO;

public class CompactImage {
public class CompactImage implements Comparable<CompactImage> {
int[] dots;
int count;
private long key;

public CompactImage(int size) {
dots = new int[size];
count = 1;
key = -1;
}

public void addDot(int x, int y) {
Expand All @@ -40,6 +43,10 @@ public List<Dot> getDots() {
return dl;
}

/**
* create a new image that is the 90 degree rotation of the image
* @return
*/
public CompactImage rotate() {
int dim = dots.length;
CompactImage ni = new CompactImage(dim);
Expand All @@ -51,6 +58,9 @@ public CompactImage rotate() {
return ni;
}

/**
* rotate the image by 90 degree, and re-use this object
*/
public void rotateSelf() {
int dim = dots.length;
int[] shadow = new int[dim];
Expand All @@ -60,10 +70,47 @@ public void rotateSelf() {
shadow[x1] = y1;
}
System.arraycopy(shadow, 0, dots, 0, dim);
key = -1;
}

/**
* calculate an unique key for each image by using polynomial.
* @return
*/
private static final int P_PRIME = 19;
public long imageKey() {
if (key == -1) {
key = 0;
long poly = 1;
for (int x = 0; x < dots.length; x++) {
key += dots[x] * poly;
poly *= P_PRIME;
}
}
return key;
}

@Override
public int hashCode() {
return (int) key;
}

public boolean equals(CompactImage img) {
@Override
public boolean equals(Object obj) {
CompactImage img = (CompactImage) obj;
return imageKey() == img.imageKey();
}

/**
* Compare two Image by compare each dot, slowest method
*
* @param img
* @return True - when two images are equal; False - when two images are not
* same
*/
public boolean compareWith(CompactImage img) {
assert (img != null);

int dim = dots.length;
if (dim != img.dots.length)
return false;
Expand All @@ -82,7 +129,10 @@ public CompactImage clone() {
return ni;
}

// imprint rotated images
/**
* imprint 3 rotated images with original and put all dots in a collection.
* @return
*/
public Collection<Dot> imprint() {
TreeSet<Dot> ts = new TreeSet<Dot>();
ts.addAll(getDots());
Expand All @@ -95,6 +145,10 @@ public Collection<Dot> imprint() {
return ts;
}

/**
* draw the visual representation of the image in Ascii graphics
* @param pw
*/
public void visualize(PrintStream pw) {
int dim = dots.length;
char[][] visual = new char[dim][dim];
Expand All @@ -111,11 +165,14 @@ public void visualize(PrintStream pw) {
pw.println();
}
}


/**
* generate the PNG graphics representation of the image and save to file
*/
public void visualize() {
int dim = dots.length;
int width = dim * 17 + 1;
BufferedImage img = new BufferedImage(width,width,BufferedImage.TYPE_INT_RGB);
int width = dim * 17 + 1;
BufferedImage img = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
visualize(0, 0, 17, g2d);
try {
Expand All @@ -128,16 +185,29 @@ public void visualize() {
g2d.dispose();
}

/**
* draw the graphic representation of the image in a graphic device
* @param posX
* @param posY
* @param gridSize
* @param g2d
*/
public void visualize(int posX, int posY, int gridSize, Graphics2D g2d) {
int dim = dots.length;
int width = dim * gridSize + 1;
g2d.drawRect(posX, posY, width - 1, width - 1);
for(int step = dim - 1; step >= 0; step--) {
for (int step = dim - 1; step >= 0; step--) {
g2d.drawLine(posX, posY + step * gridSize, posX + width - 1, posY + step * gridSize);
g2d.drawLine(posX + step * gridSize, posY, posX + step * gridSize, posY + width - 1);
}
for (int x = 0; x < dim; x++) {
g2d.fillRect(posX + x * gridSize + 1, posY + dots[x] * gridSize + 1, gridSize - 1, gridSize - 1);
}
}

@Override
public int compareTo(CompactImage img) {
return (int)(key - img.key);
}

}
20 changes: 20 additions & 0 deletions src/com/github/permatrix/DictOrderPermutator.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,24 @@ private static long getFactorial(int n) {
}
return fact;
}

/**
* A test method of the permutation generator
* @param args
*/
public static void main(String[] args) {
DictOrderPermutator pc = new DictOrderPermutator(4);
while (pc.hasNext()) {
int[] dots = pc.next();
long hash = 0;
long poly = 1;
for (int x = 0; x < dots.length; x++) {
System.out.print(dots[x]);
hash += dots[x] * poly;
poly *= 11;

}
System.out.println(" => " + hash);
}
}
}
Loading

0 comments on commit 39b368f

Please sign in to comment.