uniqued.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Boost.Range library
  2. //
  3. // Copyright Thorsten Ottosen, Neil Groves 2006 - 2008. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see http://www.boost.org/libs/range/
  9. //
  10. #ifndef BOOST_RANGE_ADAPTOR_UNIQUED_IMPL_HPP
  11. #define BOOST_RANGE_ADAPTOR_UNIQUED_IMPL_HPP
  12. #include <boost/range/adaptor/adjacent_filtered.hpp>
  13. #include <boost/range/concepts.hpp>
  14. namespace boost
  15. {
  16. namespace range_detail
  17. {
  18. struct unique_forwarder { };
  19. struct unique_not_equal_to
  20. {
  21. typedef bool result_type;
  22. template< class T >
  23. bool operator()( const T& l, const T& r ) const
  24. {
  25. return !(l == r);
  26. }
  27. };
  28. template<class ForwardRng>
  29. class uniqued_range : public adjacent_filtered_range<unique_not_equal_to, ForwardRng, true>
  30. {
  31. typedef adjacent_filtered_range<unique_not_equal_to, ForwardRng, true> base;
  32. public:
  33. explicit uniqued_range(ForwardRng& rng)
  34. : base(unique_not_equal_to(), rng)
  35. {
  36. }
  37. };
  38. template< class ForwardRng >
  39. inline uniqued_range<ForwardRng>
  40. operator|( ForwardRng& r,
  41. unique_forwarder )
  42. {
  43. BOOST_RANGE_CONCEPT_ASSERT((ForwardRangeConcept<ForwardRng>));
  44. return uniqued_range<ForwardRng>(r);
  45. }
  46. template< class ForwardRng >
  47. inline uniqued_range<const ForwardRng>
  48. operator|( const ForwardRng& r,
  49. unique_forwarder )
  50. {
  51. BOOST_RANGE_CONCEPT_ASSERT((ForwardRangeConcept<const ForwardRng>));
  52. return uniqued_range<const ForwardRng>(r);
  53. }
  54. } // 'range_detail'
  55. using range_detail::uniqued_range;
  56. namespace adaptors
  57. {
  58. namespace
  59. {
  60. const range_detail::unique_forwarder uniqued =
  61. range_detail::unique_forwarder();
  62. }
  63. template<class ForwardRange>
  64. inline uniqued_range<ForwardRange>
  65. unique(ForwardRange& rng)
  66. {
  67. BOOST_RANGE_CONCEPT_ASSERT((ForwardRangeConcept<ForwardRange>));
  68. return uniqued_range<ForwardRange>(rng);
  69. }
  70. template<class ForwardRange>
  71. inline uniqued_range<const ForwardRange>
  72. unique(const ForwardRange& rng)
  73. {
  74. BOOST_RANGE_CONCEPT_ASSERT((
  75. ForwardRangeConcept<const ForwardRange>));
  76. return uniqued_range<const ForwardRange>(rng);
  77. }
  78. } // 'adaptors'
  79. }
  80. #endif