diff --git a/bin/main.cpp b/bin/main.cpp index 2736396..7ac9f66 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include "string/string.hpp" @@ -10,27 +11,27 @@ 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) std::cout << c << std::endl; - // } - // - // {// ex: within - // std::cout << std::boolalpha << hack::range::within(12, 34, 12, 23, 31, 17, 22, 33) << std::endl; - // } - // - // {// ex: v_multiset - // std::vector v; - // hack::container::v_multiset(v, "asdf", "qwer", "zcv"); - // for(const auto& c : v) std::cout << c << std::endl; - // } - // - // {// ex: s_multiset - // std::set s; - // hack::container::s_multiset(s, 1, 2, 3, 3, 2, 1); - // for(const auto& c : s) std::cout << c << std::endl; - // } + {// ex: split_str + std::string str { "asdf,qwer,zxcv" }; + hack::string::v_str v = hack::string::split_str(str, ','); + for (const auto& c : v) std::cout << c << std::endl; + } + + {// ex: within + std::cout << std::boolalpha << hack::range::within(12, 34, 12, 23, 31, 17, 22, 33) << std::endl; + } + + {// 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); @@ -52,4 +53,15 @@ int main(int argc, char *argv[]) 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); + } } diff --git a/bin/meson.build b/bin/meson.build index 312c7e2..0c9c1f4 100644 --- a/bin/meson.build +++ b/bin/meson.build @@ -1,4 +1,5 @@ deps += view_dep +deps += concepts_dep deps += iterators_dep deps += string_dep deps += range_dep diff --git a/src/concepts/concepts.hpp b/src/concepts/concepts.hpp new file mode 100644 index 0000000..d8e99b4 --- /dev/null +++ b/src/concepts/concepts.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace hack::concepts +{ + template + concept is_sequence_container = std::same_as> || + std::same_as>; + + template + concept is_tuple = requires (T t) + { + std::tuple_cat(t, std::make_tuple(1, "tuple")); + }; + + template + concept is_string = std::is_convertible_v; + + template + concept is_map = std::same_as> || + std::same_as>; +} diff --git a/src/concepts/meson.build b/src/concepts/meson.build new file mode 100644 index 0000000..acfdb02 --- /dev/null +++ b/src/concepts/meson.build @@ -0,0 +1,14 @@ +headers = ['concepts.hpp'] +sources = [] + +lib = library( + 'concepts', + include_directories : inc, + install : true, + sources: [headers, sources] +) + +concepts_dep = declare_dependency( + include_directories: inc, + link_with: lib +) diff --git a/src/container/container.hpp b/src/container/container.hpp index fbb47b6..4f03b51 100644 --- a/src/container/container.hpp +++ b/src/container/container.hpp @@ -1,7 +1,6 @@ #pragma once -#include -#include +#include namespace hack::container { @@ -18,4 +17,33 @@ namespace hack::container { (r.insert(args), ...); } + + template + int matches(Range r, Args... args) + { + return (std::count(std::cbegin(r), std::cend(r), args) + ...); + } + + // ----------------------------------------------------- + // remove vector element if vector sorting no important + template + void vector_remove_at(std::vector& v, std::size_t idx) + { + if (idx < v.size()) + { + v[idx] = std::move(v.back()); + v.pop_back(); + } + } + + template + void vector_remove_at(std::vector& v, typename std::vector::iterator it) + { + if (it != v.end()) + { + *it = std::move(v.back()); + v.pop_back(); + } + } + // ----------------------------------------------------- } diff --git a/src/logger/logger.hpp b/src/logger/logger.hpp index 4aa52fb..d6755f2 100644 --- a/src/logger/logger.hpp +++ b/src/logger/logger.hpp @@ -1,13 +1,9 @@ #pragma once -#include #include -#include -#include -#include -#include #include "view/color.hpp" +#include "concepts/concepts.hpp" #include "iterators/sequence_ostream_iterator.hpp" #include "iterators/associative_ostream_iterator.hpp" @@ -113,6 +109,14 @@ namespace hack std::cout << " }" << (count != 0 ? devider : ""); } + template