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

Add helpers for validating ranges #124

Merged
merged 2 commits into from
Nov 7, 2024
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
9 changes: 9 additions & 0 deletions inst/include/dust2/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ void read_dimensions(cpp11::list args, const char * name, dust2::array::dimensio
template <>
void read_dimensions(cpp11::list args, const char * name, dust2::array::dimensions<1>& dest);

template <typename T>
void check_min_array(const std::vector<T>& x, const T min, const char * name);
template <typename T>
void check_max_array(const std::vector<T>& x, const T min, const char * name);
template <typename T>
void check_min_scalar(const T x, const T min, const char * name);
template <typename T>
void check_max_scalar(const T x, const T min, const char * name);

}

}
40 changes: 40 additions & 0 deletions inst/include/dust2/r/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,46 @@ void read_int_array(cpp11::list args, const dust2::array::dimensions<rank>& dim,
}
}

template <typename T>
void check_min_array(const std::vector<T>& x, const T min, const char * name) {
for (size_t i = 0; i < x.size(); ++i) {
if (x[i] < min) {
const auto min_str = std::to_string(min);
cpp11::stop("All values of '%s' must be at least %s",
name, min_str.c_str());
}
}
}

template <typename T>
void check_max_array(const std::vector<T>& x, const T max, const char * name) {
for (size_t i = 0; i < x.size(); ++i) {
if (x[i] > max) {
const auto max_str = std::to_string(max);
cpp11::stop("All values of '%s' must be at most %s",
name, max_str.c_str());
}
}
}

template <typename T>
void check_min_scalar(const T x, const T min, const char * name) {
if (x < min) {
const auto min_str = std::to_string(min);
cpp11::stop("'%s' must be at least %s",
name, min_str.c_str());
}
}

template <typename T>
void check_max_scalar(const T x, const T max, const char * name) {
if (x > max) {
const auto max_str = std::to_string(max);
cpp11::stop("'%s' must be at most %s",
name, max_str.c_str());
}
}

inline cpp11::sexp packing_to_r(const dust2::packing& packing_info) {
if (packing_info.size() == 0) {
return R_NilValue;
Expand Down
Loading