-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearncpp-com-15-3.cpp
134 lines (120 loc) · 3.46 KB
/
learncpp-com-15-3.cpp
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
#include <iostream>
#include <chrono>
// a time logger
class Timer {
private:
using clock_t = std::chrono::high_resolution_clock;
using second_t = std::chrono::duration<double, std::ratio<1> >;
std::chrono::time_point<clock_t> m_beg;
public:
Timer(): m_beg(clock_t::now()) { }
void reset() { m_beg = clock_t::now(); }
double elapsed() const
{
return std::chrono::duration_cast<second_t>(clock_t::now() - m_beg).count();
}
};
// a dynamic array class template
template <class T>
class DynamicArray {
public:
DynamicArray(int length):
m_array(new T[length]), m_length(length) { }
~DynamicArray() { delete [] m_array; }
// copy ctor
DynamicArray(const DynamicArray& arr):
m_array(new T[arr.m_length]),
m_length(arr.m_length)
{
for (int i = 0; i != m_length; ++i)
m_array[i] = arr.m_array[i];
}
// copy assignment
DynamicArray& operator=(const DynamicArray& arr)
{
if (&arr == this) return *this;
// delete old thing
delete [] m_array;
m_length = arr.m_length;
m_array = new T[m_length];
for (int i = 0; i != m_length; ++i)
m_array[i] = arr.m_array[i];
return *this;
}
int getLength() const { return m_length; }
T& operator[](int index) { return m_array[index]; }
const T& operator[](int index) const
{
return m_array[index];
}
private:
T* m_array;
int m_length;
};
// return a copy of arr with all of the values doubled
DynamicArray<int> cloneArrayAndDouble(const DynamicArray<int>& arr)
{
DynamicArray<int> dbl(arr.getLength());
for (int i = 0; i != arr.getLength(); ++i)
dbl[i] = arr[i] * 2;
return dbl;
}
/* movable DynamicArray */
namespace mv {
template <class T>
class DynamicArray {
private:
T* m_array;
int m_length;
public:
DynamicArray(int length):
m_array(new T[length]), m_length(length) { }
~DynamicArray() { delete [] m_array; }
// disable copy ctor
DynamicArray(const DynamicArray&) = delete;
// disable copy assignment
DynamicArray& operator=(const DynamicArray&) = delete;
// move ctor
DynamicArray(DynamicArray&& arr):
m_array(arr.m_array), m_length(arr.m_length)
{
arr.m_length = 0;
arr.m_array = nullptr;
}
// move assignment
DynamicArray& operator=(DynamicArray&& arr)
{
if (&arr == this) return *this;
delete [] m_array;
m_length = arr.m_length;
m_array = arr.m_array;
arr.m_length = 0;
arr.m_array = nullptr;
return *this;
}
int getLength() const { return m_length; }
T& operator[](int index) { return m_array[index]; }
const T& operator[](int index) const
{
return m_array[index];
}
}; // DynamicArray
//(mv) return a copy of arr with all of the values doubled
DynamicArray<int> cloneArrayAndDouble(const DynamicArray<int>& arr)
{
DynamicArray<int> dbl(arr.getLength());
for (int i = 0; i != arr.getLength(); ++i)
dbl[i] = arr[i] * 2;
return dbl;
}
}; // mv
// test
int main ()
{
Timer t;
DynamicArray<int> arr(1000000);
for (int i = 0; i != arr.getLength(); ++i)
arr[i] = i;
arr = cloneArrayAndDouble(arr);
std::cout << t.elapsed();
}