Advertisement
FNSY

Untitled

May 10th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. package hashing;
  2.  
  3. import java.io.*;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6.  
  7. public class UniversalHashTable {
  8. private int[] hashArray;
  9. private boolean[] hashCollided;
  10. private PerfectHashingFunction hashingFunction;
  11. private File file;
  12. private int sizeSquared;
  13. private int rebuildsNumber;
  14. private int[] dataArray;
  15.  
  16. public static void main(String[] args) {
  17. UniversalHashTable u = new UniversalHashTable("/Users/FNSY/Desktop/Matlab.txt");
  18. System.out.println(u.isThere(1221));
  19. System.out.println(u.isThere(1221));
  20. System.out.println(u.isThere(902));
  21. System.out.println(u.getRebuildsNumber());
  22.  
  23. }
  24.  
  25. public UniversalHashTable(String filePath) {
  26. try {
  27. initiallizingHashTable(filePath);
  28. } catch (FileNotFoundException e) {
  29.  
  30. }
  31. rebuildsNumber = 0;
  32. createHashTable();
  33. }
  34.  
  35. private PerfectHashingFunction getHachingFunction(int sizeSquared) {
  36. return new PerfectHashingFunction(sizeSquared);
  37. }
  38.  
  39. private void initiallizingHashTable(String path) throws FileNotFoundException {
  40. PreHashingOperations preHashing = new PreHashingOperations(path);
  41. this.sizeSquared = (int) Math.pow(preHashing.getFileSize(), 2);
  42. this.file = preHashing.getFile();
  43. this.dataArray = preHashing.getFileArray();
  44. newChance(sizeSquared);
  45. }
  46.  
  47. private void createHashingFunction(int sizeSquared) {
  48. hashingFunction = getHachingFunction(sizeSquared);
  49. }
  50.  
  51. private void createHashArray(int sizeSquared) {
  52. hashArray = new int[sizeSquared];
  53. }
  54.  
  55. private void createHashCollided(int sizeSquared) {
  56. hashCollided = new boolean[sizeSquared];
  57. Arrays.fill(hashCollided, false);
  58. }
  59.  
  60. private void newChance(int sizeSquared) {
  61. createHashArray(sizeSquared);
  62. createHashCollided(sizeSquared);
  63. createHashingFunction(sizeSquared);
  64. }
  65.  
  66. private void createHashTable() {
  67. int scannedFromFile, hashedValue;
  68. int index = 0;
  69. newChance(sizeSquared);
  70. try (Scanner scan = new Scanner(file)) {
  71. while (scan.hasNextLine()) {
  72. scannedFromFile = scan.nextInt();
  73. hashedValue = this.hashingFunction.hashFunction(scannedFromFile);
  74. if (!hashCollided[hashedValue]) {
  75. hashArray[hashedValue] = scannedFromFile;
  76. hashCollided[hashedValue] = true;
  77. } else {
  78. ++rebuildsNumber;
  79. createHashTable();
  80. break;
  81. }
  82. }
  83. } catch (Exception e) {
  84.  
  85. }
  86. }
  87.  
  88. public int getRebuildsNumber() {
  89. return this.rebuildsNumber;
  90. }
  91.  
  92. public boolean isThere(int key) {
  93. int hashedValue = this.hashingFunction.hashFunction(key);
  94. if (hashCollided[hashedValue])
  95. return true;
  96. return false;
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement