Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. //Charles Pierse
  2. //11510667
  3.  
  4. package Lexical_Analyzer;
  5.  
  6.  
  7. import java.util.Arrays;
  8.  
  9. public class HashTable {
  10.  
  11. private int MAX_INTEGER = 512;
  12. private char MAX_CHARACTER = 4096;
  13. private int[] num_array = new int[MAX_INTEGER];
  14. private char[] char_array = new char[MAX_CHARACTER];
  15. private int counter1 = -1;
  16. private int counter2 = 0;
  17. private int prime_counter = 0;
  18. private int hash_code = 0 ;
  19.  
  20.  
  21.  
  22. public void array_fill() {
  23.  
  24.  
  25. Arrays.fill(num_array, -1);
  26. Arrays.fill(char_array, '~');
  27.  
  28. }
  29.  
  30. public int store_next_character(char nextChar, int h_slot){
  31. char_array[counter2] = nextChar;
  32.  
  33. int prime_Arr[] =
  34. {13001,13003,13007,13009,13033,13037,13043,13049,13063,13093,13099,
  35. 13103,13109,13121,13127,13147,13151,13159,13163,13171,13177,13183,13187,13217,13219};
  36. int ascii_num = nextChar;
  37. hash_code = ascii_num*prime_Arr[prime_counter];
  38.  
  39. prime_counter++;
  40. counter2++;
  41. return ((h_slot + hash_code)%MAX_INTEGER);
  42. }
  43.  
  44. public int determine_slot(int h_slot) {
  45. if (num_array[h_slot] == -1)
  46. {
  47.  
  48. num_array[h_slot] = counter1 +1;
  49. char_array[counter2 +1] ='~';
  50. counter1 = counter2;
  51. counter2 = counter2 +1;
  52. if (h_slot!=136){ //this ignores printing the id at start for the stoplexer
  53. System.out.println("<id," + h_slot+">");
  54. }
  55.  
  56.  
  57. }
  58. else if (match_check(num_array[h_slot], counter1+1) == true ){
  59.  
  60. counter2 = counter1 + 1;
  61.  
  62.  
  63. }
  64. else if (match_check(num_array[h_slot], counter1+1) == false){ //collision has occured,
  65. //find new hash slot
  66.  
  67. h_slot = h_slot + 1;
  68. if (h_slot >511){ //if we incremented the hash_slot and
  69. //it goes over the int array size reset it to 0
  70. h_slot = 0;
  71. }
  72. determine_slot(h_slot);
  73.  
  74.  
  75.  
  76.  
  77. }
  78. for ( int dupl_counter = counter2; char_array[dupl_counter] != '~'; dupl_counter++){
  79. //this cleans out duplicate id's in the char_array
  80. char_array[dupl_counter] = '~';
  81. }
  82. prime_counter= 0;
  83. return h_slot;
  84.  
  85. }
  86.  
  87.  
  88. public boolean match_check(int index1, int index2){
  89.  
  90. int temp_pos = 0;
  91.  
  92. while(char_array[index1+ temp_pos]!='~' && char_array[index2 +temp_pos] != '~'){
  93.  
  94. if (char_array[index1+temp_pos] != char_array[index2 + temp_pos]){
  95. return false;
  96. }
  97. temp_pos = temp_pos + 1;
  98. }
  99.  
  100. if(char_array[index1 + temp_pos]!='~'||char_array[index2+temp_pos] != '~'){
  101. return false;
  102. }
  103.  
  104. else{
  105.  
  106. return true;
  107.  
  108. }
  109.  
  110. }
  111. // public static void main(String[] args){ // this was my testing stub for this class
  112. //
  113. // HashTable table = new HashTable();
  114. // table.array_fill();
  115. //
  116. // String tester = "bog~bogota~bog~";
  117. //
  118. // for(Character c: tester.toCharArray()) {
  119. //
  120. // if ((c>='A') && (c<='Z') || (c>='a') && (c<='z')){
  121. // table.store_next_character(c);
  122. // }
  123. //
  124. // else {
  125. //
  126. // table.determine_slot();
  127. //
  128. // }
  129. //
  130. // System.out.println(new String(table.char_array).substring(0, 200));
  131. // }
  132. // }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement