NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
packed_vector_inl.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 
28 #pragma once
29 
30 namespace nvbio {
31 
32 // constructor
33 //
34 template <typename SystemTag, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T, typename IndexType>
36  m_storage( util::divide_ri( size, SYMBOLS_PER_WORD ) ), m_size( size )
37 {}
38 
39 // reserve
40 //
41 template <typename SystemTag, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T, typename IndexType>
43 {
44  if (m_storage.size() < util::divide_ri( m_size, SYMBOLS_PER_WORD ))
45  m_storage.resize( util::divide_ri( m_size, SYMBOLS_PER_WORD ) );
46 }
47 
48 // resize
49 //
50 template <typename SystemTag, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T, typename IndexType>
52 {
53  m_size = size;
54  reserve(size);
55 }
56 
57 
58 // clear
59 //
60 template <typename SystemTag, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T, typename IndexType>
62 {
63  resize(0);
64 }
65 
66 // return the begin iterator
67 //
68 template <typename SystemTag, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T, typename IndexType>
71 {
72  stream_type stream( &m_storage.front() );
73  return stream.begin();
74 }
75 
76 // return the end iterator
77 //
78 template <typename SystemTag, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T, typename IndexType>
81 {
82  stream_type stream( &m_storage.front() );
83  return stream.begin() + m_size;
84 }
85 
86 // return the begin iterator
87 //
88 template <typename SystemTag, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T, typename IndexType>
91 {
92  const_stream_type stream( &m_storage.front() );
93  return stream.begin();
94 }
95 
96 // return the end iterator
97 //
98 template <typename SystemTag, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T, typename IndexType>
101 {
102  const_stream_type stream( &m_storage.front() );
103  return stream.begin() + m_size;
104 }
105 
106 // push back a symbol
107 //
108 template <typename SystemTag, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T, typename IndexType>
110 {
111  if (m_storage.size() < util::divide_ri( m_size+1, SYMBOLS_PER_WORD ))
112  m_storage.resize( util::divide_ri( m_size+1, SYMBOLS_PER_WORD ) );
113 
114  begin()[ m_size++ ] = s;
115 }
116 
117 // return the base address of a symbol in the stream
118 // note that several symbols may share the same base address
119 template <typename SystemTag, uint32 SYMBOL_SIZE_T, bool BIG_ENDIAN_T, typename IndexType>
121 {
122  index_type off = i / SYMBOLS_PER_WORD;
123  return &m_storage[off];
124 }
125 
126 } // namespace nvbio