Minsky
units.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2021
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef UNITS_H
21 #define UNITS_H
22 #include <string>
23 #include <ostream>
24 #include <map>
25 
26 namespace minsky
27 {
30 
34  struct Units: public std::map<std::string,int>
35  {
36  Units() {}
37  Units(const std::string&);
38  std::string str() const;
40  std::string latexStr() const;
41  // remove entries that are unitary
42  void normalise() {
43  for (auto i=begin(); i!=end(); ) {
44  auto j=i; ++i;
45  if (j->second==0 || j->first.empty()) erase(j);
46  }
47  }
48 
49  };
50 
51  inline std::ostream& operator<<(std::ostream& o, const Units& u)
52  {
53  bool first=true;
54  for (auto& i: u)
55  {
56  if (i.first.empty() || i.second==0) continue; // don't display empty units
57  if (!first) o<<" ";
58  first=false;
59  o<<i.first;
60  if (i.second!=1)
61  o<<"^"<<i.second;
62  }
63  return o;
64  }
65 
66 
67 }
68 
69 
70 #include "units.cd"
71 #include "units.xcd"
72 #endif
std::string str() const
Definition: variableType.cc:33
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
std::ostream & operator<<(std::ostream &o, OperationType::Type t)
Definition: operationType.h:70
represents the units (in sense of dimensional analysis) of a variable.
Definition: units.h:34
void normalise()
Definition: units.h:42
std::string latexStr() const
insert braces around exponents for LaTeX processing
Definition: variableType.cc:40