Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hashing;
- import java.io.*;
- import java.util.Arrays;
- import java.util.Scanner;
- public class UniversalHashTable {
- private int[] hashArray;
- private boolean[] hashCollided;
- private PerfectHashingFunction hashingFunction;
- private File file;
- private int sizeSquared;
- private int rebuildsNumber;
- private int[] dataArray;
- public static void main(String[] args) {
- UniversalHashTable u = new UniversalHashTable("/Users/FNSY/Desktop/Matlab.txt");
- System.out.println(u.isThere(1221));
- System.out.println(u.isThere(1221));
- System.out.println(u.isThere(902));
- System.out.println(u.getRebuildsNumber());
- }
- public UniversalHashTable(String filePath) {
- try {
- initiallizingHashTable(filePath);
- } catch (FileNotFoundException e) {
- }
- rebuildsNumber = 0;
- createHashTable();
- }
- private PerfectHashingFunction getHachingFunction(int sizeSquared) {
- return new PerfectHashingFunction(sizeSquared);
- }
- private void initiallizingHashTable(String path) throws FileNotFoundException {
- PreHashingOperations preHashing = new PreHashingOperations(path);
- this.sizeSquared = (int) Math.pow(preHashing.getFileSize(), 2);
- this.file = preHashing.getFile();
- this.dataArray = preHashing.getFileArray();
- newChance(sizeSquared);
- }
- private void createHashingFunction(int sizeSquared) {
- hashingFunction = getHachingFunction(sizeSquared);
- }
- private void createHashArray(int sizeSquared) {
- hashArray = new int[sizeSquared];
- }
- private void createHashCollided(int sizeSquared) {
- hashCollided = new boolean[sizeSquared];
- Arrays.fill(hashCollided, false);
- }
- private void newChance(int sizeSquared) {
- createHashArray(sizeSquared);
- createHashCollided(sizeSquared);
- createHashingFunction(sizeSquared);
- }
- private void createHashTable() {
- int scannedFromFile, hashedValue;
- int index = 0;
- newChance(sizeSquared);
- try (Scanner scan = new Scanner(file)) {
- while (scan.hasNextLine()) {
- scannedFromFile = scan.nextInt();
- hashedValue = this.hashingFunction.hashFunction(scannedFromFile);
- if (!hashCollided[hashedValue]) {
- hashArray[hashedValue] = scannedFromFile;
- hashCollided[hashedValue] = true;
- } else {
- ++rebuildsNumber;
- createHashTable();
- break;
- }
- }
- } catch (Exception e) {
- }
- }
- public int getRebuildsNumber() {
- return this.rebuildsNumber;
- }
- public boolean isThere(int key) {
- int hashedValue = this.hashingFunction.hashFunction(key);
- if (hashCollided[hashedValue])
- return true;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement