All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
VectorView.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_SCAL_META_VECTORVIEW_HPP
#define STAN_MATH_PRIM_SCAL_META_VECTORVIEW_HPP
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/scal/meta/scalar_type.hpp>
#include <stan/math/prim/mat/meta/is_vector_like.hpp>
#include <stan/math/prim/scal/meta/is_vector_like.hpp>
#include <stdexcept>
#include <vector>
namespace stan {
template <typename T,
bool is_array = stan::is_vector_like<T>::value,
bool throw_if_accessed = false>
class VectorView {
public:
typedef typename scalar_type<T>::type scalar_t;
explicit VectorView(scalar_t& c) : x_(&c) { }
explicit VectorView(std::vector<scalar_t>& v) : x_(&v[0]) { }
template <int R, int C>
explicit VectorView(Eigen::Matrix<scalar_t, R, C>& m) : x_(&m(0)) { }
explicit VectorView(scalar_t* x) : x_(x) { }
scalar_t& operator[](int i) {
if (throw_if_accessed)
throw std::out_of_range("VectorView: this cannot be accessed");
if (is_array)
return x_[i];
else
return x_[0];
}
private:
scalar_t* x_;
};
template <typename T, bool is_array, bool throw_if_accessed>
class VectorView<const T, is_array, throw_if_accessed> {
public:
typedef typename scalar_type<T>::type scalar_t;
explicit VectorView(const scalar_t& c) : x_(&c) { }
explicit VectorView(const scalar_t* x) : x_(x) { }
explicit VectorView(const std::vector<scalar_t>& v) : x_(&v[0]) { }
template <int R, int C>
explicit VectorView(const Eigen::Matrix<scalar_t, R, C>& m) : x_(&m(0)) { }
const scalar_t& operator[](int i) const {
if (throw_if_accessed)
throw std::out_of_range("VectorView: this cannot be accessed");
if (is_array)
return x_[i];
else
return x_[0];
}
private:
const scalar_t* x_;
};
// simplify to hold value in common case where it's more efficient
template <>
class VectorView<const double, false, false> {
public:
explicit VectorView(double x) : x_(x) { }
double operator[](int /* i */) const {
return x_;
}
private:
const double x_;
};
}
#endif
stan::VectorView::operator[]
scalar_t & operator[](int i)
Definition: VectorView.hpp:54
stan::VectorView::VectorView
VectorView(scalar_t &c)
Definition: VectorView.hpp:45
stan::VectorView::scalar_t
scalar_type< T >::type scalar_t
Definition: VectorView.hpp:43
stan::is_vector_like
Definition: is_vector_like.hpp:12
is_vector_like.hpp
stan::scalar_type::type
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: scalar_type.hpp:38
stan::VectorView::VectorView
VectorView(std::vector< scalar_t > &v)
Definition: VectorView.hpp:47
stan::VectorView< const T, is_array, throw_if_accessed >::VectorView
VectorView(const scalar_t *x)
Definition: VectorView.hpp:78
stan::VectorView< const T, is_array, throw_if_accessed >::scalar_t
scalar_type< T >::type scalar_t
Definition: VectorView.hpp:74
is_vector_like.hpp
stan::VectorView< const double, false, false >::VectorView
VectorView(double x)
Definition: VectorView.hpp:101
stan::VectorView< const T, is_array, throw_if_accessed >::VectorView
VectorView(const std::vector< scalar_t > &v)
Definition: VectorView.hpp:80
scalar_type.hpp
stan::VectorView::VectorView
VectorView(Eigen::Matrix< scalar_t, R, C > &m)
Definition: VectorView.hpp:50
stan::math::out_of_range
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.
Definition: out_of_range.hpp:30
Eigen.hpp
stan::VectorView< const T, is_array, throw_if_accessed >::operator[]
const scalar_t & operator[](int i) const
Definition: VectorView.hpp:85
stan::VectorView< const double, false, false >::operator[]
double operator[](int) const
Definition: VectorView.hpp:102
stan::VectorView< const T, is_array, throw_if_accessed >::VectorView
VectorView(const scalar_t &c)
Definition: VectorView.hpp:76
stan::VectorView::VectorView
VectorView(scalar_t *x)
Definition: VectorView.hpp:52
stan::VectorView
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: VectorView.hpp:41
stan::VectorView< const T, is_array, throw_if_accessed >::VectorView
VectorView(const Eigen::Matrix< scalar_t, R, C > &m)
Definition: VectorView.hpp:83

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