NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
index_transform_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>
33 
34 namespace nvbio {
35 
38 
41 
46 template <typename T, typename Transform>
48 {
50  typedef typename Transform::result_type value_type;
53  typedef value_type* pointer;
54  typedef typename std::iterator_traits<T>::difference_type difference_type;
55  //typedef typename std::iterator_traits<T>::distance_type distance_type;
56  typedef std::random_access_iterator_tag iterator_category;
57 
62 
66  index_transform_iterator(const T base, const Transform f, const difference_type i = 0) : m_base( base ), m_f( f ), m_index( i ) {}
67 
72 
76  void set(const value_type v) { m_base[ m_f( m_index ) ] = v; }
77 
81  const_reference operator[](const uint32 i) const { return m_base[ m_f( m_index + i ) ]; }
82 
86  reference operator[](const uint32 i) { return reference( *this + i ); }
87 
91  value_type operator*() const { return m_base[ m_f(m_index) ]; }
92 
96  reference operator*() { return reference( *this ); }
97 
102  {
103  ++m_index;
104  return *this;
105  }
106 
111  {
113  ++m_index;
114  return r;
115  }
116 
121  {
122  --m_index;
123  return *this;
124  }
125 
130  {
132  --m_index;
133  return r;
134  }
135 
140  {
141  return index_transform_iterator( m_base, m_f, m_index + i );
142  }
143 
148  {
149  return index_transform_iterator( m_base, m_f, m_index - i );
150  }
151 
156  {
157  m_index += i;
158  return *this;
159  }
160 
165  {
166  m_index -= i;
167  return *this;
168  }
169 
174  {
175  return m_index - it.m_index;
176  }
177 
182  {
183  m_base = it.m_base;
184  m_f = it.m_f;
185  m_index = it.m_index;
186  return *this;
187  }
188 
190  Transform m_f;
192 };
193 
196 template <typename T, typename Transform>
199 {
201 }
202 
203 
206 template <typename T, typename Transform>
209 {
210  return (it1.m_base == it2.m_base) && (it1.m_f == it2.m_f) && (it1.m_index == it2.m_index);
211 }
214 template <typename T, typename Transform>
217 {
218  return (it1.m_base != it2.m_base) || (it1.m_f != it2.m_f) || (it1.m_index != it2.m_index);
219 }
222 template <typename T, typename Transform>
224 bool operator<(const index_transform_iterator<T,Transform> it1, const index_transform_iterator<T,Transform> it2) { return (it1.m_index < it2.m_index); }
227 template <typename T, typename Transform>
229 bool operator<=(const index_transform_iterator<T,Transform> it1, const index_transform_iterator<T,Transform> it2) { return (it1.m_index <= it2.m_index); }
232 template <typename T, typename Transform>
237 template <typename T, typename Transform>
240 
243 
244 } // namespace nvbio