lexicographical_compare.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*!
  2. @file
  3. Forward declares `boost::hana::lexicographical_compare`.
  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_LEXICOGRAPHICAL_COMPARE_HPP
  9. #define BOOST_HANA_FWD_LEXICOGRAPHICAL_COMPARE_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/core/when.hpp>
  12. BOOST_HANA_NAMESPACE_BEGIN
  13. //! Short-circuiting lexicographical comparison of two `Iterable`s with
  14. //! an optional custom predicate, by default `hana::less`.
  15. //! @ingroup group-Iterable
  16. //!
  17. //! Given two `Iterable`s `xs` and `ys` and a binary predicate `pred`,
  18. //! `lexicographical_compare` returns whether `xs` is to be considered
  19. //! less than `ys` in a lexicographical ordering. Specifically, let's
  20. //! denote the linearizations of `xs` and `ys` by `[x1, x2, ...]` and
  21. //! `[y1, y2, ...]`, respectively. If the first couple satisfying the
  22. //! predicate is of the form `xi, yi`, `lexicographical_compare` returns
  23. //! true. Otherwise, if the first couple to satisfy the predicate is of
  24. //! the form `yi, xi`, `lexicographical_compare` returns false. If no
  25. //! such couple can be found, `lexicographical_compare` returns whether
  26. //! `xs` has fewer elements than `ys`.
  27. //!
  28. //! @note
  29. //! This algorithm will short-circuit as soon as it can determine that one
  30. //! sequence is lexicographically less than the other. Hence, it can be
  31. //! used to compare infinite sequences. However, for the procedure to
  32. //! terminate on infinite sequences, the predicate has to be satisfied
  33. //! at a finite index.
  34. //!
  35. //!
  36. //! Signature
  37. //! ---------
  38. //! Given two `Iterable`s `It1(T)` and `It2(T)` and a predicate
  39. //! \f$ pred : T \times T \to Bool \f$ (where `Bool` is some `Logical`),
  40. //! `lexicographical_compare` has the following signatures. For the
  41. //! variant with a provided predicate,
  42. //! \f[
  43. //! \mathtt{lexicographical\_compare}
  44. //! : It1(T) \times It2(T) \times (T \times T \to Bool) \to Bool
  45. //! \f]
  46. //!
  47. //! for the variant without a custom predicate, `T` is required to be
  48. //! `Orderable`. The signature is then
  49. //! \f[
  50. //! \mathtt{lexicographical\_compare} : It1(T) \times It2(T) \to Bool
  51. //! \f]
  52. //!
  53. //! @param xs, ys
  54. //! Two `Iterable`s to compare lexicographically.
  55. //!
  56. //! @param pred
  57. //! A binary function called as `pred(x, y)` and `pred(y, x)`, where `x`
  58. //! and `y` are elements of `xs` and `ys`, respectively. `pred` must
  59. //! return a `Logical` representing whether its first argument is to be
  60. //! considered as less than its second argument. Also note that `pred`
  61. //! must define a total ordering as defined by the `Orderable` concept.
  62. //! When `pred` is not provided, it defaults to `less`.
  63. //!
  64. //!
  65. //! Example
  66. //! -------
  67. //! @include example/lexicographical_compare.cpp
  68. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  69. constexpr auto lexicographical_compare = [](auto const& xs, auto const& ys, auto const& pred = hana::less) {
  70. return tag-dispatched;
  71. };
  72. #else
  73. template <typename T, typename = void>
  74. struct lexicographical_compare_impl : lexicographical_compare_impl<T, when<true>> { };
  75. struct lexicographical_compare_t {
  76. template <typename Xs, typename Ys>
  77. constexpr auto operator()(Xs const& xs, Ys const& ys) const;
  78. template <typename Xs, typename Ys, typename Pred>
  79. constexpr auto operator()(Xs const& xs, Ys const& ys, Pred const& pred) const;
  80. };
  81. constexpr lexicographical_compare_t lexicographical_compare{};
  82. #endif
  83. BOOST_HANA_NAMESPACE_END
  84. #endif // !BOOST_HANA_FWD_LEXICOGRAPHICAL_COMPARE_HPP