Debian Codegraph @

src/thirdparty/PoissonRecon/BlockedVector.h

raw
1 /*
2 Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28
29
30 #ifndef BLOCKED_VECTOR_INCLUDED
31 #define BLOCKED_VECTOR_INCLUDED
32
33 #include "MyMiscellany.h"
34
35 namespace PoissonRecon
36 {
37 // This represents a vector that can only grow in size.
38 // It has the property that once a reference to an element is returned, that reference remains valid until the vector is destroyed.
39 template< typename T , unsigned int LogBlockSize=10 , unsigned int InitialBlocks=10 , unsigned int AllocationMultiplier=2 >
40 struct BlockedVector
41 {
42 BlockedVector( size_t sz=0 , T defaultValue=T() ) : _defaultValue( defaultValue )
43 {
44 _reservedBlocks = InitialBlocks;
45 _blocks = NewPointer< Pointer( T ) >( _reservedBlocks );
46 for( size_t i=0 ; i<_reservedBlocks ; i++ ) _blocks[i] = NullPointer( T );
47 _allocatedBlocks = _size = 0;
48 if( sz ) resize( sz );
49 }
50
51 ~BlockedVector( void )
52 {
53 for( size_t i=0 ; i<_allocatedBlocks ; i++ ) DeletePointer( _blocks[i] );
54 DeletePointer( _blocks );
55 }
56
57 BlockedVector( const BlockedVector& v )
58 {
59 #ifdef SANITIZED_PR
60 _reservedBlocks = v._reservedBlocks , _allocatedBlocks = v._allocatedBlocks , _size = v._size.load() , _defaultValue = v._defaultValue;
61 #else // !SANITIZED_PR
62 _reservedBlocks = v._reservedBlocks , _allocatedBlocks = v._allocatedBlocks , _size = v._size , _defaultValue = v._defaultValue;
63 #endif // SANITIZED_PR
64 _blocks = NewPointer< Pointer( T ) >( _reservedBlocks );
65 for( size_t i=0 ; i<_allocatedBlocks ; i++ )
66 {
67 _blocks[i] = NewPointer< T >( _BlockSize );
68 memcpy( _blocks[i] , v._blocks[i] , sizeof(T)*_BlockSize );
69 }
70 for( size_t i=_allocatedBlocks ; i<_reservedBlocks ; i++ ) _blocks[i] = NullPointer( Pointer ( T ) );
71 }
72
73 BlockedVector& operator = ( const BlockedVector& v )
74 {
75 for( size_t i=0 ; i<_allocatedBlocks ; i++ ) DeletePointer( _blocks[i] );
76 DeletePointer( _blocks );
77 #ifdef SANITIZED_PR
78 _reservedBlocks = v._reservedBlocks , _blocks = v._blocks.load() , _allocatedBlocks = v._allocatedBlocks , _size = v._size.load() , _defaultValue = v._defaultValue;
79 #else // !SANITIZED_PR
80 _reservedBlocks = v._reservedBlocks , _blocks = v._blocks , _allocatedBlocks = v._allocatedBlocks , _size = v._size , _defaultValue = v._defaultValue;
81 #endif // SANITIZED_PR
82 _blocks = NewPointer< Pointer( T ) >( _reservedBlocks );
83 for( size_t i=0 ; i<_allocatedBlocks ; i++ )
84 {
85 _blocks[i] = NewPointer< T >( _BlockSize );
86 memcpy( _blocks[i] , v._blocks[i] , sizeof(T)*_BlockSize );
87 }
88 for( size_t i=_allocatedBlocks ; i<_reservedBlocks ; i++ ) _blocks[i] = NullPointer( T );
89 return *this;
90 }
91
92 BlockedVector( BlockedVector&& v )
93 {
94 #ifdef SANITIZED_PR
95 _reservedBlocks = v._reservedBlocks , _allocatedBlocks = v._allocatedBlocks , _size = v._size.load() , _defaultValue = v._defaultValue , _blocks = v._blocks.load();
96 #else // !SANITIZED_PR
97 _reservedBlocks = v._reservedBlocks , _allocatedBlocks = v._allocatedBlocks , _size = v._size , _defaultValue = v._defaultValue , _blocks = v._blocks;
98 #endif // SANITIZED_PR
99 v._reservedBlocks = v._allocatedBlocks = v._size = 0 , v._blocks = NullPointer( Pointer( T ) );
100 }
101
102 BlockedVector& operator = ( BlockedVector&& v )
103 {
104 for( size_t i=0 ; i<_allocatedBlocks ; i++ ) DeletePointer( _blocks[i] );
105 DeletePointer( _blocks );
106 #ifdef SANITIZED_PR
107 _reservedBlocks = v._reservedBlocks , _allocatedBlocks = v._allocatedBlocks , _size = v._size.load() , _defaultValue = v._defaultValue , _blocks = v._blocks.load();
108 #else // !SANITIZED_PR
109 _reservedBlocks = v._reservedBlocks , _allocatedBlocks = v._allocatedBlocks , _size = v._size , _defaultValue = v._defaultValue , _blocks = v._blocks;
110 #endif // SANITIZED_PR
111 v._reservedBlocks = v._allocatedBlocks = v._size = 0 , v._blocks = NullPointer( Pointer( T ) );
112 return *this;
113 }
114
115 size_t size( void ) const { return _size; }
116
117 const T& operator[]( size_t idx ) const { return _blocks[idx>>LogBlockSize][idx&_Mask]; }
118 T& operator[]( size_t idx ){ return _blocks[idx>>LogBlockSize][idx&_Mask]; }
119
120 void resize( size_t size ){ resize( size , _defaultValue ); }
121 void resize( size_t size , const T& defaultValue )
122 {
123 reserve( size , defaultValue );
124 _size = size;
125 }
126
127 void clear( void ){ _size = 0; }
128
129 size_t reserved( void ) const { return _allocatedBlocks * _BlockSize; }
130
131 void reserve( size_t size ){ reserve( size , _defaultValue ); }
132 void reserve( size_t size , const T& defaultValue )
133 {
134 if( size<=_allocatedBlocks * _BlockSize ) return;
135 size_t index = size-1;
136 size_t block = index >> LogBlockSize;
137 size_t blockIndex = index & _Mask;
138
139 // If there are insufficiently many blocks
140 if( block>=_reservedBlocks )
141 {
142 size_t newReservedSize = std::max< size_t >( _reservedBlocks * AllocationMultiplier , block+1 );
143 Pointer( Pointer( T ) ) __blocks = NewPointer< Pointer( T ) >( newReservedSize );
144 memcpy( __blocks , _blocks , sizeof( Pointer( T ) ) * _reservedBlocks );
145 for( size_t i=_reservedBlocks ; i<newReservedSize ; i++ ) __blocks[i] = NullPointer( T );
146 Pointer( Pointer( T ) ) _oldBlocks = _blocks;
147 _blocks = __blocks;
148 _reservedBlocks = newReservedSize;
149 DeletePointer( _oldBlocks );
150 }
151
152 // If the block hasn't been allocated
153 if( block>=_allocatedBlocks )
154 {
155 for( size_t b=_allocatedBlocks ; b<=block ; b++ )
156 {
157 _blocks[b] = NewPointer< T >( _BlockSize );
158 for( size_t i=0 ; i<_BlockSize ; i++ ) _blocks[b][i] = defaultValue;
159 }
160 _allocatedBlocks = block+1;
161 }
162 }
163 void push_back( const T &value )
164 {
165 resize( _size+1 );
166 operator[]( _size-1 ) = value;
167 }
168 T &back( void ){ return operator[]( _size-1 ); }
169 const T &back( void ) const { return operator[]( _size-1 ); }
170 void pop_back( void ){ _size--; }
171
172 void write( BinaryStream &stream ) const
173 {
174 stream.write( _size );
175 stream.write( _defaultValue );
176 stream.write( _reservedBlocks );
177 stream.write( _allocatedBlocks );
178 for( size_t i=0 ; i<_allocatedBlocks ; i++ ) stream.write( _blocks[i] , _BlockSize );
179 }
180
181 void read( BinaryStream &stream )
182 {
183 for( size_t i=0 ; i<_allocatedBlocks ; i++ ) DeletePointer( _blocks[i] );
184 DeletePointer( _blocks );
185 if( !stream.read( _size ) ) MK_THROW( "Failed to read _size" );
186 if( !stream.read( _defaultValue ) ) MK_THROW( "Failed to read _defaultValue" );
187 if( !stream.read( _reservedBlocks ) ) MK_THROW( "Failed to read _reservedBlocks" );
188 if( !stream.read( _allocatedBlocks ) ) MK_THROW( "Failed to read _allocatedBlocks" );
189
190 _blocks = NewPointer< Pointer( T ) >( _reservedBlocks );
191 if( !_blocks ) MK_THROW( "Failed to allocate _blocks: " , _reservedBlocks );
192
193 for( size_t i=0 ; i<_allocatedBlocks ; i++ )
194 {
195 _blocks[i] = NewPointer< T >( _BlockSize );
196 if( !_blocks[i] ) MK_THROW( "Failed to allocate _blocks[" , i , "]" );
197 if( !stream.read( _blocks[i] , _BlockSize ) ) MK_THROW( "Failed to read _blocks[" , i , "]" );
198 }
199 for( size_t i=_allocatedBlocks ; i<_reservedBlocks ; i++ ) _blocks[i] = NullPointer( T );
200 }
201
202 void read( BinaryStream &stream , const Serializer< T > &serializer )
203 {
204 const size_t serializedSize = serializer.size();
205
206 for( size_t i=0 ; i<_allocatedBlocks ; i++ ) DeletePointer( _blocks[i] );
207 DeletePointer( _blocks );
208 _size = _allocatedBlocks = _reservedBlocks = 0;
209
210 if( !stream.read( _size ) ) MK_THROW( "Failed to read _size" );
211 #ifdef SHOW_WARNINGS
212 #pragma message( "[WARNING] Should deserialize default value" )
213 #endif // SHOW_WARNINGS
214 if( !stream.read( _defaultValue ) ) MK_THROW( "Failed to read _defaultValue" );
215 if( !stream.read( _reservedBlocks ) ) MK_THROW( "Failed to read _reservedBlocks" );
216 if( !stream.read( _allocatedBlocks ) ) MK_THROW( "Failed to read _allocatedBlocks" );
217 _blocks = NewPointer< Pointer( T ) >( _reservedBlocks );
218 if( !_blocks ) MK_THROW( "Failed to allocate _blocks: " , _reservedBlocks );
219 for( size_t i=0 ; i<_allocatedBlocks ; i++ )
220 {
221 _blocks[i] = NewPointer< T >( _BlockSize );
222 if( !_blocks[i] ) MK_THROW( "Failed to allocate _blocks[" , i , "]" );
223 }
224 if( _size )
225 {
226 Pointer( char ) buffer = NewPointer< char >( _size * serializedSize );
227 if( !stream.read( buffer , serializedSize*_size ) ) MK_THROW( "Failed tor read in data" );
228 for( unsigned int i=0 ; i<_size ; i++ ) serializer.deserialize( buffer+i*serializedSize , operator[]( i ) );
229 DeletePointer( buffer );
230 }
231 }
232
233 void write( BinaryStream &stream , const Serializer< T > &serializer ) const
234 {
235 const size_t serializedSize = serializer.size();
236
237 stream.write( _size );
238 #ifdef SHOW_WARNINGS
239 #pragma message( "[WARNING] Should serialize default value" )
240 #endif // SHOW_WARNINGS
241 stream.write( _defaultValue );
242 stream.write( _reservedBlocks );
243 stream.write( _allocatedBlocks );
244 if( _size )
245 {
246 Pointer( char ) buffer = NewPointer< char >( _size * serializedSize );
247 for( unsigned int i=0 ; i<_size ; i++ ) serializer.serialize( operator[]( i ) , buffer+i*serializedSize );
248 stream.write( buffer , serializedSize*_size );
249 DeletePointer( buffer );
250 }
251 }
252
253
254 protected:
255 static const size_t _BlockSize = 1<<LogBlockSize;
256 static const size_t _Mask = (1<<LogBlockSize)-1;
257
258 T _defaultValue;
259 size_t _allocatedBlocks , _reservedBlocks;
260 #ifdef SANITIZED_PR
261 std::atomic< size_t > _size;
262 std::atomic< Pointer( Pointer( T ) ) > _blocks;
263 #else // !SANITIZED_PR
264 size_t _size;
265 Pointer( Pointer( T ) ) _blocks;
266 #endif // SANITIZED_PR
267 };
268 }
269 #endif // BLOCKED_VECTOR_INCLUDED