-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.cpp
80 lines (65 loc) · 1.43 KB
/
Vector.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
#include "Vector.h"
Vector::Vector() {
m_vector = nullptr;
m_length = 0;
}
Vector::Vector(double *vector, int length) {
init(vector, length);
}
Vector::Vector(const Vector &V) {
init(V.m_vector, V.m_length);
}
void Vector::clean() {
if (m_length > 0 && m_vector != nullptr) {
delete[] m_vector;
m_length = 0;
}
}
void Vector::init(double *vector, int length) {
if (length > 0) {
clean();
m_vector = new double[length];
if (m_vector) {
m_length = length;
for (int i = 0; i < length; i++)
m_vector[i] = vector[i];
}
}
}
void Vector::copy(const Vector &V) {
init(V.m_vector, V.m_length);
}
Vector::~Vector() {
clean();
}
double Vector::get(int index) {
if (index < m_length && index >= 0)
return m_vector[index];
return 0;
}
void Vector::set(int index, const double data) {
if (index < m_length && index >= 0)
m_vector[index] = data;
}
int Vector::length() {
return m_length;
}
double Vector::operator *(const Vector &rhs) {
double ret = 0;
if (m_length > 0 && m_length == rhs.m_length) {
for (int i = 0; i < m_length; i++)
ret += (m_vector[i] * rhs.m_vector[i]);
}
return ret;
}
void Vector::debug() {
printf("\n---------- DEBUG (Vector) ----------\n");
printf("[");
for (int i = 0; i < m_length; i++) {
if (i > 0)
printf(", ");
printf("%lf", m_vector[i]);
}
printf("]\n");
printf("---------- -------------- ----------\n");
}