-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResizableArrayBag.java
More file actions
200 lines (164 loc) · 6.3 KB
/
ResizableArrayBag.java
File metadata and controls
200 lines (164 loc) · 6.3 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
package bagPrograms;
import java.util.Arrays;
public class ResizableArrayBag<T> implements BagInterface<T> {
private T[] bag;
private int numberOfEntries;
private boolean integrityOK = false;
private static final int DEFAULT_CAPACITY = 25;
private static final int MAX_CAPACITY = 10000;
public ResizableArrayBag() {
this(DEFAULT_CAPACITY);
}
public ResizableArrayBag(int initialCapacity) {
checkCapacity(initialCapacity);
@SuppressWarnings("unchecked")
T[] tempBag = (T[]) new Object[initialCapacity];
bag = tempBag;
numberOfEntries = 0;
integrityOK = true;
}
public ResizableArrayBag(T[] contents) {
checkCapacity(contents.length);
bag = Arrays.copyOf(contents, contents.length);
numberOfEntries = contents.length;
integrityOK = true;
}
public boolean add(T newEntry) {
checkintegrity();
if (isArrayFull()) {
doubleCapacity();
}
bag[numberOfEntries] = newEntry;
numberOfEntries++;
return true;
}
public T[] toArray() {
checkintegrity();
@SuppressWarnings("unchecked")
T[] result = (T[]) new Object[numberOfEntries];
for (int index = 0; index < numberOfEntries; index++) {
result[index] = bag[index];
}
return result;
}
public boolean isEmpty() {
return numberOfEntries == 0;
}
public int getCurrentSize() {
return numberOfEntries;
}
public int getFrequencyOf(T anEntry) {
checkintegrity();
int counter = 0;
for (int index = 0; index < numberOfEntries; index++) {
if (anEntry.equals(bag[index])) {
counter++;
}
}
return counter;
}
public boolean contains(T anEntry) {
checkintegrity();
return getIndexOf(anEntry) > -1;
}
public void clear() {
while (!isEmpty())
remove();
}
public T remove() {
checkintegrity();
T result = removeEntry(numberOfEntries - 1);
return result;
}
public boolean remove(T anEntry) {
checkintegrity();
int index = getIndexOf(anEntry);
T result = removeEntry(index);
return anEntry.equals(result);
}
private int getIndexOf(T anEntry) {
int where = -1;
boolean found = false;
int index = 0;
while (!found && (index < numberOfEntries)) {
if (anEntry.equals(bag[index])) {
found = true;
where = index;
}
index++;
}
return where;
}
private T removeEntry(int givenIndex) {
T result = null;
if (!isEmpty() && (givenIndex >= 0)) {
result = bag[givenIndex];
int lastIndex = numberOfEntries - 1;
bag[givenIndex] = bag[lastIndex];
bag[lastIndex] = null;
numberOfEntries--;
}
return result;
}
private boolean isArrayFull() {
return numberOfEntries >= bag.length;
}
private void doubleCapacity() {
int newLength = 2 * bag.length;
checkCapacity(newLength);
bag = Arrays.copyOf(bag, newLength);
}
private void checkCapacity(int capacity) {
if (capacity > MAX_CAPACITY)
throw new IllegalStateException(
"Attempt to create a bag whose capacity exceeds " + "allowed maximum of " + MAX_CAPACITY);
}
private void checkintegrity() {
if (!integrityOK)
throw new SecurityException("ArrayBag object is corrupt.");
}
// Union method
public BagInterface<T> union(BagInterface<T> inputBag) { // Gets bag as input and stores it as inputBag
T[] thisBagArray = this.toArray(); // Copies contents of the bag in this class into an array
T[] inputBagArray = inputBag.toArray(); // Copies the content of inputBag into an array
int totalLength = (this.getCurrentSize() + (inputBag.getCurrentSize())); // Total length of both arrays combined
@SuppressWarnings("unchecked")
T[] outputArray = (T[]) new Object[totalLength]; // Creates new array with size of total length
for (int i = 0; i < this.getCurrentSize(); i++) {
outputArray[i] = thisBagArray[i]; // Copies everything from thisBagArray into output array
}
for (int i = this.getCurrentSize(); i < totalLength; i++) {
outputArray[i] = inputBagArray[i - this.getCurrentSize()]; // Copies everything from inputBagArray into outputArray
}
BagInterface<T> outputBag = new ResizableArrayBag<>(outputArray); // Makes new Array Bag with the outputArray
return outputBag; // Returns outputBag
}
// Intersection method
public BagInterface<T> intersection(BagInterface<T> inputBag) { // Gets bag as input and stores it as inputBag
T[] inputBagArray = inputBag.toArray(); // Copies content of inputBag into inputBagArray
BagInterface<T> outputBag = new ResizableArrayBag<>(); // Creates new ResizableArrayBag named outputBag
for (int i = 0; i < inputBagArray.length; i++) {
// For loop goes for as long as inputBagArray is. The reason we don't need to do
// another for loop for this.ResizableBagArray length is because we only need to use one bag to compare to the other and see
// where the intersection is.
if(this.contains(inputBagArray[i]) && !outputBag.contains(inputBagArray[i])) {
// The if statement checks to make sure that inputBagArray[i] is contained in this.Bag, and is also not already in the output bag
int timesToInput = inputBag.getFrequencyOf(inputBagArray[i]); // How many times inputBagArray[i] will be put into output bag
int frequencyOfThis = this.getFrequencyOf(inputBagArray[i]); // Used to compare with timesToInput
// The if statement below works by setting timesToInput to the frequency inputBagArray[i] is found in inputBag, however, in a intersection
// an object that is found in different quantities in 2 different bags/arrays/etc... will always be put into the output with the lesser value
// since anything that isn't matched is discarded.
// Ex: bag1 has a,b,b,c,c and bag2 has a,b,d. The intersected string is b, but the output will only have 1 b in it since bag 2 only has 1 b
// The if statement determines the maximum amount of times the object is added to the outputBag by finding the lowest frequency the object
// is found in either bags. Think of the lower amount as a bottleneck to how many times the object is added to outputBag
if (timesToInput > frequencyOfThis) {
timesToInput = frequencyOfThis;
}
for (int j = 0; j < timesToInput; j++) {
outputBag.add(inputBagArray[i]); // Copies inputBagArray[i] into outputBag the amount of times needed.
}
}
}
return outputBag; // Returns outputBag
}
}