Bolt  1.3
C++ template library with support for OpenCL
permutation_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_PERMUTATION_ITERATOR_H )
19 #define BOLT_AMP_PERMUTATION_ITERATOR_H
20 #include "bolt/amp/bolt.h"
23 
29 namespace bolt {
30 namespace amp {
31 
33  : public fancy_iterator_tag
34  { // identifying tag for random-access iterators
35  };
36 
86  template< typename element_type, typename key_type >
87  class permutation_iterator: public std::iterator< permutation_iterator_tag, typename element_type, int>
88  {
89  public:
90  typedef typename std::iterator< permutation_iterator_tag, typename element_type, int>::difference_type
91  difference_type;
92 
94  typedef typename iterator_traits<element_type>::value_type value_type;
95  typedef typename iterator_traits<key_type>::value_type index_type;
96  typedef concurrency::array_view< value_type > arrayview_type;
97 
98 
99 
100  // Default constructor
101  permutation_iterator():key_iterator(), element_iterator(), m_Index(0) { }
102 
103  // Basic constructor requires a reference to the container and a positional element
104  permutation_iterator( element_type ivalue, key_type ikey, const control& ctl = control::getDefault( ) ):
105  element_iterator ( ivalue ), key_iterator ( ikey ), m_Index( ikey.getIndex( ) ) { }
106 
107  // This copy constructor allows an iterator to convert into a perm_iterator, but not vica versa
108  template< typename OtherValType, typename OtherKeyType >
109  permutation_iterator( const permutation_iterator< OtherValType, OtherKeyType >& rhs ):m_Index( rhs.m_Index ){}
110 
111  // This copy constructor allows an iterator to convert into a perm_iterator, but not vica versa
113  {
114  if( this == &rhs )
115  return *this;
116 
117  key_iterator = rhs.key_iterator;
118  element_iterator = rhs.element_iterator;
119 
120  m_Index = rhs.m_Index;
121  return *this;
122  }
123 
124  permutation_iterator< element_type, key_type >& operator+= ( const difference_type & n ) const restrict (cpu,amp)
125  {
126  advance( n );
127  return *this;
128  }
129 
130  const permutation_iterator< element_type, key_type > operator+ ( const difference_type & n ) const restrict (cpu,amp)
131  {
133  result.advance( n );
134  return result;
135  }
136 
137  const permutation_iterator< element_type, key_type > operator- ( const difference_type & n ) const restrict (cpu,amp)
138  {
140  result.advance( -n );
141  return result;
142  }
143 
144  const concurrency::array_view<int> & getBuffer( perm_iterator itr ) const
145  {
146  return *value;
147  }
148 
149 
150  const permutation_iterator< element_type, key_type > & getContainer( ) const
151  {
152  return *this;
153  }
154 
155  difference_type operator- ( const permutation_iterator< element_type, key_type >& rhs ) const
156  {
157  return element_iterator.getIndex() - rhs.element_iterator.getIndex();
158  }
159 
160  // Public member variables
161  difference_type m_Index;
162 
163  // Used for templatized copy constructor and the templatized equal operator
164  template < typename, typename > friend class permutation_iterator;
165 
166  void advance( difference_type n ) restrict ( cpu, amp )
167  {
168  m_Index += n;
169  }
170 
171 
172  // Pre-increment
174  {
175  advance( 1 );
177  return result;
178  }
179 
180  // Post-increment
182  {
184  result.advance( 1 );
185  return result;
186  }
187 
188  // Pre-decrement
190  {
192  result.advance( -1 );
193  return result;
194  }
195 
196  // Post-decrement
197  permutation_iterator< element_type, key_type > operator--( int ) const
198  {
200  result.advance( -1 );
201  return result;
202  }
203 
204  difference_type getIndex() const
205  {
206  return m_Index;
207  }
208 
209  template< typename OtherValue, typename OtherKey >
211  {
212  bool sameIter = ( rhs.m_Index == m_Index );
213  return sameIter;
214  }
215 
216  template< typename OtherValue, typename OtherKey >
218  {
219  bool sameIter = ( rhs.m_Index != m_Index );
220  return sameIter;
221  }
222 
223  template< typename OtherValue, typename OtherKey >
224  bool operator< ( const permutation_iterator< OtherValue, OtherKey >& rhs ) const
225  {
226  bool sameIndex = (m_Index < rhs.m_Index);
227 
228  return sameIndex;
229  }
230 
231  // Dereference operators
232  value_type& operator*() const restrict(cpu,amp)
233  {
234  index_type temp_index = key_iterator[m_Index];
235  return element_iterator[temp_index];
236  }
237 
238  value_type& operator[](int x) restrict(cpu,amp)
239  {
240  index_type temp_index = key_iterator[x];
241  return element_iterator[temp_index];
242  }
243 
244  value_type& operator[](int x) const restrict(cpu,amp)
245  {
246  index_type temp_index = key_iterator[x];
247  return element_iterator[temp_index];
248  }
249 
250  key_type key_iterator;
251  element_type element_iterator;
252  };
253 
254 
255  template< typename valueType, typename keyType >
256  permutation_iterator< valueType, keyType > make_permutation_iterator( valueType value, keyType key )
257  {
259  return tmp;
260  }
261 
262 }
263 }
264 
265 
266 #endif