-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrderedVector.java
292 lines (172 loc) · 5.22 KB
/
OrderedVector.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// PROGRAMMER: Jonathan Ty
// ACCOUNT: masc0208
// RED ID: 812942657
// DATE MODIFIED: July 28, 2015
package data_structures;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class OrderedVector<E> implements OrderedListADT<E> {
private E[] storage;
private int arraySize, currentSize;
public OrderedVector() {
arraySize = DEFAULT_MAX_CAPACITY; // default = [100]
currentSize = 0;
storage = (E[]) new Object[arraySize];
}
// Adds the Object obj to the list in the correct position as determined by
// the Comparable interface.
public void insert(E obj) {
if (currentSize == arraySize) {
arrayIncrease();
}
int index = FIP(obj, 0, currentSize - 1);
for (int i = currentSize - 1; i >= index; i--) {
storage[i + 1] = storage[i];
}
storage[index] = obj;
currentSize++;
}
// Removes and returns the object located at the parameter index position
// (zero based).
// Throws IndexOutOfBoundsException if the index does not map to a valid
// position within the list.
public E remove(int index) {
if(currentSize < arraySize/4)
arrayDecrease();
if(index < 0 || index > currentSize-1)
throw new IndexOutOfBoundsException();
E temp = storage[index];
for(int i =index; i < currentSize-1; i++)
storage[i] = storage[i+1];
currentSize--;
return temp;
}
// Removes and returns the parameter object obj from the list if the list
// contains it, null otherwise.
public E remove(E obj) {
if (isEmpty())
return null;
if(find(obj)==-1)
return null;
return remove(find(obj));
}
// Removes and returns the smallest element in the list and null if the it
// is empty.
public E removeMin() {
if(isEmpty())
return null;
return remove(0);
}
// Removes and returns the largest element in the list and null if the it is
// empty.
public E removeMax() {
if(isEmpty())
return null;
return remove(currentSize-1);
}
// Returns the parameter object located at the parameter index position
// (zero based).
// Throws IndexOutOfBoundsException if the index does not map to a valid
// position within the underlying array
public E get(int index) {
if (index < 0 || index > currentSize-1)
throw new IndexOutOfBoundsException();
return storage[index];
}
// Returns the list object that matches the parameter, and null if the list
// is empty.
// This method is stable, if obj matches more than one element, the element
// that
// has been in the list longest is returned.
public E get(E obj) {
int result = binSearch(obj,0,currentSize-1);
if(result==-1)
return null;
return storage[result];
}
// Returns the index of the first element that matches the parameter obj
// and -1 if the item is not in the list.
public int find(E obj) {
int index = binSearch(obj, 0, currentSize-1);
return index;
}
// Returns true if the parameter object obj is in the list, false otherwise.
public boolean contains(E obj) {
if (binSearch(obj, 0, currentSize-1) == -1)
return false;
return true;
}
// The list is returned to an empty state.
public void clear() {
currentSize = 0; // empty array
}
// Returns true if the list is empty, otherwise false
public boolean isEmpty() {
if (currentSize == 0) {
return true;
}
return false;
}
// Returns the number of Objects currently in the list.
public int size() {
return currentSize;
}
// Returns an Iterator of the values in the list, presented in
// the same order as the list.
public Iterator<E> iterator() {
return new IteratorHelper();
}
public class IteratorHelper<E> implements Iterator<E> {
int iterIndex; // pg.88 lecture notes
public IteratorHelper() {
iterIndex = 0;
}
public boolean hasNext() { // is there another item in the list?
return iterIndex < currentSize;
}
public E next() { // go to next item in list and return it.
if (!hasNext())
throw new NoSuchElementException();
return (E) storage[iterIndex++];
}
public void remove() { // not needed.just returns error.
throw new UnsupportedOperationException();
}
}
private void arrayIncrease() {
arraySize *= 2; // mult by 2
E[] temp = (E[]) new Object[arraySize];
for (int i = 0; i < currentSize; i++) {
temp[i] = storage[i];
}
storage = temp;
}
private void arrayDecrease() {
arraySize = (arraySize / 2); // div by 2
E[] temp = (E[]) new Object[arraySize];
for (int i = 0; i < currentSize; i++) {
temp[i] = storage[i];
}
storage = temp;
}
private int binSearch(E obj, int lo, int hi) {
if (hi<lo) {
if(lo > currentSize-1) return -1;
if ((((Comparable<E>) obj).compareTo(storage[lo]))==0)
return lo;
return -1;
}
int mid = (lo+hi)>>1;
if (((Comparable<E>) obj).compareTo(storage[mid]) <= 0)
return binSearch(obj, lo, mid - 1); // go left
return binSearch(obj, mid + 1, hi); // go right
}
private int FIP(E obj, int lo, int hi) {
if (hi < lo)
return lo;
int mid = (lo + hi) / 2;
if (((Comparable<E>) obj).compareTo(storage[mid]) < 0)
return FIP(obj, lo, mid - 1);
return FIP(obj, mid + 1, hi);
}
} // end class