leaf_access_tree.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2016 by Horatiu George Todoran <todorangrg@gmail.com> *
4  * *
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * 2. Redistributions in binary form must reproduce the above copyright *
12  * notice, this list of conditions and the following disclaimer in *
13  * the documentation and/or other materials provided with the *
14  * distribution. *
15  * 3. Neither the name of the copyright holder nor the names of its *
16  * contributors may be used to endorse or promote products derived *
17  * from this software without specific prior written permission. *
18  * *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY *
29  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
30  * POSSIBILITY OF SUCH DAMAGE. *
31  ***************************************************************************/
32 
33 #ifndef TUW_LEAF_ACCESS_TREE_HPP
34 #define TUW_LEAF_ACCESS_TREE_HPP
35 
36 #include <tuw_control/utils.h>
37 
38 namespace tuw
39 {
40 template <typename LeafType, typename FixContainer, int ContSize = 0>
42 
44 template <typename SubType>
46 {
47  std::vector<SubType> sub_;
48 };
49 template <typename SubType, size_t N>
51 {
52  std::array<SubType, N> sub_;
53 };
54 template <typename... SubType>
56 {
57  std::tuple<SubType...> sub_;
58 };
59 struct SubEmpty
60 {
61 };
62 
63 template <typename NumType, size_t N>
64 struct ValArr
65 {
66  std::array<NumType, N> sub_;
67 };
68 template <typename NumType>
69 struct ValVec
70 {
71  std::vector<NumType> sub_;
72 };
73 
74 template <typename NumType>
75 struct ValRef
76 {
77  std::vector<NumType*> valRef_;
78 };
79 
80 // template<class Derived, typename LeafType, typename DynContainer > class StateBaseCRTP;
81 // template<class Derived, typename LeafType, typename FixContainer, int ContSize = 0> class LeafAccessTreeBaseCRTP;
82 // template<class Derived, typename LeafType, typename SetContainer > class StateBaseCRTP;
83 
84 template <class Derived, typename LeafType, typename FixContainer, int ContSize = 0>
85 class LeafAccessTreeBaseCRTP /*< Derived, LeafType, FixContainer, ContSize >*/
86  : std::conditional<std::is_same<LeafType, FixContainer>::value,
87  typename std::conditional<ContSize == -1, // isLeaf is true,
88  ValVec<LeafType>, // val is dynamic
89  ValArr<LeafType, ContSize> >::type,
90  typename std::conditional<ContSize == -1, // isLeaf is false
91  SubVecContainer<LeafType>, // sub is dynamic
92  SubArrContainer<LeafType, ContSize> >::type>::type
93 {
94 public:
96  {
97  }
98 
99  // private :
100  static constexpr const bool isLeaf = std::is_same<LeafType, FixContainer>::value;
101  static constexpr const bool subDynamic = ContSize == -1;
102 
103  // here have to add efficient ones for full static substates
104  // constexpr LeafType& val(const size_t& _i) { return *(this->valRef_[_i]); }
105  // constexpr const LeafType& val(const size_t& _i) { return *(this->valRef_[_i]); }
106 
107  template <bool IsLeaf = isLeaf, typename std::enable_if<(IsLeaf)>::type* = nullptr>
108  /*constexpr */ LeafType& val(const size_t& _i)
109  {
110  return this->sub_[_i];
111  }
112  template <bool IsLeaf = isLeaf, typename std::enable_if<(IsLeaf)>::type* = nullptr>
113  /*constexpr */ const LeafType& val(const size_t& _i) const
114  {
115  return this->sub_[_i];
116  }
117 
118  // here have to add efficient ones for full static substates
119  template <bool IsLeaf = isLeaf, typename std::enable_if<(!IsLeaf)>::type* = nullptr>
120  constexpr LeafType& val(const size_t& _i)
121  {
122  return *(this->valRef_[_i]);
123  }
124  template <bool IsLeaf = isLeaf, typename std::enable_if<(!IsLeaf)>::type* = nullptr>
125  constexpr const LeafType& val(const size_t& _i)
126  {
127  return *(this->valRef_[_i]);
128  }
129 };
130 
131 template <class Derived, typename LeafType, typename... SetTypes, int ContSize>
132 class LeafAccessTreeBaseCRTP<Derived, LeafType, std::tuple<SetTypes...>, ContSize>
133 {
134 public:
135  LeafAccessTreeBaseCRTP() : sub_(std::make_tuple(SetTypes(/*this*/)...))
136  {
137  // store_tuple_ptr_to_array(sub_, subRef_);
138  }
139  using TupleType = std::tuple<SetTypes...>;
140  constexpr LeafType& val(const size_t& _i)
141  {
142  return *(this->valRef_[_i]);
143  }
144 
145  template <size_t _I> /*constexpr*/ typename std::tuple_element<_I, TupleType>::type& sub()
146  {
147  return std::get<_I>(this->sub_);
148  }
149  template <size_t _I> /*constexpr*/ const typename std::tuple_element<_I, TupleType>::type& sub() const
150  {
151  return std::get<_I>(this->sub_);
152  }
153  // /*constexpr */ LeafAccessTreeBase<LeafType, std::tuple<SetTypes...>>& sub (const size_t& _i) {
154  // return this->subRef_[_i]; }
155  // /*constexpr */const LeafAccessTreeBase<LeafType, std::tuple<SetTypes...>>& sub (const size_t& _i) const {
156  // return this->subRef_[_i]; }
157 
158  std::vector<LeafType*> valRef_;
160  // std::array<LeafAccessTreeBase<LeafType, std::tuple<SetTypes...>>*, (sizeof...(SetTypes))> subRef_;
161 };
162 
163 // template<class Derived, typename NumType, int SubSize, class... SubType>
164 // class StateBaseCRTP : std::conditional<std::is_same<NumType,typename std::tuple_element<0, std::tuple< SubType...
165 // >>::type>::value,
166 // typename std::conditional<SubSize == -1,//isLeaf is true,
167 // ValVec<NumType>,//val is dynamic
168 // ValArr<SubSize, NumType>
169 // >::type,
170 // typename std::conditional<SubSize == -1, //isLeaf is false
171 // SubVecContainer<SubType...>, //sub is dynamic
172 // typename std::conditional<(sizeof...(SubType) >= 2), //sub is set
173 // SubSetContainer<SubType...>,
174 // SubArrContainer<SubSize, SubType...>
175 // >::type
176 // >::type
177 // >::type,
178 // std::conditional</*my fancy stuff to chekc*/true,
179 // SubEmpty,//todo for fancy efficient stuff
180 // ValRef<NumType>
181 // >::type {
182 // public:
183 // StateBaseCRTP() {}
184 // const double& val(const size_t& _i) { return ((Derived*)(this))->valImplCRTP(_i); }
185 // // private :
186 // static constexpr const bool isLeaf = std::is_same<NumType,typename std::tuple_element<0, std::tuple< SubType...
187 // >>::type>::value;
188 // static constexpr const bool subDynamic = SubSize == -1;
189 // static constexpr const bool subIsSet = sizeof...(SubType) >= 2;
190 // // constexpr const bool anyDynamic = forAllSubType();
191 //
192 // template<bool IsLeaf = isLeaf, typename std::enable_if< ( IsLeaf ) >::type* = nullptr> constexpr NumType&
193 // val(const size_t& _i) { return this->sub_[_i]; }
194 // template<bool IsLeaf = isLeaf, typename std::enable_if< ( IsLeaf ) >::type* = nullptr> constexpr const NumType&
195 // val(const size_t& _i) { return this->sub_[_i]; }
196 //
197 // //here have to add efficient ones for full static substates
198 // template<bool IsLeaf = isLeaf, typename std::enable_if< ( !IsLeaf ) >::type* = nullptr> constexpr NumType&
199 // val(const size_t& _i) { return *(this->valRef_[_i]); }
200 // template<bool IsLeaf = isLeaf, typename std::enable_if< ( !IsLeaf ) >::type* = nullptr> constexpr const NumType&
201 // val(const size_t& _i) { return *(this->valRef_[_i]); }
202 //
203 //
204 //
205 // template<bool IsLeaf = isLeaf, bool SubIsSet = subIsSet, typename std::enable_if< (!IsLeaf && !subIsSet) >::type*
206 // = nullptr> constexpr State& sub(const size_t& _i) { return this->sub_[_i]; }
207 // template<bool IsLeaf = isLeaf, bool SubIsSet = subIsSet, typename std::enable_if< (!IsLeaf && !subIsSet) >::type*
208 // = nullptr> constexpr const State& sub(const size_t& _i) { return this->sub_[_i]; }
209 // template<bool IsLeaf = isLeaf, bool SubIsSet = subIsSet, typename std::enable_if< (!IsLeaf && subIsSet) >::type*
210 // = nullptr> constexpr State& sub(const size_t& _i) { return std::get<_i>(this->sub_); }
211 // template<bool IsLeaf = isLeaf, bool SubIsSet = subIsSet, typename std::enable_if< (!IsLeaf && subIsSet) >::type*
212 // = nullptr> constexpr const State& sub(const size_t& _i) { return std::get<_i>(this->sub_); }
213 //
214 // template<bool IsLeaf = isLeaf, bool SubIsSet = subIsSet, typename std::enable_if< (!IsLeaf && !subIsSet) >::type*
215 // = nullptr> constexpr const size_t& size(const size_t& _i) { return this->sub_.size(); }
216 // template<bool IsLeaf = isLeaf, bool SubIsSet = subIsSet, typename std::enable_if< (!IsLeaf && subIsSet) >::type*
217 // = nullptr> constexpr const size_t& size(const size_t& _i) { return (sizeof...(SubType)); }
218 //
219 // //need subResize();
220 //
221 // //need updateSize();
222 // };
223 
225 {
226 public:
227  virtual ~LeafAccessTreeBaseVirt() = default;
228  const double& val(const size_t& _i)
229  {
230  return valImplVirt(_i);
231  }
232 
233 private:
234  virtual const double& valImplVirt(const size_t& _i) = 0;
235 };
236 
237 template <typename LeafType, typename FixContainer, int ContSize>
238 class LeafAccessTreeBase : public LeafAccessTreeBaseCRTP<LeafAccessTreeBase<LeafType, FixContainer, ContSize>, LeafType,
239  FixContainer, ContSize>,
241 {
242 public:
244 
245 private:
247  const double& valImplVirt(const size_t& _i)
248  {
250  }
251  // const double& valImplCRTP(const size_t& _i) { x = 10 * _i; return x; }
252  friend class LeafAccessTreeBaseCRTP<LeafAccessTreeBase, LeafType, FixContainer, ContSize>;
253  double x;
254 };
255 
256 // template<class Derived>
257 // class StateBase {
258 // public:
259 // StateBase() {}
260 // const double& val(const size_t& _i) { return ((Derived*)(this))->valImplCRTP(_i); }
261 // };
262 //
263 // class StateBaseVirtual {
264 // public:
265 // virtual ~StateBaseVirtual() = default;
266 // const double& val(const size_t& _i) { return valImplVirt(_i); }
267 // private:
268 // virtual const double& valImplVirt (const size_t& _i) = 0;
269 // };
270 //
271 // class State : public StateBase<State>, public StateBaseVirtual {
272 // public:
273 // using StateBase::val;
274 // private:
275 // using StateBaseVirtual::val;
276 // const double& valImplVirt(const size_t& _i) { return valImplCRTP(_i); }
277 // const double& valImplCRTP(const size_t& _i) { x = 10 * _i; return x; }
278 // friend class StateBase;
279 // double x;
280 // };
281 //
282 //
283 // class StateOnlyVirtBase : public StateBaseVirtual {
284 // public:
285 // private:
286 // const double& valImplVirt(const size_t& _i) { x = 10 * _i; return x; }
287 // double x;
288 // };
289 
290 // void doTestVirt(double nn, double& x2, StateBaseVirtual* stateBaseVirt) {
291 // cout<<"timeVirt: ";
292 // { boost::timer::auto_cpu_timer tt;
293 // for(size_t i = 0; i < nn; ++i){
294 // for(size_t j = 0; j < nn; ++j){
295 // x2 += stateBaseVirt->val(i*nn+j);
296 // }
297 // }
298 // cout<<"(x="<<x2<<") :";
299 // }
300 // }
301 // void doTestCRTP(double nn, double& x1, StateBase<State>* stateBaseCRTP) {
302 // cout<<"timeCRTP: ";
303 // { boost::timer::auto_cpu_timer tt;
304 // for(size_t i = 0; i < nn; ++i){
305 // for(size_t j = 0; j < nn; ++j){
306 // x1 += stateBaseCRTP->val(i*nn+j);
307 // }
308 // }
309 // cout<<"(x="<<x1<<") :";
310 // }
311 // }
312 //
313 // StateBaseCRTP<State, double, std::tuple<State, State> > testtt;
314 // // cout<<testtt.isLeaf<<endl;
315 // // cout<<testtt.subDynamic<<endl;
316 // // cout<<testtt.subIsSet<<endl;
317 //
318 // StateOnlyVirtBase state0;
319 // State state1, state2;
320 //
321 //
322 // StateBase<State>* stateBaseCRTP;
323 // StateBaseVirtual* stateBaseVirt;
324 //
325 // stateBaseCRTP = &state1;
326 // // stateBaseVirt = &state2;
327 // stateBaseVirt = &state0;
328 //
329 // // stateBaseVirt = &state0;
330 // // stateBaseVirt = &state;
331 //
332 // double x1 = 0;
333 // double x2 = 0;
334 // size_t nn = 10000;
335 //
336 //
337 // doTestCRTP(nn, x1, stateBaseCRTP);
338 // doTestVirt(nn, x2, stateBaseVirt);
339 //
340 // cout<<endl;
341 }
342 
343 #endif // TUW_LEAF_ACCESS_TREE_HPP
std::vector< NumType * > valRef_
constexpr LeafType & val(const size_t &_i)
Helper function needed to upgrade c++ 2011.
Definition: utils.h:193
constexpr const LeafType & val(const size_t &_i)
std::vector< NumType > sub_
std::vector< SubType > sub_
static constexpr auto value
Definition: utils.h:205
std::array< SubType, N > sub_
const double & valImplVirt(const size_t &_i)
LeafType & val(const size_t &_i)
std::array< NumType, N > sub_
std::tuple< SubType... > sub_
const LeafType & val(const size_t &_i) const
const double & val(const size_t &_i)


tuw_control
Author(s): George Todoran
autogenerated on Mon Jun 10 2019 15:27:21