diff --git a/CMakePresets.json b/CMakePresets.json index ec88efa..a060f8f 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -77,16 +77,15 @@ "binaryDir": "${sourceDir}/out/build/${presetName}", "installDir": "${sourceDir}/out/install/${presetName}", "cacheVariables": { + "CMAKE_COMPILE_WARNING_AS_ERROR": true, + "CMAKE_C_FLAGS": "-Wall -Wextra -pedantic", + "CMAKE_CXX_FLAGS": "-Wall -pedantic -Wcast-align=strict -Wcast-qual -Wchanges-meaning -Wconversion -Wconversion-null -Wctor-dtor-privacy -Wdangling-pointer -Wdangling-reference -Wdate-time -Wdelete-incomplete -Wdelete-non-virtual-dtor -Wdeprecated-copy -Wdeprecated-copy-dtor -Wdeprecated-enum-enum-conversion -Wdeprecated-enum-float-conversion -Wdisabled-optimization -Wdouble-promotion -Wduplicated-branches -Wduplicated-cond -Wexceptions -Wextra -Wformat=2 -Wimplicit-fallthrough=5 -Winaccessible-base -Winherited-variadic-ctor -Winit-list-lifetime -Winit-self -Winterference-size -Winvalid-constexpr -Winvalid-imported-macros -Winvalid-offsetof -Winvalid-pch -Winvalid-utf8 -Wlogical-op -Wmisleading-indentation -Wmismatched-new-delete -Wmismatched-tags -Wmissing-declarations -Wmissing-noreturn -Wmissing-requires -Wmissing-template-keyword -Wmultichar -Wmultiple-inheritance -Wnarrowing -Wnoexcept -Wnoexcept-type -Wnon-template-friend -Wnon-virtual-dtor -Wnull-dereference -Wold-style-cast -Wopenacc-parallelism -Woverlength-strings -Woverloaded-virtual -Wpacked -Wpacked-bitfield-compat -Wrange-loop-construct -Wredundant-decls -Wredundant-move -Wredundant-tags -Wregister -Wreorder -Wself-move -Wshadow -Wshift-negative-value -Wsign-conversion -Wsign-promo -Wsized-deallocation -Wstack-protector -Wstrict-null-sentinel -Wstrict-overflow -Wunused -Wunused-macros -Wuseless-cast -Wvariadic-macros -Wvector-operation-performance -Wvexing-parse -Wvirtual-inheritance -Wvirtual-move-assign -Wvla -Wvolatile -Wwrite-strings -Wzero-as-null-pointer-constant", "CMAKE_EXPORT_COMPILE_COMMANDS": true, "CMAKE_CONFIGURATION_TYPES": "Release;RelWithDebInfo;Debug", "CMAKE_TOOLCHAIN_FILE": { "value": "${sourceDir}/vcpkg/scripts/buildsystems/vcpkg.cmake", "type": "FILEPATH" } - }, - "environment": { - "CFLAGS": "-Wall -Wextra -pedantic", - "CXXFLAGS": "-Wall -pedantic -Wcast-align=strict -Wcast-qual -Wchanges-meaning -Wconversion -Wconversion-null -Wctor-dtor-privacy -Wdangling-pointer -Wdangling-reference -Wdate-time -Wdelete-incomplete -Wdelete-non-virtual-dtor -Wdeprecated-copy -Wdeprecated-copy-dtor -Wdeprecated-enum-enum-conversion -Wdeprecated-enum-float-conversion -Wdisabled-optimization -Wdouble-promotion -Wduplicated-branches -Wduplicated-cond -Wexceptions -Wextra -Wformat=2 -Wimplicit-fallthrough=5 -Winaccessible-base -Winherited-variadic-ctor -Winit-list-lifetime -Winit-self -Winterference-size -Winvalid-constexpr -Winvalid-imported-macros -Winvalid-offsetof -Winvalid-pch -Winvalid-utf8 -Wlogical-op -Wmisleading-indentation -Wmismatched-new-delete -Wmismatched-tags -Wmissing-declarations -Wmissing-noreturn -Wmissing-requires -Wmissing-template-keyword -Wmultichar -Wmultiple-inheritance -Wnarrowing -Wnoexcept -Wnoexcept-type -Wnon-template-friend -Wnon-virtual-dtor -Wnull-dereference -Wold-style-cast -Wopenacc-parallelism -Woverlength-strings -Woverloaded-virtual -Wpacked -Wpacked-bitfield-compat -Wrange-loop-construct -Wredundant-decls -Wredundant-move -Wredundant-tags -Wregister -Wreorder -Wself-move -Wshadow -Wshift-negative-value -Wsign-conversion -Wsign-promo -Wsized-deallocation -Wstack-protector -Wstrict-null-sentinel -Wstrict-overflow -Wunused -Wunused-macros -Wuseless-cast -Wvariadic-macros -Wvector-operation-performance -Wvexing-parse -Wvirtual-inheritance -Wvirtual-move-assign -Wvla -Wvolatile -Wwrite-strings -Wzero-as-null-pointer-constant" } } ], diff --git a/benchmark/last_stone_weight_benchmark.cpp b/benchmark/last_stone_weight_benchmark.cpp index 758601c..0c3e4a5 100644 --- a/benchmark/last_stone_weight_benchmark.cpp +++ b/benchmark/last_stone_weight_benchmark.cpp @@ -29,7 +29,7 @@ TEST_CASE("Last stone weight benchmarking", "[benchmark][last_stone_weight]") .run( NAMEOF_RAW(naive::last_stone_weight).c_str(), []() noexcept { - std::vector stones{ + std::vector stones{ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, @@ -47,7 +47,7 @@ TEST_CASE("Last stone weight benchmarking", "[benchmark][last_stone_weight]") .run( NAMEOF_RAW(partially_sorted::last_stone_weight).c_str(), []() noexcept { - std::vector stones{ + std::vector stones{ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, @@ -65,7 +65,7 @@ TEST_CASE("Last stone weight benchmarking", "[benchmark][last_stone_weight]") .run( NAMEOF_RAW(priority_queue_based::last_stone_weight).c_str(), []() noexcept { - std::vector stones{ + std::vector stones{ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, @@ -83,7 +83,7 @@ TEST_CASE("Last stone weight benchmarking", "[benchmark][last_stone_weight]") .run( NAMEOF_RAW(heapified::last_stone_weight).c_str(), []() noexcept { - std::vector stones{ + std::vector stones{ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, diff --git a/include/forfun/last_stone_weight.hpp b/include/forfun/last_stone_weight.hpp index 04e375d..6bd71cf 100644 --- a/include/forfun/last_stone_weight.hpp +++ b/include/forfun/last_stone_weight.hpp @@ -125,25 +125,27 @@ namespace partially_sorted { /// non-negative, otherwise the function's behavior is undefined. template Sentinel> requires std::integral> -[[nodiscard]] constexpr auto -last_stone_weight(Iter const first, Sentinel end) noexcept +[[nodiscard]] constexpr auto last_stone_weight(Iter iter, Sentinel end) noexcept -> std::iter_value_t { - using ValueType = std::iter_value_t; - using DiffType = std::iter_difference_t; + auto const first{iter}; + auto const second{++iter}; - assert(std::distance(first, end) > DiffType{0}); - - if ((end - first) > ValueType{1}) + if (iter != end) { - auto iter{first}; - auto const sort_end{first + DiffType{2}}; - for (++iter; iter != end;) + for (++iter; second != end;) { - std::partial_sort(first, sort_end, end, std::greater<>()); - - *first -= *iter; - *iter = *--end; +#if __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wstrict-overflow" +#endif // __GNUC__ + std::partial_sort(first, iter, end, std::greater<>()); +#if __GNUC__ +#pragma GCC diagnostic pop +#endif // __GNUC__ + + *first -= *second; + *second = *--end; } }