-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathequation.cpp
138 lines (119 loc) · 3.02 KB
/
equation.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
135
136
137
138
#include "equation.hpp"
std::vector <unsigned short int>
addSortedVectors(Equation &v1, Equation &v2)
{
std::vector <unsigned short int> result;
std::vector <unsigned short int>::iterator it1 = v1.begin();
std::vector <unsigned short int>::iterator it2 = v2.begin();
std::vector <unsigned short int>::iterator end1 = v1.end();
std::vector <unsigned short int>::iterator end2 = v2.end();
result.reserve(v1.size() + v2.size());
while (it1 < end1 || it2 < end2) {
if (it1 < end1 && it2 < end2) {
if (*it1 < *it2) {
result.push_back(*it1);
it1 ++;
} else if (*it2 < *it1) {
result.push_back(*it2);
it2 ++;
} else {
it1 ++;
it2 ++;
}
} else if (it1 < end1) {
result.push_back(*it1);
it1 ++;
} else if (it2 < end2) {
result.push_back(*it2);
it2 ++;
}
}
return result;
}
Equation &
Equation::operator+=(Equation &e)
{
/*
* This can be possibly optimized a bit, by doing the operations on the
* vector itself instead of copying the result afterwards
*/
std::vector <unsigned short int> result;
std::vector <unsigned short int>::iterator it1 = this->begin();
std::vector <unsigned short int>::iterator it2 = e.begin();
std::vector <unsigned short int>::iterator end1 = this->end();
std::vector <unsigned short int>::iterator end2 = e.end();
result.reserve(this->size() + e.size());
while (it1 < end1 || it2 < end2) {
if (it1 < end1 && it2 < end2) {
if (*it1 < *it2) {
result.push_back(*it1);
it1 ++;
} else if (*it2 < *it1) {
result.push_back(*it2);
it2 ++;
} else {
it1 ++;
it2 ++;
}
} else if (it1 < end1) {
result.push_back(*it1);
it1 ++;
} else if (it2 < end2) {
result.push_back(*it2);
it2 ++;
}
}
assign(result.begin(), result.end());
return *this;
}
Equation operator+(Equation &e1, Equation &e2)
{
Equation result;
Equation::iterator it1 = e1.begin();
Equation::iterator it2 = e2.begin();
Equation::iterator end1 = e1.end();
Equation::iterator end2 = e2.end();
// maybe wasting some space but improves performance
result.reserve(e1.size() + e2.size());
while (it1 < end1 || it2 < end2) {
if (it1 < end1 && it2 < end2) {
if (*it1 < *it2) {
result.push_back(*it1);
it1 ++;
} else if (*it2 < *it1) {
result.push_back(*it2);
it2 ++;
} else {
it1 ++;
it2 ++;
}
} else if (it1 < end1) {
result.push_back(*it1);
it1 ++;
} else if (it2 < end2) {
result.push_back(*it2);
it2 ++;
}
}
return result;
}
void
Equation::print()
{
std::cout <<" ";
for (Equation::iterator it = this -> begin();
it != this -> end() - 1; it ++)
std::cout << *it << " + ";
std::cout << at(this -> size() - 1) << std::endl;
return;
}
void
Equation::printPython()
{
std::cout << "[";
for (Equation::iterator it = this -> begin();
it != this -> end() - 1; it ++)
std::cout << *it << ",";
std::cout << at(this->size() - 1) << "]" << std::endl;
return;
}