-
Notifications
You must be signed in to change notification settings - Fork 0
/
pic10b_vector.h
341 lines (293 loc) · 7.67 KB
/
pic10b_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
//Name: Nicholas Cantrell
//ID# 704957985
//PIC10C Homework #1 Assignment A
#include <iostream> // std::ostream, std::cout
#include <stdio.h>
#include <string>
namespace Pic10b {
template <class T>
class vector {
private:
T * the_data;
size_t the_size;
size_t the_capacity;
static const int INIT_CAP = 10;
public:
// The big 4
vector();
vector(const vector&);
vector& operator=(const vector&);
vector& operator+(const vector&);
vector& operator+=(const vector&);
bool operator<(const vector&);
bool operator<=(const vector&);
bool operator>(const vector&);
bool operator>=(const vector&);
bool operator!=(const vector&);
bool operator==(const vector&);
~vector();
// Other members [public]
bool empty() const;
size_t norm() const;
size_t size() const;
size_t capacity() const;
T front() const;
T back() const;
T at(size_t index) const;
T& operator[](size_t index);
T operator[](size_t index) const;
void dump_data_to(std::ostream& out) const;
void dump_data() const;
void push_back(T new_value);
void pop_back();
private:
//Other members [private]
void reserve(size_t new_capacity);
}; // end Pic10b::vector
/** ************************* THE BIG 4 ************************* **/
template <class T>
vector<T>::vector()
: the_data(nullptr), the_size(0), the_capacity(INIT_CAP) {
the_data = new T[the_capacity];
for (size_t i = 0; i < the_capacity; ++i)
the_data[i] = 0;
}
template <class T>
vector<T>::vector(const vector& source)
: the_data(nullptr), the_size(source.the_size),
the_capacity(source.the_capacity) {
the_data = new T[the_capacity];
for (size_t i = 0; i < the_capacity; ++i)
the_data[i] = 0;
// Deep copy of internal array
for (int i = 0; i < the_size; ++i) {
the_data[i] = source.the_data[i];
}
}
template <class T>
vector<T>& vector<T>::operator=(const vector& rhs) {
if (this != &rhs) { // Self-assignment?
// Release old memory and request more
delete[] the_data;
the_data = new double[rhs.the_capacity];
// Shallow copy non-pointers
the_size = rhs.the_size;
the_capacity = rhs.the_capacity;
// Deep copy internal array
for (int i = 0; i < the_size; ++i)
the_data[i] = rhs.the_data[i];
}
return *this;
}
template <class T>
vector<T>& vector<T>::operator+(const vector& rhs) {
if (this != &rhs) { // Self-assignment?
// Release old memory and request more
delete[] the_data;
the_data = new T[rhs.the_capacity];
for (size_t i = 0; i <= rhs.the_capacity; ++i)
the_data[i] = 0;
// Shallow copy non-pointers
the_size = rhs.the_size;
the_capacity = rhs.the_capacity;
// Deep copy internal array
for (int i = 0; i < the_capacity; ++i)
the_data[i] = rhs.the_data[i];
}
return *this;
}
template <class T>
vector<T>& vector<T>::operator+=(const vector& rhs)
{
for (int i = 0; i < the_capacity; ++i)
the_data[i] += rhs.the_data[i];
return *this;
}
template <class T>
bool vector<T>::operator <(const vector& rhs) {
if (this.norm() < rhs.norm()) {
return true;
}
return false;
}
template <class T>
bool vector<T>::operator<=(const vector& rhs) {
if (this.norm() < rhs.norm()) {
return true;
}
if (this.norm() == rhs.norm()) {
return true;
}
return false;
}
template <class T>
bool vector<T>::operator >(const vector& rhs) {
if (this.norm() > rhs.norm()) {
return true;
}
return false;
}
template <class T>
bool vector<T>::operator>=(const vector& rhs) {
if (this.norm() > rhs.norm()) {
return true;
}
if (this.norm() == rhs.norm()) {
return true;
}
return false;
}
template <class T>
bool vector<T>::operator!=(const vector& rhs) {
bool out = false;
for (int i = 0; i < the_capacity; ++i)
if (the_data[i] != rhs.the_data[i]) { out = true; }
return out;
}
template <class T>
bool vector<T>::operator==(const vector& rhs) {
bool out = true;
for (int i = 0; i < the_capacity; ++i)
if (the_data[i] != rhs.the_data[i]) { out = false; }
return out;
}
template <class T>
vector<T>::~vector() {
delete[] the_data;
}
/** *********************** OTHER MEMBERS *********************** **/
template <class T>
bool vector<T>::empty() const {
return the_size == 0;
}
template <class T>
size_t vector<T>::size() const {
return the_size;
}
template <class T>
size_t vector<T>::norm() const {
size_t norm = 0;
for (size_t i = 0; i < the_capacity; ++i)
norm += the_data[i];
//norm = sqrt(norm);
return norm;
}
template <class T>
size_t vector<T>::capacity() const {
return the_capacity;
}
template <class T>
T vector<T>::front() const {
return *the_data;
}
template <class T>
T vector<T>::back() const {
return *(the_data + the_size - 1);
}
template <class T>
T vector<T>::at(size_t index) const {
if (index < the_size)
return the_data[index];
return the_data[0];
}
template <class T>
T& vector<T>::operator[](size_t index) {
return the_data[index];
}
template <class T>
T vector<T>::operator[](size_t index) const {
return the_data[index];
}
template <class T>
void vector<T>::dump_data_to(std::ostream& out) const {
out << "Vector (dump): ";
for (size_t i = 0; i < the_capacity; ++i)
out << the_data[i] << ' ';
out << '\n';
}
template <class T>
void vector<T>::dump_data() const {
dump_data_to(std::cout);
}
template <class T>
void vector<T>::push_back(T new_value) {
if (the_size == the_capacity)
reserve(the_capacity + 1); // `the_data` is reassigned
the_data[the_size++] = new_value;
}
// This implementation does not shrink the vector (ever)
template <class T>
void vector<T>::pop_back() {
if (the_size > 0)
--the_size;
}
template <class T>
void vector<T>::reserve(size_t new_capacity) {
if (new_capacity > the_capacity) {
if (new_capacity <= 2 * the_capacity)
new_capacity = 2 * the_capacity;
T* old_location = the_data;
the_data = new T[new_capacity];
the_capacity = new_capacity;
for (size_t i = 0; i < the_size; ++i)
the_data[i] = old_location[i];
delete old_location;
}
}
} // end Pic10b namespace
/** ************************ OTHER FUNCTIONS ************************ **/
template <class T>
std::ostream& operator<<(std::ostream& out, const Pic10b::vector<T>& v) {
for (size_t i = 0; i < v.size(); ++i)
out << v[i] << ' ';
return out;
}
template <class T>
void print_vector(const Pic10b::vector<T>& v) {
if (v.empty())
std::cout << "Vector is empty\n";
else
std::cout << "Vector (contents): " << v << '\n';
}
/*
int main() {
using Pic10b::vector;
using std::cout;
cout << "Create & display empty vector (v1)\n";
vector<double> v1;
print_vector(v1);
v1.dump_data();
int size = 15;
cout << "\nPopulate & display vector with " << size << " entries (v1)\n";
for (int i = 1; i <= size; ++i)
v1.push_back(i);
print_vector(v1);
cout << "\nCopy non-empty vector, pop back last entry & display (v2)\n";
vector<double> v2(v1);
v2.pop_back();
print_vector(v2);
cout << "\nReassign vector (v1 = v2) & display\n";
v1 = v2;
print_vector(v1);
cout << "\nDump contents of vectors (v1,v2)\n";
v1.dump_data();
v2.dump_data();
std::string input;
getline(std::cin, input);
return 0;
}
/**
OUTPUT:
Create & display empty vector (v1)
Vector is empty
Vector (dump): 0 0 0 0 0 0 0 0 0 0
Populate & display vector with 15 entries (v1)
Vector (contents): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Copy non-empty vector, pop back last entry & display (v2)
Vector (contents): 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Reassign vector (v1 = v2) & display
Vector (contents): 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Dump contents of vectors (v1,v2)
Vector (dump): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 0 0 0 0
Vector (dump): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 0 0 0 0
**/