-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtvector.cpp
More file actions
220 lines (194 loc) · 5.83 KB
/
Copy pathtvector.cpp
File metadata and controls
220 lines (194 loc) · 5.83 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
// *******************************************************************
// see tvector.h for complete documentation of functions
//
// vector class consistent with a subset of the standard C++ vector class
// as defined in the draft ANSI standard (part of standard template library)
// *******************************************************************
#include <stdlib.h>
#include <iostream>
using namespace std;
#include "tvector.h"
template <class itemType>
tvector<itemType>::tvector()
//postcondition: vector has a capacity of 0 items
: mySize(0),
myCapacity(0),
myList(0)
{
}
template <class itemType>
tvector<itemType>::tvector(int size)
// precondition: size >= 0
// postcondition: vector has size/capacity of size items
: mySize(size),
myCapacity(size),
myList(new itemType[size])
{
}
template <class itemType>
tvector<itemType>::tvector(int size, const itemType & fillValue)
// precondition: size >= 0
// postcondition: vector has size/capacity of size items, all of which are set
// by assignment to fillValue after default construction
: mySize(size),
myCapacity(size),
myList(new itemType[size])
{
int k;
for(k = 0; k < size; k++)
{
myList[k] = fillValue;
}
}
template <class itemType>
tvector<itemType>::tvector(const tvector<itemType> & vec)
// postcondition: vector is a copy of vec
: mySize(vec.size()),
myCapacity(vec.capacity()),
myList(new itemType[myCapacity])
{
int k;
// copy elements
for(k = 0; k < mySize; k++){
myList[k] = vec.myList[k];
}
}
template <class itemType>
tvector<itemType>::~tvector ()
// postcondition: vector is destroyed
{
delete [] myList;
myList = 0; // fail fast
}
template <class itemType>
const tvector<itemType> &
tvector<itemType>::operator = (const tvector<itemType> & rhs)
// postcondition: normal assignment via copying has been performed;
// if vector and rhs were different sizes, vector
// has been resized to match the size of rhs
{
if (this != &rhs) // don't assign to self!
{
delete [] myList; // get rid of old storage
myCapacity = rhs.capacity();
mySize = rhs.size();
myList = new itemType [myCapacity]; // allocate new storage
// copy rhs
int k;
for(k=0; k < mySize; k++)
{
myList[k] = rhs.myList[k];
}
}
return *this; // permit a = b = c = d
}
template <class itemType>
int tvector<itemType>::length() const
// postcondition: returns vector's size (number of memory cells
// allocated for vector) THIS METHOD IS DEPRECATED
{
return myCapacity;
}
template <class itemType>
int tvector<itemType>::capacity() const
// postcondition: returns vector's size (number of memory cells
// allocated for vector)
{
return myCapacity;
}
template <class itemType>
int tvector<itemType>::size() const
{
return mySize;
}
template <class itemType>
void tvector<itemType>::push_back(const itemType& t)
{
if (mySize >= myCapacity)
{
reserve(myCapacity == 0 ? 2 : 2*myCapacity);
}
myList[mySize] = t;
mySize++;
}
template <class itemType>
void tvector<itemType>::pop_back()
{
if (mySize > 0)
{
mySize--;
}
}
template <class itemType>
itemType & tvector<itemType>::operator [] (int k)
// description: range-checked indexing, returning kth item
// precondition: 0 <= k < length()
// postcondition: returns the kth item
{
if (k < 0 || myCapacity <= k)
{
cerr << "Illegal vector index: " << k << " max index = ";
cerr << (mySize-1) << endl;
exit(1);
}
return myList[k];
}
template <class itemType>
const itemType & tvector<itemType>::operator [] (int k) const
// safe indexing, returning const reference to avoid modification
// precondition: 0 <= index < length
// postcondition: return index-th item
// exception: aborts if index is out-of-bounds
{
if (k < 0 || myCapacity <= k)
{
cerr << "Illegal vector index: " << k << " max index = ";
cerr << (mySize-1) << endl;
exit(1);
}
return myList[k];
}
template <class itemType>
void tvector<itemType>::resize(int newSize)
// description: resizes the vector to newSize elements
// precondition: the current capacity of vector is capacity(); newSize >= 0
// the current size is size()
// postcondition: size() == newSize. If newSize > oldsize then
// the current capacity of vector is newSize; otherwise
// capacity isn't changed. for each k
// such that 0 <= k <= min(mySize, newSize), vector[k]
// is a copy of the original; other elements of vector are
// initialized using the 0-argument itemType constructor
{
if (newSize < mySize)
{
mySize = newSize; // capacity doesn't "shrink"
return;
}
// allocate new storage and copy element into new storage
int k;
itemType * newList = new itemType[newSize];
for(k=0; k < mySize; k++)
{
newList[k] = myList[k];
}
delete [] myList; // de-allocate old storage
myCapacity = mySize = newSize; // assign new storage/size
myList = newList;
}
template <class itemType>
void tvector<itemType>::reserve(int size)
{
// punt to resize in current implementation
int oldSize = mySize;
if (size > myCapacity)
{
resize(size);
}
mySize = oldSize;
}
template <class itemType>
void tvector<itemType>::clear()
{
mySize = 0;
}