File: is_contiguous_iterator.cu

package info (click to toggle)
cccl 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,248 kB
  • sloc: cpp: 264,457; python: 6,421; sh: 2,762; perl: 460; makefile: 114; xml: 13
file content (139 lines) | stat: -rw-r--r-- 6,631 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <thrust/detail/static_assert.h>

#include <array>
#include <iterator>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <unittest/unittest.h>
#if _CCCL_STD_VER >= 2017
#  include <string_view>
#endif
#include <thrust/device_ptr.h>
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/type_traits/is_contiguous_iterator.h>

#include <deque>
#include <list>
#include <map>
#include <set>

THRUST_STATIC_ASSERT((thrust::is_contiguous_iterator<std::string::iterator>::value));

THRUST_STATIC_ASSERT((thrust::is_contiguous_iterator<std::wstring::iterator>::value));

#if _CCCL_STD_VER >= 2017
THRUST_STATIC_ASSERT((thrust::is_contiguous_iterator<std::string_view::iterator>::value));

THRUST_STATIC_ASSERT((thrust::is_contiguous_iterator<std::wstring_view::iterator>::value));
#endif

THRUST_STATIC_ASSERT((!thrust::is_contiguous_iterator<std::vector<bool>::iterator>::value));

template <typename T>
_CCCL_HOST void test_is_contiguous_iterator()
{
  THRUST_STATIC_ASSERT((thrust::is_contiguous_iterator<T*>::value));

  THRUST_STATIC_ASSERT((thrust::is_contiguous_iterator<T const*>::value));

  THRUST_STATIC_ASSERT((thrust::is_contiguous_iterator<thrust::device_ptr<T>>::value));

  THRUST_STATIC_ASSERT((thrust::is_contiguous_iterator<typename std::vector<T>::iterator>::value));

  THRUST_STATIC_ASSERT((!thrust::is_contiguous_iterator<typename std::vector<T>::reverse_iterator>::value));

  THRUST_STATIC_ASSERT((thrust::is_contiguous_iterator<typename std::array<T, 1>::iterator>::value));

  THRUST_STATIC_ASSERT((!thrust::is_contiguous_iterator<typename std::list<T>::iterator>::value));

  THRUST_STATIC_ASSERT((!thrust::is_contiguous_iterator<typename std::deque<T>::iterator>::value));

  THRUST_STATIC_ASSERT((!thrust::is_contiguous_iterator<typename std::set<T>::iterator>::value));

  THRUST_STATIC_ASSERT((!thrust::is_contiguous_iterator<typename std::multiset<T>::iterator>::value));

  THRUST_STATIC_ASSERT((!thrust::is_contiguous_iterator<typename std::map<T, T>::iterator>::value));

  THRUST_STATIC_ASSERT((!thrust::is_contiguous_iterator<typename std::multimap<T, T>::iterator>::value));

  THRUST_STATIC_ASSERT((!thrust::is_contiguous_iterator<typename std::unordered_set<T>::iterator>::value));

  THRUST_STATIC_ASSERT((!thrust::is_contiguous_iterator<typename std::unordered_multiset<T>::iterator>::value));

  THRUST_STATIC_ASSERT((!thrust::is_contiguous_iterator<typename std::unordered_map<T, T>::iterator>::value));

  THRUST_STATIC_ASSERT((!thrust::is_contiguous_iterator<typename std::unordered_multimap<T, T>::iterator>::value));

  THRUST_STATIC_ASSERT((!thrust::is_contiguous_iterator<std::istream_iterator<T>>::value));

  THRUST_STATIC_ASSERT((!thrust::is_contiguous_iterator<std::ostream_iterator<T>>::value));
}
DECLARE_GENERIC_UNITTEST(test_is_contiguous_iterator);

template <typename Vector>
_CCCL_HOST void test_is_contiguous_iterator_vectors()
{
  THRUST_STATIC_ASSERT((thrust::is_contiguous_iterator<typename Vector::iterator>::value));
}
DECLARE_VECTOR_UNITTEST(test_is_contiguous_iterator_vectors);

struct expect_pointer
{};
struct expect_passthrough
{};

