group.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*!
  2. @file
  3. Forward declares `boost::hana::group`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_FWD_GROUP_HPP
  9. #define BOOST_HANA_FWD_GROUP_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. #include <boost/hana/detail/nested_by_fwd.hpp>
  13. BOOST_HANA_NAMESPACE_BEGIN
  14. //! Group adjacent elements of a sequence that all respect a binary
  15. //! predicate, by default equality.
  16. //! @ingroup group-Sequence
  17. //!
  18. //! Given a _finite_ Sequence and an optional predicate (by default
  19. //! `equal`), `group` returns a sequence of subsequences representing
  20. //! groups of adjacent elements that are "equal" with respect to the
  21. //! predicate. In other words, the groups are such that the predicate is
  22. //! satisfied when it is applied to any two adjacent elements in that
  23. //! group. The sequence returned by `group` is such that the concatenation
  24. //! of its elements is equal to the original sequence, which is equivalent
  25. //! to saying that the order of the elements is not changed.
  26. //!
  27. //! If no predicate is provided, adjacent elements in the sequence must
  28. //! all be compile-time `Comparable`.
  29. //!
  30. //!
  31. //! Signature
  32. //! ---------
  33. //! Given a Sequence `s` with tag `S(T)`, an `IntegralConstant` `Bool`
  34. //! holding a value of type `bool`, and a predicate
  35. //! \f$ pred : T \times T \to Bool \f$, `group` has the following
  36. //! signatures. For the variant with a provided predicate,
  37. //! \f[
  38. //! \mathtt{group} : S(T) \times (T \times T \to Bool) \to S(S(T))
  39. //! \f]
  40. //!
  41. //! for the variant without a custom predicate, `T` is required to be
  42. //! Comparable. The signature is then
  43. //! \f[
  44. //! \mathtt{group} : S(T) \to S(S(T))
  45. //! \f]
  46. //!
  47. //! @param xs
  48. //! The sequence to split into groups.
  49. //!
  50. //! @param predicate
  51. //! A binary function called as `predicate(x, y)`, where `x` and `y` are
  52. //! _adjacent_ elements in the sequence, whether both elements should be
  53. //! in the same group (subsequence) of the result. In the current version
  54. //! of the library, the result returned by `predicate` must be an
  55. //! `IntegralConstant` holding a value of a type convertible to `bool`.
  56. //! Also, `predicate` has to define an equivalence relation as defined by
  57. //! the `Comparable` concept. When this predicate is not provided, it
  58. //! defaults to `equal`, which requires the comparison of any two adjacent
  59. //! elements in the sequence to return a boolean `IntegralConstant`.
  60. //!
  61. //!
  62. //! Syntactic sugar (`group.by`)
  63. //! ----------------------------
  64. //! `group` can be called in a third way, which provides a nice syntax
  65. //! especially when working with the `comparing` combinator:
  66. //! @code
  67. //! group.by(predicate, xs) == group(xs, predicate)
  68. //! group.by(predicate) == group(-, predicate)
  69. //! @endcode
  70. //!
  71. //! where `group(-, predicate)` denotes the partial application of
  72. //! `group` to `predicate`.
  73. //!
  74. //!
  75. //! Example
  76. //! -------
  77. //! @include example/group.cpp
  78. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  79. constexpr auto group = [](auto&& xs[, auto&& predicate]) {
  80. return tag-dispatched;
  81. };
  82. #else
  83. template <typename S, typename = void>
  84. struct group_impl : group_impl<S, when<true>> { };
  85. struct group_t : detail::nested_by<group_t> {
  86. template <typename Xs>
  87. constexpr auto operator()(Xs&& xs) const;
  88. template <typename Xs, typename Predicate>
  89. constexpr auto operator()(Xs&& xs, Predicate&& pred) const;
  90. };
  91. constexpr group_t group{};
  92. #endif
  93. BOOST_HANA_NAMESPACE_END
  94. #endif // !BOOST_HANA_FWD_GROUP_HPP