Bolt  1.3
C++ template library with support for OpenCL
constant_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_CONSTANT_ITERATOR_H )
19 #define BOLT_AMP_CONSTANT_ITERATOR_H
20 #include "bolt/amp/bolt.h"
22 
28 namespace bolt {
29 namespace amp {
30 
32  : public fancy_iterator_tag
33  { // identifying tag for random-access iterators
34  };
35 
71  template< typename value_type >
72  class constant_iterator: public std::iterator< constant_iterator_tag, typename value_type, int>
73  {
74  public:
75  typedef typename std::iterator< constant_iterator_tag, typename value_type, int>::difference_type
76  difference_type;
77 
78  typedef concurrency::array_view< value_type > arrayview_type;
80 
81 
82  struct Payload
83  {
84  value_type m_Value;
85  };
86 
87  // Basic constructor requires a reference to the container and a positional element
88  constant_iterator( value_type init, const control& ctl = control::getDefault( ) ):
89  m_constValue( init ), m_Index( 0 ){}
90 
91  // This copy constructor allows an iterator to convert into a const_iterator, but not vica versa
92  template< typename OtherType >
93  constant_iterator( const constant_iterator< OtherType >& rhs ): m_devMemory( rhs.m_devMemory ),
94  m_Index( rhs.m_Index ), m_constValue( rhs.m_constValue ){}
95 
96  // This copy constructor allows an iterator to convert into a const_iterator, but not vica versa
97  constant_iterator< value_type >& operator= ( const constant_iterator< value_type >& rhs )
98  {
99  if( this == &rhs )
100  return *this;
101 
102  m_constValue = rhs.m_constValue;
103  m_Index = rhs.m_Index;
104  return *this;
105  }
106 
107  constant_iterator< value_type >& operator+= ( const difference_type & n )
108  {
109  advance( n );
110  return *this;
111  }
112 
113  const constant_iterator< value_type > operator+ ( const difference_type & n ) const
114  {
115  constant_iterator< value_type > result( *this );
116  result.advance( n );
117  return result;
118  }
119 
120  const constant_iterator< value_type > operator- ( const difference_type & n ) const
121  {
122  constant_iterator< value_type > result( *this );
123  result.advance( -n );
124  return result;
125  }
126 
127  const constant_iterator< value_type > & getBuffer( const_iterator itr ) const
128  {
129  return *this;
130  }
131 
132 
133  const constant_iterator< value_type > & getContainer( ) const
134  {
135  return *this;
136  }
137 
138  difference_type operator- ( const constant_iterator< value_type >& rhs ) const
139  {
140  //return static_cast< typename iterator_facade::difference_type >( 1 );
141  return m_Index - rhs.m_Index;
142  }
143 
144  // Public member variables
145  difference_type m_Index;
146 
147 
148  // Used for templatized copy constructor and the templatized equal operator
149  template < typename > friend class constant_iterator;
150 
151  // For a constant_iterator, do nothing on an advance
152  void advance( difference_type n )
153  {
154  m_Index += n;
155  }
156 
157  value_type* getPointer()
158  {
159  return &m_constValue;
160  }
161 
162  const value_type* getPointer() const
163  {
164  return &m_constValue;
165  }
166 
167  // Pre-increment
168  constant_iterator< value_type > operator++ ( )
169  {
170  advance( 1 );
171  constant_iterator< value_type > result( *this );
172  return result;
173  }
174 
175  // Post-increment
176  constant_iterator< value_type > operator++ ( int ) const
177  {
178  constant_iterator< value_type > result( *this );
179  result.advance( 1 );
180  return result;
181  }
182 
183  // Pre-decrement
184  constant_iterator< value_type > operator--( ) const
185  {
186  constant_iterator< value_type > result( *this );
187  result.advance( -1 );
188  return result;
189  }
190 
191  // Post-decrement
192  constant_iterator< value_type > operator--( int ) const
193  {
194  constant_iterator< value_type > result( *this );
195  result.advance( -1 );
196  return result;
197  }
198 
199  difference_type getIndex() const
200  {
201  return m_Index;
202  }
203 
204  template< typename OtherType >
205  bool operator== ( const constant_iterator< OtherType >& rhs ) const
206  {
207  bool sameIndex = (rhs.m_constValue == m_constValue) && (rhs.m_Index == m_Index);
208 
209  return sameIndex;
210  }
211 
212  template< typename OtherType >
213  bool operator!= ( const constant_iterator< OtherType >& rhs ) const
214  {
215  bool sameIndex = (rhs.m_constValue != m_constValue) || (rhs.m_Index != m_Index);
216 
217  return sameIndex;
218  }
219 
220  template< typename OtherType >
221  bool operator< ( const constant_iterator< OtherType >& rhs ) const
222  {
223  bool sameIndex = (m_Index < rhs.m_Index);
224 
225  return sameIndex;
226  }
227 
228  value_type operator*() const restrict(cpu,amp)
229  {
230  value_type xy = m_constValue;
231  return xy;
232  }
233 
234  value_type operator[](int x) const restrict(cpu,amp)
235  {
236  value_type xy = m_constValue;
237  return xy;
238  }
239 
240  private:
241  value_type m_constValue;
242  };
243 
244 
245  template< typename Type >
246  constant_iterator< Type > make_constant_iterator( Type constValue )
247  {
248  constant_iterator< Type > tmp( constValue );
249  return tmp;
250  }
251 
252 }
253 }
254 
255 
256 #endif