-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathMultiArrayView.h
More file actions
338 lines (302 loc) · 11.7 KB
/
MultiArrayView.h
File metadata and controls
338 lines (302 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* Copyright (c) 2017 HERE Europe B.V.
* See the LICENSE file in the root of this project for license details.
*/
#pragma once
#include "ExternalVector.h"
#include "internal/MultiArrayViewImpl.h"
#include <cstdint>
#include <functional>
#include <sstream>
#include <string>
#include <tuple>
namespace flatdata
{
/*
* Allows reading data stored with the MultiVector:
* Each item i has a list of objects attached to it
* To retrieve such a list use for_each with an appropriate functor:
*
* MultiArrayView< A, B, C > view = ...;
* struct ABReader
* {
* void operator( )( A ) const { ... }
* void operator( )( B ) const { ... }
* }
*
* view.for_each< A, B >( 10 ).call( ABReader( ... ) ); // explicit with functor
* view.for_each< C >( 10 ).call( [&]( C c ){ ... } ); // explicit with lambda
* view.for_each( 10 ).call( make_overload( // implicit with overloaded lambda
* [&]( A ) { ... },
* [&]( B ) { ... }
* ) );
*/
template < typename IndexType, typename... Args >
class MultiArrayView
{
private:
template < typename... AcceptedArgs >
using AcceptedArgsList = typename std::conditional< ( sizeof...( AcceptedArgs ) > 0 ),
static_list::list< AcceptedArgs... >,
static_list::list< Args... > >::type;
public:
using StreamType = const unsigned char*;
MultiArrayView( );
MultiArrayView( ArrayView< IndexType > index, StreamType data_begin );
/**
* Iterate through objects of specified types of the item specified by `index`.
*
* Usage example:
* MultiArrayView<A, B, C> view = ...;
* view.for_each<A, B>( index, callback );
* // callback must be callable with types A and B
*
* If the callback should be called with every type in the container, there is a shortcut
* method (note that template arguments are not specified):
* view.for_each( index, callback );
* // callback must be callable with all types of the container, i.e. A, B and C
*
* It is also possible to overload a function partially and not to specify all accepted types
* explicitly. For that use flatdata::make_overload. It stores the information, which types are
* accepted by the callback in the callback's type:
* view.for_each( index, make_overload(
* [](A) { ... },
* [](B) { ... },
* );
* // callback will be called for objects of types A and B
*
* @tparam AcceptedArgs... Types of objects, for which the callback should be called
* @param index Index of the item
* @return Proxy object that calls a given function. In particular, the given function must be
* callable for each type in AcceptedArgs.
*/
template < typename... AcceptedArgs,
typename F,
typename = enable_if_t< !has_args_list< F >::value >,
typename
= enable_if_t< internal::ExplicitForEachAssert< F,
AcceptedArgsList< AcceptedArgs... >,
Args... >::value > >
void
for_each( uint64_t index, F&& callback ) const
{
for_each_impl( index, std::forward< F >( callback ) );
}
/** Show static assert of failed explicit check */
template < typename... AcceptedArgs,
typename F,
typename = enable_if_t< !has_args_list< F >::value >,
typename
= enable_if_t< !internal::ExplicitForEachAssert< F,
AcceptedArgsList< AcceptedArgs... >,
Args... >::value > >
std::false_type
for_each( uint64_t /*unused*/, F&& /*unused*/ ) const
{
internal::ExplicitForEachAssert< F, AcceptedArgsList< AcceptedArgs... >,
Args... >::do_assert( );
return { };
}
/**
* Implicit version of `for_each` to use with lambda's constructed by flatdata::make_overload.
*/
template < typename F,
typename = enable_if_t< has_args_list< F >::value >,
typename = enable_if_t< internal::ImplicitForEachAssert< F, Args... >::value > >
void
for_each( uint64_t index, F&& callback ) const
{
for_each_impl( index, std::forward< F >( callback ) );
}
/** Show static assert of failed implicit check */
template < typename F,
typename = enable_if_t< has_args_list< F >::value >,
typename = enable_if_t< !internal::ImplicitForEachAssert< F, Args... >::value > >
std::false_type
for_each( uint64_t /*unused*/, F&& /*unused*/ ) const
{
internal::ImplicitForEachAssert< F, Args... >::do_assert( );
return { };
}
size_t size( ) const;
std::string describe( size_t unused = 0 ) const;
explicit operator bool( ) const;
template < typename ElementType >
class Iterator
{
public:
Iterator( ) = default;
Iterator( StreamType data_current, StreamType data_end );
bool valid( ) const;
ElementType operator*( ) const; // get current value
void operator++( ); // go to the next element
void operator++( int ); // go to the next element
private:
StreamType m_data_current = nullptr;
StreamType m_data_end = nullptr;
bool m_valid = false;
};
template < typename ElementType >
Iterator< ElementType > iterator( uint64_t index ) const;
private:
template < typename F >
void for_each_impl( uint64_t index, F&& callback ) const;
template < typename Functor, size_t index >
static size_t get_impl( const unsigned char* /*unused*/,
size_t /*unused*/,
Functor&& /*unused*/ );
template < typename Functor, size_t index, typename Arg, typename... PackedArgs >
static size_t get_impl( const unsigned char* data, size_t type, Functor&& callback );
private:
ArrayView< IndexType > m_index;
StreamType m_data_begin;
};
// -------------------------------------------------------------------------------------------------
template < typename IndexType, typename... Args >
MultiArrayView< IndexType, Args... >::MultiArrayView( )
: m_data_begin( nullptr )
{
}
template < typename IndexType, typename... Args >
MultiArrayView< IndexType, Args... >::MultiArrayView( ArrayView< IndexType > index,
StreamType data_begin )
: m_index( index )
, m_data_begin( data_begin )
{
}
template < typename IndexType, typename... Args >
size_t
MultiArrayView< IndexType, Args... >::size( ) const
{
return m_index.size( );
}
template < typename IndexType, typename... Args >
std::string
MultiArrayView< IndexType, Args... >::describe( size_t /*unused*/ ) const
{
std::ostringstream ss;
if ( this->operator bool( ) )
{
ss << "MultiArray of size " << size( ) << ", with index: " << m_index.describe( );
}
else
{
ss << "Uninitialized MultiArray";
}
return ss.str( );
}
template < typename IndexType, typename... Args >
MultiArrayView< IndexType, Args... >::operator bool( ) const
{
return static_cast< bool >( m_index ) && m_data_begin != nullptr;
}
template < typename IndexType, typename... Args >
template < typename F >
void
MultiArrayView< IndexType, Args... >::for_each_impl( uint64_t index, F&& callback ) const
{
std::pair< size_t, size_t > range = m_index[ index ].range;
for ( auto data = m_data_begin + range.first, end = m_data_begin + range.second; data < end; )
{
unsigned char type = *data;
data++;
data += get_impl< F, 0, typename Args::AccessorType... >( data, type,
std::forward< F >( callback ) );
}
}
template < typename IndexType, typename... Args >
template < typename Functor, size_t index >
size_t
MultiArrayView< IndexType, Args... >::get_impl( const unsigned char* /*unused*/,
size_t /*unused*/,
Functor&& /*unused*/ )
{
throw std::runtime_error( "Corrupted MultiArrayView data, unexpected type" );
}
template < typename IndexType, typename... Args >
template < typename Functor, size_t index, typename Arg, typename... PackedArgs >
size_t
MultiArrayView< IndexType, Args... >::get_impl( const unsigned char* data,
size_t type,
Functor&& callback )
{
if ( type == index )
{
apply_if_accepts_argument( callback, Arg( data ) );
return Arg::size_in_bytes( );
}
return get_impl< Functor&&, index + 1, PackedArgs... >( data, type,
std::forward< Functor >( callback ) );
}
// Needed to resolve typename issue correctly on different compilers
// See e.g.
// http://stackoverflow.com/questions/6232294/which-compiler-is-right-template-before-templated-return-type-needed
// and http://stackoverflow.com/questions/8208203/nested-templates-workaround-for-msvc2010
// Unfortunately both of Boost macros are not working as expected, so have to define our own
#if defined( _MSC_VER )
#define TEMPLATE_WORKAROUND
#else
#define TEMPLATE_WORKAROUND template
#endif
template < typename IndexType, typename... Args >
template < typename ElementType >
typename MultiArrayView< IndexType, Args... >::TEMPLATE_WORKAROUND Iterator< ElementType >
MultiArrayView< IndexType, Args... >::iterator( uint64_t index ) const
{
std::pair< size_t, size_t > range = m_index[ index ].range;
return MultiArrayView< IndexType, Args... >::Iterator< ElementType >(
m_data_begin + range.first, m_data_begin + range.second );
}
// -------------------------------------------------------------------------------------------------
template < typename IndexType, typename... Args >
template < typename ElementType >
MultiArrayView< IndexType, Args... >::Iterator< ElementType >::Iterator( StreamType data_current,
StreamType data_end )
: m_data_current( data_current )
, m_data_end( data_end )
{
if ( m_data_current < m_data_end )
{
++( *this );
}
}
template < typename IndexType, typename... Args >
template < typename ElementType >
void
MultiArrayView< IndexType, Args... >::Iterator< ElementType >::operator++( )
{
bool found = false;
auto callback = [ & ]( ElementType ) { found = true; };
while ( !found && m_data_current != m_data_end )
{
unsigned char type = *m_data_current;
m_data_current++;
m_data_current
+= MultiArrayView< IndexType, Args... >::get_impl< decltype( callback )&, 0,
typename Args::AccessorType... >(
m_data_current, type, callback );
}
m_valid = found;
}
template < typename IndexType, typename... Args >
template < typename ElementType >
void
MultiArrayView< IndexType, Args... >::Iterator< ElementType >::operator++( int )
{
operator++( );
}
template < typename IndexType, typename... Args >
template < typename ElementType >
ElementType
MultiArrayView< IndexType, Args... >::Iterator< ElementType >::operator*( ) const
{
return ElementType{ m_data_current - ElementType::size_in_bytes( ) };
}
template < typename IndexType, typename... Args >
template < typename ElementType >
bool
MultiArrayView< IndexType, Args... >::Iterator< ElementType >::valid( ) const
{
return m_valid;
}
} // namespace flatdata