NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
strided_iterator.h
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
30 #include <nvbio/basic/types.h>
31 #include <nvbio/basic/iterator.h>
32 
33 namespace nvbio {
34 
47 
50 
53 
59 template <typename T>
61 {
62  typedef typename std::iterator_traits<T>::value_type value_type;
63  typedef typename std::iterator_traits<T>::reference reference;
65  typedef typename std::iterator_traits<T>::pointer pointer;
66  typedef typename std::iterator_traits<T>::difference_type difference_type;
67  //typedef typename std::iterator_traits<T>::distance_type distance_type;
68  typedef typename std::iterator_traits<T>::iterator_category iterator_category;
69 
74 
78  strided_iterator(T vec, const uint32 stride) : m_vec( vec ), m_stride( stride ) {}
79 
83  const_reference operator*() const { return *m_vec; }
84 
88  reference operator*() { return *m_vec; }
89 
93  const_reference operator[](const uint32 i) const { return m_vec[i*m_stride]; }
94 
98  reference operator[](const uint32 i) { return m_vec[i*m_stride]; }
99 
104  {
105  return strided_iterator( m_vec + i * m_stride, m_stride );
106  }
107 
112  {
113  return m_vec - it.m_vec;
114  }
115 
120  {
121  m_vec += m_stride;
122  return *this;
123  }
124 
125  T m_vec;
127 };
128 
131 template <typename T>
134 {
135  return strided_iterator<T>( it, stride );
136 }
137 
140 template <typename T>
143 {
144  return (it1.m_vec == it2.m_vec) && (it1.m_stride == it2.m_stride);
145 }
148 template <typename T>
151 {
152  return (it1.m_vec != it2.m_vec) || (it1.m_stride != it2.m_stride);
153 }
156 template <typename T>
158 bool operator<(const strided_iterator<T> it1, const strided_iterator<T> it2) { return (it1.m_vec < it2.m_vec); }
161 template <typename T>
163 bool operator<=(const strided_iterator<T> it1, const strided_iterator<T> it2) { return (it1.m_vec <= it2.m_vec); }
166 template <typename T>
168 bool operator>(const strided_iterator<T> it1, const strided_iterator<T> it2) { return (it1.m_vec > it2.m_vec); }
171 template <typename T>
173 bool operator>=(const strided_iterator<T> it1, const strided_iterator<T> it2) { return (it1.m_vec >= it2.m_vec); }
174 
176 {
179 };
180 
192 template <uint32 BLOCKSIZE, typename T, block_strided_layout LAYOUT = ROW_MAJOR_LAYOUT>
194 {
195  typedef typename std::iterator_traits<T>::value_type value_type;
196  typedef typename std::iterator_traits<T>::reference reference;
198  typedef typename std::iterator_traits<T>::pointer pointer;
199  typedef typename std::iterator_traits<T>::difference_type difference_type;
200  //typedef typename std::iterator_traits<T>::distance_type distance_type;
201  typedef typename std::iterator_traits<T>::iterator_category iterator_category;
202 
207 
211  block_strided_iterator(T vec, const uint32 stride, const uint32 offset = 0) : m_vec( vec ), m_offset(offset), m_stride( stride ) {}
212 
217 
222  {
223  if (LAYOUT == ROW_MAJOR_LAYOUT)
224  return m_vec[((i+m_offset) / BLOCKSIZE)*m_stride + ((i+m_offset) % BLOCKSIZE)];
225  else
226  return m_vec[((i+m_offset) % BLOCKSIZE)*m_stride + ((i+m_offset) / BLOCKSIZE)];
227  }
228 
233  {
234  if (LAYOUT == ROW_MAJOR_LAYOUT)
235  return m_vec[((i+m_offset) / BLOCKSIZE)*m_stride + ((i+m_offset) % BLOCKSIZE)];
236  else
237  return m_vec[((i+m_offset) % BLOCKSIZE)*m_stride + ((i+m_offset) / BLOCKSIZE)];
238  }
239 
244  {
246  }
247 
252  {
253  return (m_vec + m_offset) - (it.m_vec + it.m_offset);
254  }
255 
260  {
261  ++m_offset;
262  return *this;
263  }
264 
265  T m_vec;
268 };
269 
272 template <uint32 BLOCKSIZE,typename T, block_strided_layout LAYOUT>
275 {
276  return (it1.m_vec == it2.m_vec) && (it1.m_offset == it2.m_offset) && (it1.m_stride == it2.m_stride);
277 }
280 template <uint32 BLOCKSIZE,typename T, block_strided_layout LAYOUT>
283 {
284  return (it1.m_vec != it2.m_vec) || (it1.m_offset != it2.m_offset) || (it1.m_stride != it2.m_stride);
285 }
286 
289 
290 } // namespace nvbio