File: CVariableNames.h

package info (click to toggle)
brial 1.2.12-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,404 kB
  • sloc: cpp: 219,610; ansic: 43,783; python: 4,172; makefile: 336; sh: 5
file content (109 lines) | stat: -rw-r--r-- 2,512 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// -*- c++ -*-
//*****************************************************************************
/** @file CVariableNames.h
 *
 * @author Alexander Dreyer
 * @date 2006-24-10
 *
 * This file defines an array-like type for storing variables names by index.
 *
 * @par Copyright:
 *   (c) 2006 by The PolyBoRi Team
**/
//*****************************************************************************

#ifndef polybori_ring_CVariableNames_h_
#define polybori_ring_CVariableNames_h_

// include basic definitions
#include <polybori/pbori_defs.h>

// get standard vector functionality
#include <vector>

// get standard string functionalities
#include <string>
#include <sstream>

BEGIN_NAMESPACE_PBORI

class CVariableNames {
public:

  /// @name adopt global type definitions
  //@{
  typedef CTypes::size_type size_type;
  typedef CTypes::idx_type idx_type;
  //@}

  /// Define type for setting/getting names of variables
  typedef CTypes::vartext_type vartext_type;

  /// Define type for storing names of variables
  typedef std::string varname_type;

  /// Define type for storing names of variables
  typedef std::vector<varname_type> storage_type;

  /// Define type for write accessing elements
  typedef storage_type::reference reference;

  /// Define type for outputing variable names
  typedef vartext_type const_reference;

  /// Define type of *this
  typedef CVariableNames self;

  /// Constructor
  CVariableNames(size_type nvars): m_data(nvars) {  reset(); }

  /// Copy Constructor
  CVariableNames(const self& rhs): m_data(rhs.m_data) { }

  /// Set default variable names
  void reset(idx_type idx = 0);

  /// Get name of variable with index idx
  const_reference operator[](idx_type idx) const { 

    if PBORI_UNLIKELY(size_type(idx) >= m_data.size())
      return undefName();
    return m_data[idx].c_str(); 
  }

  /// Get writable reference to name of variable with index idx
  void set(idx_type idx, const varname_type& varname) { 

    size_type nlen = m_data.size();

    if PBORI_UNLIKELY((size_type)idx >= nlen) {
      m_data.resize((size_type)idx + 1);
      reset((idx_type)nlen);
    }

    m_data[idx] = varname;
  }

protected:
  static const_reference undefName() {  return "UNDEF"; }

private:
  storage_type m_data;
};

inline 
void CVariableNames::reset(idx_type idx) {

  idx_type nlen = (idx_type)m_data.size();

  for (; idx < nlen; ++idx){
    std::ostringstream sstrg; 
    sstrg << "x(" << idx << ')';
    m_data[idx] = sstrg.str();
  }
}


END_NAMESPACE_PBORI

#endif