NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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>
32 
33 namespace nvbio {
34 
37 
40 
45 template <typename T, typename Transform>
47 {
48  typedef typename Transform::result_type value_type;
51  typedef value_type* pointer;
52  typedef typename std::iterator_traits<T>::difference_type difference_type;
53  //typedef typename std::iterator_traits<T>::distance_type distance_type;
54  typedef typename std::iterator_traits<T>::iterator_category iterator_category;
55 
60 
64  transform_iterator(const T base, const Transform f) : m_base( base ), m_f( f ) {}
65 
70 
74  const_reference operator[](const uint32 i) const { return m_f( m_base[i] ); }
75 
79  value_type operator*() const { return m_f( m_base[0] ); }
80 
85  {
86  ++m_base;
87  return *this;
88  }
89 
94  {
96  ++m_base;
97  return r;
98  }
99 
104  {
105  --m_base;
106  return *this;
107  }
108 
113  {
115  --m_base;
116  return r;
117  }
118 
123  {
124  return transform_iterator( m_base + i, m_f );
125  }
126 
131  {
132  return transform_iterator( m_base - i, m_f );
133  }
134 
139  {
140  m_base += i;
141  return *this;
142  }
143 
148  {
149  m_base -= i;
150  return *this;
151  }
152 
157  {
158  return m_base - it.m_base;
159  }
160 
165  {
166  m_base = it.m_base;
167  m_f = it.m_f;
168  return *this;
169  }
170 
172  Transform m_f;
173 };
174 
177 template <typename T, typename Transform>
180 {
181  return transform_iterator<T,Transform>( it, f );
182 }
183 
184 
187 template <typename T, typename Transform>
190 {
191  return (it1.m_base == it2.m_base)/* && (it1.m_f == it2.m_f)*/;
192 }
195 template <typename T, typename Transform>
198 {
199  return (it1.m_base != it2.m_base)/* || (it1.m_f != it2.m_f)*/;
200 }
203 template <typename T, typename Transform>
205 bool operator<(const transform_iterator<T,Transform> it1, const transform_iterator<T,Transform> it2) { return (it1.m_base < it2.m_base); }
208 template <typename T, typename Transform>
210 bool operator<=(const transform_iterator<T,Transform> it1, const transform_iterator<T,Transform> it2) { return (it1.m_base <= it2.m_base); }
213 template <typename T, typename Transform>
218 template <typename T, typename Transform>
221 
222 
227 template <typename T1, typename T2, typename Transform>
229 {
230  typedef typename Transform::result_type value_type;
233  typedef value_type* pointer;
234  typedef typename std::iterator_traits<T1>::difference_type difference_type;
235  //typedef typename std::iterator_traits<T1>::distance_type distance_type;
236  typedef typename std::iterator_traits<T1>::iterator_category iterator_category;
237 
242 
246  binary_transform_iterator(const T1 base1, const T2 base2, const Transform f) : m_base1( base1 ), m_base2( base2 ), m_f( f ) {}
247 
252 
256  const_reference operator[](const uint32 i) const { return m_f( m_base1[i], m_base2[i] ); }
257 
261  value_type operator*() const { return m_f( m_base1[0], m_base2[0] ); }
262 
267  {
268  ++m_base1;
269  ++m_base2;
270  return *this;
271  }
272 
277  {
279  ++m_base1;
280  ++m_base2;
281  return r;
282  }
283 
288  {
289  --m_base1;
290  --m_base2;
291  return *this;
292  }
293 
298  {
300  --m_base1;
301  --m_base2;
302  return r;
303  }
304 
309  {
310  return binary_transform_iterator( m_base1 + i, m_base2 + i, m_f );
311  }
312 
317  {
318  return binary_transform_iterator( m_base1 - i, m_base2 - i, m_f );
319  }
320 
325  {
326  m_base1 += i;
327  m_base2 += i;
328  return *this;
329  }
330 
335  {
336  m_base1 -= i;
337  m_base2 -= i;
338  return *this;
339  }
340 
345  {
346  return m_base1 - it.m_base1;
347  }
348 
353  {
354  m_base1 = it.m_base1;
355  m_base2 = it.m_base2;
356  m_f = it.m_f;
357  return *this;
358  }
359 
362  Transform m_f;
363 };
364 
367 template <typename T1, typename T2, typename Transform>
370 {
371  return binary_transform_iterator<T1,T2,Transform>( it1, it2, f );
372 }
373 
374 
377 template <typename T1, typename T2, typename Transform>
380 {
381  return (it1.m_base1 == it2.m_base1) && (it1.m_base2 == it2.m_base2)/* && (it1.m_f == it2.m_f)*/;
382 }
385 template <typename T1, typename T2, typename Transform>
388 {
389  return (it1.m_base1 != it2.m_base1) || (it1.m_base2 != it2.m_base2)/* || (it1.m_f != it2.m_f)*/;
390 }
393 template <typename T1, typename T2, typename Transform>
395 bool operator<(const binary_transform_iterator<T1,T2,Transform> it1, const binary_transform_iterator<T1,T2,Transform> it2) { return (it1.m_base1 < it2.m_base1); }
398 template <typename T1, typename T2, typename Transform>
400 bool operator<=(const binary_transform_iterator<T1,T2,Transform> it1, const binary_transform_iterator<T1,T2,Transform> it2) { return (it1.m_base1 <= it2.m_base1); }
403 template <typename T1, typename T2, typename Transform>
408 template <typename T1, typename T2, typename Transform>
411 
414 
415 } // namespace nvbio