Stan Math Library  2.9.0
reverse mode automatic differentiation
VectorView.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_META_VECTORVIEW_HPP
2 #define STAN_MATH_PRIM_SCAL_META_VECTORVIEW_HPP
3 
8 #include <stdexcept>
9 #include <vector>
10 
11 namespace stan {
12 
38  template <typename T,
39  bool is_array = stan::is_vector_like<T>::value,
40  bool throw_if_accessed = false>
41  class VectorView {
42  public:
43  typedef typename scalar_type<T>::type scalar_t;
44 
45  explicit VectorView(scalar_t& c) : x_(&c) { }
46 
47  explicit VectorView(std::vector<scalar_t>& v) : x_(&v[0]) { }
48 
49  template <int R, int C>
50  explicit VectorView(Eigen::Matrix<scalar_t, R, C>& m) : x_(&m(0)) { }
51 
52  explicit VectorView(scalar_t* x) : x_(x) { }
53 
54  scalar_t& operator[](int i) {
55  if (throw_if_accessed)
56  throw std::out_of_range("VectorView: this cannot be accessed");
57  if (is_array)
58  return x_[i];
59  else
60  return x_[0];
61  }
62  private:
63  scalar_t* x_;
64  };
65 
66 
71  template <typename T, bool is_array, bool throw_if_accessed>
72  class VectorView<const T, is_array, throw_if_accessed> {
73  public:
74  typedef typename scalar_type<T>::type scalar_t;
75 
76  explicit VectorView(const scalar_t& c) : x_(&c) { }
77 
78  explicit VectorView(const scalar_t* x) : x_(x) { }
79 
80  explicit VectorView(const std::vector<scalar_t>& v) : x_(&v[0]) { }
81 
82  template <int R, int C>
83  explicit VectorView(const Eigen::Matrix<scalar_t, R, C>& m) : x_(&m(0)) { }
84 
85  const scalar_t& operator[](int i) const {
86  if (throw_if_accessed)
87  throw std::out_of_range("VectorView: this cannot be accessed");
88  if (is_array)
89  return x_[i];
90  else
91  return x_[0];
92  }
93  private:
94  const scalar_t* x_;
95  };
96 
97  // simplify to hold value in common case where it's more efficient
98  template <>
99  class VectorView<const double, false, false> {
100  public:
101  explicit VectorView(double x) : x_(x) { }
102  double operator[](int /* i */) const {
103  return x_;
104  }
105  private:
106  const double x_;
107  };
108 
109 
110 
111 }
112 #endif
113 
scalar_t & operator[](int i)
Definition: VectorView.hpp:54
VectorView(scalar_t &c)
Definition: VectorView.hpp:45
scalar_type< T >::type scalar_t
Definition: VectorView.hpp:43
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: scalar_type.hpp:38
VectorView(std::vector< scalar_t > &v)
Definition: VectorView.hpp:47
VectorView(Eigen::Matrix< scalar_t, R, C > &m)
Definition: VectorView.hpp:50
void out_of_range(const char *function, const int max, const int index, const char *msg1="", const char *msg2="")
Throw an out_of_range exception with a consistently formatted message.
VectorView(scalar_t *x)
Definition: VectorView.hpp:52
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: VectorView.hpp:41
VectorView(const Eigen::Matrix< scalar_t, R, C > &m)
Definition: VectorView.hpp:83

     [ Stan Home Page ] © 2011–2015, Stan Development Team.