File: static_atoms.hh

package info (click to toggle)
dynare 5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 77,852 kB
  • sloc: cpp: 94,481; ansic: 28,551; pascal: 14,532; sh: 5,453; objc: 4,671; yacc: 4,442; makefile: 2,923; lex: 1,612; python: 677; ruby: 469; lisp: 156; xml: 22
file content (107 lines) | stat: -rw-r--r-- 3,853 bytes parent folder | download
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
/*
 * Copyright © 2006 Ondra Kamenik
 * Copyright © 2019 Dynare Team
 *
 * This file is part of Dynare.
 *
 * Dynare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dynare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dynare.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef OGP_STATIC_ATOMS
#define OGP_STATIC_ATOMS

#include "dynamic_atoms.hh"

namespace ogp
{
  class StaticAtoms : public Atoms, public Constants
  {
  protected:
    using Tvarmap = map<string, int>;
    using Tinvmap = map<int, string>;
    /** Storage for names. */
    NameStorage varnames;
    /** Outer order of variables. */
    vector<string> varorder;
    /** This is the map mapping a variable name to the tree
     * index. */
    Tvarmap vars;
    /** This is the inverse mapping. It maps a tree index to the
     * variable name. */
    Tinvmap indices;
  public:
    StaticAtoms() = default;
    /** Conversion from DynamicAtoms. This takes all atoms from
     * the DynamicAtoms and adds its static version. The new tree
     * indices are allocated in the passed OperationTree. Whole
     * the process is traced in the map mapping old tree indices
     * to new tree indices. */
    StaticAtoms(const DynamicAtoms &da, OperationTree &otree, Tintintmap &tmap)
      : Atoms(), Constants(), varnames(), varorder(), vars()
    {
      import_atoms(da, otree, tmap);
    }
    /* Destructor. */
    ~StaticAtoms() override = default;
    /** This imports atoms from dynamic atoms inserting the new
     * tree indices to the given tree (including constants). The
     * mapping from old atoms to new atoms is traced in tmap. */
    void import_atoms(const DynamicAtoms &da, OperationTree &otree,
                      Tintintmap &tmap);
    /** If the name is constant, it returns its tree index if the
     * constant is registered in Constants, it returns -1
     * otherwise. If the name is not constant, it returns result
     * from check_variable, which is implemented by a subclass. */
    int check(const string &name) const override;
    /** This assigns a given tree index to the variable name. The
     * name should have been checked before the call. */
    void assign(const string &name, int t) override;
    int
    nvar() const override
    {
      return varnames.num();
    }
    /** This returns a vector of all variables. */
    vector<int> variables() const override;
    /** This returns a tree index of the given variable. */
    int index(const string &name) const;
    /** This returns a name in a outer ordering. (There is no other ordering.) */
    const string &
    name(int i) const
    {
      return varorder[i];
    }
    /** Debug print. */
    void print() const override;
    /** This registers a variable. A subclass can reimplement
     * this, for example, to ensure uniqueness of the
     * name. However, this method should be always called in
     * overriding methods to do the registering job. */
    virtual void register_name(string name);
    /** Return the name storage to allow querying to other
     * classes. */
    const NameStorage &
    get_name_storage() const
    {
      return varnames;
    }
  protected:
    /** This checks the variable. The implementing subclass might
     * want to throw an exception if the variable has not been
     * registered. */
    virtual int check_variable(const string &name) const = 0;
  };
};

#endif