mmCEsim 0.3.0
mmWave Channel Estimation Simulation
alg.h
Go to the documentation of this file.
1
12#ifndef _EXPORT_ALG_H_
13#define _EXPORT_ALG_H_
14
15#include "_boost_config.h"
16#include "export/alg_line.h"
17#include "export/alg_opt.h"
18#include "export/calc.h"
19#include "export/macro.h"
20#include "export/type.h"
21#include "utils.h"
22#include <boost/algorithm/string.hpp>
23#include <fmt/core.h>
24#include <fstream>
25#include <iostream>
26#include <numeric>
27#include <stack>
28
29class Alg {
30 public:
31 struct Error {
32 std::string msg;
33 std::string raw_str;
34 size_t line;
35 };
36 using Warning = Error;
37 using Errors = std::vector<Error>;
38 using Warnings = std::vector<Warning>;
39 using Alg_Lines = std::vector<Alg_Line>;
40 using Raw_Strings = std::vector<std::string>;
41 using Line_Nos = std::vector<Raw_Strings::size_type>;
42 using Keys = std::vector<std::string>;
43
44 struct Var {
45 std::string name;
54 int8_t dim = 0;
55 };
56 using Vars = std::vector<Var>;
57
58 public:
59 Alg() = default;
60
61 Alg(const std::string& str, const Macro& macro = macro_none, int job_cnt = -1, int alg_cnt = -1,
62 bool fail_fast = false, bool add_comment = false, bool add_semicolon = true, ALG_Opt opt = ALG_Opt::NONE);
63
64 const Alg_Lines& lines() const;
65
66 const Alg_Line& line(Alg_Lines::size_type index) const;
67
68 const Errors& errors() const;
69
70 const Error& error(Errors::size_type index) const;
71
72 const Warnings& warnings() const;
73
74 const Warning& warning(Errors::size_type index) const;
75
76 bool hasError() const noexcept;
77
78 bool write(std::ofstream& f, const std::string& lang);
79
80 std::string inlineCalc(const std::string& s, const std::string& lang);
81
82 void setIndent(bool use_space = true, unsigned indent_size = 4);
83
84 private:
85 std::ofstream& _wComment(std::ofstream& f, const std::string& lang, const std::string& before = "");
86
87 std::string _indent(size_t indent_size) const noexcept;
88
89 bool _applyKey(Alg_Line& line, const Keys& keys) const;
90
91 void _recoverPrint() const;
92
99 bool _failed = false;
100 bool _use_space = false;
101 bool _add_semicolon = true;
102 bool _add_comment = true;
103 unsigned _indent_size = 4;
104 std::stack<std::string> _contents_at_end;
106 int _alg_cnt = 0;
107 int _job_cnt = 0;
109 std::vector<std::string> _recover_cnt_var;
111
112 const static int max_length = 100000;
113};
114
115inline const Alg::Alg_Lines& Alg::lines() const { return _lines; }
116
117inline const Alg_Line& Alg::line(Alg_Lines::size_type index) const {
118 assert((index < _lines.size() && "Check Alg::line index range."));
119 return _lines[index];
120}
121
122inline const Alg::Errors& Alg::errors() const { return _errors; }
123
124inline const Alg::Error& Alg::error(Errors::size_type index) const {
125 assert((index < _errors.size() && "Check Alg::error index range."));
126 return _errors[index];
127}
128
129inline const Alg::Warnings& Alg::warnings() const { return _warnings; }
130
131inline const Alg::Warning& Alg::warning(Warnings::size_type index) const {
132 assert((index < _warnings.size() && "Check Alg::warning index range."));
133 return _warnings[index];
134}
135
136inline bool Alg::hasError() const noexcept { return !_errors.empty(); }
137
138inline void Alg::setIndent(bool use_space, unsigned indent_size) {
139 _use_space = use_space;
140 _indent_size = indent_size;
141}
142
143inline std::string Alg::_indent(size_t indent_size) const noexcept {
144 if (_use_space) return std::string(indent_size, ' ');
145 else return std::string(indent_size, '\t');
146}
147
148inline bool Alg::_applyKey(Alg_Line& line, const Keys& keys) const {
149 if (line.params().size() > keys.size()) {
150 return false;
151 } else {
152 for (decltype(keys.size()) i = 0; i != line.params().size(); ++i) {
153 // Set the key until the key is specified by user.
154 if (!line.setKey(i, keys[i])) break;
155 }
156 return true;
157 }
158}
159
160#endif
Boost Configurations.
Parse Line of Alg.
ALG_Opt Enum.
ALG_Opt
ALG export options.
Definition: alg_opt.h:20
@ NONE
Definition: alg_opt.h:20
Make Calculation in Alg.
Process line of Alg into standard formats.
Definition: alg_line.h:36
const std::vector< Param_Type > & params() const noexcept
Parameter variables.
Definition: alg_line.h:283
bool setKey(std::vector< Param_Type >::size_type index, const std::string &key)
Set parameter variable key.
Definition: alg_line.h:348
Definition: alg.h:29
Errors _errors
Definition: alg.h:94
bool _add_comment
Definition: alg.h:102
bool _add_semicolon
Definition: alg.h:101
int _branch_line
Definition: alg.h:110
std::ofstream & _wComment(std::ofstream &f, const std::string &lang, const std::string &before="")
Definition: alg.cpp:782
int _recover_cnt
Definition: alg.h:108
Warnings _warnings
Definition: alg.h:95
bool hasError() const noexcept
Definition: alg.h:136
std::string inlineCalc(const std::string &s, const std::string &lang)
Definition: alg.cpp:771
std::vector< std::string > Keys
Definition: alg.h:42
int _alg_cnt
Definition: alg.h:106
Macro _macro
Definition: alg.h:105
Vars _vars
Definition: alg.h:98
std::vector< Var > Vars
Definition: alg.h:56
unsigned _indent_size
Definition: alg.h:103
int _job_cnt
Definition: alg.h:107
const Warnings & warnings() const
Definition: alg.h:129
static const int max_length
Definition: alg.h:112
Alg()=default
bool _failed
Definition: alg.h:99
std::vector< Alg_Line > Alg_Lines
Definition: alg.h:39
const Error & error(Errors::size_type index) const
Definition: alg.h:124
void _recoverPrint() const
std::stack< std::string > _contents_at_end
Definition: alg.h:104
std::vector< Raw_Strings::size_type > Line_Nos
Definition: alg.h:41
std::string _indent(size_t indent_size) const noexcept
Definition: alg.h:143
std::vector< Error > Errors
Definition: alg.h:37
Line_Nos _line_nos
Definition: alg.h:97
std::vector< Warning > Warnings
Definition: alg.h:38
void setIndent(bool use_space=true, unsigned indent_size=4)
Definition: alg.h:138
Alg_Lines _lines
Definition: alg.h:93
bool write(std::ofstream &f, const std::string &lang)
Definition: alg.cpp:115
const Warning & warning(Errors::size_type index) const
Definition: alg.h:131
const Alg_Lines & lines() const
Definition: alg.h:115
std::vector< std::string > Raw_Strings
Definition: alg.h:40
bool _use_space
Definition: alg.h:100
const Alg_Line & line(Alg_Lines::size_type index) const
Definition: alg.h:117
Raw_Strings _raw_strings
Definition: alg.h:96
bool _applyKey(Alg_Line &line, const Keys &keys) const
Definition: alg.h:148
const Errors & errors() const
Definition: alg.h:122
std::vector< std::string > _recover_cnt_var
Definition: alg.h:109
ALG Macro.
static const Macro macro_none
Definition: macro.h:168
str
Definition: version_bump.py:14
Definition: alg.h:31
std::string msg
Definition: alg.h:32
size_t line
Definition: alg.h:34
std::string raw_str
Definition: alg.h:33
Definition: alg.h:44
int8_t dim
Definition: alg.h:54
std::string name
Definition: alg.h:45
Definition: macro.h:62
Type Specification in Alg.
Utilities.