-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashStringSet.java
111 lines (101 loc) · 2.97 KB
/
HashStringSet.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
110
111
import java.util.ArrayList;
import java.util.LinkedList;
/**
* This class represents a hash set of string.
* It provides methods for adding strings and retrieving stats about the set
*/
public class HashStringSet {
// private LinkedList<String>[] buckets;
private ArrayList<LinkedList<String>> buckets;
private int size;
/**
* Constructs a new HashStringSet with the specified number of buckets
*
* @param numBuckets The number of buckets
*/
public HashStringSet(int numBuckets) {
if (numBuckets <= 0) {
throw new IllegalArgumentException("Number of buckets must be positive");
}
// buckets = new LinkedList[numBuckets];
// for (int i = 0; i < numBuckets; i++) {
// buckets[i] = new LinkedList<>();
// }
buckets = new ArrayList<>(numBuckets);
for (int i = 0; i < numBuckets; i++) {
buckets.add(new LinkedList<>());
}
size = 0;
}
/**
* Adds a string to the set.
* If the string is already present, this method has no effect
*
* @param str The string to add
*/
public void add(String str) {
if (str == null) {
throw new IllegalArgumentException("Null string found. Cannot add null to the set.");
}
int bucketIndex = Math.abs(str.hashCode()) % buckets.size();
if (!buckets.get(bucketIndex).contains(str)) {
buckets.get(bucketIndex).add(str);
size++;
}
}
/**
* Returns the number of strings in the set
*
* @return The number of strings in the set
*/
public int size() {
return size;
}
/**
* Returns the number of buckets in the set
*
* @return The number of buckets in the set
*/
public int numBuckets() {
return buckets.size();
}
/**
* Returns the number of used buckets in the set
*
* @return The number of used buckets in the set
*/
public int numUsedBuckets() {
int count = 0;
for (LinkedList<String> bucket : buckets) {
if (!bucket.isEmpty()) {
count++;
}
}
return count;
}
/**
* Returns the load factor of the set; defined as:
* number of strings / number of buckets
*
* @return The load factor of the set
*/
public double loadFactor() {
return (double) size / buckets.size();
}
/**
* Returns the memory efficiency factor of the set; defined as:
* number of bytes used / total number of bytes used by the set
*
* @return
*/
public double memoryEfficiencyFactor() {
int totalBytesValues = 0;
int totalBytesObjects = 4 * size;
for (LinkedList<String> bucket : buckets) {
for (String str : bucket) {
totalBytesValues += str.length();
}
}
return (double) totalBytesValues / (totalBytesValues + totalBytesObjects);
}
}