add sort and max
This commit is contained in:
commit
2cc6a75ba6
2
.gitignore
vendored
Executable file
2
.gitignore
vendored
Executable file
@ -0,0 +1,2 @@
|
||||
build
|
||||
.cache
|
2
README.md
Executable file
2
README.md
Executable file
@ -0,0 +1,2 @@
|
||||
mt - набор разнообразных математических решений на с++
|
||||
Часть перенесена сюда из hack (https://gitcast.ru/chatlanin/hack)
|
33
examples/algorithms/main.cpp
Normal file
33
examples/algorithms/main.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include <vector>
|
||||
#include <forward_list>
|
||||
#include <iostream>
|
||||
|
||||
#include "mt/algorithms/sort.hpp"
|
||||
#include "mt/algorithms/max.hpp"
|
||||
|
||||
auto main(int argc, char *argv[]) -> int
|
||||
{
|
||||
{ // 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 };
|
||||
|
||||
mt::algorithms::sort(v);
|
||||
mt::algorithms::sort(l);
|
||||
|
||||
for (auto d : v)
|
||||
std::cout << d << " ";
|
||||
std::cout << std::endl;
|
||||
|
||||
for (auto d : l)
|
||||
std::cout << d << " ";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
{
|
||||
// max
|
||||
int a = 4, b = 5;
|
||||
int& c = a;
|
||||
std::cout << mt::algorithms::max(4, 5) << std::endl;
|
||||
std::cout << mt::algorithms::max(c, b) << std::endl;
|
||||
}
|
||||
}
|
35
meson.build
Executable file
35
meson.build
Executable file
@ -0,0 +1,35 @@
|
||||
project(
|
||||
meson.current_source_dir().split('/').get(-1),
|
||||
'cpp',
|
||||
version : run_command('git', 'rev-parse', '--short', 'HEAD', check: false).stdout().strip(),
|
||||
default_options : [
|
||||
'warning_level=1',
|
||||
'optimization=3',
|
||||
'cpp_std=c++20',
|
||||
])
|
||||
|
||||
add_project_arguments (
|
||||
'-Wpedantic',
|
||||
'-Wno-shadow',
|
||||
'-Wno-unused-but-set-variable',
|
||||
'-Wno-comment',
|
||||
'-Wno-unused-parameter',
|
||||
'-Wno-unused-value',
|
||||
'-Wno-unused-header',
|
||||
'-Wno-missing-field-initializers',
|
||||
'-Wno-narrowing',
|
||||
'-Wno-deprecated-enum-enum-conversion',
|
||||
'-Wno-volatile',
|
||||
'-Wno-format-security',
|
||||
'-Wno-switch',
|
||||
'-Wno-ignored-attributes',
|
||||
'-Wno-unused-variable',
|
||||
language: 'cpp'
|
||||
)
|
||||
|
||||
args = []
|
||||
inc = []
|
||||
deps = []
|
||||
|
||||
subdir('src')
|
||||
subdir('tests')
|
21
run.sh
Executable file
21
run.sh
Executable file
@ -0,0 +1,21 @@
|
||||
#!/bin/zsh
|
||||
|
||||
PROJECT_NAME=$(basename $PWD)
|
||||
|
||||
run() {
|
||||
command meson compile -C build
|
||||
if [[ -z "$1" ]]; then
|
||||
cd build
|
||||
./tests/$PROJECT_NAME
|
||||
cd ..
|
||||
else
|
||||
meson test $1 -C build
|
||||
fi
|
||||
}
|
||||
|
||||
if [ -d "build" ]; then
|
||||
run
|
||||
else
|
||||
command meson setup build
|
||||
run
|
||||
fi
|
24
src/meson.build
Executable file
24
src/meson.build
Executable file
@ -0,0 +1,24 @@
|
||||
inc += include_directories('.')
|
||||
|
||||
headers = [
|
||||
'mt/algorithms/sort.hpp',
|
||||
'mt/algorithms/max.hpp',
|
||||
]
|
||||
|
||||
sources = []
|
||||
|
||||
lib = library(
|
||||
meson.project_name(),
|
||||
include_directories : inc,
|
||||
sources: [headers, sources],
|
||||
dependencies : deps,
|
||||
cpp_args: args
|
||||
)
|
||||
|
||||
hack_dep = declare_dependency(
|
||||
include_directories: inc,
|
||||
link_with: lib,
|
||||
)
|
||||
|
||||
deps += hack_dep
|
||||
|
16
src/mt/algorithms/max.hpp
Executable file
16
src/mt/algorithms/max.hpp
Executable file
@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <type_traits>
|
||||
#include <algorithm>
|
||||
|
||||
namespace mt::algorithms
|
||||
{
|
||||
// std::common_type_t - делает сравнение по правилу тенарного оператора
|
||||
// и выводит низводящий возвращающий тип. Т.е. если в качестве одного из
|
||||
// параметров передалась ссылка, то произойдет низведление до типа ссылки
|
||||
template<typename T, typename U, typename RT = std::common_type_t<T, U>>
|
||||
inline RT max(T a, U b)
|
||||
{
|
||||
return std::max(a, b);
|
||||
}
|
||||
}
|
28
src/mt/algorithms/sort.hpp
Normal file
28
src/mt/algorithms/sort.hpp
Normal file
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace mt::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);
|
||||
}
|
||||
}
|
||||
|
||||
|
33
tests/main.cpp
Normal file
33
tests/main.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include <vector>
|
||||
#include <forward_list>
|
||||
#include <iostream>
|
||||
|
||||
#include "mt/algorithms/sort.hpp"
|
||||
#include "mt/algorithms/max.hpp"
|
||||
|
||||
auto main(int argc, char *argv[]) -> int
|
||||
{
|
||||
{ // 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 };
|
||||
|
||||
mt::algorithms::sort(v);
|
||||
mt::algorithms::sort(l);
|
||||
|
||||
for (auto d : v)
|
||||
std::cout << d << " ";
|
||||
std::cout << std::endl;
|
||||
|
||||
for (auto d : l)
|
||||
std::cout << d << " ";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
{
|
||||
// max
|
||||
int a = 4, b = 5;
|
||||
int& c = a;
|
||||
std::cout << mt::algorithms::max(4, 5) << std::endl;
|
||||
std::cout << mt::algorithms::max(c, b) << std::endl;
|
||||
}
|
||||
}
|
7
tests/meson.build
Executable file
7
tests/meson.build
Executable file
@ -0,0 +1,7 @@
|
||||
executable(
|
||||
meson.project_name(),
|
||||
'main.cpp',
|
||||
dependencies : deps,
|
||||
cpp_args: args,
|
||||
include_directories : inc
|
||||
)
|
Loading…
Reference in New Issue
Block a user