Stan Math Library  2.12.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  template <typename T, bool is_array>
74  class VectorView<T, is_array, true> {
75  public:
76  typedef typename
77  boost::conditional<boost::is_const<T>::value,
78  typename boost::add_const<
79  typename scalar_type<T>::type>::type,
80  typename scalar_type<T>::type>::type scalar_t;
81  VectorView() { }
82 
83  template <typename X>
84  explicit VectorView(X x) { }
85 
86  scalar_t& operator[](int i) {
87  throw std::logic_error("VectorView: this cannot be accessed");
88  }
89 
90  scalar_t& operator[](int i) const {
91  throw std::logic_error("VectorView: this cannot be accessed");
92  }
93  };
94 
95  // this covers non-vectors: double
96  template <typename T>
97  class VectorView<T, false, false> {
98  public:
99  typedef typename
100  boost::conditional<boost::is_const<T>::value,
101  typename boost::add_const<
102  typename scalar_type<T>::type>::type,
104 
105  explicit VectorView(scalar_t& x) : x_(&x) { }
106 
107  explicit VectorView(scalar_t* x) : x_(x) { }
108 
109  scalar_t& operator[](int i) {
110  return *x_;
111  }
112 
113  scalar_t& operator[](int i) const {
114  return *x_;
115  }
116  private:
117  scalar_t* x_;
118  };
119 
120  // this covers raw memory: double*
121  template <typename T>
122  class VectorView<T, true, false> {
123  public:
124  typedef typename
125  boost::conditional<boost::is_const<T>::value,
126  typename boost::add_const<
127  typename scalar_type<T>::type>::type,
129 
130  explicit VectorView(scalar_t* x) : x_(x) { }
131 
132  scalar_t& operator[](int i) {
133  return x_[i];
134  }
135 
136  scalar_t& operator[](int i) const {
137  return x_[i];
138  }
139 
140  private:
141  scalar_t* x_;
142  };
143 }
144 #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:80
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:34
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:128
scalar_t & operator[](int i) const
Definition: VectorView.hpp:136
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:103
scalar_t & operator[](int i) const
Definition: VectorView.hpp:90
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:113

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