From 1c83c3e87c1bfa2a941da03201d77586e3916006 Mon Sep 17 00:00:00 2001 From: chatlanin Date: Wed, 23 Mar 2022 22:02:17 +0300 Subject: [PATCH] add matrix and matrix log --- bin/main.cpp | 121 +++++++++------ bin/meson.build | 1 + meson.build | 12 +- src/concepts/concepts.hpp | 11 +- .../associative_ostream_iterator.hpp | 12 +- src/iterators/sequence_ostream_iterator.hpp | 12 +- src/logger/logger.cpp | 16 +- src/logger/logger.hpp | 70 +++++---- src/matrix/matrix.hpp | 143 ++++++++++++++++++ src/matrix/meson.build | 14 ++ src/meson.build | 1 + 11 files changed, 313 insertions(+), 100 deletions(-) create mode 100644 src/matrix/matrix.hpp create mode 100644 src/matrix/meson.build diff --git a/bin/main.cpp b/bin/main.cpp index b77dcc3..e8c3aff 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -3,65 +3,92 @@ #include #include #include +#include +#include #include "string/string.hpp" #include "range/range.hpp" #include "container/container.hpp" #include "logger/logger.hpp" +#include "matrix/matrix.hpp" + +#include int main(int argc, char *argv[]) { - {// ex: split_str - std::string str { "asdf,qwer,zxcv" }; - hack::string::v_str v = hack::string::split_str(str, ','); - for (const auto& c : v) hack::log()(c); - } + // {// ex: split_str + // std::string str { "asdf,qwer,zxcv" }; + // hack::string::v_str v = hack::string::split_str(str, ','); + // for (const auto& c : v) hack::log()(c); + // } + // + // {// ex: within + // hack::log()(hack::range::within(12, 34, 12, 23, 31, 17, 22, 33)); + // } + // + // {// ex: vector_multiset + // std::vector v; + // hack::container::vector_multiset(v, "asdf", "qwer", "zcv"); + // for(const auto& c : v) std::cout << c << std::endl; + // } + // + // {// ex: set_multiset + // std::set s; + // hack::container::set_multiset(s, 1, 2, 3, 3, 2, 1); + // for(const auto& c : s) std::cout << c << std::endl; + // } + // + // {// ex: log + // hack::log()(1234, "run in main", 1234); + // hack::warn(" # ")(1234, "run in main", 1234); + // hack::error(" - ")(1234, "run in main", 1234); + // + // std::string str { "hi" }; + // hack::log()(str); + // + // std::vector vs { "asdf", "qwer", "zxcv" }; + // hack::log()("vector", vs, 1, 2, 'a'); + // + // std::list ls { "asdf", "qwer", "zxcv" }; + // hack::log()(vs, ls); + // + // std::map m { { 1, "asdf" }, { 2, "qwer" }, { 3, "zxcv" } }; + // hack::log()(vs, ls, m); + // + // std::tuple tp { 1, "tuple test", false }; + // hack::log()(tp); + // } + // + // {// ex: matches + // std::vector v { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + // hack::log()(hack::container::matches(v, 2, 5, 4, 12)); + // } + // + // {// ex: vector_remove_at + // std::vector v { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + // hack::container::vector_remove_at(v, 3); + // hack::log()(v); + // } - {// ex: within - hack::log()(hack::range::within(12, 34, 12, 23, 31, 17, 22, 33)); - } + {// ex: matrix + hack::matrix m_int; + hack::matrix m_int_c; + hack::matrix m_float; - {// ex: vector_multiset - std::vector v; - hack::container::vector_multiset(v, "asdf", "qwer", "zcv"); - for(const auto& c : v) std::cout << c << std::endl; - } + m_int[0][0][0] = 123; + m_int[0][0][1] = 23; + m_int[0][0][2] = 43; + m_int_c[0][0][0] = 123; + m_float[0][0][0] = 123.123; - {// ex: set_multiset - std::set s; - hack::container::set_multiset(s, 1, 2, 3, 3, 2, 1); - for(const auto& c : s) std::cout << c << std::endl; - } + auto i = m_int[0][0][0]; + auto f = m_float[0][0][0]; - {// ex: log - hack::log()(1234, "run in main", 1234); - hack::warn(" # ")(1234, "run in main", 1234); - hack::error(" - ")(1234, "run in main", 1234); + hack::log()("m_int", i); + hack::log()("m_float", f); - std::string str { "hi" }; - hack::log()(str); - - std::vector vs { "asdf", "qwer", "zxcv" }; - hack::log()("vector", vs, 1, 2, 'a'); - - std::list ls { "asdf", "qwer", "zxcv" }; - hack::log()(vs, ls); - - std::map m { { 1, "asdf" }, { 2, "qwer" }, { 3, "zxcv" } }; - hack::log()(vs, ls, m); - - std::tuple tp { 1, "tuple test", false }; - hack::log()(tp); - } - - {// ex: matches - std::vector v { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; - hack::log()(hack::container::matches(v, 2, 5, 4, 12)); - } - - {// ex: vector_remove_at - std::vector v { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; - hack::container::vector_remove_at(v, 3); - hack::log()(v); + hack::log("")("compare (true): ", m_int == m_int_c); + hack::log("")("compare (false): ", m_int == m_float); + hack::log("")(m_int); } } diff --git a/bin/meson.build b/bin/meson.build index 0c9c1f4..ab8253e 100644 --- a/bin/meson.build +++ b/bin/meson.build @@ -5,6 +5,7 @@ deps += string_dep deps += range_dep deps += container_dep deps += logger_dep +deps += matrix_dep executable( 'hack', 'main.cpp', diff --git a/meson.build b/meson.build index d1d1702..1ad4935 100644 --- a/meson.build +++ b/meson.build @@ -7,13 +7,21 @@ project( ) add_project_arguments ( - '-pedantic', + '-Wpedantic', + '-Wshadow', '-Wno-comment', - '-Wno-gnu-zero-variadic-macro-arguments', + #'-Wno-gnu-zero-variadic-macro-arguments', '-Wunused-but-set-variable', language: 'cpp' ) +compiler = meson.get_compiler('cpp') +if compiler.get_id() == 'gcc' + message('Compiler: GCC') +elif compiler.get_id() == 'clang' + message('Compiler: LLVM/clang') +endif + args = [] deps = [] diff --git a/src/concepts/concepts.hpp b/src/concepts/concepts.hpp index d8e99b4..791fbf4 100644 --- a/src/concepts/concepts.hpp +++ b/src/concepts/concepts.hpp @@ -18,10 +18,17 @@ namespace hack::concepts std::tuple_cat(t, std::make_tuple(1, "tuple")); }; - template + template concept is_string = std::is_convertible_v; - template + template concept is_map = std::same_as> || std::same_as>; + + template + concept not_defined = !std::enable_if_t || + is_sequence_container || + is_map || + is_tuple || + is_string), bool>() == true; } diff --git a/src/iterators/associative_ostream_iterator.hpp b/src/iterators/associative_ostream_iterator.hpp index 0a550e2..432f6d5 100644 --- a/src/iterators/associative_ostream_iterator.hpp +++ b/src/iterators/associative_ostream_iterator.hpp @@ -11,18 +11,18 @@ namespace hack::iterators using ostream_type = std::basic_ostream; private: - std::basic_ostream* os; - const std::string devider = ", "; - std::size_t size; + std::basic_ostream* os_; + const std::string devider_ = ", "; + std::size_t size_; public: - associative_ostream_iterator(std::size_t size, ostream_type& s) : os { &s }, size { size } { } + associative_ostream_iterator(std::size_t size, ostream_type& os) : os_ { &os }, size_ { size } { } auto& operator=(T const& item) { - --size; + --size_; const auto& [key, value] = item; - *os << "{ " << key << ":" << value << " }" << (size != 0 ? devider : ""); + *os_ << "{ " << key << ":" << value << " }" << (size_ != 0 ? devider_ : ""); return *this; } diff --git a/src/iterators/sequence_ostream_iterator.hpp b/src/iterators/sequence_ostream_iterator.hpp index 596ebef..6c2a362 100644 --- a/src/iterators/sequence_ostream_iterator.hpp +++ b/src/iterators/sequence_ostream_iterator.hpp @@ -11,17 +11,17 @@ namespace hack::iterators using ostream_type = std::basic_ostream; private: - std::basic_ostream* os; - const std::string devider = ", "; - std::size_t size; + std::basic_ostream* os_; + const std::string devider_ = ", "; + std::size_t size_; public: - sequence_ostream_iterator(std::size_t size, ostream_type& s) : os { &s }, size { size } { } + sequence_ostream_iterator(std::size_t size, ostream_type& os) : os_ { &os }, size_ { size } { } auto& operator=(T const& item) { - --size; - *os << item << (size != 0 ? devider : ""); + --size_; + *os_ << item << (size_ != 0 ? devider_ : ""); return *this; } diff --git a/src/logger/logger.cpp b/src/logger/logger.cpp index 3981011..d080005 100644 --- a/src/logger/logger.cpp +++ b/src/logger/logger.cpp @@ -2,22 +2,22 @@ namespace hack { - std::string log::devider = " "; - int log::count = 0; + std::string log::devider_ = " "; + int log::count_ = 0; - log::log(const std::string devider, std::experimental::source_location location) : location { location } + log::log(const std::string devider, std::experimental::source_location location) : location_ { location } { - this->devider = devider; + this->devider_ = devider; } - warn::warn(const std::string devider, std::experimental::source_location location) : location { location } + warn::warn(const std::string devider, std::experimental::source_location location) : location_ { location } { - this->devider = devider; + this->devider_ = devider; } - error::error(const std::string devider, std::experimental::source_location location) : location { location } + error::error(const std::string devider, std::experimental::source_location location) : location_ { location } { - this->devider = devider; + this->devider_ = devider; } void log::print() { std::cout << std::endl; } diff --git a/src/logger/logger.hpp b/src/logger/logger.hpp index ec44a3d..f4d881a 100644 --- a/src/logger/logger.hpp +++ b/src/logger/logger.hpp @@ -6,6 +6,7 @@ #include "concepts/concepts.hpp" #include "iterators/sequence_ostream_iterator.hpp" #include "iterators/associative_ostream_iterator.hpp" +#include "matrix/matrix.hpp" namespace hack { @@ -21,17 +22,17 @@ namespace hack void operator() (const Args&... args) { std::cout << make_type_view - << location.file_name() << ":" << view::color::reset - << view::color::italic << view::color::yellow << location.function_name() << "()" << view::color::reset - << view::color::bold << view::color::blue << "[" << location.line() << "]" << view::color::reset << ": "; - count = sizeof...(Args); + << location_.file_name() << ":" << view::color::reset + << view::color::italic << view::color::yellow << location_.function_name() << "()" << view::color::reset + << view::color::bold << view::color::blue << "[" << location_.line() << "]" << view::color::reset << ": "; + count_ = sizeof...(Args); print(args...); } private: - std::experimental::source_location location; - static int count; - static std::string devider; + std::experimental::source_location location_; + static int count_; + static std::string devider_; private: static void print(); @@ -45,7 +46,7 @@ namespace hack template static void print(const T& data, const Args&... args) { - count--; + count_--; print_t(data); print(args...); } @@ -53,13 +54,13 @@ namespace hack template static void print_t(const T& data) { - std::cout << data << (count != 0 ? devider : ""); + std::cout << data << (count_ != 0 ? devider_ : ""); } template static void print_t(const T& data) { - std::cout << data << (count != 0 ? devider : ""); + std::cout << data << (count_ != 0 ? devider_ : ""); } template @@ -67,7 +68,7 @@ namespace hack { std::cout << "{ "; std::copy(data.cbegin(), data.cend(), iterators::sequence_ostream_iterator(data.size(), std::cout)); - std::cout << " }" << (count != 0 ? devider : ""); + std::cout << " }" << (count_ != 0 ? devider_ : ""); } template @@ -75,7 +76,7 @@ namespace hack { std::cout << "{"; std::copy(data.cbegin(), data.cend(), iterators::associative_ostream_iterator(data.size(), std::cout)); - std::cout << "}" << (count != 0 ? devider : ""); + std::cout << "}" << (count_ != 0 ? devider_ : ""); } template @@ -88,16 +89,27 @@ namespace hack static void print_t(const T& data, std::index_sequence) { std::cout << "{ "; - ((std::cout << std::get(data) << (idx != std::tuple_size::value - 1 ? devider : "")), ...); - std::cout << " }" << (count != 0 ? devider : ""); + ((std::cout << std::get(data) << (idx != std::tuple_size::value - 1 ? devider_ : "")), ...); + std::cout << " }" << (count_ != 0 ? devider_ : ""); } - template