feat(cpp-object-oriented-modular-refactoring): step 3 - foundation-google-style
This commit is contained in:
+66
-69
@@ -1,4 +1,4 @@
|
||||
#include "fesa/math/vector.hpp"
|
||||
#include "fesa/math/vector.h"
|
||||
|
||||
#include <mkl.h>
|
||||
|
||||
@@ -9,111 +9,108 @@
|
||||
namespace fesa {
|
||||
namespace {
|
||||
|
||||
MKL_INT toMklSize(const std::size_t size) {
|
||||
if (size > static_cast<std::size_t>((std::numeric_limits<MKL_INT>::max)())) {
|
||||
throw std::length_error{"Dense vector size exceeds the MKL integer range."};
|
||||
}
|
||||
return static_cast<MKL_INT>(size);
|
||||
/// @brief Converts a dense vector size to the private MKL integer contract.
|
||||
MKL_INT ToMklSize(const std::size_t size) {
|
||||
if (size > static_cast<std::size_t>((std::numeric_limits<MKL_INT>::max)())) {
|
||||
throw std::length_error{"Dense vector size exceeds the MKL integer range."};
|
||||
}
|
||||
return static_cast<MKL_INT>(size);
|
||||
}
|
||||
|
||||
void copyValues(const std::vector<double>& source, std::vector<double>& destination) {
|
||||
if (source.empty()) {
|
||||
return;
|
||||
}
|
||||
/// @brief Copies owned values without exposing the dense backend publicly.
|
||||
void CopyValues(const std::vector<double>& source,
|
||||
std::vector<double>& destination) {
|
||||
if (source.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Keep the backend operation in this translation unit so public ownership
|
||||
// remains independent of MKL headers and integer types.
|
||||
cblas_dcopy(toMklSize(source.size()), source.data(), 1, destination.data(), 1);
|
||||
// Keep the backend operation in this translation unit so public ownership
|
||||
// remains independent of MKL headers and integer types.
|
||||
cblas_dcopy(ToMklSize(source.size()), source.data(), 1, destination.data(),
|
||||
1);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
Vector::Vector(const std::size_t size, const double value)
|
||||
: values_(size, value) {}
|
||||
|
||||
Vector::Vector(const Vector& other)
|
||||
: values_(other.size()) {
|
||||
copyValues(other.values_, values_);
|
||||
Vector::Vector(const Vector& other) : values_(other.Size()) {
|
||||
CopyValues(other.values_, values_);
|
||||
}
|
||||
|
||||
Vector::Vector(Vector&& other) noexcept
|
||||
: values_(std::move(other.values_)) {
|
||||
other.values_.clear();
|
||||
Vector::Vector(Vector&& other) noexcept : values_(std::move(other.values_)) {
|
||||
other.values_.clear();
|
||||
}
|
||||
|
||||
Vector& Vector::operator=(const Vector& other) {
|
||||
if (this != &other) {
|
||||
std::vector<double> copied(other.size());
|
||||
copyValues(other.values_, copied);
|
||||
values_.swap(copied);
|
||||
}
|
||||
return *this;
|
||||
if (this != &other) {
|
||||
std::vector<double> copied(other.Size());
|
||||
CopyValues(other.values_, copied);
|
||||
values_.swap(copied);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector& Vector::operator=(Vector&& other) noexcept {
|
||||
if (this != &other) {
|
||||
values_ = std::move(other.values_);
|
||||
other.values_.clear();
|
||||
}
|
||||
return *this;
|
||||
if (this != &other) {
|
||||
values_ = std::move(other.values_);
|
||||
other.values_.clear();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::size_t Vector::size() const noexcept {
|
||||
return values_.size();
|
||||
}
|
||||
std::size_t Vector::Size() const noexcept { return values_.size(); }
|
||||
|
||||
double* Vector::data() noexcept {
|
||||
return values_.data();
|
||||
}
|
||||
double* Vector::Data() noexcept { return values_.data(); }
|
||||
|
||||
const double* Vector::data() const noexcept {
|
||||
return values_.data();
|
||||
}
|
||||
const double* Vector::Data() const noexcept { return values_.data(); }
|
||||
|
||||
double& Vector::operator[](const std::size_t index) {
|
||||
return values_.at(index);
|
||||
return values_.at(index);
|
||||
}
|
||||
|
||||
const double& Vector::operator[](const std::size_t index) const {
|
||||
return values_.at(index);
|
||||
return values_.at(index);
|
||||
}
|
||||
|
||||
double Vector::dot(const Vector& rhs) const {
|
||||
if (size() != rhs.size()) {
|
||||
throw std::invalid_argument{"Vector dot product requires equal dimensions."};
|
||||
}
|
||||
if (values_.empty()) {
|
||||
return 0.0;
|
||||
}
|
||||
double Vector::Dot(const Vector& rhs) const {
|
||||
if (Size() != rhs.Size()) {
|
||||
throw std::invalid_argument{
|
||||
"Vector dot product requires equal dimensions."};
|
||||
}
|
||||
if (values_.empty()) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return cblas_ddot(toMklSize(size()), data(), 1, rhs.data(), 1);
|
||||
return cblas_ddot(ToMklSize(Size()), Data(), 1, rhs.Data(), 1);
|
||||
}
|
||||
|
||||
double Vector::norm() const {
|
||||
if (values_.empty()) {
|
||||
return 0.0;
|
||||
}
|
||||
double Vector::Norm() const {
|
||||
if (values_.empty()) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return cblas_dnrm2(toMklSize(size()), data(), 1);
|
||||
return cblas_dnrm2(ToMklSize(Size()), Data(), 1);
|
||||
}
|
||||
|
||||
void Vector::scale(const double alpha) {
|
||||
if (values_.empty()) {
|
||||
return;
|
||||
}
|
||||
void Vector::Scale(const double alpha) {
|
||||
if (values_.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
cblas_dscal(toMklSize(size()), alpha, data(), 1);
|
||||
cblas_dscal(ToMklSize(Size()), alpha, Data(), 1);
|
||||
}
|
||||
|
||||
void Vector::axpy(const double alpha, const Vector& x) {
|
||||
if (size() != x.size()) {
|
||||
throw std::invalid_argument{"Vector axpy requires equal dimensions."};
|
||||
}
|
||||
if (values_.empty()) {
|
||||
return;
|
||||
}
|
||||
void Vector::Axpy(const double alpha, const Vector& x) {
|
||||
if (Size() != x.Size()) {
|
||||
throw std::invalid_argument{"Vector axpy requires equal dimensions."};
|
||||
}
|
||||
if (values_.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
cblas_daxpy(toMklSize(size()), alpha, x.data(), 1, data(), 1);
|
||||
cblas_daxpy(ToMklSize(Size()), alpha, x.Data(), 1, Data(), 1);
|
||||
}
|
||||
|
||||
} // namespace fesa
|
||||
} // namespace fesa
|
||||
|
||||
Reference in New Issue
Block a user