Stan Math Library  2.10.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 
6 #include <boost/type_traits.hpp>
7 #include <stdexcept>
8 
9 namespace stan {
10 
45  template <typename T,
46  bool is_array = stan::is_vector_like<T>::value,
47  bool throw_if_accessed = false>
48  class VectorView {
49  public:
50  typedef typename
51  boost::conditional<boost::is_const<T>::value,
52  typename boost::add_const<
53  typename scalar_type<T>::type>::type,
54  typename scalar_type<T>::type>::type scalar_t;
55 
56  template <typename X>
57  explicit VectorView(X x) {
58  throw std::logic_error("VectorView: the default template "
59  "specialization not implemented");
60  }
61 
62  scalar_t& operator[](int i) {
63  throw std::logic_error("VectorView: the default template "
64  "specialization not implemented");
65  }
66 
67  scalar_t& operator[](int i) const {
68  throw std::logic_error("VectorView: the default template "
69  "specialization not implemented");
70  }
71  };
72 
73 
74  template <typename T, bool is_array>
75  class VectorView<T, is_array, true> {
76  public:
77  typedef typename
78  boost::conditional<boost::is_const<T>::value,
79  typename boost::add_const<
80  typename scalar_type<T>::type>::type,
81  typename scalar_type<T>::type>::type scalar_t;
82  VectorView() { }
83 
84  template <typename X>
85  explicit VectorView(X x) { }
86 
87  scalar_t& operator[](int i) {
88  throw std::logic_error("VectorView: this cannot be accessed");
89  }
90 
91  scalar_t& operator[](int i) const {
92  throw std::logic_error("VectorView: this cannot be accessed");
93  }
94  };
95 
96  // this covers non-vectors: double
97  template <typename T>
98  class VectorView<T, false, false> {
99  public:
100  typedef typename
101  boost::conditional<boost::is_const<T>::value,
102  typename boost::add_const<
103  typename scalar_type<T>::type>::type,
105 
106  explicit VectorView(scalar_t& x) : x_(&x) { }
107 
108  explicit VectorView(scalar_t* x) : x_(x) { }
109 
110  scalar_t& operator[](int i) {
111  return *x_;
112  }
113 
114  scalar_t& operator[](int i) const {
115  return *x_;
116  }
117  private:
118  scalar_t* x_;
119  };
120 
121 
122  // this covers raw memory: double*
123  template <typename T>
124  class VectorView<T, true, false> {
125  public:
126  typedef typename
127  boost::conditional<boost::is_const<T>::value,
128  typename boost::add_const<
129  typename scalar_type<T>::type>::type,
131 
132  explicit VectorView(scalar_t* x) : x_(x) { }
133 
134  scalar_t& operator[](int i) {
135  return x_[i];
136  }
137 
138  scalar_t& operator[](int i) const {
139  return x_[i];
140  }
141 
142  private:
143  scalar_t* x_;
144  };
145 }
146 #endif
boost::conditional< boost::is_const< T >::value, typename boost::add_const< typename scalar_type< T >::type >::type, typename scalar_type< T >::type >::type scalar_t
Definition: VectorView.hpp:81
scalar_t & operator[](int i)
Definition: VectorView.hpp:62
Template metaprogram indicates whether a type is vector_like.
boost::conditional< boost::is_const< T >::value, typename boost::add_const< typename scalar_type< T >::type >::type, typename scalar_type< T >::type >::type scalar_t
Definition: VectorView.hpp:54
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: scalar_type.hpp:35
scalar_t & operator[](int i) const
Definition: VectorView.hpp:67
boost::conditional< boost::is_const< T >::value, typename boost::add_const< typename scalar_type< T >::type >::type, typename scalar_type< T >::type >::type scalar_t
Definition: VectorView.hpp:130
scalar_t & operator[](int i) const
Definition: VectorView.hpp:138
boost::conditional< boost::is_const< T >::value, typename boost::add_const< typename scalar_type< T >::type >::type, typename scalar_type< T >::type >::type scalar_t
Definition: VectorView.hpp:104
scalar_t & operator[](int i) const
Definition: VectorView.hpp:91
VectorView is a template expression that is constructed with a container or scalar, which it then allows to be used as an array using operator[].
Definition: VectorView.hpp:48
scalar_t & operator[](int i) const
Definition: VectorView.hpp:114

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