is_contiguous_iterator.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_DETAIL_IS_CONTIGUOUS_ITERATOR_HPP
  11. #define BOOST_COMPUTE_DETAIL_IS_CONTIGUOUS_ITERATOR_HPP
  12. #include <vector>
  13. #include <valarray>
  14. #include <boost/config.hpp>
  15. #include <boost/type_traits.hpp>
  16. #include <boost/utility/enable_if.hpp>
  17. namespace boost {
  18. namespace compute {
  19. namespace detail {
  20. // default = false
  21. template<class Iterator, class Enable = void>
  22. struct _is_contiguous_iterator : public boost::false_type {};
  23. // std::vector<T>::iterator = true
  24. template<class Iterator>
  25. struct _is_contiguous_iterator<
  26. Iterator,
  27. typename boost::enable_if<
  28. typename boost::is_same<
  29. Iterator,
  30. typename std::vector<typename Iterator::value_type>::iterator
  31. >::type
  32. >::type
  33. > : public boost::true_type {};
  34. // std::vector<T>::const_iterator = true
  35. template<class Iterator>
  36. struct _is_contiguous_iterator<
  37. Iterator,
  38. typename boost::enable_if<
  39. typename boost::is_same<
  40. Iterator,
  41. typename std::vector<typename Iterator::value_type>::const_iterator
  42. >::type
  43. >::type
  44. > : public boost::true_type {};
  45. // std::valarray<T>::iterator = true
  46. template<class Iterator>
  47. struct _is_contiguous_iterator<
  48. Iterator,
  49. typename boost::enable_if<
  50. typename boost::is_same<
  51. Iterator,
  52. typename std::valarray<typename Iterator::value_type>::iterator
  53. >::type
  54. >::type
  55. > : public boost::true_type {};
  56. // std::valarray<T>::const_iterator = true
  57. template<class Iterator>
  58. struct _is_contiguous_iterator<
  59. Iterator,
  60. typename boost::enable_if<
  61. typename boost::is_same<
  62. Iterator,
  63. typename std::valarray<typename Iterator::value_type>::const_iterator
  64. >::type
  65. >::type
  66. > : public boost::true_type {};
  67. // T* = true
  68. template<class Iterator>
  69. struct _is_contiguous_iterator<
  70. Iterator,
  71. typename boost::enable_if<
  72. boost::is_pointer<Iterator>
  73. >::type
  74. > : public boost::true_type {};
  75. // the is_contiguous_iterator meta-function returns true if Iterator points
  76. // to a range of contiguous values. examples of contiguous iterators are
  77. // std::vector<>::iterator and float*. examples of non-contiguous iterators
  78. // are std::set<>::iterator and std::insert_iterator<>.
  79. //
  80. // the implementation consists of two phases. the first checks that value_type
  81. // for the iterator is not void. this must be done as for many containers void
  82. // is not a valid value_type (ex. std::vector<void>::iterator is not valid).
  83. // after ensuring a non-void value_type, the _is_contiguous_iterator function
  84. // is invoked. it has specializations retuning true for all (known) contiguous
  85. // iterators types and a default value of false.
  86. template<class Iterator, class Enable = void>
  87. struct is_contiguous_iterator :
  88. public _is_contiguous_iterator<
  89. typename boost::remove_cv<Iterator>::type
  90. > {};
  91. // value_type of void = false
  92. template<class Iterator>
  93. struct is_contiguous_iterator<
  94. Iterator,
  95. typename boost::enable_if<
  96. typename boost::is_void<
  97. typename Iterator::value_type
  98. >::type
  99. >::type
  100. > : public boost::false_type {};
  101. } // end detail namespace
  102. } // end compute namespace
  103. } // end boost namespace
  104. #endif // BOOST_COMPUTE_DETAIL_IS_CONTIGUOUS_ITERATOR_HPP