distance.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #if !defined(FUSION_DISTANCE_09172005_0721)
  7. #define FUSION_DISTANCE_09172005_0721
  8. #include <boost/fusion/support/config.hpp>
  9. #include <boost/fusion/iterator/detail/distance.hpp>
  10. #include <boost/fusion/support/category_of.hpp>
  11. #include <boost/mpl/int.hpp>
  12. #include <boost/mpl/assert.hpp>
  13. #include <boost/type_traits/is_same.hpp>
  14. #include <boost/fusion/support/tag_of.hpp>
  15. namespace boost { namespace fusion
  16. {
  17. struct random_access_traversal_tag;
  18. // Special tags:
  19. struct iterator_facade_tag; // iterator facade tag
  20. struct boost_array_iterator_tag; // boost::array iterator tag
  21. struct mpl_iterator_tag; // mpl sequence iterator tag
  22. struct std_pair_iterator_tag; // std::pair iterator tag
  23. namespace extension
  24. {
  25. template <typename Tag>
  26. struct distance_impl
  27. {
  28. // default implementation
  29. template <typename First, typename Last>
  30. struct apply : distance_detail::linear_distance<First, Last>
  31. {};
  32. };
  33. template <>
  34. struct distance_impl<iterator_facade_tag>
  35. {
  36. template <typename First, typename Last>
  37. struct apply : First::template distance<First, Last> {};
  38. };
  39. template <>
  40. struct distance_impl<boost_array_iterator_tag>;
  41. template <>
  42. struct distance_impl<mpl_iterator_tag>;
  43. template <>
  44. struct distance_impl<std_pair_iterator_tag>;
  45. }
  46. namespace result_of
  47. {
  48. template <typename First, typename Last>
  49. struct distance
  50. : extension::distance_impl<typename detail::tag_of<First>::type>::
  51. template apply<First, Last>
  52. {
  53. typedef typename extension::distance_impl<typename detail::tag_of<First>::type>::
  54. template apply<First, Last>::type distance_application;
  55. BOOST_STATIC_CONSTANT(int, value = distance_application::value);
  56. };
  57. }
  58. template <typename First, typename Last>
  59. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  60. inline typename result_of::distance<First, Last>::type
  61. distance(First const& a, Last const& b)
  62. {
  63. return result_of::distance<First, Last>::call(a,b);
  64. }
  65. }}
  66. #endif