package test;

import java.io.PrintStream;
import uk.ac.ic.doc.automed.util.NumberToolkit;
import uk.ac.ic.doc.automed.util.hashing.StringHash;
import uk.ac.ic.doc.automed.util.hashing.StringHashFactory;


/**
 * TestStringHashing.java<br><br>
 *
 *
 *
 * @author Duc M Le  <a href="mailto:dmle@doc.ic.ac.uk"><i>dmle@doc.ic.ac.uk</i></a>
 * @version 1.0
 * <br>Department of Computing, Imperial College
 */

public class TestStringHashing {
  
  private static PrintStream out = System.out;
  
  /**
   * Creates a new instance of TestStringHashing
   */
  public TestStringHashing() {
  }
  
  public static void main (String[] args) {
    StringHash sh = StringHashFactory.getStringHashProvider("SummativeStringHashing");
    StringHash sh2 = StringHashFactory.getStringHashProvider("SummativeStringHashing");
    StringHash sh3 = StringHashFactory.getStringHashProvider("SimpleStringHashing");
    
    String[] strings1 = new String[] {
    "book", "author", "publication", "writing", "journal"};
    int n1 = 3;
    
    String[] strings2 = new String[] {
    "bkid", "title", "id", "name", "pubid", "year", "auid", "jaid"};
    int n2  = 3;
    
    String[] strings3 = new String[] {
    "book,bkid", "book,title", 
    "author,id", "author,name", 
    "publication,pubid", "publication,title", "publication,year", 
    "writing,pubid", "writing,auid", "writing,jaid",
    "journal,jaid", "journal,title", "journal,year"};
    int n3_1  = n1;
    int n3_2 = n2;
    int n3_3 = 4;
    
    for (int i = 0; i < strings1.length; i++) {
      String str = strings1[i];
      int h = sh.hash(str, n1);
      out.println("hash('" + str + "') -> " + h + "(or "+NumberToolkit.getBitString(h)+")");
    }
    
    out.println("===============================================");
    for (int i = 0; i < strings2.length; i++) {
      String str = strings2[i];
      int h = sh2.hash(str, n2);
      out.println("hash('" + str + "') -> " + h + "(or "+NumberToolkit.getBitString(h)+")");
    }
  
    out.println("===============================================");
    int m = (int) Math.pow(2, n3_3);
    for (int i = 0; i < strings3.length; i++) {
      String str = strings3[i];
      String[] strItems = uk.ac.ic.doc.automed.Util.split(str, ",");
      int val1 = sh.hash(strItems[0], n3_1);
      int val2  = sh.hash(strItems[1], n3_2);
      int val = (val1 << n3_2) + val2;
//      out.println("hash('" + str + "') <-> (("+val1+" << "+n3_2+") + "+val2+") mod "+m+" <-> "+val+" mod "+m+" = " + (val % m));
      out.println("hash('" + str + "') <-> (("+val1+" << "+n3_2+") + "+val2+") = " + val + 
        " (or "+NumberToolkit.getBitString(val1)+"-"+NumberToolkit.getBitString(val2)+")");
    }
  }  
}
