unique.hpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*!
  2. @file
  3. Forward declares `boost::hana::unique`.
  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_UNIQUE_HPP
  9. #define BOOST_HANA_FWD_UNIQUE_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. //! Removes all consecutive duplicate elements from a Sequence.
  15. //! @ingroup group-Sequence
  16. //!
  17. //! Given a `Sequence` and an optional binary predicate, `unique` returns
  18. //! a new sequence containing only the first element of every subrange
  19. //! of the original sequence whose elements are all equal. In other words,
  20. //! it turns a sequence of the form `[a, a, b, c, c, c, d, d, d, a]` into
  21. //! a sequence `[a, b, c, d, a]`. The equality of two elements is
  22. //! determined by the provided `predicate`, or by `equal` if no
  23. //! `predicate` is provided.
  24. //!
  25. //!
  26. //! Signature
  27. //! ---------
  28. //! Given a `Sequence` `S(T)`, a `Logical` `Bool` and a binary predicate
  29. //! \f$ T \times T \to Bool \f$, `unique` has the following signature:
  30. //! \f[
  31. //! \mathtt{unique} : S(T) \times (T \times T \to Bool) \to S(T)
  32. //! \f]
  33. //!
  34. //! @param xs
  35. //! The sequence from which to remove consecutive duplicates.
  36. //!
  37. //! @param predicate
  38. //! A function called as `predicate(x, y)`, where `x` and `y` are adjacent
  39. //! elements of the sequence, and returning a `Logical` representing
  40. //! whether `x` and `y` should be considered equal. `predicate` should
  41. //! define an [equivalence relation][1] over the elements of the sequence.
  42. //! In the current implementation of the library, `predicate` has to
  43. //! return a compile-time `Logical`. This parameter is optional; it
  44. //! defaults to `equal` if it is not provided, which then requires the
  45. //! elements of the sequence to be compile-time `Comparable`.
  46. //!
  47. //!
  48. //! Syntactic sugar (`unique.by`)
  49. //! -----------------------------
  50. //! `unique` can be called in an alternate way, which provides a nice
  51. //! syntax, especially in conjunction with the `comparing` combinator:
  52. //! @code
  53. //! unique.by(predicate, xs) == unique(xs, predicate)
  54. //! unique.by(predicate) == unique(-, predicate)
  55. //! @endcode
  56. //!
  57. //! where `unique(-, predicate)` denotes the partial application of
  58. //! `unique` to `predicate`.
  59. //!
  60. //!
  61. //! Example
  62. //! -------
  63. //! @include example/unique.cpp
  64. //!
  65. //! [1]: http://en.wikipedia.org/wiki/Equivalence_relation#Definition
  66. #if defined(BOOST_HANA_DOXYGEN_INVOKED)
  67. constexpr auto unique = [](auto&& xs[, auto&& predicate]) {
  68. return tag-dispatched;
  69. };
  70. #else
  71. template <typename S, typename = void>
  72. struct unique_impl : unique_impl<S, when<true>> { };
  73. struct unique_t : detail::nested_by<unique_t> {
  74. template <typename Xs>
  75. constexpr auto operator()(Xs&& xs) const;
  76. template <typename Xs, typename Predicate>
  77. constexpr auto operator()(Xs&& xs, Predicate&& predicate) const;
  78. };
  79. constexpr unique_t unique{};
  80. #endif
  81. BOOST_HANA_NAMESPACE_END
  82. #endif // !BOOST_HANA_FWD_UNIQUE_HPP