mmCEsim 0.3.0
mmWave Channel Estimation Simulation
alg_line.h
Go to the documentation of this file.
1
12#ifndef _EXPORT_ALG_LINE_H_
13#define _EXPORT_ALG_LINE_H_
14
15#include "export/alg_opt.h"
16#include "export/functions.h"
17#include "log_global.h"
18#include "utils.h"
19#include <algorithm>
20#include <cassert>
21#include <exception>
22#include <iomanip>
23#include <iostream>
24#include <regex>
25#include <sstream>
26#include <stack>
27#include <stdexcept>
28#include <string>
29#include <vector>
30
36class Alg_Line {
37 public:
43 struct Return_Type {
44 std::string name;
45 std::string type;
46 };
47
53 struct Param_Type {
54 std::string key;
55 std::string value;
56 std::string type;
57 };
58
59 public:
60 // Default constructor.
61 Alg_Line() = default;
62
70 Alg_Line(const std::string& str, ALG_Opt opt = ALG_Opt::NONE);
71
77 const std::string& func() const noexcept;
78
84 const std::vector<Return_Type>& returns() const noexcept;
85
92 const Return_Type& returns(std::vector<Return_Type>::size_type index) const;
93
99 const std::vector<Param_Type>& params() const noexcept;
100
107 const Param_Type& params(std::vector<Param_Type>::size_type index) const;
108
115 const Param_Type& params(const std::string& key) const;
116
123 const std::string& operator[](const std::string key) const;
124
130 const std::string& rawStr() const;
131
141 bool hasKey(const std::string& key) const noexcept;
142
150 bool hasUnknownKey(const std::vector<std::string>& keys) const noexcept;
151
152 bool hasUnknownKey(const std::vector<std::string>& keys, std::string& unknown_key) const noexcept;
153
160 bool hasRepeatedKey() const noexcept;
161
177 bool isValidKey(const std::string& key, const std::vector<std::string>& keys) const noexcept;
178
187 bool setKey(std::vector<Param_Type>::size_type index, const std::string& key);
188
195 bool needsEnd() const noexcept;
196
203 bool isEnd() const noexcept;
204
211 bool isFunctionDeclaration() const noexcept;
212
221 std::ostream& print(std::ostream& out = std::cout, std::string prefix = "") const;
222
223 private:
231 std::string::size_type _findChars(const std::string& s, std::string cs) const noexcept;
232
240 std::string::size_type _findChar(const std::string& s, char c) const noexcept;
241
248 void _removeComment(std::string& s) const noexcept;
249
255 void _processReturns(const std::vector<std::string>& v);
256
262 void _processFuncParams(const std::vector<std::string>& v);
263
264 std::string _decodeEscape(const std::string& s) const;
265
266 private:
267 std::string _func;
268 std::vector<Return_Type> _returns;
269 std::vector<Param_Type> _params;
270 std::string _raw_str;
272};
273
274inline const std::string& Alg_Line::func() const noexcept { return _func; }
275
276inline const std::vector<Alg_Line::Return_Type>& Alg_Line::returns() const noexcept { return _returns; }
277
278inline const Alg_Line::Return_Type& Alg_Line::returns(std::vector<Return_Type>::size_type index) const {
279 assert(index < _returns.size() && "Alg_Line check returns index size within bound.");
280 return _returns[index];
281}
282
283inline const std::vector<Alg_Line::Param_Type>& Alg_Line::params() const noexcept { return _params; }
284
285inline const Alg_Line::Param_Type& Alg_Line::params(std::vector<Param_Type>::size_type index) const {
286 assert((index < _params.size() && "Alg_Line check params index size within bound."));
287 return _params[index];
288}
289
290inline const Alg_Line::Param_Type& Alg_Line::params(const std::string& key) const {
291 for (auto&& elem : _params) {
292 if (elem.key == key) return elem;
293 }
294 throw std::runtime_error("No key '" + key + "'!");
295}
296
297inline const std::string& Alg_Line::operator[](const std::string key) const { return params(key).value; }
298
299inline const std::string& Alg_Line::rawStr() const { return _raw_str; }
300
301inline bool Alg_Line::hasKey(const std::string& key) const noexcept {
302 for (auto&& elem : _params) {
303 if (elem.key == key) return true;
304 }
305 return false;
306}
307
308inline bool Alg_Line::hasUnknownKey(const std::vector<std::string>& keys) const noexcept {
309 for (auto&& elem : _params) {
310 if (!contains(keys, elem.key)) return true;
311 }
312 return false;
313}
314
315inline bool Alg_Line::hasUnknownKey(const std::vector<std::string>& keys, std::string& unkown_key) const noexcept {
316 for (auto&& elem : _params) {
317 if (!contains(keys, elem.key)) {
318 unkown_key = elem.key;
319 return true;
320 }
321 }
322 unkown_key.clear(); // return an empty string
323 return false;
324}
325
326inline bool Alg_Line::hasRepeatedKey() const noexcept {
327 std::vector<std::string> keys(_params.size());
328 for (decltype(_params.size()) i = 0; i != _params.size(); ++i) { keys[i] = _params[i].key; }
329 std::sort(keys.begin(), keys.end());
330 return std::adjacent_find(keys.begin(), keys.end()) != keys.end();
331}
332
333inline bool Alg_Line::isValidKey(const std::string& key, const std::vector<std::string>& keys) const noexcept {
334 if (!hasKey(key)) return false;
335 std::vector<bool> exists(keys.size(), false);
336 for (auto&& elem : _params) {
337 auto&& the_key = elem.key;
338 for (decltype(keys.size()) i = 0; i != keys.size(); ++i) {
339 if (keys[i] == the_key) exists[i] = true;
340 }
341 }
342 for (auto&& ck : exists) {
343 if (!ck) return false;
344 }
345 return true;
346}
347
348inline bool Alg_Line::setKey(std::vector<Param_Type>::size_type index, const std::string& key) {
349 assert(index < _params.size() && "Alg_Line check params index size within bound when setting key.");
350 if (_params[index].key == "") {
351 _params[index].key = key;
352 return true;
353 } else {
354 return false;
355 }
356}
357
358inline bool Alg_Line::needsEnd() const noexcept { return isFuncNeedsEnd(_func); }
359
360inline bool Alg_Line::isEnd() const noexcept { return isFuncIsEnd(_func); }
361
362inline bool Alg_Line::isFunctionDeclaration() const noexcept { return _opt == ALG_Opt::FUNCTION_DECLARATION; }
363
364inline std::ostream& Alg_Line::print(std::ostream& out, std::string prefix) const {
365 out << prefix << "$ " << _raw_str << '\n';
366 if (_func.empty()) return out;
367 out << prefix << "* FUNCTION: " << _func << '\n';
368 if (_func != "COMMENT" && !_params.empty()) {
369 out << prefix << "* PARAMS:\n";
370 for (auto&& p : _params) out << prefix << " > {" << p.key << "}={" << p.value << "}::{" << p.type << "}\n";
371 }
372 if (!isFuncNoEnd(_func)) {
373 out << prefix << "* RETURN:\n";
374 for (auto&& r : _returns) out << prefix << " > {" << r.name << "}::{" << r.type << "}\n";
375 }
376 out << std::flush;
377 return out;
378}
379
380inline std::string::size_type Alg_Line::_findChar(const std::string& s, char c) const noexcept {
381 return _findChars(s, std::string(1, c));
382}
383
384inline std::string Alg_Line::_decodeEscape(const std::string& s) const {
385 return std::regex_replace(s, std::regex(R"(\\:\\:)"), "::");
386}
387
396static inline std::ostream& operator<<(std::ostream& out, const Alg_Line& line) { return line.print(out); }
397
398#endif
static std::ostream & operator<<(std::ostream &out, const Alg_Line &line)
Print Alg_Line contents (including return values, function names and parameters).
Definition: alg_line.h:396
ALG_Opt Enum.
ALG_Opt
ALG export options.
Definition: alg_opt.h:20
@ FUNCTION_DECLARATION
Definition: alg_opt.h:20
@ NONE
Definition: alg_opt.h:20
Process line of Alg into standard formats.
Definition: alg_line.h:36
void _processFuncParams(const std::vector< std::string > &v)
Process function name and parameter variables.
Definition: alg_line.cpp:184
std::string::size_type _findChar(const std::string &s, char c) const noexcept
Find the first occupance of character in string that is not an escape or within quotes.
Definition: alg_line.h:380
const std::string & operator[](const std::string key) const
Get the parameter value with the key.
Definition: alg_line.h:297
bool hasRepeatedKey() const noexcept
Check if parameters have repeated keys.
Definition: alg_line.h:326
std::vector< Return_Type > _returns
Definition: alg_line.h:268
bool hasKey(const std::string &key) const noexcept
Check whether parameters contain the key.
Definition: alg_line.h:301
const std::vector< Param_Type > & params() const noexcept
Parameter variables.
Definition: alg_line.h:283
bool needsEnd() const noexcept
Test whether the function name needs an 'end'.
Definition: alg_line.h:358
bool hasUnknownKey(const std::vector< std::string > &keys) const noexcept
Check if parameters have unknown keys.
Definition: alg_line.h:308
bool setKey(std::vector< Param_Type >::size_type index, const std::string &key)
Set parameter variable key.
Definition: alg_line.h:348
Alg_Line()=default
std::ostream & print(std::ostream &out=std::cout, std::string prefix="") const
Print Alg_Line contents (including return values, function names and parameters).
Definition: alg_line.h:364
std::vector< Param_Type > _params
Definition: alg_line.h:269
void _removeComment(std::string &s) const noexcept
Remove the comment from the string.
Definition: alg_line.cpp:155
const std::vector< Return_Type > & returns() const noexcept
Return variables.
Definition: alg_line.h:276
std::string::size_type _findChars(const std::string &s, std::string cs) const noexcept
Find the first occupance of substring in string that is not an escape or within quotes.
Definition: alg_line.cpp:135
std::string _decodeEscape(const std::string &s) const
Definition: alg_line.h:384
void _processReturns(const std::vector< std::string > &v)
Process returns variables.
Definition: alg_line.cpp:157
std::string _func
Definition: alg_line.h:267
ALG_Opt _opt
Definition: alg_line.h:271
bool isFunctionDeclaration() const noexcept
Check whether the line is for function declaration.
Definition: alg_line.h:362
const std::string & rawStr() const
Return the raw string of the line.
Definition: alg_line.h:299
bool isEnd() const noexcept
Test whether the function name serves as an 'end'.
Definition: alg_line.h:360
bool isValidKey(const std::string &key, const std::vector< std::string > &keys) const noexcept
Check is the key is valid. (not violating the needed sequence.)
Definition: alg_line.h:333
std::string _raw_str
Definition: alg_line.h:270
const std::string & func() const noexcept
Get the function name.
Definition: alg_line.h:274
Function Lists.
static bool isFuncNeedsEnd(const std::string &str) noexcept
Definition: functions.h:35
static bool isFuncIsEnd(const std::string &str) noexcept
Definition: functions.h:37
static bool isFuncNoEnd(const std::string &str) noexcept
Definition: functions.h:33
Global Access of Log.
str
Definition: version_bump.py:14
Parameter type.
Definition: alg_line.h:53
std::string type
Definition: alg_line.h:56
std::string value
Definition: alg_line.h:55
std::string key
Definition: alg_line.h:54
Return type.
Definition: alg_line.h:43
std::string name
Definition: alg_line.h:44
std::string type
Definition: alg_line.h:45
Utilities.
static bool contains(const T &container, const typename T::value_type value)
Definition: utils.h:133