-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhiVector.cpp
154 lines (115 loc) · 3.63 KB
/
PhiVector.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <bit>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <float.h>
#include <fstream>
#include <numbers>
#include <map>
#include <set>
#include <sstream>
#include <unordered_map>
#include "PhiVector.h"
CPhiVector::CPhiVector() : m_Vec({0}) {}
CPhiVector::CPhiVector(const std::vector<s32>& rVec) : m_Vec(rVec) {}
CPhiVector::CPhiVector(s32 sScalar) : m_Vec({sScalar}) {}
CPhiVector::operator f32() const {
f32 fSum = 0.0;
f32 fPower = 1.0;
for (u32 i = 0; i < m_Vec.size(); ++i) {
fSum += m_Vec[i] * fPower;
fPower *= std::numbers::phi_v<f32>;
}
return fSum;
}
s32 CPhiVector::operator[](u32 uIndex) const {
return uIndex < m_Vec.size() ? m_Vec[uIndex] : 0;
}
CPhiVector CPhiVector::operator-() const {
return CPhiVector(*this) *= -1;
}
CPhiVector CPhiVector::operator*(const CPhiVector& rPhiVector) const {
return CPhiVector(*this) *= rPhiVector;
}
CPhiVector CPhiVector::operator*(s32 sScalar) const {
return CPhiVector(m_Vec) *= sScalar;
}
CPhiVector CPhiVector::operator+(const CPhiVector& rPhiVector) const {
return CPhiVector(*this) += rPhiVector;
}
CPhiVector CPhiVector::operator-(const CPhiVector& rPhiVector) const {
return CPhiVector(*this) -= rPhiVector;
}
std::strong_ordering CPhiVector::operator<=>(const CPhiVector& rPhiVector) const {
CPhiVector diffPhiVector = *this - rPhiVector;
f32 fDiff = diffPhiVector;
if (abs(fDiff) > 1.0) {
return fDiff < 0 ? std::strong_ordering::greater : std::strong_ordering::less;
}
CPhiVector additiveIdentityPhiVector(std::vector<s32>{-1, -1, 1});
CPhiVector scalingPhiVector(std::vector<s32>{0, 1});
for (u32 i = 0; i < diffPhiVector.m_Vec.size() - 2; ++i) {
if (diffPhiVector.m_Vec[i]) {
diffPhiVector += diffPhiVector.m_Vec[i] * additiveIdentityPhiVector;
}
additiveIdentityPhiVector *= scalingPhiVector;
}
fDiff = additiveIdentityPhiVector;
return fDiff == 0.0 ?
std::strong_ordering::equivalent :
fDiff < 0 ?
std::strong_ordering::greater :
std::strong_ordering::less;
}
CPhiVector& CPhiVector::operator*=(const CPhiVector& rPhiVector) {
const std::vector<s32>& rVec = rPhiVector.m_Vec;
std::vector<s32> resultVector(m_Vec.size() + rVec.size() - 1, 0);
for (u32 i = 0; i < m_Vec.size(); ++i) {
if (m_Vec[i]) {
for (u32 j = 0; j < rVec.size(); ++j) {
resultVector[i + j] += m_Vec[i] * rVec[j];
}
}
}
m_Vec.assign(resultVector.begin(), resultVector.end());
return *this;
}
CPhiVector& CPhiVector::operator*=(s32 sScalar) {
if (sScalar) {
for (u32 i = 0; i < m_Vec.size(); ++i) {
m_Vec[i] *= sScalar;
}
} else {
m_Vec.assign(1, 0);
}
return *this;
}
CPhiVector& CPhiVector::operator+=(const CPhiVector& rPhiVector) {
const std::vector<s32>& rVec = rPhiVector.m_Vec;
m_Vec.resize(std::max(m_Vec.size(), rVec.size()), 0);
for (u32 i = 0; i < rVec.size(); ++i) {
m_Vec[i] += rVec[i];
}
return *this;
}
CPhiVector& CPhiVector::operator-=(const CPhiVector& rPhiVector) {
const std::vector<s32>& rVec = rPhiVector.m_Vec;
m_Vec.resize(std::max(m_Vec.size(), rVec.size()), 0);
for (u32 i = 0; i < rVec.size(); ++i) {
m_Vec[i] -= rVec[i];
}
return *this;
}
CPhiVector operator*(s32 sScalar, const CPhiVector& rPhiVector) {
return rPhiVector * sScalar;
}
std::ostream& operator<<(std::ostream& rOStream, const CPhiVector& rPhiVector) {
rOStream << '[' << rPhiVector.m_Vec[0];
if (rPhiVector.m_Vec.size() > 1) {
for (u32 i = 1; i < rPhiVector.m_Vec.size(); ++i) {
rOStream << ", " << rPhiVector.m_Vec[i];
}
}
return rOStream << ']';
}