Fork me on GitHub
 All Classes Files Functions Variables Groups Pages
random_iterator.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2014 NVIDIA Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
21 #pragma once
22 
23 #include <cusp/detail/config.h>
24 
25 #include <thrust/functional.h>
26 
27 #include <thrust/iterator/counting_iterator.h>
28 #include <thrust/iterator/permutation_iterator.h>
29 #include <thrust/iterator/transform_iterator.h>
30 
31 namespace cusp
32 {
33 
35 namespace detail
36 {
37 // Forward definition
38 template<typename> struct random_functor_type;
39 template<typename,typename> struct random_integer_functor;
40 } // end detail
80 template<typename T>
82 {
83 public:
84 
86  typedef T value_type;
87  typedef T* pointer;
88  typedef T& reference;
89  typedef size_t difference_type;
90  typedef size_t size_type;
91  typedef thrust::random_access_traversal_tag iterator_category;
92 
93  typedef std::ptrdiff_t IndexType;
94  typedef detail::random_integer_functor<IndexType,T> IndexFunctor;
95  typedef typename thrust::counting_iterator<IndexType> CountingIterator;
96  typedef typename thrust::transform_iterator<IndexFunctor, CountingIterator, IndexType> RandomCountingIterator;
97 
98  // type of the random_range iterator
99  typedef typename detail::random_functor_type<T>::type Functor;
100  typedef typename thrust::transform_iterator<Functor, RandomCountingIterator, T> RandomTransformIterator;
101  typedef RandomTransformIterator iterator;
107  random_iterator(const size_t seed = 0)
108  : random_counting_iterator(CountingIterator(0), IndexFunctor(seed)) {}
109 
114  iterator begin(void) const
115  {
116  return RandomTransformIterator(random_counting_iterator, Functor());
117  }
118 
125  value_type operator[](size_type n) const
126  {
127  return *(begin() + n);
128  }
129 
130 protected:
131 
133  RandomCountingIterator random_counting_iterator;
136 }; // end random_iterator
137 
141 } // end namespace cusp
142 
143 #include <cusp/iterator/detail/random_iterator.inl>
random_iterator(const size_t seed=0)
This constructor builds a random_iterator using a specified seed.
Iterator for generating random values.
iterator begin(void) const
This method returns an iterator pointing to the beginning of this random sequence of entries...
value_type operator[](size_type n) const
Subscript access to the data contained in this iterator.