-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticSet.java
More file actions
207 lines (171 loc) · 4.11 KB
/
StaticSet.java
File metadata and controls
207 lines (171 loc) · 4.11 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
public class StaticSet<E> implements Set<E> {
// current number of elements in set
private int currentSize;
// array of elements
private E elements[];
private static final int DEFAULT_SET_SIZE = 10;
public StaticSet(int maxCapacity) {
if (maxCapacity < 1)
throw new IllegalArgumentException("Max capacity must be at least 1");
this.currentSize = 0;
this.elements = (E[]) new Object[maxCapacity];
}
private class SetIterator<T> implements Iterator<T> {
private int currentPosition;
public SetIterator() {
this.currentPosition = 0;
}
@Override
public boolean hasNext() {
return this.currentPosition < size();
}
@Override
public T next() {
if (this.hasNext()) {
T result = (T) elements[this.currentPosition++];
return result;
}
else
throw new NoSuchElementException();
}
}
@Override
public boolean add(E obj) {
if (this.isMember(obj))
return false;
else {
if (this.size() == this.elements.length)
throw new IllegalStateException("The set is full.");
else {
this.elements[this.currentSize++] = obj;
return true;
}
}
}
public boolean isMember(E obj) {
for (int i = 0; i < this.size(); i++)
if (this.elements[i].equals(obj))
return true;
return false;
}
public boolean remove(E obj) {
for (int i = 0; i < this.size(); i++)
if (this.elements[i].equals(obj))
{
this.elements[i] = this.elements[--this.currentSize];
this.elements[this.currentSize] = null;
return true;
}
return false;
}
@Override
public boolean isEmpty() {
return this.size() == 0;
}
@Override
public int size() {
return this.currentSize;
}
@Override
public void clear() {
for (int i = 0; i < this.size(); i++)
this.elements[i] = null;
this.currentSize = 0;
}
public Set<E> union(Set<E> S2) {
Set<E> S3 = new StaticSet<E>(DEFAULT_SET_SIZE);
// Copy S1
for (Object obj : this)
S3.add((E) obj);
// Copy elements of S2 not already in S1
for (Object obj : S2)
{
E e = (E) obj;
if (!((StaticSet<E>) S3).isMember(e))
S3.add(e);
}
return S3;
}
public Set<E> difference(Set<E> S2) {
Set<E> S3 = new StaticSet<E>(DEFAULT_SET_SIZE);
for (Object obj : this)
{
E e = (E) obj;
if (!((StaticSet<E>) S2).isMember(e))
S3.add(e);
}
return S3;
}
public Set<E> intersection(Set<E> S2) {
return this.difference(this.difference(S2));
}
public boolean isSubSet(Set<E> S2) {
for (Object obj : this)
if (!((StaticSet<E>) S2).isMember((E) obj))
return false;
return true;
}
public boolean equals(Set<E> S2) {
if(this.difference(S2).isEmpty()) {
return true;
}
else return false;
}
public Set<Set<E>> singletonSets(){
Set<Set<E>> singleton = new StaticSet<Set<E>>(this.size());
for(E obj : this) {
Set<E> single = new StaticSet<E>(this.size());
single.add(obj);
singleton.add(single);
}
return singleton;
}
@Override
public Iterator<E> iterator() {
return new SetIterator<E>();
}
@Override
public boolean contains(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> T[] toArray(T[] a) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean remove(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(Collection<? extends E> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
}