/Src/Dependencies/Boost/boost/range/adaptor/indexed.hpp

http://hadesmem.googlecode.com/ · C++ Header · 156 lines · 114 code · 26 blank · 16 comment · 3 complexity · 254a104481ad38a8c2ecb366b792949a MD5 · raw file

  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_INDEXED_IMPL_HPP
  11. #define BOOST_RANGE_ADAPTOR_INDEXED_IMPL_HPP
  12. #include <boost/config.hpp>
  13. #ifdef BOOST_MSVC
  14. #pragma warning( push )
  15. #pragma warning( disable : 4355 )
  16. #endif
  17. #include <boost/range/adaptor/argument_fwd.hpp>
  18. #include <boost/range/iterator_range.hpp>
  19. #include <boost/range/begin.hpp>
  20. #include <boost/range/end.hpp>
  21. #include <boost/iterator/iterator_adaptor.hpp>
  22. namespace boost
  23. {
  24. namespace adaptors
  25. {
  26. // This structure exists to carry the parameters from the '|' operator
  27. // to the index adapter. The expression rng | indexed(1) instantiates
  28. // this structure and passes it as the right-hand operand to the
  29. // '|' operator.
  30. struct indexed
  31. {
  32. explicit indexed(std::size_t x) : val(x) {}
  33. std::size_t val;
  34. };
  35. }
  36. namespace range_detail
  37. {
  38. template< class Iter >
  39. class indexed_iterator
  40. : public boost::iterator_adaptor< indexed_iterator<Iter>, Iter >
  41. {
  42. private:
  43. typedef boost::iterator_adaptor< indexed_iterator<Iter>, Iter >
  44. base;
  45. typedef BOOST_DEDUCED_TYPENAME base::difference_type index_type;
  46. index_type m_index;
  47. public:
  48. explicit indexed_iterator( Iter i, index_type index )
  49. : base(i), m_index(index)
  50. {
  51. BOOST_ASSERT( m_index >= 0 && "Indexed Iterator out of bounds" );
  52. }
  53. index_type index() const
  54. {
  55. return m_index;
  56. }
  57. private:
  58. friend class boost::iterator_core_access;
  59. void increment()
  60. {
  61. ++m_index;
  62. ++(this->base_reference());
  63. }
  64. void decrement()
  65. {
  66. BOOST_ASSERT( m_index > 0 && "Indexed Iterator out of bounds" );
  67. --m_index;
  68. --(this->base_reference());
  69. }
  70. void advance( index_type n )
  71. {
  72. m_index += n;
  73. BOOST_ASSERT( m_index >= 0 && "Indexed Iterator out of bounds" );
  74. this->base_reference() += n;
  75. }
  76. };
  77. template< class Rng >
  78. struct indexed_range :
  79. iterator_range< indexed_iterator<BOOST_DEDUCED_TYPENAME range_iterator<Rng>::type> >
  80. {
  81. private:
  82. typedef indexed_iterator<BOOST_DEDUCED_TYPENAME range_iterator<Rng>::type>
  83. iter_type;
  84. typedef iterator_range<iter_type>
  85. base;
  86. public:
  87. template< class Index >
  88. indexed_range( Index i, Rng& r )
  89. : base( iter_type(boost::begin(r), i), iter_type(boost::end(r),i) )
  90. { }
  91. };
  92. } // 'range_detail'
  93. // Make this available to users of this library. It will sometimes be
  94. // required since it is the return type of operator '|' and
  95. // index().
  96. using range_detail::indexed_range;
  97. namespace adaptors
  98. {
  99. template< class SinglePassRange >
  100. inline indexed_range<SinglePassRange>
  101. operator|( SinglePassRange& r,
  102. const indexed& f )
  103. {
  104. return indexed_range<SinglePassRange>( f.val, r );
  105. }
  106. template< class SinglePassRange >
  107. inline indexed_range<const SinglePassRange>
  108. operator|( const SinglePassRange& r,
  109. const indexed& f )
  110. {
  111. return indexed_range<const SinglePassRange>( f.val, r );
  112. }
  113. template<class SinglePassRange, class Index>
  114. inline indexed_range<SinglePassRange>
  115. index(SinglePassRange& rng, Index index_value)
  116. {
  117. return indexed_range<SinglePassRange>(index_value, rng);
  118. }
  119. template<class SinglePassRange, class Index>
  120. inline indexed_range<const SinglePassRange>
  121. index(const SinglePassRange& rng, Index index_value)
  122. {
  123. return indexed_range<const SinglePassRange>(index_value, rng);
  124. }
  125. } // 'adaptors'
  126. }
  127. #ifdef BOOST_MSVC
  128. #pragma warning( pop )
  129. #endif
  130. #endif