This repository was archived by the owner on Apr 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathex14_18_StrVec.h
226 lines (191 loc) · 6.39 KB
/
ex14_18_StrVec.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
#include "headers.h"
class StrVec {
friend bool operator==(const StrVec &lhs, const StrVec &rhs);
friend bool operator!=(const StrVec &lhs, const StrVec &rhs);
friend bool operator<(const StrVec &lhs, const StrVec &rhs);
public:
StrVec() : elements(nullptr), first_free(nullptr), cap(nullptr) {
cout << "StrVec()" << endl;
}
StrVec(initializer_list<string>);
StrVec(const StrVec &);
StrVec(StrVec &&other);
StrVec &operator=(const StrVec &sv);
StrVec &operator=(StrVec &&rhs);
~StrVec();
size_t size() const;
size_t capacity() const;
string *begin() const;
string *end() const;
void push_back(const string &s);
void reserve(const int new_capacity);
void resize(const int new_size);
string *at(const int pos);
void info();
private:
allocator<string> alloc;
pair<string *, string *> allocate_n_copy(const string *b, const string *e);
void free();
void possible_reallocate();
void reallocate();
void reallocate_by_capacity(const int new_capacity);
string *elements;
string *first_free;
string *cap;
};
StrVec::StrVec(const StrVec &sv) {
cout << "StrVec(const StrVec &)" << endl;
auto pair = allocate_n_copy(sv.begin(), sv.end());
elements = pair.first;
first_free = pair.second;
cap = pair.second;
}
StrVec::StrVec(StrVec &&other) : elements(other.elements), first_free(other.first_free), cap(other.cap) {
cout << "StrVec(StrVec &&)" << endl;
other.elements = other.first_free = other.cap = nullptr;
}
StrVec::StrVec(initializer_list<string> il) {
cout << "StrVec(initializer_list<string>" << endl;
auto init_size = il.size();
elements = alloc.allocate(2 * init_size);
for (auto p = il.begin(); p != il.end(); p++) {
alloc.construct(p - il.begin() + elements, *p);
}
first_free = elements + init_size;
cap = elements + 2 * init_size;
}
StrVec &StrVec::operator=(const StrVec &sv) {
cout << "StrVec::operator=(StrVec &). Copying resource." << endl;
auto pair = allocate_n_copy(sv.begin(), sv.end());
free();
elements = pair.first;
first_free = pair.second;
cap = pair.second;
return *this;
}
StrVec &StrVec::operator=(StrVec &&rhs) {
cout << "StrVec::operator=(StrVec &&). Moving resource." << endl;
if (this != &rhs) { // rhs is still a reference. When you dereference it, you get a pointer.
free();
elements = rhs.elements;
first_free = rhs.elements;
cap = rhs.cap;
}
return *this;
}
StrVec::~StrVec() {
cout << "~StrVec()" << endl;
free();
}
size_t StrVec::size() const { return first_free - elements; }
size_t StrVec::capacity() const { return cap - elements; }
string *StrVec::begin() const { return elements; }
string *StrVec::end() const { return first_free; }
void StrVec::push_back(const string &s) {
possible_reallocate();
alloc.construct(first_free++, s);
}
string *StrVec::at(const int pos) {
return (elements + pos);
}
pair<string *, string *> StrVec::allocate_n_copy(const string *b, const string *e) {
auto data = alloc.allocate(e - b);
return {data, uninitialized_copy(b, e, data)};
}
void StrVec::free() {
if (elements) {
for_each(elements, first_free, [this](const string &s) { alloc.destroy(&s); });
alloc.deallocate(elements, cap - elements);
}
}
void StrVec::possible_reallocate() {
if (size() == capacity()) {
reallocate();
}
}
void StrVec::reallocate_by_capacity(const int new_capacity) {
auto new_elements = alloc.allocate(new_capacity);
const size_t origin_size = size();
for (size_t pos = 0; pos < size(); pos++) {
alloc.construct(new_elements + pos, std::move(*(elements + pos)));
}
free();
elements = new_elements;
first_free = new_elements + origin_size;
cap = new_elements + new_capacity;
}
void StrVec::reallocate() { // called when size == capacity, cap == first_free
auto new_capacity = capacity() ? 2 * capacity() : 1;
reallocate_by_capacity(new_capacity);
}
void StrVec::reserve(const int new_capacity) {
if (new_capacity > capacity()) {
reallocate_by_capacity(new_capacity);
}
}
void StrVec::resize(const int new_size) {
if (new_size > size()) {
reallocate_by_capacity(new_size);
for (auto p = first_free; p != cap; p++) {
alloc.construct(p, "");
}
} else if (new_size < size()) {
for (auto p = first_free; p != elements + new_size;) {
alloc.destroy(--p);
}
first_free = elements + new_size;
}
}
void StrVec::info() {
cout << "size: " << size() << ", capacity: " << capacity() << " [ ";
for (auto p = elements; p != first_free; p++) {
cout << *p << " ";
}
cout << "]" << endl;
}
bool operator==(const StrVec &lhs, const StrVec &rhs) {
// http://www.cplusplus.com/reference/vector/vector/operators/
// The equality comparison (operator==) is performed by first comparing sizes,
// and if they match, the elements are compared sequentially using operator==,
// stopping at the first mismatch (as if using algorithm equal).
if (lhs.size() == rhs.size()) {
for (auto lhs_p = lhs.elements, rhs_p = rhs.elements;
lhs_p != lhs.first_free && rhs_p != rhs.first_free;
lhs_p++, rhs_p++) {
if (*lhs_p != *rhs_p) {
return false;
}
}
return true;
} else {
return false;
}
}
bool operator!=(const StrVec &lhs, const StrVec &rhs) {
return !(lhs == rhs);
}
bool operator<(const StrVec &lhs, const StrVec &rhs) {
// http://www.cplusplus.com/reference/vector/vector/operators/
// The less-than comparison (operator<) behaves as if using algorithm
// lexicographical_compare, which compares the elements sequentially using operator<
// in a reciprocal manner (i.e., checking both a<b and b<a) and stopping at the first
// occurrence.
for (auto lhs_p = lhs.elements, rhs_p = rhs.elements;
lhs_p != lhs.first_free && rhs_p != rhs.first_free;
lhs_p++, rhs_p++) {
if (*lhs_p == *rhs_p) {
continue;
} else if (*lhs_p < *rhs_p) {
return true;
} else {
return false;
}
}
// Consider: ["A"] and ["A", "B"];
return lhs.size() < rhs.size();
}
int main() {
StrVec v1({"Hi", "Hello"});
StrVec v2(v1);
StrVec v3(std::move(v1));
}