-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyHashTable.java
More file actions
45 lines (40 loc) · 1.12 KB
/
MyHashTable.java
File metadata and controls
45 lines (40 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.*;
import java.io.*;
public class MyHashTable{
// My hashtable is an array of linked lists of word entries
MyLinkedList<WordEntry>[] hashtable = new MyLinkedList[26];
public MyHashTable(){
for(int j=0;j<26;j++){
hashtable[j] = new MyLinkedList<WordEntry>();
}
}
public int getHashIndex(String str){
int fuckthisshit =0;
for(int f =0;f<str.length();f++){
fuckthisshit = fuckthisshit + str.charAt(f);
}
fuckthisshit = fuckthisshit%26;
return fuckthisshit;
}
public void addPositionsForWord(WordEntry w){
try{
int hash = getHashIndex(w.str);
//System.out.println(hash);
//System.out.println(hashtable[hash].list1.size());
for(int i=0;i<hashtable[hash].list1.size();i++){
// System.out.println("ek or for loop");
WordEntry w1=hashtable[hash].list1.get(i);
if(w1.str.equals(w.str)){
hashtable[hash].list1.get(i).addPositions(w.wordPosition);
return;
}
}
WordEntry pe = new WordEntry(w.str);
pe.addPositions(w.wordPosition);
hashtable[hash].list1.add(pe);
}
catch(Exception e){
e.printStackTrace();
}
}
}