Fermat
vector_view.h
1 /*
2  * cugar
3  * Copyright (c) 2011-2018, 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 
32 #pragma once
33 
34 #include <cugar/basic/types.h>
35 #include <cugar/basic/iterator.h>
36 #include <iterator>
37 #include <vector>
38 
39 namespace cugar {
40 
76 
79 
86 template <typename Iterator, typename IndexType = uint32>
88 {
89  typedef Iterator iterator;
90  typedef Iterator const_iterator;
91  typedef typename iterator_traits<Iterator>::forward_iterator forward_iterator;
92 
93  typedef typename iterator_traits<Iterator>::value_type value_type;
94  typedef typename iterator_traits<Iterator>::reference reference;
95  typedef typename to_const<reference>::type const_reference;
96  typedef typename iterator_traits<Iterator>::pointer pointer;
97  typedef IndexType size_type;
98  typedef IndexType index_type;
99  typedef typename iterator_traits<Iterator>::difference_type difference_type;
100  typedef std::random_access_iterator_tag iterator_category;
101 
104 
107  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
109 
112  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
113  vector_view(const IndexType size, Iterator vec) : m_size( size ), m_vec( vec ) {}
114 
117  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
118  void resize(const uint32 sz) { m_size = sz; }
119 
122  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
123  void clear() { m_size = 0; }
124 
127  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
128  IndexType size() const { return m_size; }
129 
132  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
133  IndexType length() const { return m_size; }
134 
137  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
138  bool empty() const { return m_size == 0; }
139 
142  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
143  const_reference operator*() const { CUGAR_CUDA_DEBUG_ASSERT( 0 < m_size, "vector_view: access out of bounds, dereferenced zero sized vector\n" ); return *m_vec; }
144 
147  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
148  reference operator*() { CUGAR_CUDA_DEBUG_ASSERT( 0 < m_size, "vector_view: access out of bounds, dereferenced zero sized vector\n" ); return *m_vec; }
149 
152  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
153  const_reference operator[](const IndexType i) const { CUGAR_CUDA_DEBUG_ASSERT( i < m_size, "vector_view: access out of bounds, %llu >= %llu\n", uint64(i), uint64(m_size) ); return m_vec[i]; }
154 
157  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
158  reference operator[](const IndexType i) { CUGAR_CUDA_DEBUG_ASSERT( i < m_size, "vector_view: access out of bounds, %llu >= %llu\n", uint64(i), uint64(m_size) ); return m_vec[i]; }
159 
162  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
163  const_reference front(void) const { return m_vec[0]; }
164 
167  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
168  reference front(void) { return m_vec[0]; }
169 
172  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
173  const_reference back(void) const { return m_vec[m_size-1]; }
174 
177  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
178  reference back(void) { return m_vec[m_size-1]; }
179 
182  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
183  Iterator base() const { return m_vec; }
184 
187  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
188  const_iterator begin() const { return m_vec; }
189 
192  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
193  const_iterator end() const { return m_vec + m_size; }
194 
197  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
198  iterator begin() { return m_vec; }
199 
202  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
203  iterator end() { return m_vec + m_size; }
204 
207  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
208  operator Iterator() const { return m_vec; }
209 
212  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
213  void push_back(const_reference val) { m_vec[ m_size ] = val; m_size++; }
214 
217  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
218  void pop_back() { --m_size; }
219 
220  IndexType m_size;
221  Iterator m_vec;
222 };
223 
226 template <typename Iterator>
227 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
228 uint32 length(const vector_view<Iterator>& vec) { return vec.length(); }
229 
232 template <typename T>
233 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
234 T raw_pointer(const vector_view<T>& vec) { return vec.base(); }
235 
238 template <typename T, typename I>
239 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
240 T begin(vector_view<T,I>& vec) { return vec.begin(); }
241 
244 template <typename T, typename I>
245 CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
246 T begin(const vector_view<T,I>& vec) { return vec.begin(); }
247 
248 //
249 // --- std::vector views ----------------------------------------------------------------------------
250 //
251 
254 template <typename T> struct plain_view_subtype< std::vector<T> > { typedef vector_view<T*,uint64> type; };
255 
258 template <typename T> struct plain_view_subtype< const std::vector<T> > { typedef vector_view<const T*,uint64> type; };
259 
260 
263 template <typename T>
264 vector_view<T*,uint64> plain_view(std::vector<T>& vec) { return vector_view<T*,uint64>( vec.size(), vec.size() ? &vec[0] : NULL ); }
265 
268 template <typename T>
269 vector_view<const T*,uint64> plain_view(const std::vector<T>& vec) { return vector_view<const T*,uint64>( vec.size(), vec.size() ? &vec[0] : NULL ); }
270 
273 template <typename T>
274 T* raw_pointer(std::vector<T>& vec) { return vec.size() ? &vec[0] : NULL; }
275 
278 template <typename T>
279 const T* raw_pointer(const std::vector<T>& vec) { return vec.size() ? &vec[0] : NULL; }
280 
283 template <typename T> T* begin(T* vec) { return vec; }
284 
287 template <typename T> const T* begin(const T* vec) { return vec; }
288 
290 
291 } // namespace cugar
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE reference operator*()
Definition: vector_view.h:148
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE void clear()
Definition: vector_view.h:123
Definition: vector_view.h:87
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE IndexType length() const
Definition: vector_view.h:133
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE IndexType size() const
Definition: vector_view.h:128
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE vector_view()
Definition: vector_view.h:108
T forward_iterator
add forward iterator conversion
Definition: iterator.h:118
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE bool empty() const
Definition: vector_view.h:138
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE const_reference operator*() const
Definition: vector_view.h:143
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE void resize(const uint32 sz)
Definition: vector_view.h:118
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE vector_view(const IndexType size, Iterator vec)
Definition: vector_view.h:113
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE const_reference operator[](const IndexType i) const
Definition: vector_view.h:153