-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathExer14_27_StrBlob.h
345 lines (345 loc) · 10.6 KB
/
Exer14_27_StrBlob.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
342
343
344
345
#ifndef STRBLOB_H
#define STRBLOB_H
#include <cstddef>
#include <string>
#include <vector>
#include <initializer_list>
#include <memory>
#include <stdexcept>
using std::size_t;
using std::string;
using std::vector;
using std::initializer_list;
using std::shared_ptr;
using std::make_shared;
using std::weak_ptr;
using std::runtime_error;
using std::out_of_range;
class StrBlobPtr;
class ConstStrBlobPtr;
class StrBlob {
friend class StrBlobPtr;
friend class ConstStrBlobPtr;
// overloaded equality operators required by exercise 14.16
friend bool operator==(const StrBlob&, const StrBlob&);
friend bool operator!=(const StrBlob&, const StrBlob&);
// overloaded relational operator required by exercise 14.18
friend bool operator<(const StrBlob&, const StrBlob&);
friend bool operator<=(const StrBlob&, const StrBlob&);
friend bool operator>(const StrBlob&, const StrBlob&);
friend bool operator>=(const StrBlob&, const StrBlob&);
public:
typedef vector<string>::size_type size_type;
StrBlob();
StrBlob(initializer_list<string> il);
size_type size() const { return data->size(); }
// add and remove elements
void push_back(const string &t) { data->push_back(t); }
void pop_back();
// elements access
string& front();
string& back();
// const versions of front and back required by exercise 12.2
const string& front() const;
const string& back() const;
// return reference count
const size_type use_count() const { return data.use_count(); }
StrBlobPtr begin();
StrBlobPtr end();
ConstStrBlobPtr begin() const;
ConstStrBlobPtr end() const;
// access elements
string& at(size_type) const;
// subscript operator required by exercise 14.26
// the difference between at and subscript is that subscript doesn't throw
// exception if the index is out of range.
string& operator[](size_t n)
{ return (*data)[n]; }
const string& operator[](size_t n) const
{ return (*data)[n]; }
private:
shared_ptr<vector<string>> data;
// throws msg if data[i] isn't valid
void check(size_type i, const string &msg) const;
};
// constructor
StrBlob::StrBlob() : data(make_shared<vector<string>>()) {}
StrBlob::StrBlob(initializer_list<string> il) :
data(make_shared<vector<string>>(il)) {};
void StrBlob::check(size_type i, const string &msg) const
{
if(i >= data->size())
throw out_of_range(msg);
}
// add and remove elements
void StrBlob::pop_back()
{
check(0, "pop_back on empty StrBlob");
data->pop_back();
}
// elements access
string& StrBlob::front()
{
check(0, "front on empty StrBlob");
data->front();
}
string& StrBlob::back()
{
check(0, "back on empty StrBlob");
data->back();
}
const string& StrBlob::front() const
{
check(0, "front on empty StrBlob");
data->front();
}
const string& StrBlob::back() const
{
check(0, "back on empty StrBlob");
data->back();
}
string& StrBlob::at(size_type n) const
{
check(n, "index out of range");
return data->at(n);
}
// friend definition
bool operator==(const StrBlob &lhs, const StrBlob &rhs)
{
return *lhs.data == *rhs.data;
}
bool operator!=(const StrBlob &lhs, const StrBlob &rhs)
{
return !(lhs == rhs);
}
bool operator<(const StrBlob &lhs, const StrBlob &rhs)
{
return *lhs.data < *rhs.data;
}
bool operator<=(const StrBlob &lhs, const StrBlob &rhs)
{
return (lhs < rhs || lhs == rhs);
}
bool operator>(const StrBlob &lhs, const StrBlob &rhs)
{
return !(lhs < rhs || lhs == rhs);
}
bool operator>=(const StrBlob &lhs, const StrBlob &rhs)
{
return !(lhs < rhs);
}
// ******************************
// nonconst version of StrBlobPtr
// ******************************
class StrBlobPtr{
// overloaded equality operators required by exercise 14.16
friend bool operator==(const StrBlobPtr&, const StrBlobPtr&);
friend bool operator!=(const StrBlobPtr&, const StrBlobPtr&);
// overloaded relational operators required by exercise 14.18
friend bool operator<(const StrBlobPtr&, const StrBlobPtr&);
friend bool operator<=(const StrBlobPtr&, const StrBlobPtr&);
friend bool operator>(const StrBlobPtr&, const StrBlobPtr&);
friend bool operator>=(const StrBlobPtr&, const StrBlobPtr&);
public:
StrBlobPtr() : curr(0) {}
StrBlobPtr(StrBlob &a, size_t sz = 0) : wptr(a.data), curr(sz) {}
string& deref() const;
StrBlobPtr& incr();
// subscript operator required by exercise 14.26
string& operator[](size_t n);
const string& operator[](size_t n) const;
// increment and decrement operator required by exercise 14.27
StrBlobPtr& operator++(); // prefix increment, return reference
StrBlobPtr& operator--(); // prefix decrement
StrBlobPtr operator++(int); // postfix increment, return value
StrBlobPtr operator--(int); // postfix decrement
private:
shared_ptr<vector<string>> check(size_t, const string&) const;
weak_ptr<vector<string>> wptr;
size_t curr;
};
shared_ptr<vector<string>> StrBlobPtr::check(size_t i, const string& msg) const
{
auto ret = wptr.lock();
if(!ret)
throw runtime_error("unbound StrBlobPtr");
if(i >= ret->size())
throw out_of_range(msg);
return ret;
}
string& StrBlobPtr::deref() const
{
auto p = check(curr, "dereference past end");
return (*p)[curr];
}
StrBlobPtr& StrBlobPtr::incr()
{
check(curr, "increment past end of StrBlobPtr");
++curr;
return *this;
}
// It should be curr + n, because this points to the position of index curr,
// if we dereference an element by index, we should get the element at the
// position of curr + n.
string& StrBlobPtr::operator[](size_t n)
{
auto p = check(curr + n, "deference past end");
return (*p)[curr + n];
}
const string& StrBlobPtr::operator[](size_t n) const
{
auto p = check(curr + n, "deference past end");
return (*p)[curr + n];
}
StrBlobPtr& StrBlobPtr::operator++()
{
check(curr, "increment past end of StrBlobStr");
++curr; // advance the current state
return *this;
}
StrBlobPtr& StrBlobPtr::operator--()
{
--curr; // move the current state back one element
check(curr, "decrement past begin of StrBlobPtr");
return *this;
}
StrBlobPtr StrBlobPtr::operator++(int)
{
auto ret = *this; // save the current value
++*this; // advance one element; prefix ++ checks the increment
return ret; // return the saved value
}
StrBlobPtr StrBlobPtr::operator--(int)
{
auto ret = *this; // save the current value
--*this; // move backward one element; prefix -- checks the decrement
return ret; // return the saved value
}
// definitions of friend operators
// shared_ptr has its overloaded version of equality and relational operators, but weak_ptr doesn't
// so we have to use lock() to compare
bool operator==(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
return lhs.curr == rhs.curr;
}
bool operator!=(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
return !(lhs == rhs);
}
bool operator<(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
return lhs.curr < rhs.curr;
}
bool operator<=(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
return (lhs < rhs || lhs == rhs);
}
bool operator>(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
return !(lhs < rhs || lhs == rhs);
}
bool operator>=(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
return !(lhs < rhs);
}
// ***************************
// const version of StrBlobPtr
// ***************************
class ConstStrBlobPtr{
// overloaded equality operators required by exercise 14.16
friend bool operator==(const ConstStrBlobPtr&, const ConstStrBlobPtr&);
friend bool operator!=(const ConstStrBlobPtr&, const ConstStrBlobPtr&);
// overloaded relational operators required by exercise 14.18
friend bool operator<(const ConstStrBlobPtr&, const ConstStrBlobPtr&);
friend bool operator<=(const ConstStrBlobPtr&, const ConstStrBlobPtr&);
friend bool operator>(const ConstStrBlobPtr&, const ConstStrBlobPtr&);
friend bool operator>=(const ConstStrBlobPtr&, const ConstStrBlobPtr&);
public:
ConstStrBlobPtr() : curr(0) {}
ConstStrBlobPtr(const StrBlob &a, size_t sz = 0) : wptr(a.data),curr(sz) {}
string& deref() const;
ConstStrBlobPtr& incr();
// subscript operator required by exercise 14.26
// ConstStrBlobPtr should only have const version of subscript operator
string& operator[](size_t n) const;
private:
shared_ptr<vector<string>> check(size_t, const string&) const;
const weak_ptr<vector<string>> wptr;
size_t curr;
};
shared_ptr<vector<string>> ConstStrBlobPtr::check(size_t i, const string& msg) const
{
auto ret = wptr.lock();
if(!ret)
throw runtime_error("unbound StrBlobPtr");
if(i >= ret->size())
throw out_of_range(msg);
return ret;
}
string& ConstStrBlobPtr::deref() const
{
auto p = check(curr, "dereference past end");
return (*p)[curr];
}
ConstStrBlobPtr& ConstStrBlobPtr::incr()
{
check(curr, "increment past end of StrBlobPtr");
++curr;
return *this;
}
string& ConstStrBlobPtr::operator[](size_t n) const
{
auto p = check(curr + n, "deference past end");
return (*p)[curr + n];
}
// definitions of friend operators
bool operator==(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs)
{
return lhs.wptr.lock() == rhs.wptr.lock() && lhs.curr == rhs.curr;
}
bool operator!=(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs)
{
return !(lhs == rhs);
}
bool operator<(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs)
{
return lhs.curr < rhs.curr;
}
bool operator<=(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs)
{
return lhs.curr <= rhs.curr;
}
bool operator>(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs)
{
return lhs.curr > rhs.curr;
}
bool operator>=(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs)
{
return lhs.curr >= rhs.curr;
}
// we have to define these member functions here, after the definition of StrBlobPtr
StrBlobPtr StrBlob::begin()
{
return StrBlobPtr(*this);
}
StrBlobPtr StrBlob::end()
{
auto ret = StrBlobPtr(*this, data->size());
return ret;
}
ConstStrBlobPtr StrBlob::begin() const
{
return ConstStrBlobPtr(*this);
}
ConstStrBlobPtr StrBlob::end() const
{
auto ret = ConstStrBlobPtr(*this, data->size());
return ret;
}
#endif
// Note: If two pointers point to two unrelated objects, the result of comparison
// is undefined(See page 120). Here we assume that lhs and rhs point to the same
// object, thus StrBlobPtr has consistent behaviour with built-in pointer. On the
// other hand, we can compare two pointers with == regardless whether they point
// to the same object. Thus it seems that equality and relational operators are
// not consistent with each other.