sort.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*!
  2. @file
  3. Forward declares `boost::hana::sort`.
  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_SORT_HPP
  9. #define BOOST_HANA_FWD_SORT_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. //! Sort a sequence, optionally based on a custom `predicate`.
  15. //! @ingroup group-Sequence
  16. //!
  17. //! Given a Sequence and an optional predicate (by default `less`), `sort`
  18. //! returns a new sequence containing the same elements as the original,
  19. //! except they are ordered in such a way that if `x` comes before `y` in
  20. //! the sequence, then either `predicate(x, y)` is true, or both
  21. //! `predicate(x, y)` and `predicate(y, x)` are false.
  22. //!
  23. //! Also note that the sort is guaranteed to be stable. Hence, if `x`
  24. //! comes before `y` in the original sequence and both `predicate(x, y)`
  25. //! and `predicate(y, x)` are false, then `x` will come before `y` in the
  26. //! resulting sequence.
  27. //!
  28. //! If no predicate is provided, the elements in the sequence must all be
  29. //! compile-time `Orderable`.
  30. //!
  31. //! Signature
  32. //! ---------
  33. //! Given a `Sequence` `S(T)`, a boolean `IntegralConstant` `Bool` and a
  34. //! binary predicate \f$ T \times T \to Bool \f$, `sort` has the following
  35. //! signatures. For the variant with a provided predicate,
  36. //! \f[
  37. //! \mathtt{sort} : S(T) \times (T \times T \to Bool) \to S(T)
  38. //! \f]
  39. //!
  40. //! for the variant without a custom predicate, `T` is required to be
  41. //! `Orderable`. The signature is then
  42. //! \f[
  43. //! \mathtt{sort} : S(T) \to S(T)
  44. //! \f]
  45. //!
  46. //! @param xs
  47. //! The sequence to sort.
  48. //!
  49. //! @param predicate
  50. //! A function called as `predicate(x, y)` for two elements `x` and `y` of
  51. //! the sequence, and returning a boolean `IntegralConstant` representing
  52. //! whether `x` is to be considered _less_ than `y`, i.e. whether `x` should
  53. //! appear _before_ `y` in the resulting sequence. More specifically,
  54. //! `predicate` must define a [strict weak ordering][1] on the elements
  55. //! of the sequence. When the predicate is not specified, this defaults
  56. //! to `less`. In the current version of the library, the predicate has
  57. //! to return an `IntegralConstant` holding a value convertible to a `bool`.
  58. //!
  59. //!
  60. //! Syntactic sugar (`sort.by`)
  61. //! ---------------------------
  62. //! `sort` can be called in a third way, which provides a nice syntax
  63. //! especially when working with the `ordering` combinator:
  64. //! @code
  65. //! sort.by(predicate, xs) == sort(xs, predicate)
  66. //! sort.by(predicate) == sort(-, predicate)
  67. //! @endcode
  68. //!
  69. //! where `sort(-, predicate)` denotes the partial application of
  70. //! `sort` to `predicate`.
  71. //!
  72. //!
  73. //! Example
  74. //! -------
  75. //! @include example/sort.cpp
  76. //!
  77. //! [1]: http://en.wikipedia.org/wiki/Strict_weak_ordering
  78. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  79. constexpr auto sort = [](auto&& xs[, auto&& predicate]) {
  80. return tag-dispatched;
  81. };
  82. #else
  83. template <typename S, typename = void>
  84. struct sort_impl : sort_impl<S, when<true>> { };
  85. struct sort_t : detail::nested_by<sort_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 sort_t sort{};
  92. #endif
  93. BOOST_HANA_NAMESPACE_END
  94. #endif // !BOOST_HANA_FWD_SORT_HPP