-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.ixx
190 lines (148 loc) · 4.96 KB
/
Vector.ixx
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
module;
#include <stdexcept>
#include <iostream>
#include <string>
export module Vector; // defining the module called "Vector"
const bool depuracion = true;
using namespace std;
enum class Error_action { ignore, throwing, terminating, logging }; // error-handling alternatives
constexpr Error_action default_Error_action = Error_action::throwing; // a default
enum class Error_code { range_error, length_error }; // individual errors
string error_code_name[]{ "range error", "length error" };
// names of individual errors
template<Error_action action = default_Error_action, class C>
constexpr void expect(C cond, Error_code x) // take "action" if the expected condition "cond" doesn't hold
{
if constexpr (action == Error_action::logging)
if (!cond()) std::cerr << "expect() failure: " << int(x) << ' ' << error_code_name[int(x)] << '\n';
if constexpr (action == Error_action::throwing)
if (!cond()) throw x;
if constexpr (action == Error_action::terminating)
if (!cond()) terminate();
// or no action
}
export class Vector {
public:
Vector(int s);
double& operator[](int i);
Vector() { elem = nullptr; sz = 0; }; // default initalize to "empty"; that is, to no elements
int size();
~Vector() { delete[] elem; };
Vector(std::initializer_list<double>); // initialize with a list of doubles
Vector(const Vector& a); // copy constructor
Vector& operator=(const Vector& a); // copy assignment
Vector(Vector&& a); // move constructor
Vector& operator=(Vector&& a); // move assignment
void push_back(double); // add element at end, increasing the size by one
double* begin();
double* end();
private:
double* elem; // elem points to an array of sz doubles
int sz;
};
Vector::Vector(const Vector& a) // copy constructor
:elem{ new double[a.sz] }, // allocate space for elements
sz{ a.sz }
{
if (depuracion) { cout << "Vector::Vector constructor de copia " << endl; }
for (int i = 0; i != sz; ++i) // copy elements
elem[i] = a.elem[i];
}
Vector& Vector::operator=(const Vector& a) // copy assignment
{
if (depuracion) { cout << "Vector::operator= copia" << endl; }
double* p = new double[a.sz];
for (int i = 0; i != a.sz; ++i)
p[i] = a.elem[i];
delete[] elem; // delete old elements
elem = p;
sz = a.sz;
return *this;
}
Vector::Vector(Vector&& a) // move constructor
// The && means �rvalue reference� and is a reference to which we can bind an rvalue.The word �rvalue� is intended to complement
// �lvalue, � which roughly means �something that can appear on the left - hand side of an assignment�[Stroustrup, 2010].So an rvalue is
// - to a first approximation � a value that you can�t assign to, such as an integer returned by a function call.Thus, an rvalue reference is a
// reference to something that nobody else can assign to, so we can safely �steal� its value.The res local variable in operator+() for Vectors is an example.
:elem{ a.elem }, // "grab the elements" from a
sz{ a.sz }
{
if (depuracion) { cout << "Vector::Vector constructor 'move' " << endl; }
a.elem = nullptr; // now a has no elements
a.sz = 0;
}
Vector& Vector::operator=(Vector&& a) // move assignment
{
if (depuracion) { cout << "Vector::operator= move " << endl; }
delete[] elem; // delete old elements
elem = a.elem;
a.elem = nullptr; // now a has no elements
sz = a.sz;
a.sz = 0;
return *this;
}
Vector::Vector(int s)
{
if (depuracion) { cout << "Vector::Vector (int s)" << endl; }
if (s < 0)
throw std::length_error{ "Vector constructor: negative size" };
elem = new double[s];
sz = s;
}
Vector::Vector(std::initializer_list<double> lst) // initialize with a list
:elem{ new double[lst.size()] }, sz{ static_cast<int>(lst.size()) }
{
if (depuracion) { cout << "Vector::Vector(std::initializer_list<double> lst) " << endl; }
copy(lst.begin(), lst.end(), elem); // copy from lst into elem
}
void Vector::push_back(double newElement) // initialize with a list
{
if (depuracion) { cout << "Vector::push_back) " << endl; }
Vector newVector(sz + 1);
for (int i=0; i!= sz; ++i)
{
newVector[i] = elem[i];
}
newVector[sz] = newElement;
sz = newVector.size();
delete[] elem;
elem = newVector.elem;
newVector.elem=nullptr;
}
/*
double& Vector::operator[](int i)
{
if (!(0 <= i && i < size()))
throw std::out_of_range{ "Vector::operator[]" };
return elem[i];
}
*/
double& Vector::operator[](int i)
{
if (depuracion) { cout << "Vector::operator[]) " << endl; }
expect([i, this] { return 0 <= i && i < size(); },
Error_code::range_error);
return elem[i];
}
int Vector::size()
{
if (depuracion) { cout << "Vector::size() " << endl; }
return sz;
}
//export bool operator==(const Vector& v1, const Vector& v2)
export bool operator==(Vector& v1, Vector& v2)
{
if (depuracion) { cout << "Vector::operator == " << endl; }
if (v1.size() != v2.size())
return false;
for (int i = 0; i < v1.size(); ++i)
if (v1[i] != v2[i])
return false;
return true;
}
double* Vector::begin() {
return &elem[0];
}
double* Vector::end() {
return &elem[0] + sz;
}