NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vector_view.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 
32 #pragma once
33 
34 #include <nvbio/basic/types.h>
35 #include <nvbio/basic/iterator.h>
37 #include <iterator>
38 #include <vector>
39 
40 namespace nvbio {
41 
77 
80 
87 template <typename Iterator, typename IndexType = uint32>
89 {
90  typedef Iterator iterator;
91  typedef Iterator const_iterator;
93 
98  typedef IndexType size_type;
99  typedef IndexType index_type;
101  typedef std::random_access_iterator_tag iterator_category;
102 
105 
110 
114  vector_view(const IndexType size, Iterator vec) : m_size( size ), m_vec( vec ) {}
115 
119  void resize(const uint32 sz) { m_size = sz; }
120 
124  void clear() { m_size = 0; }
125 
129  IndexType size() const { return m_size; }
130 
134  IndexType length() const { return m_size; }
135 
139  bool empty() const { return m_size == 0; }
140 
144  const_reference operator*() const { NVBIO_CUDA_DEBUG_ASSERT( 0 < m_size, "vector_view: access out of bounds, dereferenced zero sized vector\n" ); return *m_vec; }
145 
149  reference operator*() { NVBIO_CUDA_DEBUG_ASSERT( 0 < m_size, "vector_view: access out of bounds, dereferenced zero sized vector\n" ); return *m_vec; }
150 
154  const_reference operator[](const IndexType i) const { NVBIO_CUDA_DEBUG_ASSERT( i < m_size, "vector_view: access out of bounds, %llu >= %llu\n", uint64(i), uint64(m_size) ); return m_vec[i]; }
155 
158  NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
159  reference operator[](const IndexType i) { NVBIO_CUDA_DEBUG_ASSERT( i < m_size, "vector_view: access out of bounds, %llu >= %llu\n", uint64(i), uint64(m_size) ); return m_vec[i]; }
160 
163  NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
164  const_reference front(void) const { return m_vec[0]; }
165 
168  NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
169  reference front(void) { return m_vec[0]; }
170 
173  NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
174  const_reference back(void) const { return m_vec[m_size-1]; }
175 
178  NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
179  reference back(void) { return m_vec[m_size-1]; }
180 
183  NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
184  Iterator base() const { return m_vec; }
185 
188  NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
189  const_iterator begin() const { return m_vec; }
190 
193  NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
194  const_iterator end() const { return m_vec + m_size; }
195 
198  NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
199  iterator begin() { return m_vec; }
200 
203  NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
204  iterator end() { return m_vec + m_size; }
205 
208  NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
209  operator Iterator() const { return m_vec; }
210 
213  NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
214  void push_back(const_reference val) { m_vec[ m_size ] = val; m_size++; }
215 
218  NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
219  void pop_back() { --m_size; }
220 
221  IndexType m_size;
222  Iterator m_vec;
223 };
224 
228 template <typename Iterator, typename IndexType>
229 struct string_traits< vector_view<Iterator,IndexType> >
230 {
231  static const uint32 SYMBOL_SIZE = string_traits<Iterator>::SYMBOL_SIZE;
232 
233  typedef typename vector_view<Iterator,IndexType>::value_type value_type;
234  typedef typename vector_view<Iterator,IndexType>::reference reference;
235  typedef typename vector_view<Iterator,IndexType>::index_type index_type;
236  typedef typename vector_view<Iterator,IndexType>::iterator iterator;
237  typedef typename vector_view<Iterator,IndexType>::const_iterator const_iterator;
238  typedef typename vector_view<Iterator,IndexType>::forward_iterator forward_iterator;
239 };
240 
243 template <typename Iterator>
244 NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
245 uint32 length(const vector_view<Iterator>& vec) { return vec.length(); }
246 
249 template <typename T>
250 NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
251 T raw_pointer(const vector_view<T>& vec) { return vec.base(); }
252 
255 template <typename T, typename I>
256 NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
257 T begin(vector_view<T,I>& vec) { return vec.begin(); }
258 
261 template <typename T, typename I>
262 NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
263 T begin(const vector_view<T,I>& vec) { return vec.begin(); }
264 
265 //
266 // --- std::vector views ----------------------------------------------------------------------------
267 //
268 
271 template <typename T> struct plain_view_subtype< std::vector<T> > { typedef vector_view<T*,uint64> type; };
272 
275 template <typename T> struct plain_view_subtype< const std::vector<T> > { typedef vector_view<const T*,uint64> type; };
276 
277 
280 template <typename T>
281 vector_view<T*,uint64> plain_view(std::vector<T>& vec) { return vector_view<T*,uint64>( vec.size(), vec.size() ? &vec[0] : NULL ); }
282 
285 template <typename T>
286 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 ); }
287 
290 template <typename T>
291 T* raw_pointer(std::vector<T>& vec) { return vec.size() ? &vec[0] : NULL; }
292 
295 template <typename T>
296 const T* raw_pointer(const std::vector<T>& vec) { return vec.size() ? &vec[0] : NULL; }
297 
300 template <typename T> T* begin(T* vec) { return vec; }
301 
304 template <typename T> const T* begin(const T* vec) { return vec; }
305 
307 
308 } // namespace nvbio