-
Notifications
You must be signed in to change notification settings - Fork 0
/
BucketHashSet.java
164 lines (131 loc) · 3.15 KB
/
BucketHashSet.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package ch.hslu.AD.SW04.HashTable;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
public class BucketHashSet<T> implements Set<T> {
private static int DEFAULT_ARRAY_SIZE = 42;
private Node[] items;
private int size = 0;
public BucketHashSet() {
this(DEFAULT_ARRAY_SIZE);
}
public BucketHashSet(int size) {
this.initialize(size);
}
@SuppressWarnings("unchecked")
final public void initialize(int size) {
this.items = Node[].class.cast(Array.newInstance(Node.class, size));
this.size = 0;
}
final public void clear() {
this.initialize(items.length);
}
final public int size() {
return size;
}
final public boolean add(T item) {
int index = item.hashCode() % items.length;
Node node = items[index];
if(containsInBucket(node, item)) {
return true;
}
Node newNode = new Node(item);
if(node != null) {
newNode.link(node);
}
items[index] = newNode;
size++;
return true;
}
final public boolean contains(Object item) {
int index = item.hashCode() % items.length;
Node node = items[index];
return containsInBucket(node, item);
}
final public boolean containsInBucket(Node head, Object item) {
Node node = head;
while(node != null) {
if(node.getElement().equals(item)) {
return true;
}
node = node.next();
}
return false;
}
final public boolean remove(Object item) {
int index = item.hashCode() % items.length;
Node prevNode = null;
Node node = items[index];
while(node != null) {
if(node.getElement().equals(item)) {
if(prevNode == null) { // if item to remove is the first in the bucket.
items[index] = node.next();
} else {
prevNode.link(node.next());
}
size--;
return true;
}
prevNode = node;
node = node.next();
}
return false;
}
final private class Node {
private T element;
private Node next;
public Node(T element) {
this.element = element;
}
public T getElement() {
return element;
}
public void link(Node next) {
this.next = next;
}
public Node next() {
return next;
}
}
@Override
public boolean addAll(Collection<? extends T> c) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException();
}
@Override
public boolean containsAll(Collection<?> c) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException();
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException();
}
@Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> c) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> c) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException();
}
@Override
public Object[] toArray() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException();
}
@Override
public <T> T[] toArray(T[] a) {
// TODO Auto-generated method stub
return null;
}
}