-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.h
More file actions
298 lines (243 loc) · 6.75 KB
/
Copy pathvector.h
File metadata and controls
298 lines (243 loc) · 6.75 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
/***************************************************************
* File: vector.h
* Author: Ryan Walker
* Purpose: Contains the definition of the Vector class
***************************************************************/
#ifndef Vector_H
#define Vector_H
#include <cassert>
#include <iostream>
using namespace std;
// forward declaration for VectorIterator
template <class T>
class VectorIterator;
/************************************************
* Vector
* A class that holds stuff
***********************************************/
template <class T>
class Vector
{
public:
T * data; // dynamically allocated array of T
int numItems; // how many items are currently in the Vector?
int cap; // how many items can I put on the Vector before full?
// default constructor : empty and kinda useless
Vector() : numItems(0), cap(0), data(NULL) {}
// copy constructor : copy it
Vector(const Vector & rhs) throw (const char *);
// non-default constructor : pre-allocate
Vector(int cap) throw (const char *);
// destructor : free everything
~Vector() { if (cap) delete [] data; }
// overloading operators
Vector<T> & operator=(Vector<T> rhs);
const T & operator[](int num) const {return data[num];}
T & operator[](int num) {return data[num];}
// is the vector empty? (numItems == 0?)
bool empty() const { return numItems == 0; }
// clear the contents (NOT THE CAPACITY!)
void clear() { numItems = 0; }
// add a variable to the array
void push_back(const T & add) throw (const char *);
// number of items in the array
int size() const { return numItems; }
// total number of spaces available in the array
int capacity() const { return cap; }
// add an item to the Vector
void insert(const T & t) throw (const char *);
// return an iterator to the beginning of the list
VectorIterator <T> begin() { return VectorIterator<T>(data); }
// return an iterator to the end of the list
VectorIterator <T> end() { return VectorIterator<T>(data + numItems);}
};
/**************************************************
* Vector ITERATOR
* An iterator through Vector
*************************************************/
template <class T>
class VectorIterator
{
public:
// default constructor
VectorIterator() : p(NULL) {}
// initialize to direct p to some item
VectorIterator(T * p) : p(p) {}
// copy constructor
VectorIterator(const VectorIterator & rhs) { *this = rhs; }
// assignment operator
VectorIterator & operator = (const VectorIterator & rhs)
{
this->p = rhs.p;
return *this;
}
// not equals operator
bool operator != (const VectorIterator & rhs) const
{
return rhs.p != this->p;
}
// dereference operator
T & operator * ()
{
return *p;
}
// prefix increment
VectorIterator <T> & operator ++ ()
{
p++;
return *this;
}
// postfix increment
VectorIterator <T> operator++(int postfix)
{
VectorIterator tmp(*this);
p++;
return tmp;
}
// prefix decrement
VectorIterator <T> & operator -- ()
{
p--;
return *this;
}
// postfix decrement
VectorIterator <T> operator--(int postfix)
{
VectorIterator tmp(*this);
p--;
return tmp;
}
private:
T * p;
};
/*******************************************
* Vector :: COPY CONSTRUCTOR
*******************************************/
template <class T>
Vector <T> :: Vector(const Vector <T> & rhs) throw (const char *)
{
assert(rhs.cap >= 0);
// do nothing if there is nothing to do
if (rhs.cap == 0)
{
cap = numItems = 0;
data = NULL;
return;
}
// attempt to allocate
try
{
data = new T[rhs.cap];
}
catch (std::bad_alloc)
{
throw "ERROR: Unable to allocate buffer";
}
// copy over the cap and size
assert(rhs.numItems >= 0 && rhs.numItems <= rhs.cap);
cap = rhs.cap;
numItems = rhs.numItems;
// copy the items over one at a time using the assignment operator
for (int i = 0; i < numItems; i++)
data[i] = rhs.data[i];
// the rest needs to be filled with the default value for T
for (int i = numItems; i < cap; i++)
data[i] = T();
}
/**********************************************
* Vector : NON-DEFAULT CONSTRUCTOR
* Preallocate the Vector to "cap"
**********************************************/
template <class T>
Vector <T> :: Vector(int cap) throw (const char *)
{
assert(cap >= 0);
// do nothing if there is nothing to do
if (cap == 0)
{
this->cap = this->numItems = 0;
this->data = NULL;
return;
}
// attempt to allocate
try
{
data = new T[cap];
}
catch (std::bad_alloc)
{
throw "ERROR: Unable to allocate buffer";
}
// copy over the stuff
this->cap = cap;
this->numItems = 0;
// initialize the Vector by calling the default constructor
for (int i = 0; i < cap; i++)
data[i] = T();
}
/************************************
* Vector :: OPERATOR=
* Overrides = to copy any value.
************************************/
template <class T>
Vector<T> & Vector <T> :: operator=(Vector<T> rhs)
{
delete [] data;
cap = rhs.cap;
numItems = rhs.numItems;
data = new T[cap];
for (int i = 0; i < numItems; i++)
{
data[i] = rhs.data[i];
}
return *this;
}
/*****************************************
* Vector :: PUSH_BACK
* Adds an object onto the vector
*****************************************/
template <class T>
void Vector <T> :: push_back(const T & add) throw (const char *)
{
if (cap == 0)
{
cap = 1; //initalize capacity to create space in the vector
data = new T[cap]; //initialize the data so there's no segmentation fault
}
try
{
if (numItems >= cap)
{
T *nData = new T[cap *= 2];
for (int i = 0; i < numItems; i++)
{
nData[i] = data[i];
}
delete [] data;
data = nData;
}
data[numItems++] = add;
}
catch (std::bad_alloc)
{
throw "ERROR: Unable to allocate a new buffer for vector";
}
}
/***************************************************
* Vector :: INSERT
* Insert an item on the end of the Vector
**************************************************/
template <class T>
void Vector <T> :: insert(const T & t) throw (const char *)
{
// do we have space?
if (cap == 0 || cap == numItems)
throw "ERROR: Insufficient space";
// add an item to the end
data[numItems++] = t;
//cout << "insert " << endl;
//cout << *data << endl;
//cout << numItems << endl;
//cout << cap << endl;
}
#endif // Vector_H