Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Charles Pierse
- //11510667
- package Lexical_Analyzer;
- import java.util.Arrays;
- public class HashTable {
- private int MAX_INTEGER = 512;
- private char MAX_CHARACTER = 4096;
- private int[] num_array = new int[MAX_INTEGER];
- private char[] char_array = new char[MAX_CHARACTER];
- private int counter1 = -1;
- private int counter2 = 0;
- private int prime_counter = 0;
- private int hash_code = 0 ;
- public void array_fill() {
- Arrays.fill(num_array, -1);
- Arrays.fill(char_array, '~');
- }
- public int store_next_character(char nextChar, int h_slot){
- char_array[counter2] = nextChar;
- int prime_Arr[] =
- {13001,13003,13007,13009,13033,13037,13043,13049,13063,13093,13099,
- 13103,13109,13121,13127,13147,13151,13159,13163,13171,13177,13183,13187,13217,13219};
- int ascii_num = nextChar;
- hash_code = ascii_num*prime_Arr[prime_counter];
- prime_counter++;
- counter2++;
- return ((h_slot + hash_code)%MAX_INTEGER);
- }
- public int determine_slot(int h_slot) {
- if (num_array[h_slot] == -1)
- {
- num_array[h_slot] = counter1 +1;
- char_array[counter2 +1] ='~';
- counter1 = counter2;
- counter2 = counter2 +1;
- if (h_slot!=136){ //this ignores printing the id at start for the stoplexer
- System.out.println("<id," + h_slot+">");
- }
- }
- else if (match_check(num_array[h_slot], counter1+1) == true ){
- counter2 = counter1 + 1;
- }
- else if (match_check(num_array[h_slot], counter1+1) == false){ //collision has occured,
- //find new hash slot
- h_slot = h_slot + 1;
- if (h_slot >511){ //if we incremented the hash_slot and
- //it goes over the int array size reset it to 0
- h_slot = 0;
- }
- determine_slot(h_slot);
- }
- for ( int dupl_counter = counter2; char_array[dupl_counter] != '~'; dupl_counter++){
- //this cleans out duplicate id's in the char_array
- char_array[dupl_counter] = '~';
- }
- prime_counter= 0;
- return h_slot;
- }
- public boolean match_check(int index1, int index2){
- int temp_pos = 0;
- while(char_array[index1+ temp_pos]!='~' && char_array[index2 +temp_pos] != '~'){
- if (char_array[index1+temp_pos] != char_array[index2 + temp_pos]){
- return false;
- }
- temp_pos = temp_pos + 1;
- }
- if(char_array[index1 + temp_pos]!='~'||char_array[index2+temp_pos] != '~'){
- return false;
- }
- else{
- return true;
- }
- }
- // public static void main(String[] args){ // this was my testing stub for this class
- //
- // HashTable table = new HashTable();
- // table.array_fill();
- //
- // String tester = "bog~bogota~bog~";
- //
- // for(Character c: tester.toCharArray()) {
- //
- // if ((c>='A') && (c<='Z') || (c>='a') && (c<='z')){
- // table.store_next_character(c);
- // }
- //
- // else {
- //
- // table.determine_slot();
- //
- // }
- //
- // System.out.println(new String(table.char_array).substring(0, 200));
- // }
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement