-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseVector.cpp
116 lines (91 loc) · 3.24 KB
/
BaseVector.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
//
// Created by mfbut on 11/14/2019.
//
#include "BaseVector.h"
#include "Vector.h"
#include "ConstVectorIterator.h"
#include "VectorIterator.h"
#include "Matrix.h"
Matrix::BaseVector::operator Matrix() const {
std::vector<std::vector<value_type>> matrixInput;
std::vector<value_type> firstRow;
// allocates memory aka reserves space
firstRow.reserve(size());
for (int i = 0; i < size(); i++) {
firstRow.push_back(at(i));
}
matrixInput.push_back(firstRow);
return *(new Matrix(matrixInput));
}
Matrix::VectorIterator Matrix::BaseVector::begin() {
// allocates memory for a pointer vectorIterator at pos 0, beg pos
return *new VectorIterator(this, 0);
}
Matrix::VectorIterator Matrix::BaseVector::end() {
// allocates memory for a pointer vectorIterator for BaseVector at last pos
return *new VectorIterator(this, this->size());
}
Matrix::ConstVectorIterator Matrix::BaseVector::begin() const {
// allocates memory for a pointer vectorIterator at pos 0, beg pos
return *new ConstVectorIterator(this, 0);
}
Matrix::ConstVectorIterator Matrix::BaseVector::end() const {
// allocates memory for a pointer vectorIterator for BaseVector at last pos
return *new ConstVectorIterator(this, this->size());
}
Matrix::ConstVectorIterator Matrix::BaseVector::cbegin() const {
// allocates memory for a pointer vectorIterator at pos 0, beg pos
return *new ConstVectorIterator(this, 0);
}
Matrix::ConstVectorIterator Matrix::BaseVector::cend() const {
// allocates memory for a pointer vectorIterator for BaseVector at last pos
return *new ConstVectorIterator(this, this->size());
}
Matrix::BaseVector& Matrix::BaseVector::operator+=(const ConstBaseVector& rhs) {
// vector += vector
// iterates over rhs and adds each element at the index i
// return the base vector
for (int i = 0; i < this->size(); i++) {
this->at(i) += rhs.at(i);
}
return *this;
}
Matrix::BaseVector& Matrix::BaseVector::operator-=(const ConstBaseVector& rhs) {
// vector -= vector
// iterates over rhs and adds each element at the index i
// return the base vector
for (int i = 0; i < rhs.size(); i++) {
this->at(i) -= rhs.at(i);
}
return *this;
}
Matrix::BaseVector& Matrix::BaseVector::operator+=(const BaseVector::value_type& rhs) {
// vector += scalar
// iterates over the size of the current instance of vector
// adds each element by the scalar
// returns the base vector
for (int i = 0; i < size(); i++) {
this->at(i) += rhs;
}
return *this;
}
Matrix::BaseVector& Matrix::BaseVector::operator-=(const BaseVector::value_type& rhs) {
// vector -= scalar
// iterates over the size of the current instance of vector
// adds each element by the scalar
// returns the base vector
for (int i = 0; i < size(); i++) {
this->at(i) -= rhs;
}
return *this;
}
Matrix::BaseVector& Matrix::BaseVector::operator*=(const BaseVector::value_type& rhs) {
// vector *= scalar
// iterates over the size of the current instance of vector
// multiplies each element by the respective scalar
// returns the base vector
for (int i = 0; i < size(); i++) {
this->at(i) *= rhs;
}
return *this;
}