Fork me on GitHub
 All Classes Files Functions Variables Groups Pages
strided_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/distance.h>
26 #include <thrust/functional.h>
27 
28 #include <thrust/iterator/counting_iterator.h>
29 #include <thrust/iterator/permutation_iterator.h>
30 #include <thrust/iterator/transform_iterator.h>
31 
32 #include <cusp/functional.h>
33 
34 namespace cusp
35 {
36 
77 template <typename RandomAccessIterator>
79 {
80 public:
81 
83  typedef typename thrust::iterator_value<RandomAccessIterator>::type value_type;
84  typedef typename thrust::iterator_system<RandomAccessIterator>::type memory_space;
85  typedef typename thrust::iterator_pointer<RandomAccessIterator>::type pointer;
86  typedef typename thrust::iterator_reference<RandomAccessIterator>::type reference;
87  typedef typename thrust::iterator_difference<RandomAccessIterator>::type difference_type;
88  typedef typename thrust::iterator_difference<RandomAccessIterator>::type size_type;
89 
90  typedef cusp::multiplies_value<difference_type> StrideFunctor;
91  typedef typename thrust::counting_iterator<difference_type> CountingIterator;
92  typedef typename thrust::transform_iterator<StrideFunctor, CountingIterator> TransformIterator;
93  typedef typename thrust::permutation_iterator<RandomAccessIterator,TransformIterator> PermutationIterator;
94 
95  // type of the strided_range iterator
96  typedef PermutationIterator iterator;
102  : stride(0) {}
103 
109  strided_iterator(RandomAccessIterator first, RandomAccessIterator last, difference_type stride)
110  : first(first), last(last), stride(stride) {}
111 
116  iterator begin(void) const
117  {
118  return PermutationIterator(first, TransformIterator(CountingIterator(0), StrideFunctor(stride)));
119  }
120 
125  iterator end(void) const
126  {
127  return begin() + (thrust::distance(first,last) + (stride - 1)) / stride;
128  }
129 
138  reference operator[](size_type n) const
139  {
140  return *(begin() + n);
141  }
142 
143 protected:
144 
146  RandomAccessIterator first;
147  RandomAccessIterator last;
148  difference_type stride;
151 }; // end strided_iterator
152 
156 } // end namespace cusp
157 
iterator end(void) const
This method returns an iterator pointing to one element past the last of this strided sequence of ent...
strided_iterator(void)
Null constructor initializes this strided_iterator's stride to zero.
iterator begin(void) const
This method returns an iterator pointing to the beginning of this strided sequence of entries...
multiplies_value is a function object that computes the multiply of a given element by a constant val...
Definition: functional.h:220
strided_iterator(RandomAccessIterator first, RandomAccessIterator last, difference_type stride)
This constructor builds a strided_iterator from a range.
reference operator[](size_type n) const
Subscript access to the data contained in this iterator.
RandomAccessIterator for strided access to array entries.
Defines templated convenience functors analogous to what is found in thrust's functional.