| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/scal/meta/scalar_type.hpp>
5 #include <stan/math/prim/scal/meta/is_vector_like.hpp>
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,
103 typename scalar_type<T>::type>::type scalar_t;
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,
128 typename scalar_type<T>::type>::type scalar_t;
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
stan::VectorView< T, is_array, true >::scalar_t
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
stan::VectorView::operator[]
scalar_t & operator[](int i)
Definition: VectorView.hpp:62
stan::VectorView< T, true, false >::VectorView
VectorView(scalar_t *x)
Definition: VectorView.hpp:130
stan::VectorView::VectorView
VectorView(X x)
Definition: VectorView.hpp:57
stan::is_vector_like
Template metaprogram indicates whether a type is vector_like.
Definition: is_vector_like.hpp:21
stan
Definition: log_sum_exp.hpp:8
stan::VectorView< T, is_array, true >::VectorView
VectorView(X x)
Definition: VectorView.hpp:84
stan::VectorView::scalar_t
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
stan::VectorView< T, is_array, true >::VectorView
VectorView()
Definition: VectorView.hpp:81
stan::scalar_type::type
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: scalar_type.hpp:34
stan::VectorView::operator[]
scalar_t & operator[](int i) const
Definition: VectorView.hpp:67
stan::VectorView< T, false, false >::operator[]
scalar_t & operator[](int i)
Definition: VectorView.hpp:109
is_vector_like.hpp
stan::VectorView< T, true, false >::scalar_t
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
stan::VectorView< T, true, false >::operator[]
scalar_t & operator[](int i) const
Definition: VectorView.hpp:136
stan::VectorView< T, false, false >::scalar_t
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_type.hpp
stan::VectorView< T, is_array, true >::operator[]
scalar_t & operator[](int i) const
Definition: VectorView.hpp:90
stan::VectorView< T, false, false >::VectorView
VectorView(scalar_t *x)
Definition: VectorView.hpp:107
stan::VectorView< T, is_array, true >::operator[]
scalar_t & operator[](int i)
Definition: VectorView.hpp:86
stan::VectorView< T, true, false >::operator[]
scalar_t & operator[](int i)
Definition: VectorView.hpp:132
stan::VectorView
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
stan::VectorView< T, false, false >::operator[]
scalar_t & operator[](int i) const
Definition: VectorView.hpp:113
stan::VectorView< T, false, false >::VectorView
VectorView(scalar_t &x)
Definition: VectorView.hpp:105

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