|
| 1 | +#ifndef STAN_MATH_PRIM_FUN_ALL_HPP |
| 2 | +#define STAN_MATH_PRIM_FUN_ALL_HPP |
| 3 | + |
| 4 | +#include <stan/math/prim/meta.hpp> |
| 5 | +#include <stan/math/prim/functor/for_each.hpp> |
| 6 | +#include <algorithm> |
| 7 | + |
| 8 | +namespace stan { |
| 9 | +namespace math { |
| 10 | + |
| 11 | +/** |
| 12 | + * Return true if all values in the input are true. |
| 13 | + * |
| 14 | + * Overload for a single integral input |
| 15 | + * |
| 16 | + * @tparam T Any type convertible to `bool` |
| 17 | + * @param x integral input |
| 18 | + * @return The input unchanged |
| 19 | + */ |
| 20 | +template <typename T, require_t<std::is_convertible<T, bool>>* = nullptr> |
| 21 | +constexpr inline bool all(T x) { |
| 22 | + return x; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Return true if all values in the input are true. |
| 27 | + * |
| 28 | + * Overload for Eigen types |
| 29 | + * |
| 30 | + * @tparam ContainerT A type derived from `Eigen::EigenBase` that has an |
| 31 | + * `integral` scalar type |
| 32 | + * @param x Eigen object of boolean inputs |
| 33 | + * @return Boolean indicating whether all elements are true |
| 34 | + */ |
| 35 | +template <typename ContainerT, |
| 36 | + require_eigen_st<std::is_integral, ContainerT>* = nullptr> |
| 37 | +inline bool all(const ContainerT& x) { |
| 38 | + return x.all(); |
| 39 | +} |
| 40 | + |
| 41 | +// Forward-declaration for correct resolution of all(std::vector<std::tuple>) |
| 42 | +template <typename... Types> |
| 43 | +inline bool all(const std::tuple<Types...>& x); |
| 44 | + |
| 45 | +/** |
| 46 | + * Return true if all values in the input are true. |
| 47 | + * |
| 48 | + * Overload for a std::vector/nested inputs. The Eigen::Map/apply_vector_unary |
| 49 | + * approach cannot be used as std::vector<bool> types do not have a .data() |
| 50 | + * member and are not always stored contiguously. |
| 51 | + * |
| 52 | + * @tparam InnerT Type within std::vector |
| 53 | + * @param x Nested container of boolean inputs |
| 54 | + * @return Boolean indicating whether all elements are true |
| 55 | + */ |
| 56 | +template <typename InnerT> |
| 57 | +inline bool all(const std::vector<InnerT>& x) { |
| 58 | + return std::all_of(x.begin(), x.end(), [](const auto& i) { return all(i); }); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Return true if all values in the input are true. |
| 63 | + * |
| 64 | + * Overload for a tuple input. |
| 65 | + * |
| 66 | + * @tparam Types of items within tuple |
| 67 | + * @param x Tuple of boolean scalar-type elements |
| 68 | + * @return Boolean indicating whether all elements are true |
| 69 | + */ |
| 70 | +template <typename... Types> |
| 71 | +inline bool all(const std::tuple<Types...>& x) { |
| 72 | + bool all_true = true; |
| 73 | + math::for_each( |
| 74 | + [&all_true](const auto& i) { |
| 75 | + all_true = all_true && all(i); |
| 76 | + return; |
| 77 | + }, |
| 78 | + x); |
| 79 | + return all_true; |
| 80 | +} |
| 81 | + |
| 82 | +} // namespace math |
| 83 | +} // namespace stan |
| 84 | + |
| 85 | +#endif |
0 commit comments