partition.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*!
  2. @file
  3. Forward declares `boost::hana::partition`.
  4. @copyright Louis Dionne 2013-2016
  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_PARTITION_HPP
  9. #define BOOST_HANA_FWD_PARTITION_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. //! Partition a sequence based on a `predicate`.
  15. //! @ingroup group-Sequence
  16. //!
  17. //! Specifically, returns an unspecified `Product` whose first element is
  18. //! a sequence of the elements satisfying the predicate, and whose second
  19. //! element is a sequence of the elements that do not satisfy the predicate.
  20. //!
  21. //!
  22. //! Signature
  23. //! ---------
  24. //! Given a Sequence `S(T)`, an `IntegralConstant` `Bool` holding a value
  25. //! of type `bool`, and a predicate \f$ T \to Bool \f$, `partition` has
  26. //! the following signature:
  27. //! \f[
  28. //! \mathtt{partition} : S(T) \times (T \to Bool) \to S(T) \times S(T)
  29. //! \f]
  30. //!
  31. //! @param xs
  32. //! The sequence to be partitioned.
  33. //!
  34. //! @param predicate
  35. //! A function called as `predicate(x)` for each element `x` in the
  36. //! sequence, and returning whether `x` should be added to the sequence
  37. //! in the first component or in the second component of the resulting
  38. //! pair. In the current version of the library, `predicate` must return
  39. //! an `IntegralConstant` holding a value convertible to `bool`.
  40. //!
  41. //!
  42. //! Syntactic sugar (`partition.by`)
  43. //! --------------------------------
  44. //! `partition` can be called in an alternate way, which provides a nice
  45. //! syntax in some cases where the predicate is short:
  46. //! @code
  47. //! partition.by(predicate, xs) == partition(xs, predicate)
  48. //! partition.by(predicate) == partition(-, predicate)
  49. //! @endcode
  50. //!
  51. //! where `partition(-, predicate)` denotes the partial application of
  52. //! `partition` to `predicate`.
  53. //!
  54. //!
  55. //! Example
  56. //! -------
  57. //! @include example/partition.cpp
  58. //!
  59. //!
  60. //! Benchmarks
  61. //! ----------
  62. //! <div class="benchmark-chart"
  63. //! style="min-width: 310px; height: 400px; margin: 0 auto"
  64. //! data-dataset="benchmark.partition.compile.json">
  65. //! </div>
  66. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  67. constexpr auto partition = [](auto&& xs, auto&& predicate) {
  68. return tag-dispatched;
  69. };
  70. #else
  71. template <typename S, typename = void>
  72. struct partition_impl : partition_impl<S, when<true>> { };
  73. struct partition_t : detail::nested_by<partition_t> {
  74. template <typename Xs, typename Pred>
  75. constexpr auto operator()(Xs&& xs, Pred&& pred) const;
  76. };
  77. constexpr partition_t partition{};
  78. #endif
  79. BOOST_HANA_NAMESPACE_END
  80. #endif // !BOOST_HANA_FWD_PARTITION_HPP