-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.h
More file actions
532 lines (460 loc) · 14.6 KB
/
Copy pathArrayList.h
File metadata and controls
532 lines (460 loc) · 14.6 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: ArrayList.h
* Author: Josh
*
* Created on February 25, 2018, 10:32 PM
*/
#ifndef ARRAYLIST_H
#define ARRAYLIST_H
using namespace std;
template <class T>
class ArrayList{
private:
/**
* The initial capacity of the ArrayList.
*/
constexpr static int CAPACITY_INITIAL = 3;
/**
* The increment to use for each increment of the ArrayList.
*/
constexpr static int CAPACITY_INCREMENT = 3;
/**
* The number of spots used in the ArrayList.
*/
int used;
/**
* The capacity that the array holds.
*/
int capacity;
/**
* The manual array that is being managed.
*/
T* array;
/**
* Resize the array in order to add more or to have capacity.
*/
void resize();
/**
* Resize the array with a size specification.
*
* @param newSize the size specification.
* @return a boolean value concerning if the resize worked.
*/
bool resize(int newSize);
public:
/**
* The default constructor for the ArrayList to build the array.
*/
ArrayList();
/**
* The destructor for the ArrayList to take down the array.
*/
~ArrayList();
/**
* The copy constructor to manage the copying of an ArrayList.
*
* @param that the ArrayList instance by reference and constant to
* be copied.
*/
ArrayList(const ArrayList& that);
/**
* The operator overload used to override the = operator in order to
* set the data of that to this.
*
* @param that the instance to set the data of for this instance
* @return this class by reference for chaining and other purposes
*/
ArrayList<T>& operator=(const ArrayList& that);
/**
* The operator overload in order to access a part of the ArrayList by
* using the [] operator.
*
* @param index the index to find
* @return the object of type T that is found at the spot index
*/
T& operator[](int index);
/**
* This function retrieves the object at index.
*
* @param index the index to retrieve from
*/
T get(int index);
/**
* This function adds the object toAdd to the end of the list.
*
* @param toAdd the object to add to the end of the list.
*/
void add(T toAdd);
/**
* This function adds the object toAdd at the index index.
*
* @param index the index to add it to
* @param toAdd the object to add
* @return bool if it was valid and worked
*/
bool add(int index,T toAdd);
/**
* This function sets the object toAdd at the index index and deletes
* the object that was previously there.
*
* @param index the index to set the object at
* @param toSet the object to set
* @return bool if it was valid and worked
*/
bool set(int index,T toSet);
/**
* This function removes the object of type T at the spot index.
*
* @param index the index to remove the object from
* @return a copy of the object that was removed
*/
T remove(int index);
/**
* This function removes a range of objects of type T from the array
* inclusively.
*
* @param start the start point to remove from
* @param end the end point to remove from, inclusively
* @return an ArrayList object of the objects that were removed from this
* instance of ArrayList of type T
*/
ArrayList<T> removeRange(int start,int end);
/**
* This function removes one copy of the T toRemove.
*
* @param toRemove the instance of T to remove
* @return if the remove was followed through and removed an object
*/
bool removeOne(T toRemove);
/**
* This function removes all copies found of the T toRemove.
*
* @param toRemove the T to remove all copies of
* @return if a copy of toRemove was removed
*/
bool removeAllOf(T toRemove);
/**
* This funciton replaces only one of the toReplace T objects with the
* replacement T object.
*
* @param toReplace the T object to replace
* @param replacement the T object to replace toReplace with
* @return if a copy of toReplace was replaced
*/
bool replaceOne(T toReplace,T replacement);
/**
* This function replaces all of the copies of toReplace with replacement.
*
* @param toReplace the copy of T to replace whenever found
* @param replacement the replacement to replace toReplace with
* @return a boolean value concerning if a copy of toReplace was found
* and then replaced, at least one will turn it to true
*/
bool replaceAllOf(T toReplace,T replacement);
/**
* This function returns a sub-ArrayList that contains all of the T
* objects from start to end, inclusively.
*
* @param start the start of the index of the new sub-ArrayList
* @param end the end of the index of the new sub-ArrayList
* @return the sub-ArrayList from start to end, inclusively
*/
ArrayList<T> getSubArrayList(int start,int end);
/**
* This function removes all objects from the ArrayList.
*/
void clear();
/**
* This function returns the size of the ArrayList, in how many objects
* are held in this ArrayList.
* @return the number of objects that are held in this ArrayList
*/
int size();
/**
* This function adds to the capacity of the ArrayList.
*
* PRECONDITION:: toAdd does not put the size of the ArrayList out of
* bounds of the integer bounds.
* @param toAdd the number of spots to add
*/
void addCapacity(unsigned int toAdd);
/**
* This function finds the index of the T object toFind and returns
* the index.
* @param toFind the object instance of T to find
* @return the index of the object instance of T that was found
*/
int indexOf(T toFind);
/**
* This function trims the size of the array, whether there are objects
* in the way or not, to the new size of newSize.
*
* @param newSize the new size to trim to
*/
void trim(int newSize);
};
template <class T>
void ArrayList<T>::resize(){
this->capacity += CAPACITY_INCREMENT;
T* temp = new T[this->used];
for(int element = 0; element < this->used; element++){
temp[element] = this->array[element];
}
delete [] this->array;
this->array = new T[this->capacity];
for(int element = 0; element < this->used; element++){
this->array[element] = temp[element];
}
delete [] temp;
}
template <class T>
bool ArrayList<T>::resize(int newSize){
if(newSize >= this->used){
this->capacity = newSize;
T* temp = new T[this->used];
for(int element = 0; element < this->used; element++){
temp[element] = this->array[element];
}
delete [] this->array;
this->array = new T[this->capacity];
for(int element = 0; element < this->used; element++){
this->array[element] = temp[element];
}
delete [] temp;
return true;
} else{
return false;
}
}
template <class T>
ArrayList<T>::ArrayList(){
this->array = new T[this->CAPACITY_INITIAL];
this->capacity = this->CAPACITY_INITIAL;
this->used = 0;
}
template <class T>
ArrayList<T>::~ArrayList(){
delete [] this->array;
}
template <class T>
ArrayList<T>::ArrayList(const ArrayList& that){
delete [] this->array;
this->used = that.used;
this->capacity = that.capacity;
this->array = new T[this->capacity];
for(int element = 0; element < this->used; element++){
this->array[element] = that.array[element];
}
}
template <class T>
ArrayList<T>& ArrayList<T>::operator=(const ArrayList<T>& that){
delete [] this->array;
this->used = that.used;
this->capacity = that.capacity;
this->array = new T[this->capacity];
for(int element = 0; element < this->used; element++){
this->array[element] = that.array[element];
}
return *this;
}
template <class T>
T& ArrayList<T>::operator[](int index){
if(index < this->used){//in range
return this->array[index];
} else{
return new T;
}
}
template <class T>
T ArrayList<T>::get(int index){
if(index < this->used){//in range
return this->array[index];
} else{
return *(new T);
}
}
template <class T>
void ArrayList<T>::add(T toAdd){
if(this->used + 1 >= this->capacity){
this->resize();
}
this->array[this->used] = toAdd;
this->used++;
}
template <class T>
bool ArrayList<T>::add(int index,T toAdd){
if(index >= 0){
if(index >= this->capacity + this->CAPACITY_INCREMENT){
this->resize(index+1);
} else if(this->used + 1 >= this->capacity){
this->resize();
}
for(int i = this->used; i > index; i--){
this->array[i] = this->array[i-1];
}
this->array[index] = toAdd;
this->used++;
return true;
} else{
return false;
}
}
template <class T>
bool ArrayList<T>::set(int index,T toSet){
if(index >= 0){
if(index >= this->capacity + this->CAPACITY_INCREMENT){
this->resize(index+1);
} else if(this->used + 1 >= this->capacity){
this->resize();
}
this->array[index] = toSet;
return true;
} else{
return false;
}
}
template <class T>
T ArrayList<T>::remove(int index){
if(index < this->used && index >= 0){
T removed;
removed = this->array[index];
for(int element = index; element < this->used - 1; element++){
this->array[element] = this->array[element+1];
}
this->used--;
return removed;
} else{
return *(new T);
}
}
template <class T>
ArrayList<T> ArrayList<T>::removeRange(int start,int end){
if(start < end && end < this->used && start >= 0){
ArrayList removed;
T empty;
for(int element = start; element <= end; element++){
removed.add(this->array[element]);
if((element + (end - start + 1)) < this->used){
//can move one back
this->array[element] = this->array[element + (end - start + 1)];
} else{
this->array[element] = empty;
}
this->used--;
}
return removed;
} else{
return new T[0];
}
}
template <class T>
bool ArrayList<T>::removeOne(T toRemove){
bool foundAndRemoved = false;
for(int element = 0; element < this->used && !foundAndRemoved; element++){
if(this->array[element] == toRemove){
for(int moveBack = element; moveBack < this->used - 1; moveBack++){
this->array[moveBack] = this->array[moveBack+1];
}
this->used--;
foundAndRemoved = true;
}
}
return foundAndRemoved;
}
template <class T>
bool ArrayList<T>::removeAllOf(T toRemove){
bool foundAndRemoved = false;
for(int element = 0; element < this->used; element++){
if(this->array[element] == toRemove){
for(int moveBack = element; moveBack < this->used - 1; moveBack++){
this->array[moveBack] = this->array[moveBack+1];
}
this->used--;
foundAndRemoved = true;
}
}
return foundAndRemoved;
}
template <class T>
bool ArrayList<T>::replaceOne(T toReplace,T replacement){
bool replaced = false;
for(int element = 0; element < this->used && !replaced; element++){
if(this->array[element] == toReplace){
this->array[element] = replacement;
replaced = true;
}
}
return replaced;
}
template <class T>
bool ArrayList<T>::replaceAllOf(T toReplace,T replacement){
bool replaced = false;
for(int element = 0; element < this->used; element++){
if(this->array[element] == toReplace){
this->array[element] = replacement;
replaced = true;
}
}
return replaced;
}
template <class T>
ArrayList<T> ArrayList<T>::getSubArrayList(int start,int end){
ArrayList<T> result;
for(int element = start; element <= end; element++){
result.add(this->array[element]);
}
return result;
}
template <class T>
void ArrayList<T>::clear(){
delete [] this->array;
this->used = 0;
this->capacity = CAPACITY_INITIAL;
this->array = new T[this->capacity];
}
template <class T>
int ArrayList<T>::size(){
return this->used;
}
template <class T>
void ArrayList<T>::addCapacity(unsigned int toAdd){
this->resize(this->capacity + toAdd);
}
template <class T>
int ArrayList<T>::indexOf(T toFind){
for(int element = 0; element < this->used; element++){
if(this->array[element] == toFind){
return element;
}
}
return -1;
}
template <class T>
void ArrayList<T>::trim(int newSize){
if(newSize >= 0 ){
if(newSize >= this->used){//all the current elements fit
this->resize(newSize);
} else{//will delete some elements
//the resize(int) function copies up to used, but used
//will be greater than this->used. Thus this compensates by
//using the new shorter capacity instead.
this->capacity = newSize;
T* temp = new T[this->capacity];
for(int element = 0; element < this->capacity; element++){
temp[element] = this->array[element];
}
delete [] this->array;
this->array = new T[this->capacity];
for(int element = 0; element < this->capacity; element++){
this->array[element] = temp[element];
}
delete [] temp;
}
}
}
#endif /* ARRAYLIST_H */