-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.h
741 lines (656 loc) · 15.8 KB
/
Vector.h
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
#pragma once
#include <iostream>
#include <initializer_list>
namespace std
{
/*
Custom Generic Vector Class to hold Elements of Type "T"
Slowly adding more functionality to this class to behave more like the generic vector class
*/
template<class T>
class Vector
{
public:
typedef typename std::size_t uint;
private:
uint size;
uint capacity;
T *element;
// Increases the capacity of the underlying array to be sz
// If sz is smaller than the current capacity then nothing is done
void allocate(uint sz)
{
try
{
if (sz <= capacity)
return;
if (sz > std::numeric_limits<uint>::max())
throw std::out_of_range("Vector Capacity limit has been reached.\nMax Capacity is: MAXSIZE_T");
// allocate a new array on the free store
T *new_element = new T[sz];
// copy old vector into new one
for (int i = 0; (uint)i < size; ++i)
{
new_element[i] = element[i];
}
// set the new capacity
capacity = sz;
// delete the old vector
delete[] element;
// Assign element to the new element
element = new_element;
}
catch (const std::out_of_range& e)
{
std::cout << e.what() << std::endl;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Decreases the capacity of the array to be sz
// If sz is larger than the current capacity, nothing is done
void deallocate(uint sz)
{
try
{
if (sz >= capacity)
return;
T *new_element = new T[sz];
for (int i = 0; (uint)i < size; ++i)
{
new_element[i] = element[i];
}
capacity = sz;
delete[] element;
element = new_element;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// =========================================================================================
// Iterator for VectorCustom
class Iterator
{
public:
typedef std::size_t uint;
typedef Iterator self;
typedef T type;
typedef T& reference;
typedef T* pointer;
// Constructors
Iterator() {}
Iterator(T* _ptr) : ptr(_ptr) { }
self operator++() { ++ptr; return *this; } // PREFIX
self operator++(int junk) { self i = *this; ptr++; return i; } // POSTFIX
reference operator*() { return *ptr; }
type* operator->() { return ptr; }
type& operator=(const type& s) { ptr = s.ptr; return *this; }
bool operator==(const self& s) { return ptr == s.ptr; } const
bool operator!=(const self& s) { return ptr != s.ptr; } const
bool operator<(const self& s) { return ptr < s.ptr; } const
bool operator>(const self& s) { return ptr > s.ptr; } const
type operator-(const self& s) const { return this->ptr - s.ptr; }
private:
pointer ptr;
};
// Constant Iterator for VectorCustom
const class ConstIterator
{
public:
typedef std::size_t uint;
typedef ConstIterator self;
typedef T type;
typedef T& reference;
typedef T* pointer;
// Constructors
ConstIterator() {}
ConstIterator(T* _ptr) : ptr(_ptr) { }
self operator++() { ++ptr; return *this; } // PREFIX
self operator++(int junk) { self i = *this; ptr++; return i; } // POSTFIX
reference operator*() { return *ptr; }
const type* operator->() { return ptr; }
const type& operator=(const type& s) { ptr = s.ptr; return *this; }
bool operator==(const self& s) { return ptr == s.ptr; } const
bool operator!=(const self& s) { return ptr != s.ptr; } const
bool operator<(const self& s) { return ptr < s.ptr; } const
bool operator>(const self& s) { return ptr > s.ptr; } const
type operator-(const self& s) const { return this->ptr - s.ptr; }
private:
pointer ptr;
};
// ===============================================================================
// private reset constructors
void Reset(std::initializer_list<T> const &s)
{
size = s.size();
capacity = 10;
int i = 0;
while (capacity < size)
capacity *= 2;
element = new T[capacity];
for (auto it = s.begin(); it < s.end(); ++it)
{
element[i] = *it;
++i;
}
}
// Private reset constructor for Vectors
void Reset(const Vector<T> &s)
{
size = s.Size();
capacity = 10;
int i = 0;
while (capacity < size)
capacity *= 2;
element = new T[capacity];
for (i = 0; i < s.size; ++i)
element[i] = s[i];
}
public:
// ===============================================================================
// Iterator Typedef
typedef Iterator iterator;
// Constant Iterator Typedef
typedef ConstIterator constIterator;
// ===============================================================================
// Default constructor with no initialization
Vector() : capacity(10), size(0)
{
element = new T[capacity];
}
// Overloaded constructor for use with an initialization list
// IE: Vector<int> d = {10, 5, 10}; or
// Vector<int> d{10, 5, 10};
Vector(std::initializer_list<T> const &s)
{
// Just call the Reset Function to create the Vector from the Initialization List
Reset(s);
}
// Copy Constructor
Vector(const Vector<T> &obj)
{
element = new T[obj.size];
size = obj.size;
capacity = obj.capacity;
for (uint i = 0; i < obj.size; ++i)
element[i] = obj[i];
}
// Free up resources when Vector is lost
~Vector()
{
delete[] element;
}
// Overload the assignment operator to Re-Assign the Vector to a new initialization list
Vector<T>& operator=(const std::initializer_list<T> &s)
{
Reset(s);
return *this;
}
// Overloads the Brackets to Get/Set an element at position i
T& operator[](uint i)
{
try
{
if (i >= 0 && i < size)
return element[i];
else
throw std::out_of_range("Out Of Range Exception: Defined Vector Position does not fall within the Vector Elements Range.");
}
catch (const std::out_of_range& e)
{
std::cout << e.what() << std::endl;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
return element[0];
}
// Overloads the Brackets to Get/Set an element at position i
const T& operator[](uint i) const
{
try
{
if (i >= 0 && i < size)
return element[i];
else
throw std::out_of_range("Out Of Range Exception: Defined Vector Position does not fall within the Vector Elements Range.");
}
catch (const std::out_of_range& e)
{
std::cout << e.what() << std::endl;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Equality Operator
const bool operator==(Vector<T>& right) const
{
if (size != right.size)
return false;
else
{
for (uint i = 0; i < Size(); i++)
{
if (element[i] != right[i])
return false;
}
}
return true;
}
// Plus Equals Operator
// Adds each element from the "a" vector to the current vector
Vector<T>& operator+=(const Vector<T>& a)
{
T* p = new T[size + a.Size()];
for (int i = 0; i < size; i++)
p[i] = element[i];
for (int i = size, ctr = 0; i < size + a.Size(); i++, ctr++)
p[i] = a[ctr];
delete[] element;
element = p;
size += a.Size();
return *this;
}
// Plus Equals Operator
// Adds an element of type T to the end of the Vector
Vector<T>& operator+=(const T a)
{
if (size + 1 >= capacity)
allocate(2 * capacity);
element[size] = a;
size++;
return *this;
}
// (PREFIX) Add One to the current Vector Size
const Vector<T>& operator++()
{
T item;
Push_Back(item);
return *this;
}
// (POSTFIX) Add One to the current Vector Size
const Vector<T>& operator++(int junk)
{
Vector<T> i = *this;
T item;
Push_Back(item);
return i;
}
// (PREFIX) Minus One Element from the current Vector
const Vector<T>& operator--()
{
size -= 1;
return *this;
}
// (POSTFIX) Minus One Element from the current Vector
const Vector<T>& operator--(int junk)
{
Vector<T> i = *this;
size -= 1;
return i;
}
// Adds an element to the end of a Vector
const Vector<T>& operator+(T& x)
{
Push_Back(x);
return *this;
}
// Adds the right Vectors elements to the left Vectors elements
// Ex. vec = vec2 (1, 2, 3) + vec3 (4, 5, 6); which makes vec = (1, 2, 3, 4 ,5 ,6)
const Vector<T>& operator+(Vector<T>& x)
{
for (int i = 0; i < x.size; ++i)
Push_Back(x[i]);
return *this;
}
// Returns the size of this vector (starting at 1)
const uint Size() const
{
try
{
return size;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// The capacity of the current Generic Vector list
const uint Capacity() const
{
try
{
return capacity;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Sets the position "pos" to the value of "item"
void Set(uint pos, T &item)
{
try
{
if (pos >= 0 && pos < size)
element[pos] = item;
else
throw std::out_of_range("Out Of Range Exception: Defined Vector Position does not fall within the Vector Elements Range.");
}
catch (const std::out_of_range& e)
{
std::cout << e.what() << std::endl;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Gets an element from position "pos"
T At(int pos)
{
try
{
if (pos >= 0 && (uint)pos < size)
return element[pos];
else
throw std::out_of_range("Out Of Range Exception: Defined Vector Position does not fall within the Vector Elements Range.");
}
catch (const std::out_of_range& e)
{
std::cout << e.what() << std::endl;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
return element[0];
}
// Returns the position in the Vector of first found value of item
// NOTE: Returns -1 if the element was not found
int Get(T &item)
{
int pos = -1;
try
{
for (uint i = 0; i < size; ++i)
{
if (element[i] == item)
{
pos = i;
break;
}
}
return pos;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Gets will return a pointer containing the length of the array in the first position and each position in the current vector where this item is found
// Ex: if item = 5 and the current Vector = {1, 3, 5, 3, 4, 5, 5, 5},
// This function will return {4, 2, 5, 6, 7}
int* Gets(T &item)
{
int *arr = new int[1];
bool firstRun = true;
int num = 0;
try
{
for (uint i = 0; i < size; ++i)
{
if (At(i) == item && firstRun)
{
num++;
}
else if (At(i) == item && !firstRun)
{
arr[num] = i;
num++;
}
if (i == size-1 && firstRun)
{
arr = new int[num];
arr[0] = num;
i = 0;
num = 1;
firstRun = !firstRun;
}
}
// Return a pointer to this array
return arr;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
return new int[0];
}
// Adds an element of Type "item" to the end of the vector
void Push_Back(T &&item)
{
try
{
// Increase the capacity of the Vector if the
// size is at least the same size as capacity
if (size >= capacity)
allocate(2 * capacity);
// Set the element at position "size"
// to the item passed in the variable
element[size] = item;
++size;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Adds an element of Type "item" to the end of the vector
void Push_Back(T &item)
{
try
{
// Increase the capacity of the Vector if the
// size is at least the same size as capacity
if (size >= capacity)
allocate(2 * capacity);
// Set the element at position "size"
// to the item passed in the variable
element[size] = item;
++size;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Adds an element of Type "item" to the position "pos"
void Insert(T &item, uint pos)
{
try
{
// Increase the capacity if we need to
if (size >= capacity)
allocate(2 * capacity);
// Loop from 1 past the end of the Vector elements
// to the position we are adding an element to
for (uint i = size; i > pos; i--)
{
// Move the item up 1 position (Starting from the top of the Vector)
element[i] = element[i - 1];
}
// Add 1 to the size of the Vector
++size;
// Finally, Add the item to the specified position
element[pos] = item;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Removes the element from the end of the Vector
void Pop_Back()
{
try
{
// Decrease the size if it is greater than or equal to zero
if (size >= 0)
size--;
// Decrease the capacity if the size is less than x% the capacity
if (size < capacity / 10 && capacity > 10)
deallocate(capacity / 10);
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Removes the first Value found of the item from the Vector
void Pop(T &item)
{
try
{
// Gets the position of the first element "item"
int pos = Get(item);
// Call the function to remove an item from position "pos"
Pop_At(pos);
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Removes every occurence of the Value "item" found in the current Vector
void Pops(T &item)
{
int removed = 0;
try
{
// Get each position that we are removing from this vector
int *pos = Gets(item);
// Remove each position from the array
for (int i = 1; i <= pos[0]; ++i)
{
Pop_At(pos[i]);
removed++;
if (i < pos[0])
pos[i + 1] -= removed;
}
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Removes an element from the position "pos" from the Vector
void Pop_At(int pos)
{
try
{
if (pos > -1 && (uint)pos < size)
{
// Loop from the position after this element,
// to the last element in the Vector
for (uint i = (uint)pos; i < size-1; ++i)
{
// Switch the elements
T temp = element[i];
element[i] = element[i+1];
element[i + 1] = temp;
}
// Call the Pop function to remove the last element in the Vector
Pop_Back();
}
else
throw std::out_of_range("Out Of Range Exception: Defined Vector Position does not fall within the Vector Elements Range.");
}
catch (const std::out_of_range& e)
{
std::cout << e.what() << std::endl;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Returns the first element in the Vector
T& Front()
{
try
{
if (size > 0)
return element[0];
else
throw exception();
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Returns the last element in the Vector
T& Back()
{
try
{
if (size > 0)
return element[size - 1];
else
throw exception();
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Returns the first position in the Vector
uint First()
{
try
{
if (size > 0)
return 0;
else
throw exception();
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Returns the last position in the Vector
uint Last()
{
try
{
if (size > 0)
return size - 1;
else
throw exception();
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
// Returns the Iterator for the Beginning of the Vector object
Iterator Begin()
{
return Iterator(element);
}
// Returns the Iterator for the End of the Vector Object
Iterator End()
{
return Iterator(element + size);
}
}; // End Vector Class
} // End Namespace