@@ -23,6 +23,38 @@ class Vector {
23
23
}
24
24
}
25
25
26
+ Vector (const Vector<T> &other)
27
+ : buf_(), objs_(NULL ), const_objs_(NULL ),
28
+ size_ (0 ), capacity_(0 ), fixed_(other.fixed_) {
29
+ if (other.buf_ .get () == NULL ) {
30
+ objs_ = other.objs_ ;
31
+ const_objs_ = other.const_objs_ ;
32
+ size_ = other.size_ ;
33
+ capacity_ = other.capacity_ ;
34
+ } else {
35
+ copy (other.const_objs_ , other.size_ , other.capacity_ );
36
+ }
37
+ }
38
+
39
+ Vector &operator =(const Vector<T> &other) {
40
+ clear ();
41
+ fixed_ = other.fixed_ ;
42
+ if (other.buf_ .get () == NULL ) {
43
+ objs_ = other.objs_ ;
44
+ const_objs_ = other.const_objs_ ;
45
+ size_ = other.size_ ;
46
+ capacity_ = other.capacity_ ;
47
+ } else {
48
+ copy (other.const_objs_ , other.size_ , other.capacity_ );
49
+ }
50
+ return *this ;
51
+ }
52
+
53
+ #if __cplusplus >= 201103L
54
+ Vector (Vector &&) noexcept = default;
55
+ Vector &operator =(Vector<T> &&) noexcept = default ;
56
+ #endif
57
+
26
58
void map (Mapper &mapper) {
27
59
Vector temp;
28
60
temp.map_ (mapper);
@@ -225,14 +257,17 @@ class Vector {
225
257
// realloc() assumes that T's placement new does not throw an exception.
226
258
void realloc (std::size_t new_capacity) {
227
259
MARISA_DEBUG_IF (new_capacity > max_size (), MARISA_SIZE_ERROR);
260
+ copy (objs_, size_, new_capacity);
261
+ }
228
262
229
- scoped_array<char > new_buf (
230
- new (std::nothrow) char [sizeof (T) * new_capacity]);
263
+ // copy() assumes that T's placement new does not throw an exception.
264
+ void copy (const T *src, std::size_t src_size, std::size_t capacity) {
265
+ scoped_array<char > new_buf (new (std::nothrow) char [sizeof (T) * capacity]);
231
266
MARISA_DEBUG_IF (new_buf.get () == NULL , MARISA_MEMORY_ERROR);
232
267
T *new_objs = reinterpret_cast <T *>(new_buf.get ());
233
268
234
- for (std::size_t i = 0 ; i < size_ ; ++i) {
235
- new (&new_objs[i]) T (objs_ [i]);
269
+ for (std::size_t i = 0 ; i < src_size ; ++i) {
270
+ new (&new_objs[i]) T (src [i]);
236
271
}
237
272
for (std::size_t i = 0 ; i < size_; ++i) {
238
273
objs_[i].~T ();
@@ -241,12 +276,9 @@ class Vector {
241
276
buf_.swap (new_buf);
242
277
objs_ = new_objs;
243
278
const_objs_ = new_objs;
244
- capacity_ = new_capacity;
279
+ size_ = src_size;
280
+ capacity_ = capacity;
245
281
}
246
-
247
- // Disallows copy and assignment.
248
- Vector (const Vector &);
249
- Vector &operator =(const Vector &);
250
282
};
251
283
252
284
} // namespace vector
0 commit comments