Bolt  1.3
C++ template library with support for OpenCL
transform_iterator.h
Go to the documentation of this file.
1 /***************************************************************************
2 * 2012,2014 Advanced Micro Devices, Inc. All rights reserved.
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 ***************************************************************************/
17 #pragma once
18 #if !defined( BOLT_AMP_TRANSFORM_ITERATOR_H )
19 #define BOLT_AMP_TRANSFORM_ITERATOR_H
20 #include "bolt/amp/bolt.h"
23 #include "bolt/amp/device_vector.h"
24 
121 namespace bolt {
122 namespace amp {
123 
125  : public fancy_iterator_tag
126  { // identifying tag for random-access iterators
127  };
128 
129  template< class UnaryFunc, class Iterator >
130  class transform_iterator: public std::iterator< transform_iterator_tag,
131  std::result_of<UnaryFunc()>,
132  int >
133  {
134 
136 
137  public:
138  typedef typename std::iterator< transform_iterator_tag, typename std::result_of<UnaryFunc()>, int>::difference_type
139  difference_type;
140 
141  typedef UnaryFunc unary_func;
142  typedef typename UnaryFunc::result_type value_type;
143  typedef typename std::iterator_traits<Iterator>::pointer pointer;
144 
145  // Default constructor
146  transform_iterator( ):m_Index( 0 ) {}
147 
148  ~transform_iterator( ) {}
149 
150  // Basic constructor requires a reference to the container and a positional element
151  transform_iterator( Iterator iiter, UnaryFunc ifunc, const control& ctl = control::getDefault( ) ) : iter(iiter), func(ifunc), m_Index(iiter.m_Index) {}
152 
153  // This copy constructor allows an iterator to convert into a transf_iterator, but not vica versa
154  template< class OtherUnaryFunc, class OtherIter>
155  transform_iterator( const transform_iterator< OtherUnaryFunc, OtherIter >& rhs ) :m_Index( rhs.m_Index ){}
156 
157  // This copy constructor allows an iterator to convert into a transf_iterator, but not vica versa
159  {
160  if( this == &rhs )
161  return *this;
162 
163  func = rhs.func;
164  iter = rhs.iter;
165 
166  m_Index = rhs.m_Index;
167  return *this;
168  }
169 
170  transform_iterator< UnaryFunc, Iterator >& operator+= ( const difference_type & n )
171  {
172  advance( n );
173  return *this;
174  }
175 
176  const transform_iterator< UnaryFunc, Iterator > operator+ ( const difference_type & n ) const
177  {
179  result.advance( n );
180  return result;
181  }
182 
183 
184  const transform_iterator< UnaryFunc, Iterator > operator- ( const difference_type & n ) const
185  {
187  result.advance( -n );
188  return result;
189 
190  }
191 
192 
193  const concurrency::array_view<int> & getBuffer( transf_iterator itr ) const
194  {
195  return *value;
196  }
197  UnaryFunc functor() const
198  { return func; }
199 
200  Iterator getContainer( ) const
201  {
202 
203  return iter;
204  }
205 
206  difference_type operator- ( const transform_iterator< UnaryFunc, Iterator >& rhs ) const
207  {
208  return m_Index - rhs.m_Index;
209  }
210 
211  // Public member variables
212  difference_type m_Index;
213 
214  // Used for templatized copy constructor and the templatized equal operator
215  template < typename, typename > friend class transform_iterator;
216 
217  // For a transform_iterator, do nothing on an advance
218  void advance( difference_type n )
219  {
220  m_Index += n;
221  }
222 
223  // Pre-increment
225  {
226  advance( 1 );
228  return result;
229  }
230 
231  // Post-increment
233  {
235  advance( 1 );
236  return result;
237  }
238 
239  // Pre-decrement
241  {
243  result.advance( -1 );
244  return result;
245  }
246 
247  // Post-decrement
248  transform_iterator< UnaryFunc, Iterator > operator--( int ) const
249  {
251  result.advance( -1 );
252  return result;
253  }
254 
255  difference_type getIndex() const
256  {
257  return m_Index;
258  }
259 
260  value_type* getPointer()
261  {
262  Iterator base_iterator = this->base_reference();
263  return &(*base_iterator);
264  }
265 
266  const value_type* getPointer() const
267  {
268  Iterator base_iterator = this->base_reference();
269  return &(*base_iterator);
270  }
271 
272 
273  template< class OtherUnaryFunc, class OtherIterator >
275  {
276  bool sameIndex = ( rhs.m_Index == m_Index );
277  return sameIndex;
278  }
279 
280  template< class OtherUnaryFunc, class OtherIterator >
282  {
283  bool sameIndex = ( rhs.m_Index != m_Index );
284  return sameIndex;
285  }
286 
287  template< class OtherUnaryFunc, class OtherIterator >
288  bool operator< ( const transform_iterator< OtherUnaryFunc, OtherIterator >& rhs ) const
289  {
290  bool sameIndex = (m_Index < rhs.m_Index);
291  return sameIndex;
292  }
293 
294  // Dereference operators
295  value_type operator*() const
296  {
297  return func( iter[ m_Index ] );
298  }
299 
300  value_type operator[](int x) const restrict(cpu,amp)
301  {
302  return func( iter[ x ] );
303  }
304 
305  value_type operator[](int x) restrict(cpu,amp)
306  {
307  return func( iter[ x ] );
308  }
309 
310  UnaryFunc func;
311  Iterator iter;
312  //value_type val_at;
313 
314  };
315 
316 
317  template< class UnaryFunc, class Iterator >
318  transform_iterator< UnaryFunc, Iterator > make_transform_iterator( Iterator iter, UnaryFunc func )
319  {
321  return tmp;
322  }
323 
324 }
325 }
326 
327 
328 #endif