From cac1c694c19c1e01d70c87f3d5aa5544617061f0 Mon Sep 17 00:00:00 2001 From: chatlanin Date: Sun, 27 Mar 2022 12:13:50 +0300 Subject: [PATCH] add str_concat and func_memory --- bin/main.cpp | 90 ++++++++++++++++++----------- meson.build | 12 ++++ src/concepts/meson.build | 1 - src/container/meson.build | 1 - src/iterators/meson.build | 1 - src/logger/meson.build | 1 - src/math/matrix.hpp | 19 ++++-- src/math/meson.build | 1 - src/math/vector.hpp | 4 +- src/range/meson.build | 1 - src/string/meson.build | 3 +- src/string/string.hpp | 3 + src/string/string_concat_helper.hpp | 79 +++++++++++++++++++++++++ src/utils/meson.build | 1 - src/utils/utils.hpp | 46 +++++++++++---- src/view/meson.build | 1 - 16 files changed, 201 insertions(+), 63 deletions(-) create mode 100644 src/string/string_concat_helper.hpp diff --git a/bin/main.cpp b/bin/main.cpp index fe18ce6..d0ed705 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -5,41 +5,49 @@ #include #include #include +#include #include "string/string.hpp" +#include "string/string_concat_helper.hpp" #include "range/range.hpp" #include "container/container.hpp" #include "logger/logger.hpp" #include "math/matrix.hpp" #include "math/vector.hpp" +#include "utils/utils.hpp" -#include +// for example +int f(int a) +{ + hack::log()("f implementatioln"); + return ++a; +} int main(int argc, char *argv[]) { - // {// ex: split_str + // {// ex: string::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 + // {// ex: renge::within // hack::log()(hack::range::within(12, 34, 12, 23, 31, 17, 22, 33)); // } // - // {// ex: vector_multiset + // {// ex: container::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 + // {// ex: container::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 + // {// ex: logger::log // hack::log()(1234, "run in main", 1234); // hack::warn(" # ")(1234, "run in main", 1234); // hack::error(" - ")(1234, "run in main", 1234); @@ -60,18 +68,18 @@ int main(int argc, char *argv[]) // hack::log()(tp); // } // - // {// ex: matches + // {// ex: container::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 + // {// ex: container::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: matrix + // {// ex: math::matrix // hack::matrix m_int; // hack::matrix m_int_c { { 2, 3, 4, 123 }, { 0, 2, 4, 555 } }; // hack::matrix m_float; @@ -85,7 +93,7 @@ int main(int argc, char *argv[]) // auto i = m_int[0][0][0]; // auto f = m_float[0][0][0]; // - // hack::log()("m_int", i); + // hack::log()("m_int", i); // hack::log()("m_float", f); // hack::log()("empty", m_float[123][22][33]); // @@ -100,7 +108,7 @@ int main(int argc, char *argv[]) // m_int = std::forward>(m_int_c); // hack::log("")("moved data: ", m_int); // - // hack::matrix m_int_d = m_int; + // hack::matrix m_int_d = m_int; // hack::log("")("copy data: ", m_int_d); // // hack::matrix m_int_cd { { 2, 3, 4, 3 }, { 0, 2, 4, 5 } }; @@ -108,29 +116,43 @@ int main(int argc, char *argv[]) // m_int_cd = m_int; // hack::log("")("copy data: ", m_int_cd); // } + // + // {// ex: math::vector + // hack::vector v3_1 { 8, 4, 9, }; + // hack::vector v3_2 { 1, 2, 3, }; + // hack::log()(v3_1.get_value()); + // hack::log()(v3_2.get_value()); + // + // v3_1 = v3_2; + // + // hack::log()(v3_1.get_value()); + // hack::log()(v3_2.get_value()); + // + // hack::log()("length 3", v3_2.length()); + // + // hack::vector v2_1 { 11, 22 }; + // hack::log()("length 2", v2_1.length()); + // + // hack::vector lerp_1 { 1, 2, 3 }; + // hack::vector lerp_2 { 5, 6, 7 }; + // hack::log()("lerp", lerp_1.lerp(lerp_2, 0.75f)); + // + // auto [x, y, z] = lerp_1.get_value(); + // hack::log()("get", x, y, z); + // hack::log()("get", lerp_1.x()); + // } + // + // {// ex: utils::func_memory + // auto cach_f = hack::utils::func_memory(f); + // hack::log()("result 1", cach_f(12)); + // hack::log()("result 2", cach_f(12)); + // } - {// ex: vector - hack::vector v3_1 { 8, 4, 9, }; - hack::vector v3_2 { 1, 2, 3, }; - hack::log()(v3_1.get_value()); - hack::log()(v3_2.get_value()); - - v3_1 = v3_2; - - hack::log()(v3_1.get_value()); - hack::log()(v3_2.get_value()); - - hack::log()("length 3", v3_2.length()); - - hack::vector v2_1 { 11, 22 }; - hack::log()("length 2", v2_1.length()); - - hack::vector lerp_1 { 1, 2, 3 }; - hack::vector lerp_2 { 5, 6, 7 }; - hack::log()("lerp", lerp_1.lerp(lerp_2, 0.75f)); - - auto [x, y, z] = lerp_1.get_value(); - hack::log()("get", x, y, z); - hack::log()("get", lerp_1.x()); + {// ex: string::str_concat + std::string name = "tro"; + std::string surname = "lolo"; + const auto full_name = hack::string::str_concat + name + ", " + surname; + hack::log()(full_name); + hack::log()(hack::string::str_concat + "super", + "string"); } } diff --git a/meson.build b/meson.build index 1ad4935..cf5c2f6 100644 --- a/meson.build +++ b/meson.build @@ -28,6 +28,18 @@ deps = [] inc = [] inc += include_directories('.') +conf = configuration_data() +check_headers = [ + ['ncurses.h', 'HAVE_NCURSES_H'], + ['curses.h', 'HAVE_CURSES_H'], +] + +foreach h : check_headers + if compiler.has_header(h.get(0)) + conf.set(h.get(1), 1) + endif +endforeach + subdir('src') subdir('bin') subdir('tests') diff --git a/src/concepts/meson.build b/src/concepts/meson.build index acfdb02..1c8b9d1 100644 --- a/src/concepts/meson.build +++ b/src/concepts/meson.build @@ -4,7 +4,6 @@ sources = [] lib = library( 'concepts', include_directories : inc, - install : true, sources: [headers, sources] ) diff --git a/src/container/meson.build b/src/container/meson.build index 29d3539..1375212 100644 --- a/src/container/meson.build +++ b/src/container/meson.build @@ -4,7 +4,6 @@ sources = ['container.cpp'] lib = library( 'container', include_directories : inc, - install : true, sources: [headers, sources] ) diff --git a/src/iterators/meson.build b/src/iterators/meson.build index 8457fbe..c554717 100644 --- a/src/iterators/meson.build +++ b/src/iterators/meson.build @@ -4,7 +4,6 @@ sources = [] lib = library( 'iterators', include_directories : inc, - install : true, sources: [headers, sources] ) diff --git a/src/logger/meson.build b/src/logger/meson.build index 7a05b37..398973c 100644 --- a/src/logger/meson.build +++ b/src/logger/meson.build @@ -4,7 +4,6 @@ sources = ['logger.cpp'] lib = library( 'logger', include_directories : inc, - install : true, sources: [headers, sources] ) diff --git a/src/math/matrix.hpp b/src/math/matrix.hpp index 38c0968..86a0249 100644 --- a/src/math/matrix.hpp +++ b/src/math/matrix.hpp @@ -3,13 +3,22 @@ #include #include #include -#include #include -#include "utils/utils.hpp" - namespace hack::matrix_utils { + template + struct generate_tuple + { + using type = decltype(std::tuple_cat(typename generate_tuple::type{}, std::make_tuple(T{}))); + }; + + template + struct generate_tuple + { + using type = std::tuple; + }; + template class proxy { @@ -17,7 +26,6 @@ namespace hack::matrix_utils public: proxy(const std::weak_ptr& local_storage, const index_t& index) : local_storage_ { local_storage }, index_ { index } {}; - ~proxy() = default; auto operator[](std::size_t index) const { @@ -58,7 +66,7 @@ namespace hack template class matrix { - using index_t = typename utils::generate_tuple::type; + using index_t = typename matrix_utils::generate_tuple::type; using vector_t = decltype(std::tuple_cat(index_t{}, std::make_tuple(T{}))); using index_data_t = std::vector; @@ -70,7 +78,6 @@ namespace hack } matrix(matrix& mt) noexcept : local_storage_ { mt.local_storage_ } { } matrix(matrix&& mt) noexcept : local_storage_ { mt.local_storage_ } { } - ~matrix() = default; matrix& operator=(matrix&& mt) { diff --git a/src/math/meson.build b/src/math/meson.build index af30931..07f548d 100644 --- a/src/math/meson.build +++ b/src/math/meson.build @@ -4,7 +4,6 @@ sources = [] lib = library( 'math', include_directories : inc, - install : true, sources: [headers, sources] ) diff --git a/src/math/vector.hpp b/src/math/vector.hpp index b596cce..578ae17 100644 --- a/src/math/vector.hpp +++ b/src/math/vector.hpp @@ -53,13 +53,13 @@ namespace hack auto z() const { - if (std::tuple_size{} < 3) throw std::out_of_range("You try get no valid vector date!"); + if (std::tuple_size{} < 3) throw std::out_of_range("You try get no valid vector data!"); return std::get<2>(value_); } auto w() const { - if (std::tuple_size{} < 4) throw std::out_of_range("You try get no valid vector date!"); + if (std::tuple_size{} < 4) throw std::out_of_range("You try get no valid vector data!"); return std::get<3>(value_); } diff --git a/src/range/meson.build b/src/range/meson.build index 94717ab..0a870ad 100644 --- a/src/range/meson.build +++ b/src/range/meson.build @@ -4,7 +4,6 @@ sources = ['range.cpp'] lib = library( 'range', include_directories : inc, - install : true, sources: [headers, sources] ) diff --git a/src/string/meson.build b/src/string/meson.build index a1f161d..f246a94 100644 --- a/src/string/meson.build +++ b/src/string/meson.build @@ -1,10 +1,9 @@ -headers = ['string.hpp'] +headers = ['string.hpp', 'string_concat_helper.hpp'] sources = ['string.cpp'] lib = library( 'string', include_directories : inc, - install : true, sources: [headers, sources] ) diff --git a/src/string/string.hpp b/src/string/string.hpp index 81d21be..952d286 100644 --- a/src/string/string.hpp +++ b/src/string/string.hpp @@ -2,6 +2,9 @@ #include #include +#include +#include + namespace hack::string { diff --git a/src/string/string_concat_helper.hpp b/src/string/string_concat_helper.hpp new file mode 100644 index 0000000..73b0748 --- /dev/null +++ b/src/string/string_concat_helper.hpp @@ -0,0 +1,79 @@ +#pragma once + +#include +#include +#include +#include + +namespace hack::string +{ + template + class string_concat_helper; + + template + class string_concat_helper + { + using string_part = string_concat_helper; + using string_long = string_concat_helper; + + private: + const String& data; + string_part tail; + + public: + string_concat_helper(const String& data_, string_part tail_) : data { data_ } , tail { tail_ } {} + + int size() const + { + return data.size() + tail.size(); + } + + template + void save(It end) const + { + const auto begin = end - data.size(); + std::copy(data.cbegin(), data.cend(), begin); + tail.save(begin); + } + + operator std::string() const + { + std::string result(size(), '\0'); + save(result.end()); + return result; + } + + string_long operator+(const std::string& other) const + { + return string_long(other, *this); + } + }; + + template <> + class string_concat_helper<> + { + using string_part = string_concat_helper; + + public: + int size() const + { + return 0; + } + + template + void save(It) const { } + + string_part operator+(const std::string& other) const + { + return string_part(other, *this); + } + }; + + template + Stream& operator<<(Stream& stream, const string_concat_helper strings) + { + return stream << static_cast(strings); + } + + inline string_concat_helper<> str_concat; +} diff --git a/src/utils/meson.build b/src/utils/meson.build index acd61a7..0945475 100644 --- a/src/utils/meson.build +++ b/src/utils/meson.build @@ -4,7 +4,6 @@ sources = [] lib = library( 'utils', include_directories : inc, - install : true, sources: [headers, sources] ) diff --git a/src/utils/utils.hpp b/src/utils/utils.hpp index 1cdbb2e..3bbc590 100644 --- a/src/utils/utils.hpp +++ b/src/utils/utils.hpp @@ -1,19 +1,43 @@ #pragma once -#include -#include +#include namespace hack::utils { - template - struct generate_tuple + template + auto func_memory(Result (*f)(Args...)) { - using type = decltype(std::tuple_cat(typename generate_tuple::type{}, std::make_tuple(T{}))); - }; + std::map, Result> cache; - template - struct generate_tuple - { - using type = std::tuple; - }; + return [f, cache](Args... args) mutable -> Result + { + const auto key = std::make_tuple(args...); + const auto cached = cache.find(key); + + if(cached == cache.end()) + { + auto result = f(args...); + cache[key] = result; + return result; + } + return cached->second; + }; + } } + + + // std::map, Result> cache; + // + // return [f, cache](Args... args) mutable -> Result + // { + // const auto args_tuple = std::make_tuple(args...); + // const auto cached = cache.find(args_tuple); + // + // if (cached == cache.end()) + // { + // auto result = f(args...); + // cache[args_tuple] = result; + // return result; + // } + // else + // return cached->second diff --git a/src/view/meson.build b/src/view/meson.build index 22e3044..c24c709 100644 --- a/src/view/meson.build +++ b/src/view/meson.build @@ -4,7 +4,6 @@ sources = [] lib = library( 'view', include_directories : inc, - install : true, sources: [headers, sources] )