-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.h
119 lines (98 loc) · 1.92 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
#pragma once
template<typename T>
class Vector
{
T* mData; // Arbitrary data to be inserted into the vector.
int mSize; // Size of the Vector.
int mCapacity; // How many elements the vector can hold.
T mUndefined;
public:
Vector() // O(1), this is the Vector class constructor
{
mSize = 0;
mCapacity = 15;
mData = new T[mCapacity];
}
Vector(const Vector<T>& tOther) : Vector() // O(n), Copy constructor
{
Reserve(tOther.Capacity());
for (int i = 0; i < tOther.Size(); i++)
{
mData[i] = tOther.At(i);
}
mSize = tOther.Size();
}
Vector& operator = (const Vector<T>& tRHS) // O(n)
{
Reserve(tRHS.Capacity());
for (int i = 0; i < tRHS.Size(); i++)
{
mData[i] = tRHS.At(i);
}
mSize = tRHS.Size();
return *this;
}
~Vector() // Vector class destructor
{
delete[] mData;
}
void PushBack(const T& tItem) // O(1), Push elements to the back of the vector.
{
if (mSize == mCapacity)
{
Reserve(mCapacity * 2);
}
else
{
mData[mSize] = tItem;
mSize++;
}
}
void PopBack() // O(1), Pop elements off of the vector
{
if (mSize > 0)
{
mSize--;
}
}
T At(int tWhere) const // O(1), Find an element at a specific index in the vector.
{
if (tWhere >= mSize || tWhere < 0)
{
return mUndefined;
}
else
{
return mData[tWhere];
}
}
void Clear() // O(1), Clear all elements from the vector.
{
mSize = 0;
}
int Size() const // O(1), Returns the size of the vector.
{
return mSize;
}
void Reserve(int tCount) // O(n), Reserves memory for the vector.
{
int tPreviousSize = mSize;
if (tCount < tPreviousSize)
{
tPreviousSize = tCount;
}
T* tData = new T[tCount];
for (int i = 0; i < tPreviousSize; i++)
{
tData[i] = mData[i];
}
delete[] mData;
mSize = tPreviousSize;
mData = tData;
mCapacity = tCount;
}
int Capacity() const // O(1), Returns the number of elements that the vector can hold.
{
return mCapacity;
}
};