@@ -17,21 +17,29 @@ static std::size_t next_power2(std::size_t size) {
17
17
}
18
18
19
19
template <typename T>
20
- vector<T>::vector() : _data(NULL ), _size(0 ) {
20
+ vector<T>::vector() :
21
+ _data (NULL ),
22
+ _capacity(0 ),
23
+ _size(0 ) {
21
24
resize_capacity (1 );
22
25
}
23
26
24
27
template <typename T>
25
- vector<T>::vector(std::size_t size, const T &_default): _size(size) {
26
- this ->_data = NULL ;
28
+ vector<T>::vector(std::size_t size, const T &_default):
29
+ _data (NULL ),
30
+ _capacity(0 ),
31
+ _size(size) {
27
32
resize_capacity (size);
28
33
for (std::size_t i = 0 ; i < size; i++) {
29
34
this ->_data [i] = _default;
30
35
}
31
36
}
32
37
33
38
template <typename T>
34
- vector<T>::vector(const vector<T> &o) : _size(o._size) {
39
+ vector<T>::vector(const vector<T> &o) :
40
+ _data (NULL ),
41
+ _capacity(0 ),
42
+ _size(o._size) {
35
43
resize_capacity (o._capacity );
36
44
std::memcpy (_data, o._data , _capacity*sizeof (T));
37
45
}
@@ -62,14 +70,15 @@ template <typename T>
62
70
typename vector<T>::const_iterator vector<T>::end() const { return this ->_data + this ->_size ; }
63
71
64
72
template <typename T>
65
- void vector<T>::resize_capacity(std::size_t capacity) {
73
+ void vector<T>::resize_capacity(volatile std::size_t capacity) {
66
74
// internal; assumes capacity to be power of 2
67
75
// and will also make a copy of array and move _data pointer.
68
76
// assumes size<=current_capacity and size<=new_capacity
69
- const std::size_t data_size = capacity*sizeof (T);
77
+ const std::size_t data_size = std::min ( capacity, this -> _capacity ) *sizeof (T);
70
78
T *_new_data = new T[capacity];
71
79
if (!_new_data) return ; // malloc failed
72
80
std::memcpy (_new_data, this ->_data , data_size);
81
+
73
82
delete[] this ->_data ; // should be no-op if _data == NULL
74
83
this ->_data = _new_data;
75
84
this ->_capacity = capacity;
0 commit comments