Reworking vector class with core funtionality
This commit is contained in:
+172
-69
@@ -7,91 +7,196 @@
|
||||
|
||||
|
||||
namespace utils{
|
||||
//#######################################
|
||||
//######################################
|
||||
//# VECTOR TYPE #
|
||||
//#######################################
|
||||
//# Backed by std::vector<T> #
|
||||
//######################################
|
||||
template<typename T>
|
||||
struct Vector{
|
||||
|
||||
class Vector{
|
||||
public:
|
||||
using value_type = T;
|
||||
std::vector<T> v;
|
||||
|
||||
Vector() = default;
|
||||
// Default construtor utils::Vd vector;
|
||||
Vector() = default;
|
||||
|
||||
Vector(uint64_t size, T value = T()) {
|
||||
// Construtor utils::Vd vector(2, 3.15);
|
||||
// Prevent implicit conversions like Vector<double> v = 5;
|
||||
explicit Vector(uint64_t size, T value = T()) {
|
||||
v.resize(size, value);
|
||||
}
|
||||
|
||||
|
||||
void clear(){v.clear();}
|
||||
void push_back(const T& val) { v.push_back(val); }
|
||||
//##########################################################
|
||||
//# VECTOR: --- basic properties --- #
|
||||
//##########################################################
|
||||
// vector.clear();
|
||||
void clear() noexcept {v.clear();}
|
||||
|
||||
// vector.push_back(2);
|
||||
void push_back(const T& val) { v.push_back(val); }
|
||||
|
||||
// vector[2] = 2;
|
||||
T& operator[](uint64_t idx) { return v[idx]; }
|
||||
// a = vector[2];
|
||||
const T& operator[](uint64_t idx) const { return v[idx]; }
|
||||
uint64_t size() const { return v.size(); }
|
||||
|
||||
void fill(const uint64_t size, const T num){
|
||||
v.clear();
|
||||
for (uint64_t i = 0; i < size; i++)
|
||||
{
|
||||
v.push_back(num);
|
||||
}
|
||||
// vector.size();
|
||||
uint64_t size() const noexcept { return v.size(); }
|
||||
|
||||
|
||||
//###########################################
|
||||
//# VECTOR: == and != #
|
||||
//###########################################
|
||||
bool operator==(const Vector<T>& a) const {
|
||||
if (v.size() != a.size()) {
|
||||
return false;
|
||||
}
|
||||
for (uint64_t i = 0; i < v.size(); ++i) {
|
||||
if (v[i] != a[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void fill_RNG(const uint64_t size, const double min, const double max){
|
||||
v.clear();
|
||||
|
||||
std::mt19937_64 rng{};
|
||||
rng.seed( std::random_device{}());
|
||||
bool operator!=(const Vector<T>& a) const { return !(*this == a); }
|
||||
|
||||
for (uint64_t i = 0; i < size; i++){
|
||||
//##################################################
|
||||
//# VECTOR: Scalar Addition #
|
||||
//##################################################
|
||||
template <typename U, typename = typename std::enable_if<std::is_convertible<U, T>::value>::type>
|
||||
void inplace_add(const U a){
|
||||
const uint64_t n = v.size();
|
||||
for (uint64_t i = 0; i < n; ++i){
|
||||
v[i] += a;
|
||||
}
|
||||
}
|
||||
template <typename U, typename = typename std::enable_if<std::is_convertible<U, T>::value>::type>
|
||||
Vector<T> add(const U a) const{
|
||||
Vector<T> result = *this;
|
||||
result.inplace_add(a);
|
||||
return result;
|
||||
}
|
||||
template <typename U, typename = typename std::enable_if<std::is_convertible<U, T>::value>::type>
|
||||
Vector<T> operator+(const U a) const {
|
||||
return add(a);
|
||||
}
|
||||
template <typename U, typename = typename std::enable_if<std::is_convertible<U, T>::value>::type>
|
||||
Vector<T>& operator+=(const U a) {
|
||||
inplace_add(a);
|
||||
return *this;
|
||||
}
|
||||
//##################################################
|
||||
//# VECTOR: Vector Addition #
|
||||
//##################################################
|
||||
void inplace_add(const Vector<T>& a){
|
||||
if (a.size() != v.size()){
|
||||
throw std::runtime_error("utill:Vector.inplace_add -> Dimensions does not fit");
|
||||
}
|
||||
const uint64_t n = v.size();
|
||||
for (uint64_t i = 0; i < n; ++i){
|
||||
v[i] += a[i];
|
||||
}
|
||||
}
|
||||
Vector<T> add(const Vector<T>& a) const{
|
||||
Vector<T> result = *this;
|
||||
result.inplace_add(a);
|
||||
return result;
|
||||
}
|
||||
Vector<T> operator+(const Vector<T>& a) const {
|
||||
return add(a);
|
||||
}
|
||||
Vector<T>& operator+=(const Vector<T>& a) {
|
||||
inplace_add(a);
|
||||
return *this;
|
||||
}
|
||||
//##################################################
|
||||
//# VECTOR: Vector Subtract #
|
||||
//##################################################
|
||||
void inplace_vec_subtract(const Vector<T>& a){
|
||||
if (a.size() != v.size()){
|
||||
throw std::runtime_error("utill:Vector.inplace_vec_subtract -> Dimensions does not fit");
|
||||
}
|
||||
|
||||
v.push_back(std::uniform_real_distribution<>{min, max}(rng));
|
||||
}
|
||||
uint64_t n = a.size();
|
||||
for (uint64_t i = 0; i < n; ++i){
|
||||
v[i] -= a[i];
|
||||
}
|
||||
|
||||
}
|
||||
Vector<T> vec_subtract(const Vector<T>& a) const{
|
||||
Vector<T> result = *this;
|
||||
|
||||
result.inplace_vec_subtract(a);
|
||||
|
||||
return result;
|
||||
}
|
||||
Vector<T> operator-(const Vector<T>& a) const {
|
||||
return vec_subtract(a);
|
||||
}
|
||||
Vector<T>& operator-=(const Vector<T>& a) {
|
||||
inplace_vec_subtract(a);
|
||||
return *this;
|
||||
}
|
||||
//##################################################
|
||||
//# VECTOR: Vector Multiply #
|
||||
//##################################################
|
||||
void inplace_vec_multiply(const Vector<T>& a){
|
||||
if (a.size() != v.size()){
|
||||
throw std::runtime_error("utill:Vector.inplace_vec_multiply -> Dimensions does not fit");
|
||||
}
|
||||
|
||||
uint64_t n = a.size();
|
||||
for (uint64_t i = 0; i < n; ++i){
|
||||
v[i] *= a[i];
|
||||
}
|
||||
|
||||
}
|
||||
Vector<T> vec_multiply(const Vector<T>& a) const{
|
||||
Vector<T> result = *this;
|
||||
result.inplace_vec_multiply(a);
|
||||
return result;
|
||||
}
|
||||
Vector<T> operator*(const Vector<T>& a) const {
|
||||
return vec_multiply(a);
|
||||
}
|
||||
Vector<T>& operator*=(const Vector<T>& a) {
|
||||
inplace_vec_multiply(a);
|
||||
return *this;
|
||||
}
|
||||
//##################################################
|
||||
//# VECTOR: Vector Divide #
|
||||
//##################################################
|
||||
void inplace_vec_divide(const Vector<T>& a){
|
||||
if (a.size() != v.size()){
|
||||
throw std::runtime_error("utill:Vector.inplace_vec_divide -> Dimensions does not fit");
|
||||
}
|
||||
|
||||
uint64_t n = a.size();
|
||||
for (uint64_t i = 0; i < n; ++i){
|
||||
v[i] /= a[i];
|
||||
}
|
||||
|
||||
}
|
||||
Vector<T> vec_divide(const Vector<T>& a) const{
|
||||
Vector<T> result = *this;
|
||||
result.inplace_vec_divide(a);
|
||||
return result;
|
||||
}
|
||||
Vector<T> operator/(const Vector<T>& a) const {
|
||||
return vec_divide(a);
|
||||
}
|
||||
Vector<T>& operator/=(const Vector<T>& a) {
|
||||
inplace_vec_divide(a);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void disturbance(const double min, const double max){
|
||||
//######################################################
|
||||
//# VECTOR: Support Functions #
|
||||
//######################################################
|
||||
|
||||
uint64_t size = v.size();
|
||||
|
||||
std::mt19937_64 rng{};
|
||||
rng.seed(std::random_device{}());
|
||||
|
||||
for (uint64_t i = 0; i < size; i++){
|
||||
v[i] *= std::uniform_real_distribution<>{min, max}(rng);
|
||||
}
|
||||
}
|
||||
|
||||
double max(){
|
||||
double max_value = v[0];
|
||||
|
||||
for (uint64_t i = 1; i < v.size(); i++){
|
||||
max_value = MAX(max_value, v[i]);
|
||||
}
|
||||
return max_value;
|
||||
}
|
||||
|
||||
double min(){
|
||||
double min_value = v[0];
|
||||
|
||||
for (uint64_t i = 1; i < v.size(); i++){
|
||||
min_value = MIN(min_value, v[i]);
|
||||
}
|
||||
return min_value;
|
||||
}
|
||||
|
||||
double sum() const {
|
||||
double sum_value = 0;
|
||||
|
||||
for (uint64_t i = 0; i < v.size(); i++){
|
||||
sum_value += v[i];
|
||||
}
|
||||
return sum_value;
|
||||
}
|
||||
|
||||
double mean(){
|
||||
double mean_value = sum()/v.size();
|
||||
return mean_value;
|
||||
}
|
||||
|
||||
inline friend std::ostream& operator << (std::ostream& out, const Vector& vec){
|
||||
out << "[";
|
||||
@@ -108,7 +213,7 @@ namespace utils{
|
||||
void print() const{
|
||||
std::cout << *this << std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
void linspace(const T start, const T stop, const T num){
|
||||
v.clear();
|
||||
|
||||
@@ -121,9 +226,7 @@ namespace utils{
|
||||
}
|
||||
//v.push_back(static_cast<T>(stop));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}*/
|
||||
};
|
||||
|
||||
typedef Vector<int> Vi;
|
||||
@@ -133,4 +236,4 @@ namespace utils{
|
||||
|
||||
} // namespace utils
|
||||
|
||||
#endif // _numerics_n_
|
||||
#endif // _vector_n_
|
||||
Reference in New Issue
Block a user