add common sort

This commit is contained in:
chatlanin 2023-08-14 21:11:39 +03:00
parent 1242044571
commit 86ac6b451d
5 changed files with 64 additions and 1 deletions

22
bin/algorithms/main.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <vector>
#include <forward_list>
#include "hack/logger/logger.hpp"
#include "hack/algorithms/sort.hpp"
auto main(int argc, char *argv[]) -> int
{
{// ex: sort
std::vector<int> v { 4, 4, 6, 1, 4, 3, 2 };
std::forward_list<int> 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);
}
}

View File

@ -1,6 +1,6 @@
executable( executable(
'hack', 'hack',
'utils/main.cpp', 'algorithms/main.cpp',
dependencies : deps, dependencies : deps,
cpp_args: args, cpp_args: args,
include_directories : inc include_directories : inc

View File

@ -0,0 +1,28 @@
#pragma once
#include <algorithm>
namespace hack::algorithms
{
// общая сортировка диапозонов, ку кого-то есть своя локалная сортировка, а у кого-то
// нет. А тут чтоб не реализовывать, есть общая.
template<typename Range>
void sort_imp(Range& r, long)
{
std::sort(std::begin(r), std::end(r));
}
template<typename Range, typename = decltype(std::declval<Range&>().sort())>
auto sort_imp(Range& r, int) -> void
{
r.sort();
}
template<typename Range>
void sort(Range& r)
{
sort_imp(r, 0);
}
}

View File

@ -6,6 +6,7 @@
#include <map> #include <map>
#include <set> #include <set>
#include <unordered_map> #include <unordered_map>
#include <forward_list>
#include <concepts> #include <concepts>
@ -21,6 +22,9 @@ namespace hack::concepts
template<typename T> template<typename T>
concept is_set = std::same_as<T, std::set<typename T::key_type, typename T::key_compare, typename T::allocator_type>>; concept is_set = std::same_as<T, std::set<typename T::key_type, typename T::key_compare, typename T::allocator_type>>;
template<typename T>
concept is_forward_list = std::same_as<T, std::forward_list<typename T::value_type>>;
template<typename T> template<typename T>
concept is_string = std::is_convertible_v<T, std::string_view>; concept is_string = std::is_convertible_v<T, std::string_view>;
@ -37,6 +41,7 @@ namespace hack::concepts
is_map<T> || is_map<T> ||
is_tuple<T> || is_tuple<T> ||
is_set<T> || is_set<T> ||
is_forward_list<T> ||
std::is_array<T>() || std::is_array<T>() ||
is_string<T>), bool>() == true; is_string<T>), bool>() == true;

View File

@ -99,6 +99,14 @@ namespace hack
std::cout << " }" << (count != 0 ? devider : ""); std::cout << " }" << (count != 0 ? devider : "");
} }
template<concepts::is_forward_list T>
static void print_t(const T& data)
{
std::cout << "{ ";
std::copy(data.cbegin(), data.cend(), iterators::sequence_ostream_iterator<typename T::value_type>(std::distance(data.cbegin(), data.cend()), std::cout));
std::cout << " }" << (count != 0 ? devider : "");
}
template<concepts::is_map T> template<concepts::is_map T>
static void print_t(const T& data) static void print_t(const T& data)
{ {