template <typename IteratorT, typename PointerT, typename expected_unwrapped_type /* = expect_[pointer|passthrough] */>
struct check_unwrapped_iterator
{
  using unwrapped_t = ::cuda::std::__libcpp_remove_reference_t<decltype(thrust::try_unwrap_contiguous_iterator(
    cuda::std::declval<IteratorT>()))>;

  static constexpr bool value =
    std::is_same<expected_unwrapped_type, expect_pointer>::value
      ? std::is_same<unwrapped_t, PointerT>::value
      : std::is_same<unwrapped_t, IteratorT>::value;
};

template <typename T>
void test_try_unwrap_contiguous_iterator()
{
  // Raw pointers should pass whether expecting pointers or passthrough.
  THRUST_STATIC_ASSERT((check_unwrapped_iterator<T*, T*, expect_pointer>::value));
  THRUST_STATIC_ASSERT((check_unwrapped_iterator<T*, T*, expect_passthrough>::value));
  THRUST_STATIC_ASSERT((check_unwrapped_iterator<T const*, T const*, expect_pointer>::value));
  THRUST_STATIC_ASSERT((check_unwrapped_iterator<T const*, T const*, expect_passthrough>::value));

  THRUST_STATIC_ASSERT((check_unwrapped_iterator<thrust::device_ptr<T>, T*, expect_pointer>::value));
  THRUST_STATIC_ASSERT((check_unwrapped_iterator<thrust::device_ptr<T const>, T const*, expect_pointer>::value));
  THRUST_STATIC_ASSERT((check_unwrapped_iterator<typename std::vector<T>::iterator, T*, expect_pointer>::value));
  THRUST_STATIC_ASSERT(
    (check_unwrapped_iterator<typename std::vector<T>::reverse_iterator, T*, expect_passthrough>::value));
  THRUST_STATIC_ASSERT((check_unwrapped_iterator<typename std::array<T, 1>::iterator, T*, expect_pointer>::value));
  THRUST_STATIC_ASSERT(
    (check_unwrapped_iterator<typename std::array<T const, 1>::iterator, T const*, expect_pointer>::value));
  THRUST_STATIC_ASSERT((check_unwrapped_iterator<typename std::list<T>::iterator, T*, expect_passthrough>::value));
  THRUST_STATIC_ASSERT((check_unwrapped_iterator<typename std::deque<T>::iterator, T*, expect_passthrough>::value));
  THRUST_STATIC_ASSERT((check_unwrapped_iterator<typename std::set<T>::iterator, T*, expect_passthrough>::value));
  THRUST_STATIC_ASSERT((check_unwrapped_iterator<typename std::multiset<T>::iterator, T*, expect_passthrough>::value));
  THRUST_STATIC_ASSERT(
    (check_unwrapped_iterator<typename std::map<T, T>::iterator, std::pair<T const, T>*, expect_passthrough>::value));
  THRUST_STATIC_ASSERT((
    check_unwrapped_iterator<typename std::multimap<T, T>::iterator, std::pair<T const, T>*, expect_passthrough>::value));
  THRUST_STATIC_ASSERT(
    (check_unwrapped_iterator<typename std::unordered_set<T>::iterator, T*, expect_passthrough>::value));
  THRUST_STATIC_ASSERT(
    (check_unwrapped_iterator<typename std::unordered_multiset<T>::iterator, T*, expect_passthrough>::value));
  THRUST_STATIC_ASSERT(
    (check_unwrapped_iterator<typename std::unordered_map<T, T>::iterator, std::pair<T const, T>*, expect_passthrough>::
       value));
  THRUST_STATIC_ASSERT((check_unwrapped_iterator<typename std::unordered_multimap<T, T>::iterator,
                                                 std::pair<T const, T>*,
                                                 expect_passthrough>::value));
  THRUST_STATIC_ASSERT((check_unwrapped_iterator<std::istream_iterator<T>, T*, expect_passthrough>::value));
  THRUST_STATIC_ASSERT((check_unwrapped_iterator<std::ostream_iterator<T>, void, expect_passthrough>::value));
}
DECLARE_GENERIC_UNITTEST(test_try_unwrap_contiguous_iterator);