-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrHashTable.java
More file actions
117 lines (101 loc) · 2.87 KB
/
StrHashTable.java
File metadata and controls
117 lines (101 loc) · 2.87 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
public class StrHashTable {
Node[] strHashTable;
int size;
double LOAD_FACTOR_MAX = 0.75;
public StrHashTable(int s){
size = s;
strHashTable = new Node[size];
}
/*
* Inserts a key/value pair into the hash table.
*/
public void insert(String k, String v){
int index = hashFunction(k);
strHashTable[index] = new Node(k,v);
size++;
checkForRehash();
}
/*
* Deletes a key/value pair from the hash table, from given key.
*/
public void delete(String k){
int index = hashFunction(k);
if(strHashTable[index] != null && strHashTable[index].key.equals(k)){
strHashTable[index] = null;
size--;
}
}
/*
* Calculates the hash code for a given string using
* the folding with string method
*/
public int hashFunction(String k){
int hash = 0;
for(int i = 0; i < k.length(); i++){
hash += k.charAt(i);
}
return hash % strHashTable.length;
}
public void rehash(){
size = size * 2;
Node[] newHashTable = new Node[size]; //Creates a new hash table with new size
for(Node node : strHashTable){
if(node != null){
int newIndex = hashFunction(node.key);
newHashTable[newIndex] = node;
}
}
strHashTable = newHashTable; // Update the hash table to the new version
}
/*
* Checks if rehashing is needed
* If the load factor has exceeded, rehashing will begin
*/
public void checkForRehash(){
double loadFactor = (double) size / strHashTable.length;
if(loadFactor > LOAD_FACTOR_MAX){
rehash();
}
}
/*
* Checks for key in the 420 table.
*/
public boolean contains(String k){
int index = hashFunction(k);
return strHashTable[index] != null && strHashTable[index].key.equals(k);
}
/*
* Returns the item in the hash table,
* given the key k
*/
public String get(String k){
int index = hashFunction(k); //get the index of the key
if(strHashTable[index] != null && strHashTable[index].key.equals(k)){
return strHashTable[index].value;
}
return null;
}
/*
* Checks if the hash table is empty
*/
public boolean isEmpty(){
return size == 0;
}
/*
* Gets the number of items stored in the hash table
*/
public int size(){
return size;
}
/*
* Prints the entire hash table
* format: index: key, value
*/
public void dump(){
for(int i = 0; i < strHashTable.length; i++){
if(strHashTable[i] != null){
System.out.println(i + ": " + strHashTable[i].key + ", " + strHashTable[i].value);
}
}
}
}