-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdense_vector.h
125 lines (101 loc) · 2.93 KB
/
dense_vector.h
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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2015 Kostiantyn Antoniuk
* Copyright (C) 2015 Kostiantyn Antoniuk
*/
#ifndef __vilma__dense_vector__
#define __vilma__dense_vector__
#include <stdio.h>
#include <fstream>
#include "vector_interface.h"
#include "sparse_vector.h"
namespace Vilma {
template <class T>
class DenseVector : public VectorInterface {
public:
DenseVector(const int dim, T *data)
: dim_(dim), data_(data), destroy_array_(false) {}
DenseVector(const int dim) : dim_(dim) {
data_ = new T[dim];
std::fill(data_, data_ + dim, 0);
destroy_array_ = true;
}
DenseVector(std::ifstream *const file) {
data_ = readArray<T>(file, &dim_);
destroy_array_ = true;
}
virtual ~DenseVector() {
if (destroy_array_) Clear();
}
/**
* index access
*/
T &operator[](int i) { return data_[i]; }
const T &operator[](int i) const { return data_[i]; }
/** First check if it is non empty */
const T max() const {
T v = data_[0];
for (int i = 1; i < dim_; ++i) v = std::max(v, data_[i]);
return v;
}
/** First check if it is non empty */
const T sum() const {
T v = data_[0];
for (int i = 1; i < dim_; ++i) v += data_[i];
return v;
}
/** First check if it is non empty */
const T norm2() const {
T v = data_[0] * data_[0];
for (int i = 1; i < dim_; ++i) v += data_[i] * data_[i];
return v;
}
void add_sparse(const SparseVector<T> &, const T &);
void add_dense(const DenseVector<T> &, const T &);
template <class scalar>
void mul(const scalar &);
void fill(const T &v) { std::fill(data_, data_ + dim_, v); }
void swap(DenseVector<T> &v) {
swap(dim_, v.dim_);
swap(data_, v.data_);
swap(destroy_array_, v.destroy_array_);
}
int dim_;
T *data_ = nullptr;
protected:
DenseVector() = delete;
private:
bool destroy_array_ = false;
void Clear() {
if (data_ != nullptr) delete[] data_;
data_ = nullptr;
}
};
template <class T>
class DenseVectorView : public DenseVector<T> {
public:
// TODO: remove it
// do not use this class
// using DenseVector<T>::DenseVector;
using DenseVector<T>::max;
using DenseVector<T>::fill;
using DenseVector<T>::add_sparse;
using DenseVector<T>::add_dense;
using DenseVector<T>::data_;
using DenseVector<T>::dim_;
DenseVectorView(const int) = delete;
DenseVectorView(std::ifstream *const) = delete;
DenseVectorView(const DenseVector<T> &dense_vector, int begin, int end)
: DenseVector<T>(end - begin, dense_vector.data_ + begin) {
// data_ = dense_vector.data_ + begin;
// dim_ = end - begin;
}
virtual ~DenseVectorView() {}
};
} // namespace end
#include "dense_vector.hpp"
#endif /* defined(__vilma__dense_vector__) */