Stan Math Library: stan/math/prim/scal/meta/VectorView.hpp Source File

| Stan Math Library 2.10.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
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,
104 typename scalar_type<T>::type>::type scalar_t;
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,
130 typename scalar_type<T>::type>::type scalar_t;
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
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:81
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:132
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:85
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:82
stan::scalar_type::type
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: scalar_type.hpp:35
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:110
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:130
stan::VectorView< T, true, false >::operator[]
scalar_t & operator[](int i) const
Definition: VectorView.hpp:138
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:104
scalar_type.hpp
stan::VectorView< T, is_array, true >::operator[]
scalar_t & operator[](int i) const
Definition: VectorView.hpp:91
stan::VectorView< T, false, false >::VectorView
VectorView(scalar_t *x)
Definition: VectorView.hpp:108
stan::VectorView< T, is_array, true >::operator[]
scalar_t & operator[](int i)
Definition: VectorView.hpp:87
stan::VectorView< T, true, false >::operator[]
scalar_t & operator[](int i)
Definition: VectorView.hpp:134
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:114
stan::VectorView< T, false, false >::VectorView
VectorView(scalar_t &x)
Definition: VectorView.hpp:106

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