add common sort
This commit is contained in:
parent
1242044571
commit
86ac6b451d
22
bin/algorithms/main.cpp
Normal file
22
bin/algorithms/main.cpp
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
executable(
|
||||
'hack',
|
||||
'utils/main.cpp',
|
||||
'algorithms/main.cpp',
|
||||
dependencies : deps,
|
||||
cpp_args: args,
|
||||
include_directories : inc
|
||||
|
28
src/hack/algorithms/sort.hpp
Normal file
28
src/hack/algorithms/sort.hpp
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
#include <forward_list>
|
||||
|
||||
#include <concepts>
|
||||
|
||||
@ -21,6 +22,9 @@ namespace hack::concepts
|
||||
template<typename T>
|
||||
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>
|
||||
concept is_string = std::is_convertible_v<T, std::string_view>;
|
||||
|
||||
@ -37,6 +41,7 @@ namespace hack::concepts
|
||||
is_map<T> ||
|
||||
is_tuple<T> ||
|
||||
is_set<T> ||
|
||||
is_forward_list<T> ||
|
||||
std::is_array<T>() ||
|
||||
is_string<T>), bool>() == true;
|
||||
|
||||
|
@ -99,6 +99,14 @@ namespace hack
|
||||
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>
|
||||
static void print_t(const T& data)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user