-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.cc
More file actions
296 lines (247 loc) · 5.88 KB
/
Vector.cc
File metadata and controls
296 lines (247 loc) · 5.88 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
// Implementation of the templated Vector class
// ECE4893/8893 lab 3
// Jacob Ashmore
#include <iostream> // debugging
#include "Vector.h"
// Your implementation here
// Fill in all the necessary functions below
using namespace std;
// Default constructor
template <typename T>
Vector<T>::Vector()
{
//cout << "Default Vector constructor Vector() has been called" << endl;
elements = NULL; count=0; reserved=0;
}
// Copy constructor
template <typename T>
Vector<T>::Vector(const Vector& rhs)
{
//LHS == RHS count and reserved. Then allocate memory for the elements and copy them to LHS
count = rhs.count;
reserved = rhs.reserved;
elements = (T*)malloc(sizeof(T)*count);
for(size_t i=0; i < count; i++) {
new (elements + i) T(rhs.elements[i]);
}
}
// Assignment operator
template <typename T>
Vector<T>& Vector<T>::operator=(const Vector& rhs)
{
if(elements!=0) {
for(size_t i=0; i<count; i++){
elements[i].~T;
}
free(elements);
}
count=rhs.count;
reserved=rhs.reserved;
elements = (T*)malloc(sizeof(T)*reserved);
for(size_t i=0; i< count; i++) {
new (elements[i]) T(rhs.elements[i]);
}
}
#ifdef GRAD_STUDENT
// Other constructors
template <typename T>
Vector<T>::Vector(size_t nReserved)
{ // Initialize with reserved memory
}
template <typename T>
Vector<T>::Vector(size_t n, const T& t)
{ // Initialize with "n" copies of "t"
new (elements) T(t)
}
template <typename T>
void Vector<T>::Reserve(size_t n)
{ // Reserve extra memory for n elements of size_t
}
#endif
// Destructor
template <typename T>
Vector<T>::~Vector()
{
Clear();
free(elements);
}
// Add and access front and back
template <typename T>
void Vector<T>::Push_Back(const T& rhs)
{
if(count < reserved) { //there is already enough memory allocated
new (&elements[count]) T(rhs);//new (&elements[count] T(rhs));
} else { //there is not enough memory allocated already
T* newElements = (T*)malloc(sizeof(T)*(count+1));
reserved = count+1;
for(size_t i=0; i<count; i++) {
new (&newElements[i]) T(elements[i]);//new (&newElements[i] T(elements[i])) //call copy constructor to deep copy old elements[i] into newElements[i]
elements[i].~T(); //explicitly call destructor on old element values
}
new (&newElements[count]) T(rhs);//new &newElements[count] T(rhs); //add in the new data at the end position
count=count+1;
free(elements); //Already freed the memory when I explicitly called the destructor, so this should be redundant
elements = newElements;
}
}
template <typename T>
void Vector<T>::Push_Front(const T& rhs)
{
if(count < reserved) {
for(size_t i=count; i>0; i--) {
elements[i]=elements[i-1]; //go from end to beginning and move values up by one
}
new (&elements[0]) T(rhs); //add in the new data at the first position
count++;
} else {
T* newElements = (T*)malloc(sizeof(T)*(count+1));
for(size_t i=count; i>0; i--) {
new (newElements + i) T(elements[i-1]);
elements[i-1].~T();
}
free(elements);
elements=newElements;
new (&elements[0]) T(rhs);
count++;
reserved = count;
}
}
template <typename T>
void Vector<T>::Pop_Back()
{ // Remove last element
if(count>0) {
elements[count-1].~T();
count=count-1;//count--;
}
}
template <typename T>
void Vector<T>::Pop_Front()
{ // Remove first element
if(count>0) {
elements[0].~T();
for(size_t i=0; i<count-1;i++) {
new (&elements[i]) T(elements[i+1]);
elements[i+1].~T();
}
count=count-1;//count--;
}
}
// Element Access
template <typename T>
T& Vector<T>::Front() const
{
return elements[0];
}
// Element Access
template <typename T>
T& Vector<T>::Back() const
{
return elements[count-1];
}
template <typename T>
const T& Vector<T>::operator[](size_t i) const
{ // const element access
return elements[i];
}
template <typename T>
T& Vector<T>::operator[](size_t i)
{//nonconst element access
return elements[i];
}
template <typename T>
size_t Vector<T>::Size() const
{
return count;
}
template <typename T>
bool Vector<T>::Empty() const
{
//Return true if there are no elements in the vector and false if there are elements
if(count==0){
return 1;
} else {
return 0;
}
}
// Implement clear
template <typename T>
void Vector<T>::Clear()
{
//get rid of all of the elements in the vector
reserved=count;
for(size_t i=0; i<count; i++){
elements[count - 1 - i].~T(); //recursively remove elements from the vector
}
count=0;
}
// Iterator access functions
template <typename T>
VectorIterator<T> Vector<T>::Begin() const
{
return VectorIterator<T>(elements);
}
template <typename T>
VectorIterator<T> Vector<T>::End() const
{
return VectorIterator<T>(elements+count);
}
#ifdef GRAD_STUDENT
// Erase and insert
template <typename T>
void Vector<T>::Erase(const VectorIterator<T>& it)
{
}
template <typename T>
void Vector<T>::Insert(const T& rhs, const VectorIterator<T>& it)
{
}
#endif
// Implement the iterators
// Constructors
template <typename T>
VectorIterator<T>::VectorIterator()
{
//initialize an empty vector
current=NULL;
}
template <typename T>
VectorIterator<T>::VectorIterator(T* c)
{
current = c;
}
// Copy constructor
template <typename T>
VectorIterator<T>::VectorIterator(const VectorIterator<T>& rhs)
{
current = rhs.current;
}
// Iterator defeferencing operator
template <typename T>
T& VectorIterator<T>::operator*() const
{
//return a pointer to the current element
return *current;
}
// Prefix incremen t
template <typename T>
VectorIterator<T> VectorIterator<T>::operator++()
{
return VectorIterator<T>(++current);
}
// Postfix increment
template <typename T>
VectorIterator<T> VectorIterator<T>::operator++(int)
{
return VectorIterator<T>(current++);
}
// Comparison operators
template <typename T>
bool VectorIterator<T>::operator !=(const VectorIterator<T>& rhs) const
{
return current!=rhs.current;
}
template <typename T>
bool VectorIterator<T>::operator ==(const VectorIterator<T>& rhs) const
{
return current==rhs.current;
}