PolyBoRi
CVariableNames.h
Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //*****************************************************************************
00013 //*****************************************************************************
00014 
00015 // include basic definitions
00016 #include "pbori_defs.h"
00017 
00018 // get standard vector functionality
00019 #include <vector>
00020 
00021 // get standard string functionalities
00022 #include <string>
00023 #include <sstream>
00024 
00025 
00026 #ifndef CVariableNames_h_
00027 #define CVariableNames_h_
00028 
00029 BEGIN_NAMESPACE_PBORI
00030 
00031 class CVariableNames {
00032 public:
00033 
00035 
00036   typedef CTypes::size_type size_type;
00037   typedef CTypes::idx_type idx_type;
00039 
00041   typedef CTypes::vartext_type vartext_type;
00042 
00044   typedef std::string varname_type;
00045 
00047   typedef std::vector<varname_type> storage_type;
00048 
00050   typedef storage_type::reference reference;
00051 
00053   typedef vartext_type const_reference;
00054 
00056   typedef CVariableNames self;
00057 
00059   CVariableNames(size_type nvars): m_data(nvars) {  reset(); }
00060 
00062   CVariableNames(const self& rhs): m_data(rhs.m_data) { }
00063 
00065   void reset(idx_type idx = 0);
00066 
00068   const_reference operator[](idx_type idx) const { 
00069 
00070     if UNLIKELY(size_type(idx) >= m_data.size())
00071       return undefName();
00072     return m_data[idx].c_str(); 
00073   }
00074 
00076   void set(idx_type idx, const varname_type& varname) { 
00077 
00078     size_type nlen = m_data.size();
00079 
00080     if UNLIKELY((size_type)idx >= nlen) {
00081       m_data.resize((size_type)idx + 1);
00082       reset((idx_type)nlen);
00083     }
00084 
00085     m_data[idx] = varname;
00086   }
00087 
00088 protected:
00089   static const_reference undefName() {  return "UNDEF"; }
00090 
00091 private:
00092   storage_type m_data;
00093 };
00094 
00095 inline 
00096 void CVariableNames::reset(idx_type idx) {
00097 
00098   idx_type nlen = (idx_type)m_data.size();
00099 
00100   for (; idx < nlen; ++idx){
00101     std::ostringstream sstrg; 
00102     sstrg << "x(" << idx << ')';
00103     m_data[idx] = sstrg.str();
00104   }
00105 }
00106 
00107 
00108 END_NAMESPACE_PBORI
00109 
00110 #endif