cmake introduction
A short example project for the CMake build system
vector.h
Go to the documentation of this file.
1 #ifndef __cxx_test_project_vector_h__
2 #define __cxx_test_project_vector_h__
3 
4 #include <memory>
5 #include <cassert>
6 #include <iomanip>
7 #include <cstddef>
8 #include <cmath>
9 
10 namespace core
11 {
12 /**
13  @brief A vector of arbitrary size.
14 
15  The template is typically a double.
16 */
17 template<typename Scalar>
18 class Vector
19 {
20 public:
21  /// @brief constructor of a vector of size @a _size
22  /// @param _size number of entries in vector.
23  Vector(int _size)
24  : size(_size)
25  {
26  values = new Scalar [size]();
27  }
28  /// @brief destructor, delete memory created during construction
29  virtual ~Vector()
30  {
31  if (values != nullptr)
32  delete[] values;
33  }
34  /// @brief access and change individual elements
35  /// @details The parameter @a i must be within \f$[1,\_size]\f$,
36  /// otherwise the program terminates with an error.
37  Scalar& operator()(int i)
38  {
39  assert((1 <= i) && (i <= size));
40 
41  return *(values + (i - 1)); }
42 
43  /// @brief access individual element
44  /// @warning no checks are done for @a i.
45  Scalar operator()(int i) const
46  { return *(values + (i - 1)); }
47 
48  /// @brief get the number of entries
49  int get_size () const
50  { return size; }
51 
52  /// @brief get the array of elements of the vector
53  /// @note be careful with this. Do not delete it!
54  /// @note use saver const version, if possible
55  Scalar* get_values()
56  { return values; }
57 
58  /// @brief get the array of elements of the vector
59  const Scalar* get_values() const
60  { return values; }
61 
62  /** @brief print the matrix in some nice form
63  *
64  * @param[in,out] os stream to which the matrix is written
65  *
66  * @todo more documentation, please!
67  */
68  template <typename OUTPUT_STREAM>
69  void print_formatted (OUTPUT_STREAM &os,
70  const unsigned int precision = 3,
71  const bool scientific = true,
72  const unsigned int width_ = 0,
73  const char *zero_string = "*",
74  const double denominator = 1.,
75  const double threshold = 0.) const
76  {
77  unsigned int width = width_;
78 
79  os << "Vector of length " << size << std::endl;
80 
81  // set output format, but store old
82  // state
83  std::ios::fmtflags old_flags = os.flags();
84  unsigned int old_precision = os.precision (precision);
85 
86  if (scientific) {
87  os.setf (std::ios::scientific, std::ios::floatfield);
88  if (!width)
89  width = precision+7;
90  }
91  else {
92  os.setf (std::ios::fixed, std::ios::floatfield);
93  if (!width)
94  width = precision+2;
95  }
96 
97  for (int i1 = 1 ; i1 <= size; i1++) {
98  if (std::fabs((*this)(i1)) > threshold)
99  os << std::setw(width)
100  << (*this)(i1) * Scalar(denominator) << ' ';
101  else
102  os << std::setw(width) << zero_string << ' ';
103  }
104 
105  os << std::endl;
106  // reset output format
107  os.flags (old_flags);
108  os.precision(old_precision);
109  }
110 
111 private:
112  int size; ///< size of vector
113  Scalar* values; ///< the entries of the vector
114 };
115 
116 }
117 
118 #endif
int size
size of vector
Definition: vector.h:112
Namespace for core components.
Definition: core.h:11
Scalar * get_values()
get the array of elements of the vector
Definition: vector.h:55
Scalar & operator()(int i)
access and change individual elements
Definition: vector.h:37
Vector(int _size)
constructor of a vector of size _size
Definition: vector.h:23
Scalar * values
the entries of the vector
Definition: vector.h:113
int get_size() const
get the number of entries
Definition: vector.h:49
virtual ~Vector()
destructor, delete memory created during construction
Definition: vector.h:29
void print_formatted(OUTPUT_STREAM &os, const unsigned int precision=3, const bool scientific=true, const unsigned int width_=0, const char *zero_string="*", const double denominator=1., const double threshold=0.) const
print the matrix in some nice form
Definition: vector.h:69
Scalar operator()(int i) const
access individual element
Definition: vector.h:45
A vector of arbitrary size.
Definition: vector.h:18
const Scalar * get_values() const
get the array of elements of the vector
Definition: vector.h:59