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
|
// StrBlob member
StrBlob::StrBlob(): data(make_shared<vector<string>>()) {}
StrBlob::StrBlob(initializer_list<string> il):
data(make_shared<vector<string>>(il)) {}
StrBlob::StrBlob(const StrBlob &s): data(make_shared<vector<string>>(*s.data)) {}
StrBlob &StrBlob::operator=(const StrBlob &s)
{
data = make_shared<vector<string>>(*s.data);
return *this;
}
inline string &StrBlob::operator[](size_t n)
{
return data->at(n);
}
inline const string &StrBlob::operator[](size_t n) const
{
return data->at(n);
}
void StrBlob::check(size_type i, const string &msg) const
{
if(i >= data->size())
throw out_of_range(msg);
}
string &StrBlob::front()
{
check(0, "front on empty StrBlob");
return data->front();
}
const string &StrBlob::front() const
{
check(0, "front on empty StrBlob");
return data->front();
}
string &StrBlob::back()
{
check(0, "badk on empty StrBlob");
return data->back();
}
const string &StrBlob::back() const
{
check(0, "back on empty StrBlob");
return data->back();
}
bool operator==(const StrBlob &s1, const StrBlob &s2)
{
return *(s1.data) == *(s2.data);
}
bool operator!=(const StrBlob &s1, const StrBlob &s2)
{
return !(s1 == s2);
}
bool operator<(const StrBlob &s1, const StrBlob &s2)
{
return *(s1.data) < *(s2.data);
}
// StrBlobPtr member
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::operator[](size_t n)
{
auto p = check(n, "out of range");
return (*p)[n];
}
const string &StrBlobPtr::operator[](size_t n) const
{
auto p = check(n, "out of range");
return (*p)[n];
}
StrBlobPtr &StrBlobPtr::operator++()
{
check(curr, "increment past end of StrBlobPtr");
++curr;
return *this;
}
StrBlobPtr &StrBlobPtr::operator--()
{
--curr;
check(curr, "increment past end of StrBlobPtr");
return *this;
}
StrBlobPtr StrBlobPtr::operator++(int)
{
StrBlobPtr ret(*this);
++*this;
return ret;
}
StrBlobPtr StrBlobPtr::operator--(int)
{
StrBlobPtr ret(*this);
--*this;
return ret;
}
string &StrBlobPtr::operator*() const
{
auto p = check(curr, "dereference pass end");
return (*p)[curr];
}
string *StrBlobPtr::operator->() const
{
return &this->operator*();
}
string &StrBlobPtr::deref() const
{
auto p = check(curr, "deference pass end");
return (*p)[curr];
}
StrBlobPtr &StrBlobPtr::incr()
{
check(curr, "increment pass end of StrBlobPtr");
++curr;
return *this;
}
bool operator==(const StrBlobPtr &s1, const StrBlobPtr &s2)
{
return s1.curr == s2.curr; // both StrBlobPtr object points to the same StrBlob object.
}
bool operator!=(const StrBlobPtr &s1, const StrBlobPtr &s2)
{
return !(s1 == s2);
}
bool operator<(const StrBlobPtr &s1, const StrBlobPtr &s2)
{
return s1.curr < s2.curr;
}
StrBlobPtr operator+(const StrBlobPtr &s1, size_t n)
{
StrBlobPtr ret(s1);
while(n)
{
++ret;
--n;
}
return ret;
}
StrBlobPtr operator-(const StrBlobPtr &s1, size_t n)
{
StrBlobPtr ret(s1);
while(n)
{
--ret;
--n;
}
return ret;
}
// ConstStrBlobPtr member
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;
}
const string &ConstStrBlobPtr::deref() const
{
auto p = check(curr, "dereference pass end");
return (*p)[curr];
}
ConstStrBlobPtr &ConstStrBlobPtr::incr()
{
check(curr, "increment pass end of StrBlobPtr");
++curr;
return *this;
}
const string &ConstStrBlobPtr::operator*()
{
auto p = check(curr, "deference past end");
return (*p)[curr];
}
const string *ConstStrBlobPtr::operator->()
{
return &this->operator*();
}
StrBlobPtr StrBlob::begin()
{
return StrBlobPtr(*this);
}
StrBlobPtr StrBlob::end()
{
auto ret = StrBlobPtr(*this, data->size());
return ret;
}
|