Hi there,
i'm trying to convert a C++ code about Hashtable in Java but i get these errors messages:
- array required, but java.lang.String found &
- array required, but java.util.Vector.
Here is the C++ code first :
#include "Hash.h"
#include <cmath>
using namespace std;
int key;
char z;
int tableSize,tSize = 1009;
char ch;
int n,base;
long int exponent;
string fname;
//int exponent;
//int base;
int c = 0; int g5 = 0;
int q = 0; int p = 1;
int x = 0; int g6 = 0;
int g0 = 0; int g7 = 0;
int g1 = 0; int g8 = 0;
int g2 = 0; int g9 = 0;
int g3 = 0; int g10 = 0;
int g4 = 0;
int y = 0;
Hash hash;
int Hash::getHashFunction()
{
enter:
while(cin.get(ch))
{
if((ch >= '0') && (ch <= '9'))
{
cin.putback(ch);
cin >> n;
break;
}
else
goto enter;
}
return n;
}
int Hash::Key(string &words,long int &exponent,int &base)
{
int hashVal;
if(words.length() < exponent)
{
//for(int j = 0; j < (exponent- words.length()); j++)
while(words.length() < exponent)
{
words += "z";
cout<<words<<" "<<"appended\n";
}
for(int i = 0; i < exponent; i++)
{
//key += (words*(int)(pow((double)base,(exponent-(i+1)))));
key += words*(base<<exponent);
}
}
else
{
for(int t = 0; t < exponent; t++)
{
//key += (double)(words[t]*(int)(pow((double)base,(double)exponent)));
key += words[t]*(base<<exponent);
}
} /*
//int hashVal;
if(words.length() < exponent)
{
//for(int i = words.length(); i < exponent; i++ )
while(words.length() < exponent)
{
words.append("z");
//words += "z";
}
}
for(int i = 0; i < exponent; i++)
{
exponent--;
key += (int)(words*(int)(pow((double)base,exponent)));
//key += words*(base>>exponent);
}*/
return hashVal = (int)(key%tableSize);
}
bool isPrime(int n)
{
if(n < 2)
return false;
for(int i = 2; i*i <= n; i++)
{
if(n%2 == 0)
return false;
}
return true;
}
int nextPrime(int n)
{
if(n%2 == 0)
n++;
for(; !isPrime(n); n+=2)
;
return n;
}
int r = 0; /**to be removed*/
void Hash::linearProbing(string &words)
{
int hashVal;
cout<<"Enter file name: ";
cin>>fname;
cout<<"Enter any one of the following hash functions: \n";
cout<<" 2,32 \n 4,32 \n 6,32 \n 2,128 \n 4,128 \n 6,128 \n";
cout<<endl;
cout<<"Hash Function: ";
exponent = hash.getHashFunction();
base = hash.getHashFunction();
cout<<"Enter table size: ";
cin>>tableSize;
tableSize = nextPrime(tableSize);
hash.readFile(fname,words,tableSize);
hash.StrArray = new string [tableSize];
hash.IntArray = new int [tableSize];
int table = int(0.1*tableSize);
for( int i = 0; i < tableSize; i++)
{
words = hash.fileVec[y];
hashVal = hash.Key(words,exponent,base);
y++;
if(hash.IntArray[hashVal] == 1)
{/**To be removed*/
cout<<"collission detected @ "<<hashVal<<" "<<words<<" # "<<r<<endl;
while(hash.IntArray[hashVal] == 1)
{
c++; //counter for collissions
hashVal++;
if(hashVal >= tableSize)
{/**To be removed*/
cout<<"\t\tTable size exceeded111 "<<hashVal<<" "<<words<< "\n";
hashVal = hashVal%tableSize;
}
}
hash.IntArray[hashVal] = 1; /**To be removed*/
hash.StrArray[hashVal] = words;
cout<<"\tManaged to hash the word(" <<hash.StrArray[hashVal]<< ")@ "<<hashVal<<" after "<<c<<" clashe(s) "<<endl;
if(c == 1 )
g1++;
else if(c == 2 )
g2++;
else if(c == 3 )
g3++;
else if(c == 4 )
g4++;
else if(c == 5 )
g5++;
else if(c == 6 )
g6++;
else if(c == 7 )
g7++;
else if(c == 8 )
g8++;
else if(c == 9 )
g9++;
else if(c == 10 )
g10++;
c = 0; /**To be removed*/
}
else if(hash.IntArray[hashVal] == 0)
{
g0++;
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1; /**To be removed*/
cout<<"HASH SPACE FOUND @ "<<hashVal<<" "<<words<<" # "<<r<<endl;
}
if((i == table) || (i == (2*table)) || (i == (3*table)) || (i == (4*table)) || (i == (5*table)) || (i == (6*table)) || (i == (7*table)) || (i == (8*table)) || (i == (9*table)) || (i == (10*table)))
{
cout<<"\n\t\tAfter "<<p*10<<"% fill"<<endl;
cout<<endl;
cout<<g0<<" were inserted first time"<<endl;
cout<<g1<<" were inserted after one clash"<<endl;
cout<<g2<<" were inserted after two clashes"<<endl;
cout<<g3<<" were inserted after three clashes"<<endl;
cout<<g4<<" were inserted after four clashes"<<endl;
cout<<g5<<" were inserted after five clashes"<<endl;
cout<<g6<<" were inserted after six clashes"<<endl;
cout<<g7<<" were inserted after seven clashes"<<endl;
cout<<g8<<" were inserted after eight clashes"<<endl;
cout<<g9<<" were inserted after nine clashes"<<endl;
cout<<g10<<" were inserted after ten clashes"<<endl;
cout<<endl;
p++;
}
r++; /**To be removed*/
}
/*
for(int o = 0; o < tableSize; o++)
{
//cout<<" "<<hash.StrArray[o]<<" "<<hash.IntArray[o]<<endl;
hash.StrArray[o] = " ";
hash.IntArray[o] = 0;
//cout<<" "<<hash.StrArray[o]<<" "<<hash.IntArray[o]<<endl;
} */
}
void Hash::quadraticProbing(string &words)
{
int hashVal;
cout<<"Enter file name: ";
cin>>fname;
//hash.readFile(fname,words);
cout<<"Enter any one of the following hash functions: \n";
cout<<" 2,32 \n 4,32 \n 6,32 \n 2,128 \n 4,128 \n 6,128 \n";
cout<<endl;
cout<<"Hash Function: ";
exponent = hash.getHashFunction();
base = hash.getHashFunction();
cout<<"Enter table size: ";
cin>>tableSize;
tableSize = nextPrime(tableSize);
hash.readFile(fname,words,tableSize);
hash.StrArray = new string [tableSize];
hash.IntArray = new int [tableSize];
int table = int(0.1*tableSize);
for( int i = 1; i <= tableSize; i++)
{
words = hash.fileVec[y];
y++;
hashVal = hash.Key(words,exponent,base);
if(hash.IntArray[hashVal] == 1)
{
//cout<<"collission detected "<<hashVal<<" "<<words<<" "<<q<<endl;
while(hash.IntArray[hashVal] == 1)
{
c++;/**To be removed*/
hashVal = (int)(hashVal + pow((double)q,2))%tableSize;
//cout<<(q^2)<<endl;
//cout<<(int)pow((double)q,2)<<endl;
if(hashVal >= tableSize)
{/**To be removed*/
//cout<<"\t\tTable size exceeded "<<hashVal<<" "<<words<<" "<<q<< "\n";
hashVal = hashVal - tableSize;
}
q++;
}
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1; /**To be removed*/
//cout<<"\tManaged to hash the word(" <<hash.StrArray[hashVal]<< ")@ "<<hashVal<<" after "<<d<<" clashe(s) "<<endl;
if(c == 1 )
g1++;
else if(c == 2 )
g2++;
else if(c == 3 )
g3++;
else if(c == 4 )
g4++;
else if(c == 5 )
g5++;
else if(c == 6 )
g6++;
else if(c == 7 )
g7++;
else if(c == 8 )
g8++;
else if(c == 9 )
g9++;
else if(c == 10 )
g10++;
c = 0; /**To be removed*/
}
else if(hash.IntArray[hashVal] == 0)
{ g0++;
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1; /**To be removed*/
//cout<<"HASH SPACE FOUND @ "<<hashVal<<" "<<words<<" "<<q<<endl;
}
if((i == table) || (i == (2*table)) || (i == (3*table)) || (i == (4*table)) || (i == (5*table)) || (i == (6*table)) || (i == (7*table)) || (i == (8*table)) || (i == (9*table)) || (i == (10*table)))
{
cout<<"\n\t\tAfter "<<p*10<<"% fill"<<endl;
cout<<endl;
cout<<g0<<" were inserted first time"<<endl;
cout<<g1<<" were inserted after one clash"<<endl;
cout<<g2<<" were inserted after two clashes"<<endl;
cout<<g3<<" were inserted after three clashes"<<endl;
cout<<g4<<" were inserted after four clashes"<<endl;
cout<<g5<<" were inserted after five clashes"<<endl;
cout<<g6<<" were inserted after six clashes"<<endl;
cout<<g7<<" were inserted after seven clashes"<<endl;
cout<<g8<<" were inserted after eight clashes"<<endl;
cout<<g9<<" were inserted after nine clashes"<<endl;
cout<<g10<<" were inserted after ten clashes"<<endl;
cout<<endl;
p++;
}
}
}
void Hash::linearProbing2(string &words)
{
int hashVal;
cout<<"Enter file name: ";
cin>>fname;
cout<<"Enter any one of the following hash functions: \n";
cout<<" 2,32 \n 4,32 \n 6,32 \n 2,128 \n 4,128 \n 6,128 \n";
cout<<endl;
cout<<"Hash Function: ";
exponent = hash.getHashFunction();
base = hash.getHashFunction();
cout<<"Enter table size: ";
cin>>tableSize;
tableSize = nextPrime(tableSize);
hash.readFile(fname,words,tableSize);
hash.StrArray = new string [tableSize];
hash.IntArray = new int [tableSize];
int table = int(0.1*tableSize);
for( int i = 1; i <= tableSize; i++)
{
words = hash.fileVec[y];
y++;
hashVal = hash.Key(words,exponent,base);
if(i <= table)
{
if(hash.IntArray[hashVal] == 1)
{//cout<<"collission detected @ "<<hashVal<<" "<<words<<endl;
while(hash.IntArray[hashVal] == 1)
{
c++;
hashVal++;
if(hashVal >= tableSize)
{
hashVal = hashVal - tableSize;
}
}
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1;
//cout<<"\tManaged to hash the word(" <<hash.StrArray[hashVal]<< ")@ "<<hashVal<<" after "<<c<<" clashe(s) "<<endl;
}
else if(hash.IntArray[hashVal] == 0)
{
//cout<<"ahaa SPACE FOUND @ "<<hashVal<<" "<<words<<endl;
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1;
}
}
else if(i > table && i <= (0.5*tableSize+table))
{
if(hash.IntArray[hashVal] == 1)
{/**To be removed*/
//cout<<"collission2 detected @ "<<hashVal<<" "<<words<<" # "<<r<<endl;
while(hash.IntArray[hashVal] == 1)
{
c++;
hashVal++;
if(hashVal >= tableSize)
{/**To be removed*/
// cout<<"\t\tTable size exceeded2 "<<hashVal<<" "<<words<< "\n";
hashVal = hashVal - tableSize;
}
}
//cout<<"\tManaged to hash the word2(" <<hash.StrArray[hashVal]<< ")@ "<<hashVal<<" after "<<c<<" clashe(s) "<<endl;
if(c == 1 )
g1++;
else if(c == 2 )
g2++;
else if(c == 3 )
g3++;
else if(c == 4 )
g4++;
else if(c == 5 )
g5++;
else if(c == 6 )
g6++;
else if(c == 7 )
g7++;
else if(c == 8 )
g8++;
else if(c == 9 )
g9++;
else if(c == 10 )
g10++;
c = 0;
}
else if(hash.IntArray[hashVal] == 0)
{
g0++;
//cout<<"HASH SPACE FOUND2 @ "<<hashVal<<" "<<words<<" # "<<r<<endl;
}
if((i == table) || (i == (2*table)) || (i == (3*table)) || (i == (4*table)) || (i == (5*table)) || (i == (6*table)) || (i == (7*table)) || (i == (8*table)) || (i == (9*table)) || (i == (10*table)))
{
cout<<"\n\t\tAfter "<<p*10<<"% fill"<<endl;
cout<<endl;
cout<<g0<<" were inserted first time"<<endl;
cout<<g1<<" were inserted after one clash"<<endl;
cout<<g2<<" were inserted after two clashes"<<endl;
cout<<g3<<" were inserted after three clashes"<<endl;
cout<<g4<<" were inserted after four clashes"<<endl;
cout<<g5<<" were inserted after five clashes"<<endl;
cout<<g6<<" were inserted after six clashes"<<endl;
cout<<g7<<" were inserted after seven clashes"<<endl;
cout<<g8<<" were inserted after eight clashes"<<endl;
cout<<g9<<" were inserted after nine clashes"<<endl;
cout<<g10<<" were inserted after ten clashes"<<endl;
cout<<endl;
p++;
}
r++; /**To be removed*/
}
}
}
void Hash::quadraticProbing2(string &words)
{
int hashVal;
cout<<"Enter file name: ";
cin>>fname;
cout<<"Enter any one of the following hash functions: \n";
cout<<" 2,32 \n 4,32 \n 6,32 \n 2,128 \n 4,128 \n 6,128 \n";
cout<<endl;
cout<<"Hash Function: ";
exponent = hash.getHashFunction();
base = hash.getHashFunction();
cout<<"Enter table size: ";
cin>>tableSize;
tableSize = nextPrime(tableSize);
hash.readFile(fname,words,tableSize);
hash.StrArray = new string [tableSize];
hash.IntArray = new int [tableSize];
int table = int(0.1*tableSize);
//cout<<"Table "<<table<<" "<<tableSize<<endl;
for( int i = 1; i <= tableSize; i++)
{
words = hash.fileVec[y];
y++;
hashVal = hash.Key(words,exponent,base);
if(i <= table)
{
if(hash.IntArray[hashVal] == 1)
{//cout<<"collission detected @ "<<hashVal<<" "<<words<<endl;
while(hash.IntArray[hashVal] == 1)
{
//c++;
hashVal = (int)(hashVal + pow((double)q,2))%tableSize;
//cout<<" Trying "<<endl;
if(hashVal >= tableSize)
{
hashVal = hashVal - tableSize;
}
q++;
}
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1;
//cout<<"\tManaged to hash the word(" <<hash.StrArray[hashVal]<< ")@ "<<hashVal<<" after "<<c<<" clashe(s) "<<endl;
}
else if(hash.IntArray[hashVal] == 0)
{
//cout<<"ahaa SPACE FOUND @ "<<hashVal<<" "<<words<<endl;
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1;
}
}
else if(i > table && i <= (0.5*tableSize+table))
{
if(hash.IntArray[hashVal] == 1)
{/**To be removed*/
//cout<<"collission detected @ "<<hashVal<<" "<<words<<" # "<<r<<endl;
while(hash.IntArray[hashVal] == 1)
{
c++;
hashVal++;
if(hashVal >= tableSize)
{/**To be removed*/
cout<<"\t\tTable size exceeded "<<hashVal<<" "<<words<< "\n";
hashVal = (int)(hashVal + pow((double)q,2))%tableSize;
}
}
//hash.StrArray[hashVal] = words;
//hash.IntArray[hashVal] = 1; /**To be removed*/
cout<<"\tManaged to hash the word(" <<hash.StrArray[hashVal]<< ")@ "<<hashVal<<" after "<<c<<" clashe(s) "<<endl;
if(c == 1 )
g1++;
else if(c == 2 )
g2++;
else if(c == 3 )
g3++;
else if(c == 4 )
g4++;
else if(c == 5 )
g5++;
else if(c == 6 )
g6++;
else if(c == 7 )
g7++;
else if(c == 8 )
g8++;
else if(c == 9 )
g9++;
else if(c == 10 )
g10++;
c = 0;
}
else if(hash.IntArray[hashVal] == 0)
{
g0++;
//hash.StrArray[hashVal] = words;
//hash.IntArray[hashVal] = 1; /**To be removed*/
cout<<"HASH SPACE FOUND @ "<<hashVal<<" "<<words<<" # "<<r<<endl;
}
if((i == table) || (i == (2*table)) || (i == (3*table)) || (i == (4*table)) || (i == (5*table)) || (i == (6*table)) || (i == (7*table)) || (i == (8*table)) || (i == (9*table)) || (i == (10*table)))
{
cout<<"\n\t\tAfter "<<p*10<<"% fill "<<endl;
cout<<endl;
cout<<g0<<" were inserted first time"<<endl;
cout<<g1<<" were inserted after one clash"<<endl;
cout<<g2<<" were inserted after two clashes"<<endl;
cout<<g3<<" were inserted after three clashes"<<endl;
cout<<g4<<" were inserted after four clashes"<<endl;
cout<<g5<<" were inserted after five clashes"<<endl;
cout<<g6<<" were inserted after six clashes"<<endl;
cout<<g7<<" were inserted after seven clashes"<<endl;
cout<<g8<<" were inserted after eight clashes"<<endl;
cout<<g9<<" were inserted after nine clashes"<<endl;
cout<<g10<<" were inserted after ten clashes"<<endl;
cout<<endl;
p++;
}
r++; /**To be removed*/
}
}
}
void Hash::makeEmpty(int &tableSize)
{
for(int i = 0; i < tableSize; i++)
{
hash.IntArray = 0;
hash.StrArray = " ";
}
}
void Hash::drawMenu()
{
cout<<endl;
cout<<"\t\t ___________________MENU__________________\n";
cout<<"\t\t| |\n";
cout<<"\t\t| Specify option(1 or 2) |\n";
cout<<"\t\t| 1.Real Insertion |\n";
cout<<"\t\t| 2.Virtual Insertion |\n";
cout<<"\t\t| a. Linear Probing |\n";
cout<<"\t\t| i. Hash Function |\n";
cout<<"\t\t| ii.Table Size |\n";
cout<<"\t\t| b. Quadratic Probing |\n";
cout<<"\t\t| i. Hash Function |\n";
cout<<"\t\t| ii.Table Size |\n";
cout<<"\t\t| 3. Exit |\n";
cout<<"\t\t|_________________________________________|\n";
}
----
Now can please have a look of the Java code (for the same C++ code:
//#include "Hash.h"
//#include <cmath>
//using namespace std;
import java.io.*;
import java.text.*;
import java.lang.*;
import java.util.*;
import java.util.Vector;
import essential.*;
//import keyboard.*;
public class HashCode
{
//public vector <String>fileVec; //vector that stores words from the file
// Create an instance of class Vector ...
Vector fileVec = new Vector();
//int *IntArray; //array that stores hash keys
public int [] IntArray;
//String *StrArray; //array that stores hashed words
public String [] StrArray;
//String words; //words read from a file
HashCode hash = new HashCode();
String fname, words;
int tableSize = 0;
//Creating a BufferedReader object
static BufferedReader bf =
new BufferedReader(new InputStreamReader(System.in));
// Create a number formatter object
static NumberFormat aNumberFormatter = NumberFormat.getInstance();
int key;
char z;
int tSize = 1009;
char ch;
int n,base;
long exponent;
//int exponent;
//int base;
int c = 0; int g5 = 0;
int q = 0; int p = 1;
int x = 0; int g6 = 0;
int g0 = 0; int g7 = 0;
int g1 = 0; int g8 = 0;
int g2 = 0; int g9 = 0;
int g3 = 0; int g10 = 0;
int g4 = 0;
int y = 0;
//public void Hash();
//public int hashFunction(int key)
//{
// return key % arraySize;
//}
//int Hash::getHashFunction()
public int getHashFunction()
{
//Scanner keyboard = new Scanner(System.in);
char ch;
//enter:
//while(cin.get(ch))
//while( bf.read(ch))
//nextValue = System.in.read();
//ch = (char) System.in.read();
//while(ch)
//do
//{
//if((ch >= '0') && (ch <= '9'))
while((ch >= '0') && (ch <= '9'))
{
//cin.putback(ch);
//ch = (char) bf.read();
ch = (char) System.in.read();
n = aNumberFormatter.parse(bf.readLine()).intValue();
break;
}
//else
//goto enter;
//exit do;
//}
return n;
}
//int Hash::Key(String &words,long int &exponent,int &base)
public int key(String words, long exponent, int base)
{
//String word [] = new String[2001];
int hashVal;
//if(words.length() < exponent)
if(words.length() < exponent)
{
//for(int j = 0; j < (exponent- words.length()); j++)
while(words.length() < exponent)
{
words += "z";
System.out.println(words+" "+"appended\n");
}
for(int i = 0; i < exponent; i++)
{
//key += (words*(int)(pow((double)base,(exponent-(i+1)))));
key += words*(base+exponent);
}
}
else
{
for(int t = 0; t < exponent; t++)
{
//key += (double)(words[t]*(int)(pow((double)base,(double)exponent)));
key += words[t]*(base<<exponent);
}
}
/*//int hashVal;
if(words.length() < exponent)
{
//for(int i = words.length(); i < exponent; i++ )
while(words.length() < exponent)
{
//words.append("z");
words += "z";
}
}
for(int i = 0; i < exponent; i++)
{
exponent--;
key += (int)(words*(int)(Math.pow((double)base,exponent)));
//key += words*(base>>exponent);
}*/
return hashVal = (int)(key%tableSize);
}
//bool isPrime(int n)
public boolean isPrime(int n)
{
if(n < 2)
return false;
for(int i = 2; i*i <= n; i++)
{
if(n%2 == 0)
return false;
}
return true;
}
//int nextPrime(int n)
public int nextPrime(int n)
{
if(n%2 == 0)
n++;
for(; !isPrime(n); n+=2)
;
return n;
}
int r = 0; /**to be removed*/
//void Hash::linearProbing(String &words)
public void linearProbing(String words)
{
int hashVal;
System.out.println("Enter file name: ");
fname = bf.readLine();
System.out.println("Enter any one of the following hash functions: \n");
System.out.println(" 2,32 \n 4,32 \n 6,32 \n 2,128 \n 4,128 \n 6,128 \n");
System.out.println();
System.out.println("Hash Function: ");
//exponent = hash.getHashFunction();
base = hash.getHashFunction();
System.out.println("Enter table size: ");
tableSize = aNumberFormatter.parse(bf.readLine()).intValue();
tableSize = nextPrime(tableSize);
hash.readFile(fname,words,tableSize);
hash.StrArray = new String [tableSize];
hash.IntArray = new int [tableSize];
//int table = (0.1*tableSize);
int table;
table %= tableSize;
for( int i = 0; i < tableSize; i++)
{
words = hash.fileVec[y];
hashVal = hash.key(words,exponent,base);
y++;
if(hash.IntArray[hashVal] == 1)
{/**To be removed*/
System.out.println("collission detected @ "+hashVal+" "+words+" # "+r);
while(hash.IntArray[hashVal] == 1)
{
c++; //counter for collissions
hashVal++;
if(hashVal >= tableSize)
{/**To be removed*/
System.out.println("\t\tTable size exceeded111 "+ hashVal+ " " +words + "\n");
hashVal = hashVal%tableSize;
}
}
hash.IntArray[hashVal] = 1; /**To be removed*/
hash.StrArray[hashVal] = words;
System.out.println("\tManaged to hash the word(" +hash.StrArray[hashVal]+
")@ "+ hashVal+" after "+c+" clashe(s) ");
if(c == 1 )
g1++;
else if(c == 2 )
g2++;
else if(c == 3 )
g3++;
else if(c == 4 )
g4++;
else if(c == 5 )
g5++;
else if(c == 6 )
g6++;
else if(c == 7 )
g7++;
else if(c == 8 )
g8++;
else if(c == 9 )
g9++;
else if(c == 10 )
g10++;
c = 0; /**To be removed*/
}
else if(hash.IntArray[hashVal] == 0)
{
g0++;
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1; /**To be removed*/
System.out.println("HASH SPACE FOUND @ "+hashVal+" "+words+" # "+r);
}
if((i == table) || (i == (2*table)) || (i == (3*table)) || (i == (4*table)) || (i == (5*table)) || (i == (6*table)) || (i == (7*table)) || (i == (8*table)) || (i == (9*table)) || (i == (10*table)))
{
System.out.println("\n\t\tAfter "+p*10+"% fill");
System.out.println();
System.out.println(g0+" were inserted first time");
System.out.println(g1+" were inserted after one clash");
System.out.println(g2+" were inserted after two clashes");
System.out.println(g3+" were inserted after three clashes");
System.out.println(g4+" were inserted after four clashes");
System.out.println(g5+" were inserted after five clashes");
System.out.println(g6+" were inserted after six clashes");
System.out.println(g7+" were inserted after seven clashes");
System.out.println(g8+" were inserted after eight clashes");
System.out.println(g9+" were inserted after nine clashes");
System.out.println(g10+" were inserted after ten clashes");
System.out.println();
p++;
}
r++; /**To be removed*/
}
/*
for(int o = 0; o < tableSize; o++)
{
//System.out.println<<" "<<hash.StrArray[o]<<" "<<hash.IntArray[o]<<;
hash.StrArray[o] = " ";
hash.IntArray[o] = 0;
//System.out.println<<" "<<hash.StrArray[o]<<" "<<hash.IntArray[o]<<;
} */
}
//void Hash::quadraticProbing(String &words)
public void quadraticProbing(String words)
{
int hashVal;
System.out.println("Enter file name: ");
fname = bf.readLine();
//hash.readFile(fname,words);
System.out.println("Enter any one of the following hash functions: \n");
System.out.println(" 2,32 \n 4,32 \n 6,32 \n 2,128 \n 4,128 \n 6,128 \n");
System.out.println();
System.out.println("Hash Function: ");
exponent = hash.getHashFunction();
base = hash.getHashFunction();
System.out.println("Enter table size: ");
tableSize = aNumberFormatter.parse(bf.readLine()).intValue();
tableSize = nextPrime(tableSize);
hash.readFile(fname,words,tableSize);
hash.StrArray = new String [tableSize];
hash.IntArray = new int [tableSize];
//int table = (0.1*tableSize);
int table;
table %= tableSize;
for( int i = 1; i <= tableSize; i++)
{
words = hash.fileVec[y];
y++;
hashVal = hash.key(words,exponent,base);
if(hash.IntArray[hashVal] == 1)
{
//System.out.println<<"collission detected "<<hashVal<<" "<<words<<" "<<q<<;
while(hash.IntArray[hashVal] == 1)
{
c++;/**To be removed*/
hashVal = (int)(hashVal + Math.pow((double)q,2))%tableSize;
//System.out.println<<(q^2)<<;
//System.out.println<<(int)pow((double)q,2)<<;
if(hashVal >= tableSize)
{/**To be removed*/
//System.out.println<<"\t\tTable size exceeded "<<hashVal<<" "<<words<<" "<<q<< "\n";
hashVal = hashVal - tableSize;
}
q++;
}
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1; /**To be removed*/
//System.out.println<<"\tManaged to hash the word(" <<hash.StrArray[hashVal]<< ")@ "<<hashVal<<" after "<<d<<" clashe(s) "<<;
if(c == 1 )
g1++;
else if(c == 2 )
g2++;
else if(c == 3 )
g3++;
else if(c == 4 )
g4++;
else if(c == 5 )
g5++;
else if(c == 6 )
g6++;
else if(c == 7 )
g7++;
else if(c == 8 )
g8++;
else if(c == 9 )
g9++;
else if(c == 10 )
g10++;
c = 0; /**To be removed*/
}
else if(hash.IntArray[hashVal] == 0)
{ g0++;
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1; /**To be removed*/
//System.out.println<<"HASH SPACE FOUND @ "<<hashVal<<" "<<words<<" "<<q<<;
}
if((i == table) || (i == (2*table)) || (i == (3*table)) || (i == (4*table)) || (i == (5*table)) || (i == (6*table)) || (i == (7*table)) || (i == (8*table)) || (i == (9*table)) || (i == (10*table)))
{
System.out.println("\n\t\tAfter "+p*10+"% fill");
System.out.println();
System.out.println(g0+" were inserted first time");
System.out.println(g1+" were inserted after one clash");
System.out.println(g2+" were inserted after two clashes");
System.out.println(g3+" were inserted after three clashes");
System.out.println(g4+" were inserted after four clashes");
System.out.println(g5+" were inserted after five clashes");
System.out.println(g6+" were inserted after six clashes");
System.out.println(g7+" were inserted after seven clashes");
System.out.println(g8+" were inserted after eight clashes");
System.out.println(g9+" were inserted after nine clashes");
System.out.println(g10+" were inserted after ten clashes");
System.out.println();
p++;
}
}
}
//void Hash::linearProbing2(String &words)
public void linerProbing2(String words)
{
int hashVal;
System.out.println("Enter file name: ");
fname = bf.readLine();
System.out.println("Enter any one of the following hash functions: \n");
System.out.println(" 2,32 \n 4,32 \n 6,32 \n 2,128 \n 4,128 \n 6,128 \n");
System.out.println();
System.out.println("Hash Function: ");
exponent = hash.getHashFunction();
base = hash.getHashFunction();
System.out.println("Enter table size: ");
tableSize = aNumberFormatter.parse(bf.readLine()).intValue();
tableSize = nextPrime(tableSize);
hash.readFile(fname,words,tableSize);
hash.StrArray = new String [tableSize];
hash.IntArray = new int [tableSize];
//int table = (0.1*tableSize);
int table;
table %= tableSize;
for( int i = 1; i <= tableSize; i++)
{
words = hash.fileVec[y];
y++;
hashVal = hash.key(words,exponent,base);
if(i <= table)
{
if(hash.IntArray[hashVal] == 1)
{//System.out.println<<"collission detected @ "<<hashVal<<" "<<words<<;
while(hash.IntArray[hashVal] == 1)
{
c++;
hashVal++;
if(hashVal >= tableSize)
{
hashVal = hashVal - tableSize;
}
}
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1;
//System.out.println<<"\tManaged to hash the word(" <<hash.StrArray[hashVal]<< ")@ "<<hashVal<<" after "<<c<<" clashe(s) "<<;
}
else if(hash.IntArray[hashVal] == 0)
{
//System.out.println<<"ahaa SPACE FOUND @ "<<hashVal<<" "<<words<<;
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1;
}
}
else if(i > table && i <= (0.5*tableSize+table))
{
if(hash.IntArray[hashVal] == 1)
{/**To be removed*/
//System.out.println<<"collission2 detected @ "<<hashVal<<" "<<words<<" # "<<r<<;
while(hash.IntArray[hashVal] == 1)
{
c++;
hashVal++;
if(hashVal >= tableSize)
{/**To be removed*/
// System.out.println<<"\t\tTable size exceeded2 "<<hashVal<<" "<<words<< "\n";
hashVal = hashVal - tableSize;
}
}
//System.out.println<<"\tManaged to hash the word2(" <<hash.StrArray[hashVal]<< ")@ "<<hashVal<<" after "<<c<<" clashe(s) "<<;
if(c == 1 )
g1++;
else if(c == 2 )
g2++;
else if(c == 3 )
g3++;
else if(c == 4 )
g4++;
else if(c == 5 )
g5++;
else if(c == 6 )
g6++;
else if(c == 7 )
g7++;
else if(c == 8 )
g8++;
else if(c == 9 )
g9++;
else if(c == 10 )
g10++;
c = 0;
}
else if(hash.IntArray[hashVal] == 0)
{
g0++;
//System.out.println<<"HASH SPACE FOUND2 @ "<<hashVal<<" "<<words<<" # "<<r<<;
}
if((i == table) || (i == (2*table)) || (i == (3*table)) || (i == (4*table)) || (i == (5*table)) || (i == (6*table)) || (i == (7*table)) || (i == (8*table)) || (i == (9*table)) || (i == (10*table)))
{
System.out.println("\n\t\tAfter "+p*10+"% fill");
System.out.println();
System.out.println(g0+" were inserted first time");
System.out.println(g1+" were inserted after one clash");
System.out.println(g2+" were inserted after two clashes");
System.out.println(g3+" were inserted after three clashes");
System.out.println(g4+" were inserted after four clashes");
System.out.println(g5+" were inserted after five clashes");
System.out.println(g6+" were inserted after six clashes");
System.out.println(g7+" were inserted after seven clashes");
System.out.println(g8+" were inserted after eight clashes");
System.out.println(g9+" were inserted after nine clashes");
System.out.println(g10+" were inserted after ten clashes");
System.out.println();
p++;
}
r++; /**To be removed*/
}
}
}
//void Hash::quadraticProbing2(String &words)
public void quadraticProbing2(String words)
{
int hashVal;
System.out.println("Enter file name: ");
fname = bf.readLine();
System.out.println("Enter any one of the following hash functions: \n");
System.out.println(" 2,32 \n 4,32 \n 6,32 \n 2,128 \n 4,128 \n 6,128 \n");
System.out.println();
System.out.println("Hash Function: ");
exponent = hash.getHashFunction();
base = hash.getHashFunction();
System.out.println("Enter table size: ");
tableSize = aNumberFormatter.parse(bf.readLine()).intValue();
tableSize = nextPrime(tableSize);
hash.readFile(fname,words,tableSize);
hash.StrArray = new String [tableSize];
hash.IntArray = new int [tableSize];
//int table = (0.1*tableSize);
int table;
table %= tableSize;
//System.out.println<<"Table "<<table<<" "<<tableSize<<;
for( int i = 1; i <= tableSize; i++)
{
words = hash.fileVec[y];
y++;
hashVal = hash.key(words,exponent,base);
if(i <= table)
{
if(hash.IntArray[hashVal] == 1)
{//System.out.println<<"collission detected @ "<<hashVal<<" "<<words<<;
while(hash.IntArray[hashVal] == 1)
{
//c++;
//sum = sum + s.charAt(i)*Math.pow(128,str_len-(i+1));
hashVal = (int)(hashVal + Math.pow((double)q,2))%tableSize;
//System.out.println<<" Trying "<<;
if(hashVal >= tableSize)
{
hashVal = hashVal - tableSize;
}
q++;
}
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1;
//System.out.println<<"\tManaged to hash the word(" <<hash.StrArray[hashVal]<< ")@ "<<hashVal<<" after "<<c<<" clashe(s) "<<;
}
else if(hash.IntArray[hashVal] == 0)
{
//System.out.println<<"ahaa SPACE FOUND @ "<<hashVal<<" "<<words<<;
hash.StrArray[hashVal] = words;
hash.IntArray[hashVal] = 1;
}
}
else if(i > table && i <= (0.5*tableSize+table))
{
if(hash.IntArray[hashVal] == 1)
{/**To be removed*/
//System.out.println<<"collission detected @ "<<hashVal<<" "<<words<<" # "<<r<<;
while(hash.IntArray[hashVal] == 1)
{
c++;
hashVal++;
if(hashVal >= tableSize)
{/**To be removed*/
System.out.println("\t\tTable size exceeded "+hashVal+" "+words+ "\n");
hashVal = (int)(hashVal + Math.pow((double)q,2))%tableSize;
}
}
//hash.StrArray[hashVal] = words;
//hash.IntArray[hashVal] = 1; /**To be removed*/
System.out.println("\tManaged to hash the word(" +hash.StrArray[hashVal]+ ")@ "+hashVal+" after "+c+" clashe(s) ");
if(c == 1 )
g1++;
else if(c == 2 )
g2++;
else if(c == 3 )
g3++;
else if(c == 4 )
g4++;
else if(c == 5 )
g5++;
else if(c == 6 )
g6++;
else if(c == 7 )
g7++;
else if(c == 8 )
g8++;
else if(c == 9 )
g9++;
else if(c == 10 )
g10++;
c = 0;
}
else if(hash.IntArray[hashVal] == 0)
{
g0++;
//hash.StrArray[hashVal] = words;
//hash.IntArray[hashVal] = 1; /**To be removed*/
System.out.println("HASH SPACE FOUND @ "+hashVal+" "+words+" # "+r);
}
if((i == table) || (i == (2*table)) || (i == (3*table)) || (i == (4*table))
|| (i == (5*table)) || (i == (6*table)) || (i == (7*table)) || (i == (8*table))
|| (i == (9*table)) || (i == (10*table)))
{
System.out.println("\n\t\tAfter "+p*10+"% fill ");
System.out.println();
System.out.println(g0+" were inserted first time");
System.out.println(g1+" were inserted after one clash");
System.out.println(g2+" were inserted after two clashes");
System.out.println(g3+" were inserted after three clashes");
System.out.println(g4+" were inserted after four clashes");
System.out.println(g5+" were inserted after five clashes");
System.out.println(g6+" were inserted after six clashes");
System.out.println(g7+" were inserted after seven clashes");
System.out.println(g8+" were inserted after eight clashes");
System.out.println(g9+" were inserted after nine clashes");
System.out.println(g10+" were inserted after ten clashes");
System.out.println();
p++;
}
r++; /**To be removed*/
}
}
}
//void Hash::makeEmpty(int &tableSize)
public void makeEmpty(int tableSize)
{
for(int i = 0; i < tableSize; i++)
{
hash.IntArray = 0;
hash.StrArray = " ";
}
}
//void Hash::drawMenu()
public void drawMenu()
{
System.out.println();
System.out.println("\t\t ___________________MENU__________________\n");
System.out.println("\t\t| |\n");
System.out.println("\t\t| Specify option(1 or 2) |\n");
System.out.println("\t\t| 1.Real Insertion |\n");
System.out.println("\t\t| 2.Virtual Insertion |\n");
System.out.println("\t\t| a. Linear Probing |\n");
System.out.println("\t\t| i. Hash Function |\n");
System.out.println("\t\t| ii.Table Size |\n");
System.out.println("\t\t| b. Quadratic Probing |\n");
System.out.println("\t\t| i. Hash Function |\n");
System.out.println("\t\t| ii.Table Size |\n");
System.out.println("\t\t| 3. Exit |\n");
System.out.println("\t\t|_________________________________________|\n");
}
public void readFile(String fname,String words,int tableSize)
throws Exception
{
//Initialising and declaring the array to read words into
//String temp [] = new String[2001];
String StrArray [] = new String[2001];
//Creating a file object
File word = new File("data.txt");
if(word.exists()&& word.canRead())
{
//Creating a buffered reader object and wrapping in it a Filereader object
BufferedReader reader = new BufferedReader(new FileReader(word));
//Reading in the words from the text file into the array
for(int n=0; n<2001; n++)
{
StrArray [n] = reader.readLine();
}
reader.close();
//To print the array with words from the text file
for(int i=0; i<2001; i++ )
{
System.out.println("Word at "+"["+i+"]"+" is " +StrArray);
}
}
}
}