state_map_vector.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2017 by George Todoran <george.todoran@tuwien.ac.at *
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 STATE_MAP_VECTOR_HPP
34 #define STATE_MAP_VECTOR_HPP
35 
37 
38 #include <iostream>
39 #include <stdexcept>
40 #include <vector>
41 #include <memory>
42 #include <eigen3/Eigen/Eigen>
43 
44 namespace tuw
45 {
46 template <class TLeafType>
48 {
49 protected:
51  {
52  }
53 
54 protected:
55  ContainerSubStateMapVector(size_t _size, const TLeafType& _vals) : subs_(_size, _vals)
56  {
57  }
58 
59 protected:
60 #ifdef USE_MAP_ALIGNMENT
61  std::vector<TLeafType, boost::alignment::aligned_allocator<TLeafType, MapAlignment>> subs_;
62 #else
63  std::vector<TLeafType> subs_;
64 #endif
65 };
67 {
68 };
69 
70 template <class TNumericType, class TLeafType>
72  : public StateMapBaseCRTP<StateMapVector<TNumericType, TLeafType>>,
73  public StateMapBaseVirt<TNumericType>,
74  public DataBuffer<TNumericType, StateMapBaseCRTP<StateMapVector<TNumericType, TLeafType>>::MapSize>,
75  public std::conditional<!std::is_same<TLeafType, TNumericType>::value, ContainerSubStateMapVector<TLeafType>,
76  ContainerSubStateMapVectorEmpty>::type
77 {
78 private:
80 
81 private:
82  using NumericType = TNumericType;
83 
84 private:
85  using LeafType = TLeafType;
86 
87 private:
89 
90 private:
91  using RootType = typename StateMapBaseCRTPTraits<ImplType>::RootType;
92 
93 public:
95 
96 private:
98 
99 private:
101 
102 private:
105 
106 private:
107  static constexpr const bool HasNumericLeaf = std::is_same<LeafType, NumericType>::value;
108 
109 private:
111 
112 private:
113  TNumericType* memStartRef_;
114 
115 private:
117 
118 private:
120 
121  // special class member functions
122 public:
123  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
127  , DataBuffer<TNumericType, StateBaseCRTPType::MapSize>(std::shared_ptr<DataBufferContainterType>(new DataBufferContainterType))
128  , ContainerSubStateMapVector<TLeafType>(0, LeafType(this, this->dataBuffer_))
129  , map_(nullptr, 0)
130  , memStartRef_(nullptr)
131  , mapElementSize_(0)
132  , root_(this)
133  {
134  }
135 
136 public:
137  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
141  , DataBuffer<TNumericType, StateBaseCRTPType::MapSize>(std::shared_ptr<DataBufferContainterType>(new DataBufferContainterType))
142  , map_(nullptr, 0)
143  , memStartRef_(nullptr)
144  , mapElementSize_(0)
145  , root_(this)
146  {
147  }
148 
149 public:
150  StateMapVector(RootType* _root, std::shared_ptr<DataBufferContainterType>& _dataBuffer)
153  , DataBuffer<TNumericType, StateBaseCRTPType::MapSize>(_dataBuffer)
154  , map_(nullptr, 0)
155  , memStartRef_(nullptr)
156  , mapElementSize_(0)
157  , root_(_root)
158  {
159  }
160 
161 public:
162  virtual ~StateMapVector() = default;
163 
164 public:
166  : DataBuffer<TNumericType, StateBaseCRTPType::MapSize>(_rhs.dataBuffer_)
167  , map_(_rhs.map_)
168  , memStartRef_(nullptr)
169  , mapElementSize_(_rhs.mapElementSize_)
170  , root_(_rhs.root_)
171  {
172  if (!_rhs.root_->internalCopy_)
173  {
174  throw std::runtime_error("Copy-constructor not allowed");
175  }
176  else
177  {
178  copyRhsSubs(_rhs);
179  }
180  }
181 
182 private:
183  template <typename SubType = LeafType,
185  void copyRhsData(const StateMapVector& _rhs)
186  {
187  subResize(_rhs.subs_.size());
188  for (size_t i = 0; i < this->subs_.size(); ++i)
189  {
190  this->subs_[i].copyRhsData(_rhs.subs_[i]);
191  }
192  }
193 
194 private:
195  template <typename SubType = LeafType,
197  void copyRhsData(const StateMapVector& _rhs)
198  {
199  subResize(_rhs.mapElementSize_);
200  }
201 
202 private:
203  template <typename SubType = LeafType,
205  void copyRhsSubs(const StateMapVector& _rhs)
206  {
207  for (size_t i = 0; i < this->subs_.size(); ++i)
208  {
209  this->subs_[i] = _rhs.subs_[i];
210  }
211  }
212 
213 private:
214  template <typename SubType = LeafType,
216  void copyRhsSubs(const StateMapVector& _rhs)
217  {
218  }
219 
220 public:
222  {
223  if (this != &_rhs)
224  {
225  if (!root_->internalCopy_)
226  {
227  copyRhsData(_rhs);
228  this->data() = _rhs.data();
229  }
230  else
231  {
232  mapElementSize_ = _rhs.mapElementSize_;
233  copyRhsSubs(_rhs);
234  }
235  }
236  return *this;
237  }
238 
239 public:
240  StateMapVector(StateMapVector&&) = default;
241 
242 public:
243  StateMapVector& operator=(StateMapVector&&) = default;
244 
245 public:
247 
248 public:
250 
251 public:
253 
254 public:
256 
257 public:
259 
260 public:
262 
263 private:
264  MapTypeVirt dataImplVirt() override final
265  {
266  return dataImplCRTP();
267  }
268 
269 private:
270  const MapTypeVirt dataImplVirt() const override final
271  {
272  return dataImplCRTP();
273  }
274 
275 private:
276  StateBaseVirtualType& subImplVirt(const size_t& _i) override final
277  {
278  return subImplVirtDispatch(_i);
279  }
280 
281 private:
282  const StateBaseVirtualType& subImplVirt(const size_t& _i) const override final
283  {
284  return subImplVirtDispatch(_i);
285  }
286 
287 private:
288  void subResizeImplVirt(const size_t& _size) override final
289  {
290  subResizeImplCRTP(_size);
291  }
292 
293 private:
294  void bindToMemoryImplVirt(TNumericType* _memRef) override final
295  {
296  bindToMemoryImplCRTP(_memRef);
297  }
298 
299 private:
300  const size_t subSizeImplVirt() const override final
301  {
302  return subSizeImplCRTP();
303  }
304 
305 private:
306  NumericType* const memStartRefImplVirt() const override final
307  {
308  return memStartRefImplCRTP();
309  }
310 
311 private:
312  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
314  {
315  return subImplCRTP(_i);
316  }
317 
318 private:
319  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
320  const StateBaseVirtualType& subImplVirtDispatch(const size_t& _i) const
321  {
322  return subImplCRTP(_i);
323  }
324 
325 private:
326  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
328  {
329  throw std::runtime_error("Access of numeric Leaf using sub function not allowed");
330  return *root_;
331  }
332 
333 private:
334  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
335  const StateBaseVirtualType& subImplVirtDispatch(const size_t& _i) const
336  {
337  throw std::runtime_error("Access of numeric Leaf using sub function not allowed");
338  return *root_;
339  }
340 
341 private:
342  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
343  void subResizeImplCRTP(const size_t& _size)
344  {
345  if (_size != this->subs_.size())
346  {
347  const size_t mapElementSizeOld = mapElementSize_;
348  const auto distFromStart = std::distance(this->dataBuffer_->data(), memStartRef_);
349  root_->internalCopy_ = true;
350  this->subs_.resize(_size, LeafType(root_, this->dataBuffer_));
351  root_->internalCopy_ = false;
352  mapElementSize_ = 0;
353  for (const auto& subI : this->subs_)
354  {
355  mapElementSize_ += subI.mapElementSize_;
356  }
357  const auto iteratorStart = this->dataBuffer_->begin() + distFromStart;
358  if (mapElementSize_ > mapElementSizeOld)
359  {
360  this->dataBuffer_->insert(iteratorStart + 1 * (mapElementSizeOld != 0), (mapElementSize_ - mapElementSizeOld),
361  0);
362  }
363  else if (mapElementSize_ < mapElementSizeOld)
364  {
365  this->dataBuffer_->erase(iteratorStart + mapElementSize_, iteratorStart + mapElementSizeOld);
366  }
367  root_->bindToMemory(this->dataBuffer_->data());
368  }
369  }
370 
371 private:
372  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
373  void subResizeImplCRTP(const size_t& _size)
374  {
375  if (_size != mapElementSize_)
376  {
377  const size_t mapElementSizeOld = mapElementSize_;
378  const auto distFromStart = std::distance(this->dataBuffer_->data(), memStartRef_);
379  mapElementSize_ = _size;
380  const auto iteratorStart = this->dataBuffer_->begin() + distFromStart;
381  if (mapElementSize_ > mapElementSizeOld)
382  {
383  this->dataBuffer_->insert(iteratorStart + 1 * (mapElementSizeOld != 0), (mapElementSize_ - mapElementSizeOld),
384  0);
385  root_->bindToMemory(this->dataBuffer_->data());
386  }
387  else if (mapElementSize_ < mapElementSizeOld)
388  {
389  this->dataBuffer_->erase(iteratorStart + mapElementSize_, iteratorStart + mapElementSizeOld);
390  root_->bindToMemory(this->dataBuffer_->data());
391  }
392  }
393  }
394 
395 private:
396  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
397  void bindToMemoryImplCRTP(TNumericType* _memRef)
398  {
399  size_t mapMemBeginShift(0);
400  for (auto& subI : this->subs_)
401  {
402  subI.bindToMemory(_memRef + mapMemBeginShift);
403  mapMemBeginShift += subI.mapElementSize_;
404  }
405  memStartRef_ = _memRef;
406  mapElementSize_ = mapMemBeginShift;
407  bindMap();
408  }
409 
410 private:
411  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
412  void bindToMemoryImplCRTP(TNumericType* _memRef)
413  {
414  memStartRef_ = _memRef;
415  bindMap();
416  }
417 
418 private:
419  template <bool dynamicMap = StateBaseCRTPType::MapSize == -1, typename std::enable_if<(dynamicMap)>::type* = nullptr>
420  void bindMap()
421  {
422  new (&map_) MapTypeCRTP(memStartRef_, mapElementSize_);
423  }
424 
425 private:
426  template <bool dynamicMap = StateBaseCRTPType::MapSize == -1, typename std::enable_if<(!dynamicMap)>::type* = nullptr>
427  void bindMap()
428  {
429  new (&map_) MapTypeCRTP(memStartRef_);
430  }
431 
432 private:
434  {
435  return map_;
436  }
437 
438 private:
439  const MapTypeCRTP& dataImplCRTP() const
440  {
441  return map_;
442  }
443 
444 private:
445  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
446  LeafType& subImplCRTP(const size_t& _i)
447  {
448  return this->subs_[_i];
449  }
450 
451 private:
452  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
453  const LeafType& subImplCRTP(const size_t& _i) const
454  {
455  return this->subs_[_i];
456  }
457 
458 private:
459  template <size_t _i, bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
461  {
462  return this->subs_[_i];
463  }
464 
465 private:
466  template <size_t _i, bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
467  const LeafType& subImplCRTP() const
468  {
469  return this->subs_[_i];
470  }
471 
472 private:
473  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(!numericLeaf)>::type* = nullptr>
474  const size_t subSizeImplCRTP() const
475  {
476  return this->subs_.size();
477  }
478 
479 private:
480  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
481  LeafType& subImplCRTP(const size_t& _i)
482  {
483  return this->data()(_i);
484  }
485 
486 private:
487  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
488  const LeafType& subImplCRTP(const size_t& _i) const
489  {
490  return this->data()(_i);
491  }
492 
493 private:
494  template <size_t _i, bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
496  {
497  return this->data()(_i);
498  }
499 
500 private:
501  template <size_t _i, bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
502  const LeafType& subImplCRTP() const
503  {
504  return this->data()(_i);
505  }
506 
507 private:
508  template <bool numericLeaf = HasNumericLeaf, typename std::enable_if<(numericLeaf)>::type* = nullptr>
509  const size_t subSizeImplCRTP() const
510  {
511  return mapElementSize_;
512  }
513 
514 private:
516  {
517  return memStartRef_;
518  }
519 
520  // friends
521  template <class Derived2>
522  friend class StateMapBaseCRTP;
523  template <class Derived2>
524  friend struct StateMapBaseCRTPTraits;
525  template <class TNumericType2>
526  friend class StateMapBaseVirt;
527  template <class TNumericType2, class... TLeafTypes2>
528  friend class StateMapTuple;
529  template <class TNumericType2, class TLeafType2>
530  friend class StateMapVector;
531  template <class TNumericType2, class TLeafType2, size_t TN2>
532  friend class StateMapArray;
533  template <class TLeafType2>
534  friend struct LeafTypeContClass;
535 };
536 
537 namespace
538 {
539 template <class TNumericType, class TLeafType>
540 struct StateMapBaseCRTPTraits<StateMapVector<TNumericType, TLeafType>>
541  : public std::conditional<std::is_same<TNumericType, TLeafType>::value, LeafTypeContNum<TNumericType>,
542  LeafTypeContClass<TLeafType>>::type
543 {
544  using NumericType = TNumericType;
545  using LeafTypeExt = TLeafType;
546  using LeafsTupleType = EmptyLeafType;
547  using LeafsTupleTypeExt = EmptyLeafType;
549  static constexpr const int subSize = -1;
550  static constexpr const bool isDynamic = true;
551 };
552 
553 } // namespace <anonymous>
554 }
555 
556 #endif // STATE_MAP_VECTOR_HPP
typename StateMapBaseCRTPTraits< ImplType >::RootType RootType
ContainerSubStateMapVector(size_t _size, const TLeafType &_vals)
std::vector< TLeafType > subs_
StateMapVector & operator=(const StateMapVector &_rhs)
void subResizeImplVirt(const size_t &_size) overridefinal
typename StateMapBaseVirt< NumericType >::StateBaseVirtualType StateBaseVirtualType
const MapTypeCRTP & dataImplCRTP() const
const size_t subSizeImplCRTP() const
Helper function needed to upgrade c++ 2011.
Definition: utils.h:193
StateBaseVirtualType & subImplVirtDispatch(const size_t &_i)
StateBaseVirtualType & subImplVirt(const size_t &_i) overridefinal
NumericType *const memStartRefImplCRTP() const
void subResizeImplCRTP(const size_t &_size)
const LeafType & subImplCRTP() const
void bindToMemoryImplVirt(TNumericType *_memRef) overridefinal
typename DataBufferContainerClass::ContainerType DataBufferContainerType
NumericType *const memStartRefImplVirt() const overridefinal
void bindToMemoryImplCRTP(TNumericType *_memRef)
void copyRhsSubs(const StateMapVector &_rhs)
Eigen::Map< Eigen::Matrix< TNumericType, Eigen::Dynamic, 1 > > MapTypeVirt
static constexpr auto value
Definition: utils.h:205
void copyRhsData(const StateMapVector &_rhs)
StateMapVector(const StateMapVector &_rhs)
typename DataBuffer< TNumericType, StateBaseCRTPType::MapSize >::DataBufferContainerType DataBufferContainterType
TNumericType * memStartRef_
MapTypeCRTP & dataImplCRTP()
StateMapVector(RootType *_root, std::shared_ptr< DataBufferContainterType > &_dataBuffer)
StateBaseVirtualType * root_
const LeafType & subImplCRTP(const size_t &_i) const
MapTypeVirt dataImplVirt() overridefinal
const MapTypeVirt dataImplVirt() const overridefinal
const size_t subSizeImplVirt() const overridefinal
LeafType & subImplCRTP(const size_t &_i)
const StateBaseVirtualType & subImplVirtDispatch(const size_t &_i) const
const StateBaseVirtualType & subImplVirt(const size_t &_i) const overridefinal
typename StateMapBaseCRTP< ImplType >::MapTypeCRTP MapTypeCRTP


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