Debian Codegraph @

src/thirdparty/PoissonRecon/NestedVector.h

raw
1 /*
2 Copyright (c) 2024, Michael Kazhdan
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 NESTED_VECTOR_INCLUDED
31 #define NESTED_VECTOR_INCLUDED
32
33 #include <mutex>
34 #include "MyAtomic.h"
35 #include "MyMiscellany.h"
36
37 namespace PoissonRecon
38 {
39 // This represents a vector that can only grow in size.
40 // It has the property that once a reference to an element is returned, that reference remains valid until the vector is destroyed.
41 template< typename T , unsigned int Depth , unsigned int LogSize=20 >
42 struct NestedVector;
43
44 // The base case, an array with a maximum of 1<<LogSize
45 template< typename T , unsigned int LogSize >
46 struct NestedVector< T , 0 , LogSize >
47 {
48 static const unsigned int Depth = 0;
49 NestedVector( void ) : _size(0) { _data = new _DataType[ _Size ]; }
50
51 ~NestedVector( void ){ delete[] _data; }
52
53 NestedVector( const NestedVector &nv ) : NestedVector()
54 {
55 _size = nv._size.load();
56 for( size_t i=0 ; i<_size ; i++ ) operator[](i) = nv[i];
57 }
58
59 NestedVector &operator = ( const NestedVector & nv )
60 {
61 _size = nv._size.load();
62 for( size_t i=0 ; i<_size ; i++ ) operator[](i) = nv[i];
63 return *this;
64 }
65
66 NestedVector( NestedVector &&nv )
67 {
68 _data = nv._data;
69 _size = nv._size.load();
70 nv._size = 0;
71 nv._data = nullptr;
72 }
73
74 NestedVector& operator = ( NestedVector &&nv )
75 {
76 size_t foo = _size;
77 _size = nv._size.load();
78 nv._size = foo;
79 std::swap( _data , nv._data );
80 return *this;
81 }
82
83 // This function is guaranteed to return a lower-bound on the actual size
84 size_t size( void ) const { return _size; }
85
86 const T& operator[]( size_t idx ) const { return _data[idx]; }
87 T& operator[]( size_t idx ){ return _data[idx]; }
88
89 size_t resize( size_t sz ){ return resize( sz , T{} ); }
90
91 size_t resize( size_t sz , const T &defaultValue )
92 {
93 if( sz>_MaxSize ) MK_THROW( "Resize size exceeds max size, considering increasing nesting: " , sz , " > " , _MaxSize );
94
95 // Quick check to see if anything needs doing
96 if( sz<_size ) return size();
97
98 // Otherwise lock it down and get to work
99 std::lock_guard lock( _mutex );
100
101 // Check if the size got changed
102 if( sz>_size )
103 {
104 for( size_t i=_size ; i<sz ; i++ ) _data[i] = defaultValue;
105 _size = sz;
106 }
107 return size();
108 }
109
110 void write( BinaryStream &stream ) const
111 {
112 size_t sz = _size;
113 stream.write( sz );
114 stream.write( GetPointer( _data , _Size ) , sz );
115 }
116
117 void read( BinaryStream &stream )
118 {
119 size_t sz;
120 if( !stream.read( sz ) ) MK_THROW( "Failed to read _size" );
121 resize( sz );
122 if( !stream.read( GetPointer( _data , _Size ) , _size ) ) MK_THROW( "Failed to read _data" );
123 }
124
125 void write( BinaryStream &stream , const Serializer< T > &serializer ) const
126 {
127 const size_t serializedSize = serializer.size();
128
129 size_t sz = _size;
130 stream.write( sz );
131 if( _size )
132 {
133 char *buffer = new char[ _size * serializedSize ];
134 for( size_t i=0 ; i<_size ; i++ ) serializer.serialize( operator[]( i ) , buffer+i*serializedSize );
135 stream.write( buffer , serializedSize*_size );
136 delete[] buffer ;
137 }
138 }
139 void read( BinaryStream &stream , const Serializer< T > &serializer )
140 {
141 const size_t serializedSize = serializer.size();
142
143 size_t sz;
144 if( !stream.read( sz ) ) MK_THROW( "Failed to read _size" );
145 if( _size )
146 {
147 resize( sz );
148 char *buffer = new char[ _size * serializedSize ];
149 if( !stream.read( buffer , serializedSize*_size ) ) MK_THROW( "Failed tor read in data" );
150 for( size_t i=0 ; i<_size ; i++ ) serializer.deserialize( buffer+i*serializedSize , operator[]( i ) );
151 delete[] buffer;
152 }
153 }
154
155 protected:
156 template< typename _T , unsigned int _Depth , unsigned int _LogSize > friend struct NestedVector;
157
158 using _DataType = T;
159 static const size_t _MaxSize = ((size_t)1)<<LogSize;
160 static const size_t _Size = ((size_t)1)<<LogSize;
161 static const size_t _Mask = _MaxSize-1;
162
163 std::mutex _mutex;
164 std::atomic< size_t > _size;
165 _DataType *_data;
166 };
167
168 // The derived case, an array a maximum of 1<<LogSize elements
169 template< typename T , unsigned int Depth , unsigned int LogSize >
170 struct NestedVector
171 {
172 NestedVector( void ) : _size(0)
173 {
174 _data = new _DataType*[ _Size ];
175 for( size_t i=0 ; i<_Size ; i++ ) _data[i] = nullptr;
176 }
177
178 ~NestedVector( void )
179 {
180 for( size_t i=0 ; i<_size ; i++ ) delete _data[i];
181 delete[] _data;
182 }
183
184 NestedVector( const NestedVector &nv ) : NestedVector()
185 {
186 _size = nv._size.load();
187 for( size_t i=0 ; i<_size ; i++ ) _data[i] = new _DataType( *nv._data[i] );
188 }
189
190 NestedVector &operator = ( const NestedVector &nv )
191 {
192 for( size_t i=0 ; i<_size ; i++ ){ delete _data[i] ; _data[i] = nullptr; }
193 _size = nv._size.load();
194 for( size_t i=0 ; i<_size ; i++ ) _data[i] = new _DataType( *nv._data[i] );
195 return *this;
196 }
197
198 NestedVector( NestedVector &&nv )
199 {
200 _size = nv._size.load();
201 _data = nv._data;
202 nv._size = 0;
203 nv._data = nullptr;
204 }
205
206 NestedVector& operator = ( NestedVector &&nv )
207 {
208 size_t foo = _size;
209 _size = nv._size.load();
210 nv._size = foo;
211 std::swap( _data , nv._data );
212 return *this;
213 }
214
215 // This function is guaranteed to return a lower-bound on the actual size
216 size_t size( void ) const
217 {
218 if( !_size ) return 0;
219 else return ( (_size-1)<<(Depth*LogSize) ) + _data[_size-1]->size();
220 }
221
222 const T& operator[]( size_t idx ) const { return ( *_data[ idx>>(LogSize*Depth) ] )[ idx & NestedVector< T , Depth-1 , LogSize >::_Mask ] ; }
223 T& operator[]( size_t idx ){ return ( *( _data[ idx>>(LogSize*Depth) ] ) )[ idx & NestedVector< T , Depth-1 , LogSize >::_Mask ] ; }
224
225 size_t resize( size_t sz ){ return resize( sz , T{} ); }
226
227 size_t resize( size_t sz , const T &defaultValue )
228 {
229 if( sz>_MaxSize ) MK_THROW( "Resize size exceeds max size, considering increasing nesting: " , sz , " > " , _MaxSize );
230
231 size_t _sz = (sz+NestedVector< T , Depth-1 , LogSize >::_Mask)>>(LogSize*Depth);
232
233 // Quick check to see if anything needs doing
234 if( !_sz || _sz<_size ) return size();
235
236 // Otherwise lock it down and get to work
237 std::lock_guard lock( _mutex );
238
239 // Check if the size got changed
240 if( _sz>_size )
241 {
242 // Complete the initialization for the last existing
243 if( _size )
244 {
245 size_t i = _size-1;
246 size_t __sz = _sz==(i+1) ? ( sz - NestedVector< T , Depth-1 , LogSize >::_MaxSize * i ) : ( NestedVector< T , Depth-1 , LogSize >::_MaxSize );
247 _data[i]->resize( __sz , defaultValue );
248 }
249 for( size_t i=_size ; i<_sz ; i++ )
250 {
251 _data[i] = new _DataType();
252 size_t __sz = _sz==(i+1) ? ( sz - NestedVector< T , Depth-1 , LogSize >::_MaxSize * i ) : ( NestedVector< T , Depth-1 , LogSize >::_MaxSize );
253 _data[i]->resize( __sz , defaultValue );
254 }
255 _size = _sz;
256 }
257 else if( _sz==_size )
258 {
259 size_t i = _sz-1;
260 size_t __sz = _sz==(i+1) ? ( sz - NestedVector< T , Depth-1 , LogSize >::_MaxSize * i ) : ( NestedVector< T , Depth-1 , LogSize >::_MaxSize );
261 _data[i]->resize( __sz , defaultValue );
262 }
263 return size();
264 }
265
266 void write( BinaryStream &stream ) const
267 {
268 stream.write( size() );
269 for( size_t i=0 ; i<_size ; i++ ) _data[i]->write( stream );
270 }
271
272 void read( BinaryStream &stream )
273 {
274 size_t sz;
275 if( !stream.read( sz ) ) MK_THROW( "Failed to read _size" );
276 resize( sz );
277 for( size_t i=0 ; i<_size ; i++ ) _data[i]->read(stream);
278 }
279
280 void write( BinaryStream &stream , const Serializer< T > &serializer ) const
281 {
282 const size_t serializedSize = serializer.size();
283
284 stream.write( size() );
285 for( size_t i=0 ; i<_size ; i++ ) _data[i]->write( stream , serializer );
286 }
287 void read( BinaryStream &stream , const Serializer< T > &serializer )
288 {
289 const size_t serializedSize = serializer.size();
290
291 size_t sz;
292 if( !stream.read( sz ) ) MK_THROW( "Failed to read _size" );
293 resize( sz );
294 for( size_t i=0 ; i<_size ; i++ ) _data[i]->read( stream , serializer );
295 }
296
297 protected:
298 template< typename _T , unsigned int _Depth , unsigned int _LogSize > friend struct NestedVector;
299
300 using _DataType = NestedVector< T , Depth-1 , LogSize >;
301 static const size_t _MaxSize = ((size_t)1)<<(LogSize*(Depth+1));
302 static const size_t _Size = ((size_t)1)<<LogSize;
303 static const size_t _Mask = _MaxSize-1;
304
305 std::mutex _mutex;
306 std::atomic< size_t > _size;
307 _DataType **_data;
308 };
309 }
310 #endif // NESTED_VECTOR_INCLUDED