40 std::string build_file_prefix(
const std::string& label);
42 #ifdef ENABLE_ARRAY_INDEX_CHECKING
43 #define Vector_check_enabled
53 static int num_allocated;
55 static double memory_in_use;
56 static double memory_returned;
57 static bool write_enabled;
58 static bool show_index_check_message;
59 static void memory(
const std::string& prefix=
"");
60 static void stats(
const std::string& prefix=
"");
84 Vector(
const int size, T* data)
86 reference_data_ =
true;
91 Vector(std::initializer_list<T> values)
93 if (values.size() == 0) {
97 allocate(values.size());
98 std::copy(values.begin(), values.end(), data_);
105 if (data_ !=
nullptr) {
106 if (!reference_data_) {
109 memory_in_use -=
sizeof(T)*size_;
110 memory_returned +=
sizeof(T)*size_;
121 if (rhs.size_ <= 0) {
125 for (
int i = 0; i < rhs.size_; i++) {
126 data_[i] = rhs.data_[i];
132 bool allocated()
const
134 return is_allocated_ ;
143 if (data_ !=
nullptr) {
144 if (reference_data_) {
145 throw std::runtime_error(
"[Vector] Can't clear a Vector with reference data.");
148 memory_in_use -=
sizeof(T) * size_;;
149 memory_returned +=
sizeof(T) * size_;;
152 is_allocated_ =
false;
157 void print(
const std::string& label)
159 printf(
"%s (%d): \n", label.c_str(), size_);
160 for (
int i = 0; i < size_; i++) {
161 printf(
"%s %d %g\n", label.c_str(), i+1, data_[i]);
173 if (data_ !=
nullptr) {
174 if (reference_data_) {
175 throw std::runtime_error(
"[Vector] Can't resize a Vector with reference data.");
178 memory_in_use -=
sizeof(T) * size_;;
179 memory_returned +=
sizeof(T) * size_;;
188 void grow(
const int size, T value={})
194 memory_in_use +=
sizeof(T) * size;;
195 int new_size = size_ + size;
196 T* new_data =
new T [new_size];
197 for (
int i = 0; i < size; i++) {
198 new_data[i+size_] = value;
200 memcpy(new_data, data_,
sizeof(T)*size_);
201 if (reference_data_) {
202 throw std::runtime_error(
"[Vector] Can't grow a Vector with reference data.");
209 void set_values(std::initializer_list<T> values)
211 if (values.size() == 0) {
215 allocate(values.size());
216 std::copy(values.begin(), values.end(), data_);
219 void set_values(std::vector<T> values)
221 if (values.size() == 0) {
225 allocate(values.size());
226 std::copy(values.begin(), values.end(), data_);
229 void read(
const std::string& file_name)
231 auto fp = fopen(file_name.c_str(),
"rb");
233 fread(&size,
sizeof(
int), 1, fp);
234 fread(data_,
sizeof(T), size_, fp);
238 void write(
const std::string& label,
const T offset={})
const
240 if (!write_enabled) {
244 auto file_prefix = build_file_prefix(label);
245 auto file_name = file_prefix +
"_cm.bin";
249 auto fp = fopen(file_name.c_str(),
"wb");
250 fwrite(&size_,
sizeof(
int), 1, fp);
251 fwrite(data_,
sizeof(T), size_, fp);
271 if (rhs.size_ <= 0) {
279 if (size_ != rhs.size_) {
284 memcpy(data_, rhs.data_,
sizeof(T) * size_);
291 for (
int i = 0; i < size_; i++) {
303 return size_ *
sizeof(T);
308 const T& operator()(
const int i)
const
310 #ifdef Vector_check_enabled
316 T& operator()(
const int i)
318 #ifdef Vector_check_enabled
324 const T& operator[](
const int i)
const
326 #ifdef Vector_check_enabled
332 T& operator[](
const int i)
334 #ifdef Vector_check_enabled
340 friend std::ostream& operator << (std::ostream& out,
const Vector<T>& lhs)
342 for (
int i = 0; i < lhs.size(); i++) {
344 if (i != lhs.size()-1) {
359 if (size_ != vec.size()) {
360 throw std::runtime_error(
"[Vector dot product] Vectors have diffrenct sizes: " +
361 std::to_string(size_) +
" != " + std::to_string(vec.size()) +
".");
364 for (
int i = 0; i < size_; i++) {
365 result(i) = data_[i] + vec[i];
373 for (
int i = 0; i < size_; i++) {
374 result(i) = data_[i] - x(i);
384 for (
int i = 0; i < size_; i++) {
385 result(i) = data_[i] + value;
393 for (
int i = 0; i < rhs.size_; i++) {
394 result(i) = rhs.data_[i] + value;
402 for (
int i = 0; i < size_; i++) {
403 result(i) = data_[i] - value;
411 for (
int i = 0; i < rhs.size_; i++) {
412 result(i) = value - rhs.data_[i];
422 for (
int i = 0; i < size_; i++) {
423 result(i) = data_[i] / value;
431 for (
int i = 0; i < rhs.size_; i++) {
432 result(i) = rhs.data_[i] / value;
442 for (
int i = 0; i < size_; i++) {
443 result(i) = value * data_[i];
451 for (
int i = 0; i < rhs.size_; i++) {
452 result(i) = value * rhs.data_[i];
462 for (
int i = 0; i < size_; i++) {
463 result(i) = -data_[i];
472 for (
int i = 0; i < size_; i++) {
473 result(i) = std::abs(data_[i]);
483 result(0) = (*this)(1)*v2(2) - (*this)(2)*v2(1);
484 result(1) = (*this)(2)*v2(0) - (*this)(0)*v2(2);
485 result(2) = (*this)(0)*v2(1) - (*this)(1)*v2(0);
493 if (size_ != v2.size()) {
494 throw std::runtime_error(
"[Vector dot product] Vectors have diffrenct sizes: " +
495 std::to_string(size_) +
" != " + std::to_string(v2.size()) +
".");
498 for (
int i = 0; i < size_; i++) {
499 sum += (*this)(i) * v2(i);
506 if (v1.size() != v2.size()) {
507 throw std::runtime_error(
"[Vector dot product] Vectors have diffrenct sizes: " +
508 std::to_string(v1.size()) +
" != " + std::to_string(v2.size()) +
".");
511 for (
int i = 0; i < v1.size(); i++) {
512 sum += v1[i] * v2[i];
524 return *std::min_element((*this).begin(), (*this).end());
534 return *std::max_element((*this).begin(), (*this).end());
540 for (
int i = 0; i < size_; i++) {
555 typedef T value_type;
556 typedef T& reference;
558 typedef int difference_type;
559 typedef std::forward_iterator_tag iterator_category;
563 Iterator& operator++() { this->ptr_ ++;
return *
this; }
564 Iterator& operator--() { this->ptr_ --;
return *
this; }
565 Iterator& operator++(
int) { this->ptr_ ++;
return *
this; }
566 Iterator& operator--(
int) { this->ptr_ --;
return *
this; }
567 T& operator*() {
return *this->ptr_; };
568 bool operator==(
const Iterator& iter) {
return this->ptr_ == iter.ptr_; }
569 bool operator!=(
const Iterator& iter) {
return this->ptr_ != iter.ptr_; }
581 return Iterator(data_+size_);
589 void allocate(
const int size)
597 data_ =
new T [size_];
598 memset(data_, 0,
sizeof(T)*(size_));
599 memory_in_use +=
sizeof(T)*size_;
602 void check_index(
const int i)
const
604 if (show_index_check_message) {
605 std::cout <<
"[Vector] **********************************" << std::endl;
606 std::cout <<
"[Vector] WARNING: Index checking is enabled " << std::endl;
607 std::cout <<
"[Vector] **********************************" << std::endl;
608 show_index_check_message =
false;
611 if (data_ ==
nullptr) {
612 std::cout <<
"[Vector] WARNING: Accessing null data in Vector at " << i << std::endl;
617 if ((i < 0) || (i >= size_)) {
618 auto index_str = std::to_string(i);
619 auto dims = std::to_string(size_);
620 throw std::runtime_error( +
"Index i=" + index_str +
" is out of bounds for " + dims +
" vector.");
628 if (!std::is_same<T, double>::value && !std::is_same<T, int>::value &&
629 !std::is_same<T,
Vector<double>>::value && !std::is_same<T, float>::value) {
630 std::string msg = std::string(
"Cannot use Vector class template for type '") +
typeid(T).name() +
"'.";
631 throw std::runtime_error(msg);
636 bool is_allocated_ =
false;
638 bool reference_data_ =
false;
This class provides an interface to access Vector like STL containers.
Definition: Vector.h:553
The Vector template class is used for storing int and double data.
Definition: Vector.h:50
void clear()
Free the array data.
Definition: Vector.h:141
Vector< T > operator-() const
Negate.
Definition: Vector.h:459
void check_type() const
Check that the Vector template type is int or double.
Definition: Vector.h:626
Vector< T > cross(const Vector< T > &v2)
Cross product.
Definition: Vector.h:479
T dot(const Vector< T > &v2)
Dot product.
Definition: Vector.h:491
Vector< T > operator+(const T value) const
Add and subtract a scalar from a vector.
Definition: Vector.h:381
Vector< T > operator+(const Vector< T > &vec) const
Add and subtract vectors.
Definition: Vector.h:357
Vector< T > abs() const
Absolute value.
Definition: Vector.h:469
Vector & operator=(const Vector &rhs)
Vector assigment.
Definition: Vector.h:269
void grow(const int size, T value={})
Grow the vector.
Definition: Vector.h:188
void resize(const int size)
Resize the vector's memory.
Definition: Vector.h:167
Vector< T > operator/(const T value) const
Divide by a scalar.
Definition: Vector.h:419
Vector< T > operator*(const T value) const
Multiply by a scalar.
Definition: Vector.h:439