-
Notifications
You must be signed in to change notification settings - Fork 125
/
chapter20_ex18.cpp
288 lines (230 loc) · 8.87 KB
/
chapter20_ex18.cpp
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
// Chapter 20, Exercise 18: define a range-checked iterator for vector (a
// random-access iterator)
// Adapting vector implementation "vectr" from Chapter 19, exercise 8 and 9
#include "../lib_files/std_lib_facilities.h"
//------------------------------------------------------------------------------
template<class T> class allocatr {
public:
// allocate space for n objects of type T
T* allocate(int n);
// deallocate n objects of type T starting at p
void deallocate(T* p) { free(p); }
// construct a T with the value v in p
void construct(T* p, const T& v) { new(p) T(v); }
// destroy the T in p
void destroy(T* p) { p->~T(); }
};
//------------------------------------------------------------------------------
// allocate space for n objects of type T
template<class T> T* allocatr<T>::allocate(int n)
{
T* p = static_cast<T*>(malloc(n*sizeof(T)));
if (p==0) error("allocate: could not allocate memory");
return p;
}
//------------------------------------------------------------------------------
template<class T, class A = allocatr<T> > class vectr {
A alloc; // used to handle memory for elements
int sz; // the size
T* elem; // a pointer to the elements
int space; // space + free space
public:
typedef unsigned long size_type;
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
class iterator_rc;
vectr() : sz(0), elem(0), space(0) { }
explicit vectr(size_type s);
vectr(const vectr&); // copy constructor
vectr& operator=(const vectr&); // copy assignment
~vectr(); // destructor
T& operator[](size_type n) { return elem[n]; } // access; return reference
const T& operator[](size_type n) const { return elem[n]; }
T& at(size_type n);
const T& at(size_type n) const; // checked access
iterator begin() { return elem; }
iterator end() { return elem+sz; }
size_type size() const { return sz; } // the current size
size_type capacity() const { return space; }
void resize(size_type newsize, T val = T()); // growth
void push_back(const T& d);
void reserve(size_type newalloc);
};
//------------------------------------------------------------------------------
template<class T, class A>
class vectr<T,A>::iterator_rc {
iterator curr;
iterator first;
iterator last;
public:
iterator_rc(iterator c , iterator f, iterator l)
: curr(c), first(f), last(l) { }
T& operator*() { return *curr; }
const T& operator*() const { return *curr; }
iterator_rc& operator++();
iterator_rc& operator--();
iterator_rc& operator+=(size_type n);
friend iterator_rc operator+(iterator_rc it, size_type n)
{ it += n; return it; }
iterator_rc& operator-=(size_type n);
friend iterator_rc operator-(iterator_rc it, size_type n)
{ it -= n; return it; }
friend size_type operator-(const iterator_rc& it1, const iterator_rc& it2)
{ return (it1.curr - it2.curr); }
bool operator ==(iterator_rc const& other) const { return curr==other.curr; }
bool operator !=(iterator_rc const& other) const { return !(*this==other); }
};
//------------------------------------------------------------------------------
template<class T, class A>
typename vectr<T,A>::iterator_rc& vectr<T,A>::iterator_rc::operator++()
{
if (curr==last-1) throw Range_error(last-first);
++curr;
return *this;
}
//------------------------------------------------------------------------------
template<class T, class A>
typename vectr<T,A>::iterator_rc& vectr<T,A>::iterator_rc::operator--()
{
if (curr==first) throw Range_error(-1);
--curr;
return *this;
}
//------------------------------------------------------------------------------
template<class T, class A>
typename vectr<T,A>::iterator_rc& vectr<T,A>::iterator_rc::operator+=(size_type n)
{
if (n >= (last-curr)) throw Range_error(curr+n-first);
curr += n;
return *this;
}
//------------------------------------------------------------------------------
template<class T, class A>
typename vectr<T,A>::iterator_rc& vectr<T,A>::iterator_rc::operator-=(size_type n)
{
if (n > curr-first) throw Range_error(curr-n-first);
curr -= n;
}
//------------------------------------------------------------------------------
template<class T, class A> vectr<T,A>::vectr(size_type s)
: sz(s), elem(alloc.allocate(s)), space(s)
{
for (int i = 0; i<sz; ++i)
alloc.construct(&elem[i],T());
}
//------------------------------------------------------------------------------
template<class T, class A> vectr<T,A>::vectr(const vectr& a)
: sz(a.sz), elem(alloc.allocate(a.sz)), space(a.sz)
{
for (int i = 0; i<sz; ++i)
alloc.construct(&elem[i],a.elem[i]);
}
//------------------------------------------------------------------------------
template<class T, class A>
vectr<T,A>& vectr<T,A>::operator=(const vectr<T,A>& a)
{
if (this==&a) return *this; // self-assignment, no work needed
if (a.sz<=space) { // enough space, no need for new allocation
for (int i = 0; i<a.sz; ++i)
alloc.construct(&elem[i],a.elem[i]);
sz = a.sz;
return *this;
}
T* p = alloc.allocate(a.sz); // allocate new space
for (int i = 0; i<a.sz; ++i) // copy elements
alloc.construct(&p[i],a.elem[i]);
for (int i = 0; i<sz; ++i) // destroy old objects
alloc.destroy(&elem[i]);
alloc.deallocate(elem); // deallocate old space
space = sz = a.sz; // set new size
elem = p; // set new elements
return *this;
}
//------------------------------------------------------------------------------
template<class T, class A> vectr<T,A>::~vectr()
{
for (int i = 0; i<sz; ++i)
alloc.destroy(&elem[i]);
alloc.deallocate(elem);
}
//------------------------------------------------------------------------------
template<class T, class A> T& vectr<T,A>::at(size_type n)
{
if (n<0 || sz<=n) throw Range_error(n);
return elem[n];
}
//------------------------------------------------------------------------------
template<class T, class A> void vectr<T,A>::resize(size_type newsize, T val)
{
reserve(newsize);
for (int i = sz; i<newsize; ++i) // construct
alloc.construct(&elem[i],val);
for (int i = newsize; i<sz; ++i) // destroy
alloc.destroy(&elem[i]);
sz = newsize;
}
//------------------------------------------------------------------------------
template<class T, class A> void vectr<T,A>::push_back(const T& val)
{
if (space==0) reserve(8); // start with space for 8 elements
else if (sz==space) reserve(2*space); // get more space
alloc.construct(&elem[sz],val); // add val at end
++sz; // increase the size
}
//------------------------------------------------------------------------------
template<class T, class A> void vectr<T,A>::reserve(size_type newalloc)
{
if (newalloc<=space) return; // never decrease allocation
T* p = alloc.allocate(newalloc); // allocate new space
for (int i = 0; i<sz; ++i) // copy
alloc.construct(&p[i],elem[i]);
for (int i = 0; i<sz; ++i) // destroy
alloc.destroy(&elem[i]);
alloc.deallocate(elem); // deallocate old space
elem = p;
space = newalloc;
}
//------------------------------------------------------------------------------
int main()
try {
vectr<int> vi;
for (int i = 0; i<5; ++i)
vi.push_back(i);
cout << "Size of vector: " << vi.size() << "\n";
for (int i = 0; i<vi.size(); ++i)
cout << vi[i] << ' ';
cout << "\n";
vectr<int>::iterator_rc it(vi.begin(),vi.begin(),vi.end());
cout << *it << "\n";
++it;
cout << *it << "\n";
--it;
cout << *it << "\n";
// --it; // throws error
it += 4;
cout << *it << "\n";
// ++it; // throws error
// it += 1; // throws error
// it = it + 1; // throws error
// it -= 5; // throws error
// it = it - 5; // throws error
vectr<int>::iterator_rc it2(vi.begin(),vi.begin(),vi.end());
int dist = it - it2;
cout << "Distance to begin(): " << dist << "\n";
cout << "it==it2 is " << (it==it2 ? "true" : "false") << "\n";
cout << "it!=it2 is " << (it!=it2 ? "true" : "false") << "\n";
it -= 4;
cout << "it==it2 is " << (it==it2 ? "true" : "false") << "\n";
cout << "it!=it2 is " << (it!=it2 ? "true" : "false") << "\n";
}
catch (Range_error& re) {
cerr << "bad index: " << re.index << "\n";
}
catch (exception& e) {
cerr << "exception: " << e.what() << endl;
}
catch (...) {
cerr << "exception\n";
}
//------------------------------------------------------------------------------