From ff9b8bb838ecdfbfc1dc81038fcf3b2a87636982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Zrounba?= <6691770+clement-z@users.noreply.github.com> Date: Sat, 30 Sep 2023 23:06:01 +0200 Subject: Initial release --- src/utils/general_utils.cpp | 1 + src/utils/general_utils.h | 35 ++++++++++++++++++++ src/utils/pqueue.h | 35 ++++++++++++++++++++ src/utils/strutils.h | 79 +++++++++++++++++++++++++++++++++++++++++++++ src/utils/sysc_utils.cpp | 70 +++++++++++++++++++++++++++++++++++++++ src/utils/sysc_utils.h | 67 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 287 insertions(+) create mode 100644 src/utils/general_utils.cpp create mode 100644 src/utils/general_utils.h create mode 100644 src/utils/pqueue.h create mode 100644 src/utils/strutils.h create mode 100644 src/utils/sysc_utils.cpp create mode 100644 src/utils/sysc_utils.h (limited to 'src/utils') 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 +#include +#include + +using std::abs; +using std::vector; +using std::cerr; +using std::endl; + +template +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 +vector range(const T &minval, const T &maxval, const T &step) +{ + vector 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 +#include + +using std::priority_queue; +using std::vector; +using std::greater; +using std::size_t; + +template +class PQueue: public priority_queue, greater>{ +public: + public: + typedef typename + std::priority_queue::container_type::iterator iterator; + typedef typename + std::priority_queue::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 // std::transform +#include + +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 + +#include + + +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_collect_children_object(sc_object* obj) +{ + auto v = obj->get_child_objects(); + auto children = set(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_collect_children_module(sc_module* obj) +{ + auto v = obj->get_child_objects(); + auto children = set(v.begin(), v.end()); + set all_children; + for (auto obj: children) + { + auto mod = dynamic_cast(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_get_all_module() { + set all_modules; + + for ( auto obj : sc_get_top_level_objects() ) { + auto mod = dynamic_cast(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_get_all_object() { + set 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 + +#include +#include + +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_collect_children_object(sc_object* obj); + +// Return vector containing all children objects (expanded) of obj +set sc_collect_children_module(sc_module* obj); + +// Return vector containing all sc_module registered with engine +set sc_get_all_module(); + +// Return vector containing all sc_object registered with engine +set sc_get_all_object(); + +// Return vector containing all sc_module of a certain type +template +set sc_get_all_module_by_type(); + +// Return vector containing all sc_object of a certain type +template +set sc_get_all_object_by_type(); + +//////////////////////////////////////// +// Definition of template functions +//////////////////////////////////////// + + +template +set sc_get_all_module_by_type() { + set all_modules = sc_get_all_module(); + set all_requested; + for (auto mod : all_modules) + { + // cout << mod->name() << endl; + auto requested = dynamic_cast(mod); + if (requested) + all_requested.insert(requested); + } + return all_requested; +} + +template +set sc_get_all_object_by_type() { + set all_objects = sc_get_all_object(); + set all_requested; + + for (auto obj : all_objects) + { + //cout << obj->name() << endl; + auto requested = dynamic_cast(obj); + if (requested) { + //cout << "\\__( WE WANT IT )" << endl; + all_requested.insert(requested); + } + } + return all_requested; +} \ No newline at end of file -- cgit v1.2.3