Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/rvalue and vector scalar deserializer #3043

Merged
merged 5 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/stan/io/deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class deserializer {
* @throws std::runtime_error if there aren't at least m reals left
*/
void check_r_capacity(size_t m) const {
STAN_NO_RANGE_CHECKS_RETURN;
if (pos_r_ + m > r_size_) {
[]() STAN_COLD_PATH {
throw std::runtime_error("no more scalars to read");
Expand All @@ -66,6 +67,7 @@ class deserializer {
* @throws std::runtime_error if there aren't at least m integers left
*/
void check_i_capacity(size_t m) const {
STAN_NO_RANGE_CHECKS_RETURN;
if (pos_i_ + m > i_size_) {
[]() STAN_COLD_PATH {
throw std::runtime_error("no more integers to read");
Expand Down Expand Up @@ -324,10 +326,11 @@ class deserializer {
* `read` functions.
*/
template <typename Ret, typename... Sizes,
require_std_vector_t<Ret>* = nullptr>
require_std_vector_t<Ret>* = nullptr,
require_not_same_t<value_type_t<Ret>, T>* = nullptr>
inline auto read(Eigen::Index m, Sizes... dims) {
if (unlikely(m == 0)) {
return Ret();
return std::decay_t<Ret>();
} else {
std::decay_t<Ret> ret_vec;
ret_vec.reserve(m);
Expand All @@ -338,6 +341,27 @@ class deserializer {
}
}

/**
* Return an `std::vector` of scalars
* @tparam Ret The type to return.
* @tparam Sizes integral types.
* @param m The size of the vector.
*/
template <typename Ret, typename... Sizes,
require_std_vector_t<Ret>* = nullptr,
require_same_t<value_type_t<Ret>, T>* = nullptr>
inline auto read(Eigen::Index m) {
t4c1 marked this conversation as resolved.
Show resolved Hide resolved
if (unlikely(m == 0)) {
return std::decay_t<Ret>();
} else {
check_r_capacity(m);
const auto* start_pos = &this->map_r_.coeffRef(this->pos_r_);
const auto* end_pos = &this->map_r_.coeffRef(this->pos_r_ + m);
this->pos_r_ += m;
return std::decay_t<Ret>(start_pos, end_pos);
}
}

/**
* Return the next object transformed to have the specified
* lower bound, possibly incrementing the specified reference with the
Expand Down
48 changes: 48 additions & 0 deletions src/stan/model/indexing/rvalue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ inline T rvalue(T&& x, const char* /*name*/) {
return std::forward<T>(x);
}

template <typename T>
inline T& rvalue(T& x, const char* /*name*/) {
return x;
}

template <typename T>
inline const T& rvalue(const T& x, const char* /*name*/) {
return x;
}

/**
* Return the result of indexing a type without taking a subset. Mostly used as
* an intermediary rvalue function when doing multiple subsets.
Expand All @@ -70,6 +80,16 @@ inline T rvalue(T&& x, const char* /*name*/, index_omni /*idx*/) {
return std::forward<T>(x);
}

template <typename T>
inline T& rvalue(T& x, const char* /*name*/, index_omni /*idx*/) {
return x;
}

template <typename T>
inline const T& rvalue(const T& x, const char* /*name*/, index_omni /*idx*/) {
return x;
}

/**
* Return the result of indexing a type without taking a subset
*
Expand All @@ -86,6 +106,18 @@ inline T rvalue(T&& x, const char* name, index_omni /*idx1*/,
return std::forward<T>(x);
}

template <typename T>
inline T& rvalue(T& x, const char* name, index_omni /*idx1*/,
index_omni /*idx2*/) {
return x;
}

template <typename T>
inline const T& rvalue(const T& x, const char* name, index_omni /*idx1*/,
index_omni /*idx2*/) {
return x;
}

/**
* Return a single element of a Vector.
*
Expand Down Expand Up @@ -675,6 +707,22 @@ inline auto rvalue(StdVec&& v, const char* name, index_uni idx1,
*/
template <typename StdVec, require_std_vector_t<StdVec>* = nullptr>
inline auto rvalue(StdVec&& v, const char* name, index_uni idx) {
math::check_range("array[uni, ...] index", name, v.size(), idx.n_);
if (std::is_rvalue_reference<StdVec>::value) {
return std::move(v[idx.n_ - 1]);
} else {
return v[idx.n_ - 1];
}
}

template <typename StdVec, require_std_vector_t<StdVec>* = nullptr>
inline auto& rvalue(StdVec& v, const char* name, index_uni idx) {
math::check_range("array[uni, ...] index", name, v.size(), idx.n_);
return v[idx.n_ - 1];
}

template <typename StdVec, require_std_vector_t<StdVec>* = nullptr>
inline const auto& rvalue(const StdVec& v, const char* name, index_uni idx) {
math::check_range("array[uni, ...] index", name, v.size(), idx.n_);
return v[idx.n_ - 1];
}
Expand Down