-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsoa_block.hpp
357 lines (356 loc) · 7.63 KB
/
soa_block.hpp
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#ifndef SOA_BLOCK_HPP_INCLUDED
#define SOA_BLOCK_HPP_INCLUDED
#include <utility>//std::index_sequence
#include <tuple>//std::make_tuple
#include <boost/align/aligned_alloc.hpp>//aligned_alloc, aligned_free
#include "index_type_buf.hpp"
#include "vec_offsets.hpp"
template
< typename Indices
, typename... Ts
>
struct
soa_impl
;
template
< std::size_t... Indices
, typename... Ts
>
struct
soa_impl
< std::index_sequence< Indices...>
, Ts...
>
: index_type_buf<Indices,typename get_type<Ts>::type>...
{
private:
std::size_t my_vec_size;
using offsets_t=decltype(vec_offsets<Ts...>(1));
offsets_t my_offsets;
void*my_storage;
template
< std::size_t Index
, typename T
>
static
index_type_buf<Index,T>*
get_self
( index_type_buf<Index,T>*self
)
{ return self;
}
template
< std::size_t Index
, typename T
>
static
index_type_buf<Index,T> const*
get_self
( index_type_buf<Index,T>const*self
)
{ return self;
}
template
< std::size_t Index
>
auto*
get_vec()
{ return get_self<Index>(this);
}
template
< std::size_t Index
>
auto const*
get_vec()const
{ return get_self<Index>(this);
}
char*
storage_char()
{ return static_cast<char*>(my_storage);
}
char const*
storage_char()const
{ return static_cast<char const*>(my_storage);
}
char*
storage_at(std::size_t index)
{ return storage_char()+my_offsets[index];
}
char const*
storage_at(std::size_t index)const
{ return storage_char()+my_offsets[index];
}
template
< std::size_t Index
>
void
construct_default
(
)
{
get_vec<Index>()->construct_default(storage_at(Index),my_vec_size);
}
template
< std::size_t Index
>
void
construct_copy
( soa_impl const& other_impl
)
{
get_vec<Index>()->construct_copy
( storage_at(Index)
, other_impl.storage_at(Index)
, my_vec_size
);
}
template
< std::size_t Index
>
void
destruct()
{
get_vec<Index>()->destruct(storage_at(Index),my_vec_size);
}
template
< std::size_t Index
>
static
auto
mk_index()
{
return std::integral_constant<std::size_t,Index>{};
}
template
< typename Fun
>
static
void
for_each_index(Fun& fun)
/**@brief
* Do fun(mk_index<Indices>())...
*/
{
using swallow = int[]; // guaranties left to right order
(void) //ignore result of following CTOR call.
swallow
{ 0 //provide something, in case sizeof...(Indices)==0.
, ( fun(mk_index<Indices>())
, 0 //ignore result of above fun call and use this value.
)...
};
}
void*
my_alloc()const throw(std::bad_alloc)
{
std::size_t const l_align=my_offsets.align_all();
std::size_t const l_size=my_offsets.size_all();
void*result=
boost::alignment::aligned_alloc
( l_align
, l_size
);
#if 0
std::cout<<__func__
<<":l_align="<<l_align
<<":l_size="<<l_size
<<":result="<<result
<<"\n";
#endif
if(!result) throw std::bad_alloc();
return result;
}
void
my_free()
{
boost::alignment::aligned_free(my_storage);
}
protected:
template
< std::size_t Index
>
void
print_index(std::ostream& sout)const
{
sout<<"soa_block<"<< Index <<">=\n";
get_vec<Index>()->print(sout,storage_at(Index),my_vec_size);
}
soa_impl
( std::size_t a_vec_size
)
: my_vec_size
( a_vec_size
)
, my_offsets
( vec_offsets<Ts...>
( a_vec_size
)
)
, my_storage
( my_alloc()
)
{
auto fun=[this](auto index)
{ //std::cout<<"index()="<<index()<<"\n";
this->template construct_default<index()>();
};
for_each_index(fun);
}
soa_impl(soa_impl const& a_other)
: my_vec_size(a_other.my_vec_size)
, my_offsets(a_other.my_offsets)
, my_storage
( my_alloc()
)
{
auto fun = [this,&a_other](auto index)
{ this->template construct_copy<index()>
( a_other
);
};
for_each_index(fun);
}
~soa_impl()
{
if(my_storage)
{
auto fun=[this](auto index)
{ //std::cout<<__func__<<":index()="<<index()<<"\n";
this->template destruct<index()>();
};
for_each_index(fun);
my_free();
}
}
void
swap(soa_impl& other)
{
std::swap(my_offsets , other.my_offsets);
std::swap(my_storage , other.my_storage);
std::swap(my_vec_size, other.my_vec_size);
}
public:
std::size_t
vec_size()const
{ return my_vec_size;
}
soa_impl&
operator=(soa_impl const& other)
{
//***TODO***
// This is probably not the fastest method ;(
soa_impl copy(other);
auto fun=[this](auto index)
{ //std::cout<<"index()="<<index()<<"\n";
this->template destruct<index()>();
};
for_each_index(fun);
swap(copy);
std::free(copy.my_storage);
copy.my_storage=0;
return *this;
}
void
resize(std::size_t a_vec_size)
{
//***TODO***
// This is probably not the fastest method ;(
soa_impl copy(a_vec_size);
this->operator=(copy);
}
template
< std::size_t Index
>
auto*
begin()
{
return get_vec<Index>()->ptr_at(storage_at(Index),0);
}
template
< std::size_t Index
>
auto*
end()
{
return get_vec<Index>()->ptr_at(storage_at(Index),my_vec_size);
}
template
< std::size_t Index
>
auto const*
begin()const
{
return get_vec<Index>()->ptr_at(storage_at(Index),0);
}
template
< std::size_t Index
>
auto const*
end()const
{
return get_vec<Index>()->ptr_at(storage_at(Index),my_vec_size);
}
auto
begin_all()
{
return std::make_tuple(this->template begin<Indices>()...);
}
auto
begin_all()const
{
return std::make_tuple(this->template begin<Indices>()...);
}
void
print
( std::ostream& sout
)const
{
auto fun=[this,&sout](auto index)
{ this->template print_index<index()>(sout);
};
for_each_index(fun);
}
};
template
< typename... Ts
>
struct
soa_block
/**@brief
* Implementation of the single block of storage for
* structure of arrays container.
*/
: public soa_impl
< std::index_sequence_for<Ts...>
, Ts...
>
{
public:
using
super_t=
soa_impl
< std::index_sequence_for<Ts...>
, Ts...
>;
using super_t::vec_size;
using super_t::resize;
soa_block
( std::size_t a_vec_size=1
)
: super_t
( a_vec_size
)
{
}
friend
std::ostream&
operator<<
( std::ostream& sout
, soa_block const& x
)
{
x.print(sout);
return sout;
}
};
#endif//SOA_BLOCK_HPP_INCLUDED