mirror of
https://github.com/ETLCPP/etl.git
synced 2026-10-28 18:07:10 +08:00
Added permutations and combinations template constants.
This commit is contained in:
parent
c14db00d88
commit
23888c4be6
55
include/etl/combinations.h
Normal file
55
include/etl/combinations.h
Normal file
@ -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<N, K> : Calculates K combinations from N.
|
||||
///\ingroup maths
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
///\ingroup combinations
|
||||
/// Calculates combinations.
|
||||
//***************************************************************************
|
||||
template <const size_t NV, const size_t KV>
|
||||
struct combinations
|
||||
{
|
||||
static const size_t value = etl::permutations<NV, KV>::value / etl::factorial<KV>::value;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
63
include/etl/permutations.h
Normal file
63
include/etl/permutations.h
Normal file
@ -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<N, K> : Calculates K permutations from N.
|
||||
///\ingroup maths
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//***************************************************************************
|
||||
///\ingroup permutations
|
||||
/// Calculates permutaions.
|
||||
//***************************************************************************
|
||||
template <const size_t NV, const size_t KV>
|
||||
struct permutations
|
||||
{
|
||||
static const size_t value = NV * permutations<NV - 1, KV - 1>::value;
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Calculates permutations.
|
||||
/// Specialisation for KV == 0.
|
||||
//***************************************************************************
|
||||
template <const size_t NV>
|
||||
struct permutations<NV, 0>
|
||||
{
|
||||
static const size_t value = 1;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -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
|
||||
|
||||
@ -1,3 +1,7 @@
|
||||
===============================================================================
|
||||
11.5.0
|
||||
Added etl::permutations and etl::combinations constant templates.
|
||||
|
||||
===============================================================================
|
||||
11.4.0
|
||||
Added improved atomics.
|
||||
|
||||
@ -92,6 +92,7 @@
|
||||
<Unit filename="../../include/etl/callback_timer.h" />
|
||||
<Unit filename="../../include/etl/char_traits.h" />
|
||||
<Unit filename="../../include/etl/checksum.h" />
|
||||
<Unit filename="../../include/etl/combinations.h" />
|
||||
<Unit filename="../../include/etl/compare.h" />
|
||||
<Unit filename="../../include/etl/constant.h" />
|
||||
<Unit filename="../../include/etl/container.h" />
|
||||
@ -163,6 +164,7 @@
|
||||
<Unit filename="../../include/etl/packet.h" />
|
||||
<Unit filename="../../include/etl/parameter_type.h" />
|
||||
<Unit filename="../../include/etl/pearson.h" />
|
||||
<Unit filename="../../include/etl/permutations.h" />
|
||||
<Unit filename="../../include/etl/platform.h" />
|
||||
<Unit filename="../../include/etl/pool.h" />
|
||||
<Unit filename="../../include/etl/power.h" />
|
||||
|
||||
@ -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));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@ -330,6 +330,7 @@
|
||||
<ClInclude Include="..\..\include\etl\atomic\atomic_gcc_sync.h" />
|
||||
<ClInclude Include="..\..\include\etl\atomic\atomic_std.h" />
|
||||
<ClInclude Include="..\..\include\etl\callback_timer.h" />
|
||||
<ClInclude Include="..\..\include\etl\combinations.h" />
|
||||
<ClInclude Include="..\..\include\etl\compare.h" />
|
||||
<ClInclude Include="..\..\include\etl\constant.h" />
|
||||
<ClInclude Include="..\..\include\etl\c\ecl_timer.h" />
|
||||
@ -347,6 +348,7 @@
|
||||
<ClInclude Include="..\..\include\etl\mutex\mutex_gcc_sync.h" />
|
||||
<ClInclude Include="..\..\include\etl\mutex\mutex_std.h" />
|
||||
<ClInclude Include="..\..\include\etl\packet.h" />
|
||||
<ClInclude Include="..\..\include\etl\permutations.h" />
|
||||
<ClInclude Include="..\..\include\etl\profiles\arduino_arm.h" />
|
||||
<ClInclude Include="..\..\include\etl\profiles\armv5.h" />
|
||||
<ClInclude Include="..\..\include\etl\profiles\armv6.h" />
|
||||
|
||||
@ -615,6 +615,12 @@
|
||||
<ClInclude Include="..\..\include\etl\mutex.h">
|
||||
<Filter>ETL\Utilities</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\etl\combinations.h">
|
||||
<Filter>ETL\Maths</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\etl\permutations.h">
|
||||
<Filter>ETL\Maths</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\main.cpp">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user