From 86ac6b451d0419e922d290d55c3b2310abd53c02 Mon Sep 17 00:00:00 2001 From: chatlanin Date: Mon, 14 Aug 2023 21:11:39 +0300 Subject: [PATCH] add common sort --- bin/algorithms/main.cpp | 22 ++++++++++++++++++++++ bin/meson.build | 2 +- src/hack/algorithms/sort.hpp | 28 ++++++++++++++++++++++++++++ src/hack/concepts/concepts.hpp | 5 +++++ src/hack/logger/logger.hpp | 8 ++++++++ 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 bin/algorithms/main.cpp create mode 100644 src/hack/algorithms/sort.hpp diff --git a/bin/algorithms/main.cpp b/bin/algorithms/main.cpp new file mode 100644 index 0000000..2373317 --- /dev/null +++ b/bin/algorithms/main.cpp @@ -0,0 +1,22 @@ +#include +#include + +#include "hack/logger/logger.hpp" +#include "hack/algorithms/sort.hpp" + +auto main(int argc, char *argv[]) -> int +{ + {// ex: sort + std::vector v { 4, 4, 6, 1, 4, 3, 2 }; + std::forward_list l { 8, 7, 5, 9, 0, 1, 3, 2, 6, 4 }; + + hack::algorithms::sort(v); + hack::algorithms::sort(l); + + hack::log log; + log(v); + log(l); + } +} + + diff --git a/bin/meson.build b/bin/meson.build index 658b058..6edbfe1 100755 --- a/bin/meson.build +++ b/bin/meson.build @@ -1,6 +1,6 @@ executable( 'hack', - 'utils/main.cpp', + 'algorithms/main.cpp', dependencies : deps, cpp_args: args, include_directories : inc diff --git a/src/hack/algorithms/sort.hpp b/src/hack/algorithms/sort.hpp new file mode 100644 index 0000000..5f78061 --- /dev/null +++ b/src/hack/algorithms/sort.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include + +namespace hack::algorithms +{ + // общая сортировка диапозонов, ку кого-то есть своя локалная сортировка, а у кого-то + // нет. А тут чтоб не реализовывать, есть общая. + template + void sort_imp(Range& r, long) + { + std::sort(std::begin(r), std::end(r)); + } + + template().sort())> + auto sort_imp(Range& r, int) -> void + { + r.sort(); + } + + template + void sort(Range& r) + { + sort_imp(r, 0); + } +} + + diff --git a/src/hack/concepts/concepts.hpp b/src/hack/concepts/concepts.hpp index c365927..67f5349 100755 --- a/src/hack/concepts/concepts.hpp +++ b/src/hack/concepts/concepts.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -21,6 +22,9 @@ namespace hack::concepts template concept is_set = std::same_as>; + template + concept is_forward_list = std::same_as>; + template concept is_string = std::is_convertible_v; @@ -37,6 +41,7 @@ namespace hack::concepts is_map || is_tuple || is_set || + is_forward_list || std::is_array() || is_string), bool>() == true; diff --git a/src/hack/logger/logger.hpp b/src/hack/logger/logger.hpp index 4248f10..b707cbb 100755 --- a/src/hack/logger/logger.hpp +++ b/src/hack/logger/logger.hpp @@ -99,6 +99,14 @@ namespace hack std::cout << " }" << (count != 0 ? devider : ""); } + template + static void print_t(const T& data) + { + std::cout << "{ "; + std::copy(data.cbegin(), data.cend(), iterators::sequence_ostream_iterator(std::distance(data.cbegin(), data.cend()), std::cout)); + std::cout << " }" << (count != 0 ? devider : ""); + } + template static void print_t(const T& data) {