diff options
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/general_utils.cpp | 1 | ||||
| -rw-r--r-- | src/utils/general_utils.h | 35 | ||||
| -rw-r--r-- | src/utils/pqueue.h | 35 | ||||
| -rw-r--r-- | src/utils/strutils.h | 79 | ||||
| -rw-r--r-- | src/utils/sysc_utils.cpp | 70 | ||||
| -rw-r--r-- | src/utils/sysc_utils.h | 67 |
6 files changed, 287 insertions, 0 deletions
diff --git a/src/utils/general_utils.cpp b/src/utils/general_utils.cpp new file mode 100644 index 0000000..0ba55be --- /dev/null +++ b/src/utils/general_utils.cpp @@ -0,0 +1 @@ +#include "general_utils.h" diff --git a/src/utils/general_utils.h b/src/utils/general_utils.h new file mode 100644 index 0000000..14757a9 --- /dev/null +++ b/src/utils/general_utils.h @@ -0,0 +1,35 @@ +#pragma once + +#include <cmath> +#include <vector> +#include <iostream> + +using std::abs; +using std::vector; +using std::cerr; +using std::endl; + +template <typename T1, typename T2> +inline bool is_close(const T1& x1, const T2& x2, const double &precision = 1e-12) +{ + return abs(x1-x2) < precision; +} + +//double linspace(double minval, double maxval, double npoints); + +template <typename T> +vector<T> range(const T &minval, const T &maxval, const T &step) +{ + vector<T> ret; + if (maxval <= minval || step <= 0) + { + cerr << "Invalid range parameters" << endl; + // because i don't have time to cover all cases + exit(1); + } + int n = floor((maxval - minval) / step); + ret.reserve(n); + for (T val = minval; val < maxval; val += step) + ret.push_back(val); + return ret; +}
\ No newline at end of file diff --git a/src/utils/pqueue.h b/src/utils/pqueue.h new file mode 100644 index 0000000..4ec5dfd --- /dev/null +++ b/src/utils/pqueue.h @@ -0,0 +1,35 @@ +#pragma once + +#include <queue> +#include <vector> + +using std::priority_queue; +using std::vector; +using std::greater; +using std::size_t; + +template <class T> +class PQueue: public priority_queue<T, vector<T>, greater<T>>{ +public: + public: + typedef typename + std::priority_queue<T>::container_type::iterator iterator; + typedef typename + std::priority_queue<T>::container_type::const_iterator const_iterator; + + iterator begin() { + return this->c.begin(); + } + iterator end() { + return this->c.end(); + } + const_iterator cbegin() const { + return this->c.cbegin(); + } + const_iterator cend() const { + return this->c.cend(); + } + size_t size() const { + return this->c.size(); + } +}; diff --git a/src/utils/strutils.h b/src/utils/strutils.h new file mode 100644 index 0000000..94e7ab7 --- /dev/null +++ b/src/utils/strutils.h @@ -0,0 +1,79 @@ +#pragma once + +#include <algorithm> // std::transform +#include <string> + +namespace strutils { +// trim from start (in place) +inline void ltrim(std::string &s) +{ + s.erase(s.begin(), + std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); })); +} + +// trim from end (in place) +inline void rtrim(std::string &s) +{ + s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }) + .base(), + s.end()); +} + +// trim from both ends (in place) +inline void trim(std::string &s) +{ + ltrim(s); + rtrim(s); +} + +// trim from start (copying) +inline std::string ltrim_copy(std::string s) +{ + ltrim(s); + return s; +} + +// trim from end (copying) +inline std::string rtrim_copy(std::string s) +{ + rtrim(s); + return s; +} + +// trim from both ends (copying) +inline std::string trim_copy(std::string s) +{ + trim(s); + return s; +} +// trim from end (in place) +inline void rtrim_comments(std::string &s, const std::string &comment_tokens) +{ + size_t pos = s.find_first_of(comment_tokens); + if (pos != s.npos) + s.erase(pos); +} +inline void toupper(std::string &s) +{ + std::transform(s.begin(), s.end(), s.begin(), ::toupper); +} + +inline bool strcontains(const std::string &s, const std::string &sub) +{ + return s.find(sub) != std::string::npos; +} + +inline bool streq(const std::string &s1, const std::string &s2) +{ + return s1 == s2; +} + +inline bool iequals(const std::string& a, const std::string& b) +{ + return std::equal(a.begin(), a.end(), + b.begin(), b.end(), + [](char a, char b) { + return tolower(a) == tolower(b); + }); +} +}; diff --git a/src/utils/sysc_utils.cpp b/src/utils/sysc_utils.cpp new file mode 100644 index 0000000..9b63632 --- /dev/null +++ b/src/utils/sysc_utils.cpp @@ -0,0 +1,70 @@ +#include <sysc_utils.h> + +#include <string> + + +inline bool sc_object_is_module(const sc_object &obj) { + return strcmp(obj.kind(), "sc_module") == 0; +} + +// Return vector containing all children objects (expanded) of obj +set<sc_object *> sc_collect_children_object(sc_object* obj) +{ + auto v = obj->get_child_objects(); + auto children = set<sc_object *>(v.begin(), v.end()); + auto all_children = children; + for (auto child : children) + if ( child ) + { + auto children_children = sc_collect_children_object(child); + for (auto child_child : children_children) + all_children.insert(child_child); + } + return all_children; +} + +// Return vector containing all children objects (expanded) of obj +set<sc_module *> sc_collect_children_module(sc_module* obj) +{ + auto v = obj->get_child_objects(); + auto children = set<sc_object *>(v.begin(), v.end()); + set<sc_module *> all_children; + for (auto obj: children) + { + auto mod = dynamic_cast<sc_module *>(obj); + if(mod) { + all_children.insert(mod); + auto children2 = sc_collect_children_module(mod); + all_children.insert(children2.begin(), children2.end()); + } + } + return all_children; +} + +set<sc_module *> sc_get_all_module() { + set<sc_module *> all_modules; + + for ( auto obj : sc_get_top_level_objects() ) { + auto mod = dynamic_cast<sc_module *>(obj); + if (mod) + { + all_modules.insert(mod); + auto all_children = sc_collect_children_module(mod); + for (auto child : all_children) + all_modules.insert(child); + } + } + return all_modules; +} + +set<sc_object *> sc_get_all_object() { + set<sc_object *> all_objects; + + for ( auto obj : sc_get_top_level_objects() ) { + all_objects.insert(obj); + auto all_children = sc_collect_children_object(obj); + for (auto child : all_children) + all_objects.insert(child); + } + return all_objects; +}
\ No newline at end of file diff --git a/src/utils/sysc_utils.h b/src/utils/sysc_utils.h new file mode 100644 index 0000000..be275a0 --- /dev/null +++ b/src/utils/sysc_utils.h @@ -0,0 +1,67 @@ +#pragma once + +#include <systemc.h> + +#include <vector> +#include <set> + +using std::vector; +using std::set; + +bool sc_object_is_module(const sc_object &obj); + +// Return vector containing all children objects (expanded) of obj +set<sc_object *> sc_collect_children_object(sc_object* obj); + +// Return vector containing all children objects (expanded) of obj +set<sc_module *> sc_collect_children_module(sc_module* obj); + +// Return vector containing all sc_module registered with engine +set<sc_module *> sc_get_all_module(); + +// Return vector containing all sc_object registered with engine +set<sc_object *> sc_get_all_object(); + +// Return vector containing all sc_module of a certain type +template<typename T> +set<T *> sc_get_all_module_by_type(); + +// Return vector containing all sc_object of a certain type +template<typename T> +set<T *> sc_get_all_object_by_type(); + +//////////////////////////////////////// +// Definition of template functions +//////////////////////////////////////// + + +template<typename T> +set<T *> sc_get_all_module_by_type() { + set<sc_module *> all_modules = sc_get_all_module(); + set<T *> all_requested; + for (auto mod : all_modules) + { + // cout << mod->name() << endl; + auto requested = dynamic_cast<T *>(mod); + if (requested) + all_requested.insert(requested); + } + return all_requested; +} + +template<typename T> +set<T *> sc_get_all_object_by_type() { + set<sc_object *> all_objects = sc_get_all_object(); + set<T *> all_requested; + + for (auto obj : all_objects) + { + //cout << obj->name() << endl; + auto requested = dynamic_cast<T *>(obj); + if (requested) { + //cout << "\\__( WE WANT IT )" << endl; + all_requested.insert(requested); + } + } + return all_requested; +}
\ No newline at end of file |
