From 2cc6a75ba6ef978e073609627f5b5498d628d1e1 Mon Sep 17 00:00:00 2001 From: chatlanin Date: Tue, 20 Aug 2024 13:01:31 +0300 Subject: [PATCH] add sort and max --- .gitignore | 2 ++ README.md | 2 ++ examples/algorithms/main.cpp | 33 +++++++++++++++++++++++++++++++++ meson.build | 35 +++++++++++++++++++++++++++++++++++ run.sh | 21 +++++++++++++++++++++ src/meson.build | 24 ++++++++++++++++++++++++ src/mt/algorithms/max.hpp | 16 ++++++++++++++++ src/mt/algorithms/sort.hpp | 28 ++++++++++++++++++++++++++++ tests/main.cpp | 33 +++++++++++++++++++++++++++++++++ tests/meson.build | 7 +++++++ 10 files changed, 201 insertions(+) create mode 100755 .gitignore create mode 100755 README.md create mode 100644 examples/algorithms/main.cpp create mode 100755 meson.build create mode 100755 run.sh create mode 100755 src/meson.build create mode 100755 src/mt/algorithms/max.hpp create mode 100644 src/mt/algorithms/sort.hpp create mode 100644 tests/main.cpp create mode 100755 tests/meson.build diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..9785597 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build +.cache diff --git a/README.md b/README.md new file mode 100755 index 0000000..412f6db --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +mt - набор разнообразных математических решений на с++ +Часть перенесена сюда из hack (https://gitcast.ru/chatlanin/hack) diff --git a/examples/algorithms/main.cpp b/examples/algorithms/main.cpp new file mode 100644 index 0000000..56c99b6 --- /dev/null +++ b/examples/algorithms/main.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +#include "mt/algorithms/sort.hpp" +#include "mt/algorithms/max.hpp" + +auto main(int argc, char *argv[]) -> int +{ + { // 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 }; + + 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; + } +} diff --git a/meson.build b/meson.build new file mode 100755 index 0000000..1ac1ae5 --- /dev/null +++ b/meson.build @@ -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') diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..d0da7f1 --- /dev/null +++ b/run.sh @@ -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 diff --git a/src/meson.build b/src/meson.build new file mode 100755 index 0000000..6c39878 --- /dev/null +++ b/src/meson.build @@ -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 + diff --git a/src/mt/algorithms/max.hpp b/src/mt/algorithms/max.hpp new file mode 100755 index 0000000..32f99f7 --- /dev/null +++ b/src/mt/algorithms/max.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +namespace mt::algorithms +{ + // std::common_type_t - делает сравнение по правилу тенарного оператора + // и выводит низводящий возвращающий тип. Т.е. если в качестве одного из + // параметров передалась ссылка, то произойдет низведление до типа ссылки + template> + inline RT max(T a, U b) + { + return std::max(a, b); + } +} diff --git a/src/mt/algorithms/sort.hpp b/src/mt/algorithms/sort.hpp new file mode 100644 index 0000000..0701fe2 --- /dev/null +++ b/src/mt/algorithms/sort.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include + +namespace mt::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/tests/main.cpp b/tests/main.cpp new file mode 100644 index 0000000..56c99b6 --- /dev/null +++ b/tests/main.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +#include "mt/algorithms/sort.hpp" +#include "mt/algorithms/max.hpp" + +auto main(int argc, char *argv[]) -> int +{ + { // 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 }; + + 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; + } +} diff --git a/tests/meson.build b/tests/meson.build new file mode 100755 index 0000000..016e053 --- /dev/null +++ b/tests/meson.build @@ -0,0 +1,7 @@ +executable( + meson.project_name(), + 'main.cpp', + dependencies : deps, + cpp_args: args, + include_directories : inc +)