PolyBoRi
CVariableNames.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //*****************************************************************************
13 //*****************************************************************************
14 
15 #ifndef polybori_ring_CVariableNames_h_
16 #define polybori_ring_CVariableNames_h_
17 
18 // include basic definitions
19 #include <polybori/pbori_defs.h>
20 
21 // get standard vector functionality
22 #include <vector>
23 
24 // get standard string functionalities
25 #include <string>
26 #include <sstream>
27 
29 
31 public:
32 
34 
38 
41 
43  typedef std::string varname_type;
44 
46  typedef std::vector<varname_type> storage_type;
47 
49  typedef storage_type::reference reference;
50 
53 
55  typedef CVariableNames self;
56 
58  CVariableNames(size_type nvars): m_data(nvars) { reset(); }
59 
61  CVariableNames(const self& rhs): m_data(rhs.m_data) { }
62 
64  void reset(idx_type idx = 0);
65 
68 
69  if PBORI_UNLIKELY(size_type(idx) >= m_data.size())
70  return undefName();
71  return m_data[idx].c_str();
72  }
73 
75  void set(idx_type idx, const varname_type& varname) {
76 
77  size_type nlen = m_data.size();
78 
79  if PBORI_UNLIKELY((size_type)idx >= nlen) {
80  m_data.resize((size_type)idx + 1);
81  reset((idx_type)nlen);
82  }
83 
84  m_data[idx] = varname;
85  }
86 
87 protected:
88  static const_reference undefName() { return "UNDEF"; }
89 
90 private:
91  storage_type m_data;
92 };
93 
94 inline
95 void CVariableNames::reset(idx_type idx) {
96 
97  idx_type nlen = (idx_type)m_data.size();
98 
99  for (; idx < nlen; ++idx){
100  std::ostringstream sstrg;
101  sstrg << "x(" << idx << ')';
102  m_data[idx] = sstrg.str();
103  }
104 }
105 
106 
108 
109 #endif