aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/vector.h22
1 files changed, 12 insertions, 10 deletions
diff --git a/src/vector.h b/src/vector.h
index 55c7663..e0dc239 100644
--- a/src/vector.h
+++ b/src/vector.h
@@ -4,41 +4,43 @@ inline double sq(double x) noexcept {
return x * x;
}
+template <typename T>
struct Vector {
- double data[2];
+ T data[2];
- double comp(int x, int y) const {
+ T comp(int x, int y) const {
return x*data[0] + y*data[1];
}
- double norm() const {
+ T norm() const {
return std::sqrt(sq(data[0]) + sq(data[1]));
}
- double& operator[](std::size_t i) {
+ T& operator[](std::size_t i) {
return data[i];
}
- double operator[](std::size_t i) const {
+ T operator[](std::size_t i) const {
return data[i];
}
- Vector operator*(double scalar) const {
- return Vector{
+ Vector<T> operator*(T scalar) const {
+ return Vector<T>{
data[0] * scalar,
data[1] * scalar
};
}
- Vector& operator+=(const Vector& rhs) {
+ Vector<T>& operator+=(const Vector<T>& rhs) {
data[0] += rhs[0];
data[1] += rhs[1];
return *this;
}
};
-Vector operator*(double scalar, const Vector& vec) {
- return Vector{
+template <typename T>
+Vector<T> operator*(T scalar, const Vector<T>& vec) {
+ return Vector<T>{
vec[0] * scalar,
vec[1] * scalar
};