CppADCodeGen  HEAD
A C++ Algorithmic Differentiation Package with Source Code Generation
code_handler_vector.hpp
1 #ifndef CPPAD_CG_CODE_HANDLER_VECTOR_INCLUDED
2 #define CPPAD_CG_CODE_HANDLER_VECTOR_INCLUDED
3 /* --------------------------------------------------------------------------
4  * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5  * Copyright (C) 2016 Ciengis
6  * Copyright (C) 2018 Joao Leal
7  *
8  * CppADCodeGen is distributed under multiple licenses:
9  *
10  * - Eclipse Public License Version 1.0 (EPL1), and
11  * - GNU General Public License Version 3 (GPL3).
12  *
13  * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
14  * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
15  * ----------------------------------------------------------------------------
16  * Author: Joao Leal
17  */
18 
19 namespace CppAD {
20 namespace cg {
21 
22 // forward declaration
23 template<class Base>
24 class CodeHandler;
25 
30 template<class Base>
32  friend class CodeHandler<Base>;
33 protected:
34  CodeHandler<Base>* handler_;
35 public:
36 
37  inline CodeHandlerVectorSync(CodeHandler<Base>& handler) :
38  handler_(&handler) {
39  handler_->addVector(this);
40  }
41 
42  inline CodeHandlerVectorSync(const CodeHandlerVectorSync& orig) :
43  handler_(orig.handler_) {
44  handler_->addVector(this);
45  }
46 
47  virtual ~CodeHandlerVectorSync() {
48  if (handler_ != nullptr)
49  handler_->removeVector(this);
50  }
51 
52  inline CodeHandler<Base>& getHandler() const {
53  return *handler_;
54  }
55 
56 protected:
61  virtual void nodesErased(size_t start,
62  size_t end) = 0;
63 };
64 
70 template<class Base, class T>
72 public:
73  using iterator = typename std::vector<T>::iterator;
74  using const_iterator = typename std::vector<T>::const_iterator;
75  using const_reverse_iterator = typename std::vector<T>::const_reverse_iterator;
76  using reverse_iterator = typename std::vector<T>::reverse_iterator;
77  using reference = typename std::vector<T>::reference;
78  using const_reference = typename std::vector<T>::const_reference;
79 private:
83  std::vector<T> data_;
84 public:
85 
86  inline CodeHandlerVector(CodeHandler<Base>& handler) :
88  }
89 
90  inline CodeHandlerVector(const CodeHandlerVector& orig) :
92  data_(orig.data_) {
93  }
94 
95  inline void clear() {
96  data_.clear();
97  }
98 
99  inline void adjustSize() {
100  size_t s = this->handler_->getManagedNodesCount();
101  if (s >= data_.capacity()) {
102  data_.reserve(s * 3 / 2 + 1);
103  }
104  data_.resize(s);
105  }
106 
107  inline void adjustSize(const OperationNode<Base>& node) {
108  CPPADCG_ASSERT_UNKNOWN(node.getCodeHandler() == this->handler_)
109 
110  size_t p = node.getHandlerPosition();
111  if (p == (std::numeric_limits<size_t>::max)())
112  throw CGException("An operation node is not managed by this code handler");
113 
114  if (p >= data_.size())
115  adjustSize();
116  }
117 
118  inline reference get(const OperationNode<Base>& node) {
119  CPPADCG_ASSERT_UNKNOWN(node.getCodeHandler() == this->handler_)
120 
121  size_t p = node.getHandlerPosition();
122  if (p == (std::numeric_limits<size_t>::max)())
123  throw CGException("An operation node is not managed by this code handler");
124  CPPADCG_ASSERT_UNKNOWN(p < data_.size())
125 
126  return data_[p];
127  }
128 
129  inline const_reference get(const OperationNode<Base>& node) const {
130  CPPADCG_ASSERT_UNKNOWN(node.getCodeHandler() == this->handler_)
131 
132  size_t p = node.getHandlerPosition();
133  if (p == (std::numeric_limits<size_t>::max)())
134  throw CGException("An operation node is not managed by this code handler");
135  CPPADCG_ASSERT_UNKNOWN(p < data_.size())
136 
137  return data_[p];
138  }
139 
140  inline void set(const OperationNode<Base>& node,
141  const T& val) {
142  CPPADCG_ASSERT_UNKNOWN(node.getCodeHandler() == this->handler_)
143 
144  size_t p = node.getHandlerPosition();
145  if (p == (std::numeric_limits<size_t>::max)())
146  throw CGException("An operation node is not managed by this code handler");
147  CPPADCG_ASSERT_UNKNOWN(p < data_.size())
148 
149  data_[node.getHandlerPosition()] = val;
150  }
151 
152  inline size_t size() const {
153  return data_.size();
154  }
155 
156  inline bool empty() const {
157  return data_.empty();
158  }
159 
160  inline void fill(const T& v) {
161  std::fill(data_.begin(), data_.end(), v);
162  }
163 
164  // iterators
165 
166  inline iterator begin() {
167  return data_.begin();
168  }
169 
170  inline const_iterator begin() const {
171  return data_.begin();
172  }
173 
174  inline iterator end() {
175  return data_.end();
176  }
177 
178  inline const_iterator end() const {
179  return data_.end();
180  }
181 
182  inline reverse_iterator rbegin() {
183  return data_.rbegin();
184  }
185 
186  inline const_reverse_iterator rbegin() const {
187  return data_.rbegin();
188  }
189 
190  inline reverse_iterator rend() {
191  return data_.rend();
192  }
193 
194  inline const_reverse_iterator rend() const {
195  return data_.rend();
196  }
197 
198  inline const_iterator cbegin() const noexcept {
199  return data_.cbegin();
200  }
201 
202  inline const_iterator cend() const noexcept {
203  return data_.cend();
204  }
205 
206  inline const_reverse_iterator crbegin() const noexcept {
207  return data_.crbegin();
208  }
209 
210  inline const_reverse_iterator crend() const noexcept {
211  return data_.crend();
212  }
213 protected:
214 
215  void nodesErased(size_t start,
216  size_t end) override {
217  if (start < data_.size()) {
218  end = std::min<size_t>(end, data_.size());
219  data_.erase(data_.begin() + start, data_.begin() + end);
220  }
221  }
222 
223 public:
224 
228  inline reference operator[](const OperationNode<Base>& node) {
229  return get(node);
230  }
231 
232  inline const_reference operator[](const OperationNode<Base>& node) const {
233  return get(node);
234  }
235 
236 };
237 
238 } // END cg namespace
239 } // END CppAD namespace
240 
241 #endif
virtual void nodesErased(size_t start, size_t end)=0
void nodesErased(size_t start, size_t end) override
size_t getHandlerPosition() const
reference operator[](const OperationNode< Base > &node)
CodeHandler< Base > * getCodeHandler() const