41 std::string build_file_prefix(
const std::string& label);
52 static int num_allocated;
54 static double memory_in_use;
55 static double memory_returned;
56 static bool write_enabled;
57 static void memory(
const std::string& prefix=
"");
58 static void stats(
const std::string& prefix=
"");
82 Vector(
const int size, T* data)
84 reference_data_ =
true;
89 Vector(std::initializer_list<T> values)
91 if (values.size() == 0) {
95 allocate(values.size());
96 std::copy(values.begin(), values.end(), data_);
103 if (data_ !=
nullptr) {
104 if (!reference_data_) {
107 memory_in_use -=
sizeof(T)*size_;
108 memory_returned +=
sizeof(T)*size_;
119 if (rhs.size_ <= 0) {
123 for (
int i = 0; i < rhs.size_; i++) {
124 data_[i] = rhs.data_[i];
130 bool allocated()
const
132 return is_allocated_ ;
141 if (data_ !=
nullptr) {
142 if (reference_data_) {
143 throw std::runtime_error(
"[Vector] Can't clear a Vector with reference data.");
146 memory_in_use -=
sizeof(T) * size_;;
147 memory_returned +=
sizeof(T) * size_;;
150 is_allocated_ =
false;
155 void print(
const std::string& label)
157 printf(
"%s (%d): \n", label.c_str(), size_);
158 for (
int i = 0; i < size_; i++) {
159 printf(
"%s %d %g\n", label.c_str(), i+1, data_[i]);
171 if (data_ !=
nullptr) {
172 if (reference_data_) {
173 throw std::runtime_error(
"[Vector] Can't resize a Vector with reference data.");
176 memory_in_use -=
sizeof(T) * size_;;
177 memory_returned +=
sizeof(T) * size_;;
186 void grow(
const int size, T value={})
192 memory_in_use +=
sizeof(T) * size;;
193 int new_size = size_ + size;
194 T* new_data =
new T [new_size];
195 for (
int i = 0; i < size; i++) {
196 new_data[i+size_] = value;
198 memcpy(new_data, data_,
sizeof(T)*size_);
199 if (reference_data_) {
200 throw std::runtime_error(
"[Vector] Can't grow a Vector with reference data.");
207 void set_values(std::initializer_list<T> values)
209 if (values.size() == 0) {
213 allocate(values.size());
214 std::copy(values.begin(), values.end(), data_);
217 void set_values(std::vector<T> values)
219 if (values.size() == 0) {
223 allocate(values.size());
224 std::copy(values.begin(), values.end(), data_);
227 void read(
const std::string& file_name)
229 auto fp = fopen(file_name.c_str(),
"rb");
231 fread(&size,
sizeof(
int), 1, fp);
232 fread(data_,
sizeof(T), size_, fp);
236 void write(
const std::string& label,
const T offset={})
const
238 if (!write_enabled) {
242 auto file_prefix = build_file_prefix(label);
243 auto file_name = file_prefix +
"_cm.bin";
247 auto fp = fopen(file_name.c_str(),
"wb");
248 fwrite(&size_,
sizeof(
int), 1, fp);
249 fwrite(data_,
sizeof(T), size_, fp);
269 if (rhs.size_ <= 0) {
277 if (size_ != rhs.size_) {
282 memcpy(data_, rhs.data_,
sizeof(T) * size_);
289 for (
int i = 0; i < size_; i++) {
301 return size_ *
sizeof(T);
306 const T& operator()(
const int i)
const
308 #ifdef Vector_check_enabled
314 T& operator()(
const int i)
316 #ifdef Vector_check_enabled
322 const T& operator[](
const int i)
const
324 #ifdef Vector_check_enabled
330 T& operator[](
const int i)
332 #ifdef Vector_check_enabled
338 friend std::ostream& operator << (std::ostream& out,
const Vector<T>& lhs)
340 for (
int i = 0; i < lhs.size(); i++) {
342 if (i != lhs.size()-1) {
357 if (size_ != vec.size()) {
358 throw std::runtime_error(
"[Vector dot product] Vectors have diffrenct sizes: " +
359 std::to_string(size_) +
" != " + std::to_string(vec.size()) +
".");
362 for (
int i = 0; i < size_; i++) {
363 result(i) = data_[i] + vec[i];
371 for (
int i = 0; i < size_; i++) {
372 result(i) = data_[i] - x(i);
382 for (
int i = 0; i < size_; i++) {
383 result(i) = data_[i] + value;
391 for (
int i = 0; i < rhs.size_; i++) {
392 result(i) = rhs.data_[i] + value;
400 for (
int i = 0; i < size_; i++) {
401 result(i) = data_[i] - value;
409 for (
int i = 0; i < rhs.size_; i++) {
410 result(i) = value - rhs.data_[i];
420 for (
int i = 0; i < size_; i++) {
421 result(i) = data_[i] / value;
429 for (
int i = 0; i < rhs.size_; i++) {
430 result(i) = rhs.data_[i] / value;
440 for (
int i = 0; i < size_; i++) {
441 result(i) = value * data_[i];
449 for (
int i = 0; i < rhs.size_; i++) {
450 result(i) = value * rhs.data_[i];
460 for (
int i = 0; i < size_; i++) {
461 result(i) = -data_[i];
470 for (
int i = 0; i < size_; i++) {
471 result(i) = std::abs(data_[i]);
481 result(0) = (*this)(1)*v2(2) - (*this)(2)*v2(1);
482 result(1) = (*this)(2)*v2(0) - (*this)(0)*v2(2);
483 result(2) = (*this)(0)*v2(1) - (*this)(1)*v2(0);
491 if (size_ != v2.size()) {
492 throw std::runtime_error(
"[Vector dot product] Vectors have diffrenct sizes: " +
493 std::to_string(size_) +
" != " + std::to_string(v2.size()) +
".");
496 for (
int i = 0; i < size_; i++) {
497 sum += (*this)(i) * v2(i);
504 if (v1.size() != v2.size()) {
505 throw std::runtime_error(
"[Vector dot product] Vectors have diffrenct sizes: " +
506 std::to_string(v1.size()) +
" != " + std::to_string(v2.size()) +
".");
509 for (
int i = 0; i < v1.size(); i++) {
510 sum += v1[i] * v2[i];
522 return *std::min_element((*this).begin(), (*this).end());
532 return *std::max_element((*this).begin(), (*this).end());
538 for (
int i = 0; i < size_; i++) {
553 typedef T value_type;
554 typedef T& reference;
556 typedef int difference_type;
557 typedef std::forward_iterator_tag iterator_category;
561 Iterator& operator++() { this->ptr_ ++;
return *
this; }
562 Iterator& operator--() { this->ptr_ --;
return *
this; }
563 Iterator& operator++(
int) { this->ptr_ ++;
return *
this; }
564 Iterator& operator--(
int) { this->ptr_ --;
return *
this; }
565 T& operator*() {
return *this->ptr_; };
566 bool operator==(
const Iterator& iter) {
return this->ptr_ == iter.ptr_; }
567 bool operator!=(
const Iterator& iter) {
return this->ptr_ != iter.ptr_; }
579 return Iterator(data_+size_);
587 void allocate(
const int size)
595 data_ =
new T [size_];
596 memset(data_, 0,
sizeof(T)*(size_));
597 memory_in_use +=
sizeof(T)*size_;
600 void check_index(
const int i)
const
602 if (data_ ==
nullptr) {
603 std::cout <<
"[Vector] WARNING: Accessing null data in Vector at " << i << std::endl;
608 if ((i < 0) || (i >= size_)) {
609 auto index_str = std::to_string(i);
610 auto dims = std::to_string(size_);
611 throw std::runtime_error( +
"Index i=" + index_str +
" is out of bounds for " + dims +
" vector.");
619 if (!std::is_same<T, double>::value && !std::is_same<T, int>::value &&
620 !std::is_same<T,
Vector<double>>::value && !std::is_same<T, float>::value) {
621 std::string msg = std::string(
"Cannot use Vector class template for type '") +
typeid(T).name() +
"'.";
622 throw std::runtime_error(msg);
627 bool is_allocated_ =
false;
629 bool reference_data_ =
false;
This class provides an interface to access Vector like STL containers.
Definition: Vector.h:551
The Vector template class is used for storing int and double data.
Definition: Vector.h:49
void clear()
Free the array data.
Definition: Vector.h:139
Vector< T > operator-() const
Negate.
Definition: Vector.h:457
void check_type() const
Check that the Vector template type is int or double.
Definition: Vector.h:617
Vector< T > cross(const Vector< T > &v2)
Cross product.
Definition: Vector.h:477
T dot(const Vector< T > &v2)
Dot product.
Definition: Vector.h:489
Vector< T > operator+(const T value) const
Add and subtract a scalar from a vector.
Definition: Vector.h:379
Vector< T > operator+(const Vector< T > &vec) const
Add and subtract vectors.
Definition: Vector.h:355
Vector< T > abs() const
Absolute value.
Definition: Vector.h:467
Vector & operator=(const Vector &rhs)
Vector assigment.
Definition: Vector.h:267
void grow(const int size, T value={})
Grow the vector.
Definition: Vector.h:186
void resize(const int size)
Resize the vector's memory.
Definition: Vector.h:165
Vector< T > operator/(const T value) const
Divide by a scalar.
Definition: Vector.h:417
Vector< T > operator*(const T value) const
Multiply by a scalar.
Definition: Vector.h:437