CppADCodeGen  HEAD
A C++ Algorithmic Differentiation Package with Source Code Generation
lang_c_custom_var_name_gen.hpp
1 #ifndef CPPAD_CG_LANG_C_CUSTOM_VAR_NAME_GEN_INCLUDED
2 #define CPPAD_CG_LANG_C_CUSTOM_VAR_NAME_GEN_INCLUDED
3 /* --------------------------------------------------------------------------
4  * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5  * Copyright (C) 2012 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 
28 template<class Base>
29 class LangCCustomVariableNameGenerator : public LangCDefaultVariableNameGenerator<Base> {
30 protected:
31  //
32  const std::vector<std::string> depNames_;
33  const std::vector<std::string> indepNames_;
34 public:
35 
36  LangCCustomVariableNameGenerator(std::vector<std::string> depNames,
37  std::vector<std::string> indepNames,
38  const std::string& depName = "y",
39  const std::string& indepName = "x",
40  const std::string& tmpName = "v",
41  const std::string& tmpArrayName = "array") :
42  LangCDefaultVariableNameGenerator<Base>(depName, indepName, tmpName, tmpArrayName),
43  depNames_(std::move(depNames)),
44  indepNames_(std::move(indepNames)) {
45  }
46 
47  inline virtual ~LangCCustomVariableNameGenerator() = default;
48 
49  std::string generateDependent(size_t index) override {
50  if (index < depNames_.size() && !depNames_[index].empty()) {
51  return depNames_[index];
52  } else {
54  }
55  }
56 
57  std::string generateIndependent(const OperationNode<Base>& independent,
58  size_t id) override {
59  size_t index = id - 1;
60  if (index < indepNames_.size() && !indepNames_[index].empty()) {
61  return indepNames_[index];
62  } else {
64  }
65  }
66 
68  size_t idFirst,
69  const OperationNode<Base>& indepSecond,
70  size_t idSecond) override {
71  size_t index1 = idFirst - 1;
72  size_t index2 = idSecond - 1;
73 
74  if ((index1 > indepNames_.size() || indepNames_[index1].empty()) &&
75  (index2 > indepNames_.size() || indepNames_[index2].empty())) {
76  return index1 + 1 == index2;
77  } else {
78  return false; // individual names used (not elements of arrays)
79  }
80  }
81 
83  size_t id1,
84  const OperationNode<Base>& indep2,
85  size_t id2) override {
86  size_t index1 = id1 - 1;
87  size_t index2 = id2 - 1;
88 
89  return (index1 > indepNames_.size() || indepNames_[index1].empty()) &&
90  (index2 > indepNames_.size() || indepNames_[index2].empty());
91  }
92 
93 };
94 
95 } // END cg namespace
96 } // END CppAD namespace
97 
98 #endif
bool isConsecutiveInIndepArray(const OperationNode< Base > &indepFirst, size_t idFirst, const OperationNode< Base > &indepSecond, size_t idSecond) override
std::string generateIndependent(const OperationNode< Base > &independent, size_t id) override
STL namespace.
std::string generateIndependent(const OperationNode< Base > &independent, size_t id) override
std::string generateDependent(size_t index) override
bool isInSameIndependentArray(const OperationNode< Base > &indep1, size_t id1, const OperationNode< Base > &indep2, size_t id2) override