forked from crosetto/SoAvsAoS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
210 lines (161 loc) · 6.29 KB
/
main.cpp
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
#include <tuple>
#include <vector>
#include <functional>
#include <string>
#include <iostream>
template <typename T>
struct ref_wrap : public std::reference_wrapper<T>{
operator T&() const noexcept { return this->get(); }
ref_wrap(T& other_) : std::reference_wrapper<T>(other_){}
void operator =(T && other_) {this->get()=other_;}
};
enum class DataLayout { SoA, //structure of arrays
AoS //array of structures
};
template <typename TContainer>
class Iterator;
template <template <typename...> class Container, DataLayout TDataLayout, typename TItem>
struct DataLayoutPolicy;
template <template <typename...> class Container, template<typename...> class TItem, typename... Types>
struct DataLayoutPolicy<Container, DataLayout::AoS, TItem<Types...>> {
using type = Container<TItem<Types...>>;
using value_type = TItem<Types...>&;
constexpr static value_type get( type& c_, std::size_t position_ ){ return value_type(*static_cast<TItem<Types...>*>(&c_[ position_ ])); }
constexpr static void resize( type& c_, std::size_t size_ ) { c_.resize( size_ ); }
template <typename TValue>
constexpr static void push_back( type& c_, TValue&& val_ ){ c_.push_back( val_ ); }
static constexpr std::size_t size(type& c_){ return c_.size(); }
};
template <template <typename...> class Container, template<typename...> class TItem, typename... Types>
struct DataLayoutPolicy<Container, DataLayout::SoA, TItem<Types...>> {
using type = std::tuple<Container<Types>...>;
using value_type = TItem<ref_wrap<Types>...>;
constexpr static value_type get( type& c_, std::size_t position_ )
{
return doGet( c_, position_, std::make_integer_sequence<unsigned, sizeof...( Types )>() ); // unrolling parameter pack
}
constexpr static void resize( type& c_, std::size_t size_ ) {
doResize( c_, size_, std::make_integer_sequence<unsigned, sizeof...( Types )>() ); // unrolling parameter pack
}
template <typename TValue>
constexpr static void push_back( type& c_, TValue&& val_ ){
doPushBack( c_, std::forward<TValue>(val_), std::make_integer_sequence<unsigned, sizeof...( Types )>() ); // unrolling parameter pack
}
static constexpr std::size_t size(type& c_){ return std::get<0>( c_ ).size(); }
private:
template <unsigned... Ids>
constexpr static auto doGet( type& c_, std::size_t position_, std::integer_sequence<unsigned, Ids...> )
{
return value_type{ ref_wrap( std::get<Ids>( c_ )[ position_ ] )... }; // guaranteed copy elision
}
template <unsigned... Ids>
constexpr static void doResize( type& c_, unsigned size_, std::integer_sequence<unsigned, Ids...> )
{
( std::get<Ids>( c_ ).resize( size_ ), ... ); //fold expressions
}
template <typename TValue, unsigned... Ids>
constexpr static void doPushBack( type& c_, TValue&& val_, std::integer_sequence<unsigned, Ids...> )
{
( std::get<Ids>( c_ ).push_back( std::get<Ids>( std::forward<TValue>( val_ ) ) ), ... ); // fold expressions
}
};
template <template <typename T> class TContainer, DataLayout TDataLayout, typename TItem>
using policy_t = DataLayoutPolicy<TContainer, TDataLayout, TItem>;
template <template <typename ValueType> class TContainer, DataLayout TDataLayout, typename TItem>
struct BaseContainer
{
using policy_t = policy_t<TContainer, TDataLayout, TItem>;
using iterator = Iterator<BaseContainer<TContainer, TDataLayout, TItem>>;
using difference_type = std::ptrdiff_t;
using value_type = typename policy_t::value_type;
using reference = typename policy_t::value_type&;
using size_type = std::size_t;
auto static constexpr data_layout = TDataLayout;
BaseContainer( size_t size_ ){
resize(size_);
}
template <typename Fwd>
void push_back( Fwd&& val )
{
policy_t::push_back( mValues, std::forward<Fwd>(val) );
}
std::size_t size(){
return policy_t::size( mValues );
}
value_type operator[]( std::size_t position_ )
{
return policy_t::get( mValues, position_ );
}
void resize( size_t size_ ) {
policy_t::resize( mValues, size_ );
}
iterator begin() { return iterator( this, 0 ); }
iterator end() { return iterator( this, size() ); }
private:
typename policy_t::type mValues;
};
template <typename TContainer>
class Iterator
{
private:
using container_t = TContainer;
public:
using policy_t = typename container_t::policy_t;
using difference_type = std::ptrdiff_t;
using value_type = typename policy_t::value_type;
using reference = value_type&;
//using pointer = value_type*;
using iterator_category = std::bidirectional_iterator_tag;
template<typename TTContainer>
Iterator( TTContainer* container_, std::size_t position_ = 0 ):
mContainer( container_ )
, mIterPosition( position_ )
{
}
Iterator& operator=( Iterator const& other_ ){
mIterPosition = other_.mIterPosition;
// mContainer = other_.mContainer;
}
friend bool operator!=( Iterator const& lhs, Iterator const& rhs ){ return lhs.mIterPosition != rhs.mIterPosition;}
friend bool operator==( Iterator const& lhs, Iterator const& rhs ){ return !operator!=(lhs, rhs);}
operator bool() const {mIterPosition != std::numeric_limits<std::size_t>::infinity(); }
Iterator& operator=( std::nullptr_t const& ){ mIterPosition = std::numeric_limits<std::size_t>::infinity();}
template <typename T>
void operator+=( T size_ ){ mIterPosition += size_; }
template <typename T>
void operator-=( T size_ ){ mIterPosition -= size_; }
void operator++(){ return operator +=(1); }
void operator--(){ return operator -=(1); }
value_type operator*() {
return (*mContainer)[ mIterPosition ];
}
private:
container_t* mContainer = nullptr;
std::size_t mIterPosition = std::numeric_limits<std::size_t>::infinity();
};
template<typename T>
using MyVector = std::vector<T, std::allocator<T>>;
enum Component : int {
eMyDouble,
eMyChar,
eMyString,
eMyPadding
};
struct Pad{
char mPad[1500];
};
template<typename ... T>
struct Item : public std::tuple<T ...>{
using std::tuple<T...>::tuple;
auto & myDouble(){return std::get<eMyDouble>(*this);}
auto & myChar() {return std::get<eMyChar>(*this);}
auto & myString(){return std::get<eMyString>(*this);}
};
int main(int argc, char** argv){
using container_t = BaseContainer<MyVector, DataLayout::SoA, Item<double, char, std::string, Pad> >;
container_t container_(1000);
std::cout<<"container size " << container_.size()<< " \n";
for(auto&& i : container_){
i.myDouble()=static_cast<double>(argc);
}
}