-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hash.java
116 lines (86 loc) · 2.79 KB
/
Hash.java
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
import java.io.*;
import java.util.ArrayList;
public class Hash {
private int M;
private static final int R = 31; // small integer to multiply by, ensures that all bits are used
private String[] hashArray;
public Hash(int size) {
hashArray = new String[size];// constructor allows for custom setting of array
M = size;
}
public int hash(String s) { //from textbook
int hash = 0;
for (int i = 0; i < s.length(); i++)
hash = (R * hash + s.charAt(i)) % M;
return hash;
}
private void rehash() {
String[] expandedArray = new String[M * 2]; // creates new array
//System.out.println("Load factor above 50 percent, rehashing...");
for (int i = 0; i < M; i++) {
if (hashArray[i] != null) {
expandedArray[i] = hashArray[i];
}
}
hashArray = expandedArray;
M = M * 2;
}
public void insert(String s) {
int index = hash(s);
if (this.getLoadFactor() >= 0.5) { //checks load factor and rehashes
this.rehash();
index = hash(s); //calls hash function on updated size
}
while (hashArray[index] != null && index < M) {
index++;
}
hashArray[index] = s;
}
public int getCapacity() { //getter method
return M;
}
public int getUniqueItems() {
int itemCount = 0;
for (int i = 0; i < M; i++) {
if (hashArray[i] != null) {
itemCount++;
}
}
return itemCount;
}
public float getLoadFactor() {
return ((float) this.getUniqueItems() / M); //cast because they're both integers
}
public void print() {
int index = 0;
while (index < M) {
if (hashArray[index] != null) {
System.out.println(hashArray[index]);
index++;
} else index++;
}
}
public static void main(String[] args) throws IOException {
if (0 < args.length) {
File in = new File(args[0]);
Integer hashSize = Integer.parseInt(args[1]);
BufferedReader br = new BufferedReader(new FileReader(in));
ArrayList<String> poemLines = new ArrayList<String>();
String line = br.readLine();
while (line != null) {
poemLines.add(line);
line = br.readLine();
}
Hash poem = new Hash(hashSize);
for (String l : poemLines) {
poem.insert(l);
}
for (String l : poemLines) {
poem.insert(l);
}
poem.print();
} else {
System.out.println("Cannot find arguments. Please try again");
}
}
}