state_nested_vector.h
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 STATE_NESTED_VECTOR_H
34 #define STATE_NESTED_VECTOR_H
35 
36 #include <float.h>
37 #include <memory>
38 
40 #include <tuw_control/utils.h>
41 
42 namespace tuw
43 {
48 template <typename SubState>
50 template <typename SubState>
51 using StateNestedVectorSPtr = std::shared_ptr<StateNestedVector<SubState> >;
52 template <typename SubState>
53 using StateNestedVectorConstSPtr = std::shared_ptr<StateNestedVector<SubState> const>;
54 template <typename SubState>
55 using StateNestedVectorUPtr = std::unique_ptr<StateNestedVector<SubState> >;
56 template <typename SubState>
57 using StateNestedVectorConstUPtr = std::unique_ptr<StateNestedVector<SubState> const>;
58 template <typename SubState>
59 class StateNestedVector : public State
60 {
61 public:
62  StateNestedVector(State* _parent) : State(_parent)
63  {
64  this->callRootUpdateSize();
65  }
66 
67 public:
69  {
70  this->callRootUpdateSize();
71  }
72 
73 public:
74  virtual ~StateNestedVector() = default;
75 
76 public:
77  StateNestedVector(const StateNestedVector&) = default;
78 
79 public:
81 
82 public:
84 
85 public:
87 
88 public:
89  virtual StateSPtr cloneState() const override
90  {
91  return std::shared_ptr<StateNestedVector<SubState> >(new StateNestedVector<SubState>(*this));
92  }
93 
94 public:
95  size_t stateSize() const override
96  {
97  return statesSize_;
98  }
99 
100 public:
101  size_t valueSize() const override
102  {
103  return valueSize_;
104  }
105 
106 public:
107  double& value(const std::size_t& _i) override
108  {
109  return *values_[_i];
110  }
111 
112 public:
113  const double& value(const std::size_t& _i) const override
114  {
115  return *values_[_i];
116  }
117 
118 public:
119  StateSPtr& state(const std::size_t& _i) override
120  {
121  return statesBase_[_i];
122  }
123 
124 public:
125  void updateSize() override
126  {
127  valueSize_ = 0;
128  for (auto& stateI : states_)
129  {
130  stateI->updateSize();
131  valueSize_ += stateI->valueSize();
132  }
133  values_.resize(valueSize_);
134  size_t valueSizeI, valueSizeSum = 0;
135  for (auto& stateI : states_)
136  {
137  valueSizeI = stateI->valueSize();
138  for (size_t i = 0; i < valueSizeI; ++i)
139  {
140  values_[valueSizeSum + i] = &(stateI->value(i));
141  }
142  valueSizeSum += valueSizeI;
143  }
144  }
145 
146 public:
147  void resize(const size_t& _i) override
148  {
149  statesSize_ = _i;
150  statesBase_.resize(_i);
151  if (_i < states_.size())
152  {
153  states_.resize(_i);
154  }
155  else
156  {
157  for (size_t i = states_.size(); i < _i; ++i)
158  {
159  states_.emplace_back(std::shared_ptr<SubState>(new SubState(this)));
160  statesBase_[i] = states_[i];
161  }
162  }
163  this->callRootUpdateSize();
164  }
166 public:
167  std::shared_ptr<SubState>& stateScoped(const size_t& _i)
168  {
169  return this->states_[_i];
170  }
171 
172 protected:
173  size_t valueSize_;
174 
175 protected:
176  size_t statesSize_;
177 
178 protected:
179  std::vector<std::shared_ptr<SubState> > states_;
180 
181 protected:
182  std::vector<StateSPtr> statesBase_;
183 
184 protected:
185  std::vector<double*> values_;
186 
187 public:
188  const SubState& operator[](size_t i) const
189  {
190  return *states_[i];
191  }
192 
193 public:
194  SubState& operator[](size_t i)
195  {
196  return *states_[i];
197  }
198 
199 public:
200  const std::shared_ptr<SubState>& at(size_t i) const
201  {
202  return states_[i];
203  }
204 
205 public:
206  std::shared_ptr<SubState>& at(size_t i)
207  {
208  return states_[i];
209  }
210 };
211 }
212 
213 #endif // STATE_NESTED_ARRAY_H
std::unique_ptr< StateNestedVector< SubState > > StateNestedVectorUPtr
StateSPtr & state(const std::size_t &_i) override
Access sub-state based on index _i.
std::shared_ptr< State > StateSPtr
Definition: state.h:129
size_t stateSize() const override
Size of the sub-states.
size_t valueSize() const override
Size of the state variables.
SubState & operator[](size_t i)
std::shared_ptr< SubState > & at(size_t i)
void resize(const size_t &_i) override
Resizes the array.
const SubState & operator[](size_t i) const
virtual StateSPtr cloneState() const override
Clone-to-base-class-ptr function.
std::shared_ptr< SubState > & stateScoped(const size_t &_i)
returns pointer to the extended class of the sub-state at index _i.
std::shared_ptr< StateNestedVector< SubState > > StateNestedVectorSPtr
std::vector< StateSPtr > statesBase_
virtual ~StateNestedVector()=default
const std::shared_ptr< SubState > & at(size_t i) const
Generic tree-like recursive structure that allows sub-structure access as well as ordered (array-like...
Definition: state.h:135
std::vector< std::shared_ptr< SubState > > states_
StateNestedVector & operator=(const StateNestedVector &)=default
double & value(const std::size_t &_i) override
Access state variable based on index _i.
StateNestedVector(State *_parent)
std::shared_ptr< StateNestedVector< SubState > const > StateNestedVectorConstSPtr
std::vector< double * > values_
void updateSize() override
Performs internal manipulation when any of the underlying arrays are being resized.
void callRootUpdateSize()
Calls (if present) the parent updateSize procedure. Otherwise performs root updateSize.
Definition: state.h:380
std::unique_ptr< StateNestedVector< SubState > const > StateNestedVectorConstUPtr
const double & value(const std::size_t &_i) const override
Const access state variable based on index _i.


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