From 23888c4be69d907be143c0166dfb7568ab2220fc Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 14 Apr 2018 10:46:15 +0100 Subject: [PATCH] Added permutations and combinations template constants. --- include/etl/combinations.h | 55 ++++++++++++++++++++++++++++ include/etl/permutations.h | 63 ++++++++++++++++++++++++++++++++ include/etl/version.h | 4 +- support/Release notes.txt | 4 ++ test/codeblocks/ETL.cbp | 2 + test/test_maths.cpp | 65 +++++++++++++++++++++++++++++++++ test/vs2017/etl.vcxproj | 2 + test/vs2017/etl.vcxproj.filters | 6 +++ 8 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 include/etl/combinations.h create mode 100644 include/etl/permutations.h diff --git a/include/etl/combinations.h b/include/etl/combinations.h new file mode 100644 index 00000000..3ecb8a5e --- /dev/null +++ b/include/etl/combinations.h @@ -0,0 +1,55 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2018 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#ifndef __ETL_COMBINATIONS__ +#define __ETL_COMBINATIONS__ + +#include "platform.h" +#include "permutations.h" +#include "factorial.h" + +///\defgroup combinations combinations +/// combinations : Calculates K combinations from N. +///\ingroup maths + +namespace etl +{ + //*************************************************************************** + ///\ingroup combinations + /// Calculates combinations. + //*************************************************************************** + template + struct combinations + { + static const size_t value = etl::permutations::value / etl::factorial::value; + }; +} + +#endif diff --git a/include/etl/permutations.h b/include/etl/permutations.h new file mode 100644 index 00000000..9a1feba4 --- /dev/null +++ b/include/etl/permutations.h @@ -0,0 +1,63 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2018 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#ifndef __ETL_PERMUTATIONS__ +#define __ETL_PERMUTATIONS__ + +#include "platform.h" + +///\defgroup permutations permutations +/// permutations : Calculates K permutations from N. +///\ingroup maths + +namespace etl +{ + //*************************************************************************** + ///\ingroup permutations + /// Calculates permutaions. + //*************************************************************************** + template + struct permutations + { + static const size_t value = NV * permutations::value; + }; + + //*************************************************************************** + /// Calculates permutations. + /// Specialisation for KV == 0. + //*************************************************************************** + template + struct permutations + { + static const size_t value = 1; + }; +} + +#endif diff --git a/include/etl/version.h b/include/etl/version.h index aa0009f1..e1d798f3 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -37,9 +37,9 @@ SOFTWARE. /// Definitions of the ETL version ///\ingroup utilities -#define ETL_VERSION "11.4.0" +#define ETL_VERSION "11.5.0" #define ETL_VERSION_MAJOR 11 -#define ETL_VERSION_MINOR 4 +#define ETL_VERSION_MINOR 5 #define ETL_VERSION_PATCH 0 #endif diff --git a/support/Release notes.txt b/support/Release notes.txt index d418f610..9dd37736 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +11.5.0 +Added etl::permutations and etl::combinations constant templates. + =============================================================================== 11.4.0 Added improved atomics. diff --git a/test/codeblocks/ETL.cbp b/test/codeblocks/ETL.cbp index 2c333ae8..69583f71 100644 --- a/test/codeblocks/ETL.cbp +++ b/test/codeblocks/ETL.cbp @@ -92,6 +92,7 @@ + @@ -163,6 +164,7 @@ + diff --git a/test/test_maths.cpp b/test/test_maths.cpp index 81832173..4d42f3c6 100644 --- a/test/test_maths.cpp +++ b/test/test_maths.cpp @@ -33,6 +33,8 @@ SOFTWARE. #include "fibonacci.h" #include "factorial.h" #include "sqrt.h" +#include "permutations.h" +#include "combinations.h" namespace { @@ -53,6 +55,36 @@ namespace return 1; } + size_t factorial(size_t n) + { + if (n == 1) + { + return 1; + } + else + { + return n * factorial(n - 1); + } + } + + size_t permutations(size_t n, size_t k) + { + size_t p = 1; + + for (size_t i = 0; i < k; ++i) + { + p *= n; + --n; + } + + return p; + } + + size_t combinations(size_t n, size_t k) + { + return permutations(n, k) / factorial(k); + } + SUITE(test_maths) { //************************************************************************* @@ -380,5 +412,38 @@ namespace CHECK_EQUAL(sqrt(9), etl::sqrt<9>::value); CHECK_EQUAL(sqrt(10), etl::sqrt<10>::value); } + + //************************************************************************* + TEST(test_permutations) + { + size_t actual; + + + CHECK_EQUAL((permutations( 6, 2)), (actual = etl::permutations< 6, 2>::value)); + CHECK_EQUAL((permutations( 7, 3)), (actual = etl::permutations< 7, 3>::value)); + CHECK_EQUAL((permutations( 8, 4)), (actual = etl::permutations< 8, 4>::value)); + CHECK_EQUAL((permutations( 9, 5)), (actual = etl::permutations< 9, 5>::value)); + CHECK_EQUAL((permutations(10, 6)), (actual = etl::permutations<10, 6>::value)); + CHECK_EQUAL((permutations(11, 7)), (actual = etl::permutations<11, 7>::value)); + CHECK_EQUAL((permutations(12, 8)), (actual = etl::permutations<12, 8>::value)); + CHECK_EQUAL((permutations(13, 9)), (actual = etl::permutations<13, 9>::value)); + CHECK_EQUAL((permutations(14, 10)), (actual = etl::permutations<14, 10>::value)); + } + + //************************************************************************* + TEST(test_combinations) + { + size_t actual; + + CHECK_EQUAL((combinations( 6, 2)), (actual = etl::combinations< 6, 2>::value)); + CHECK_EQUAL((combinations( 7, 3)), (actual = etl::combinations< 7, 3>::value)); + CHECK_EQUAL((combinations( 8, 4)), (actual = etl::combinations< 8, 4>::value)); + CHECK_EQUAL((combinations( 9, 5)), (actual = etl::combinations< 9, 5>::value)); + CHECK_EQUAL((combinations(10, 6)), (actual = etl::combinations<10, 6>::value)); + CHECK_EQUAL((combinations(11, 7)), (actual = etl::combinations<11, 7>::value)); + CHECK_EQUAL((combinations(12, 8)), (actual = etl::combinations<12, 8>::value)); + CHECK_EQUAL((combinations(13, 9)), (actual = etl::combinations<13, 9>::value)); + CHECK_EQUAL((combinations(14, 10)), (actual = etl::combinations<14, 10>::value)); + } }; } diff --git a/test/vs2017/etl.vcxproj b/test/vs2017/etl.vcxproj index 25677b31..87c633a5 100644 --- a/test/vs2017/etl.vcxproj +++ b/test/vs2017/etl.vcxproj @@ -330,6 +330,7 @@ + @@ -347,6 +348,7 @@ + diff --git a/test/vs2017/etl.vcxproj.filters b/test/vs2017/etl.vcxproj.filters index ebcddfc3..d45f1a84 100644 --- a/test/vs2017/etl.vcxproj.filters +++ b/test/vs2017/etl.vcxproj.filters @@ -615,6 +615,12 @@ ETL\Utilities + + ETL\Maths + + + ETL\Maths +