combinations.hxx
Go to the documentation of this file.
1 /*===========================================================================================================
2  *
3  * SHA - Simple Hybesis Algorithms
4  *
5  * Copyright (c) Michael Jeulin-Lagarrigue
6  *
7  * Licensed under the MIT License, you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * https://github.com/michael-jeulinl/Simple-Hybesis-Algorithms/blob/master/LICENSE
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License is
13  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and limitations under the License.
15  *
16  * The above copyright notice and this permission notice shall be included in all copies or
17  * substantial portions of the Software.
18  *
19  *=========================================================================================================*/
20 #ifndef MODULE_COMBINATORY_COMBINATIONS_HXX
21 #define MODULE_COMBINATORY_COMBINATIONS_HXX
22 
23 // STD includes
24 #include <list>
25 
26 namespace SHA_Combinatory
27 {
44  template <typename Container, typename IT>
45  std::list<Container> Combinations(const IT& begin, const IT& end)
46  {
47  std::list<Container> combinations;
48 
49  // Recusion termination
50  const auto kSeqSize = static_cast<const int>(std::distance(begin, end));
51  if (kSeqSize <= 0)
52  return combinations;
53 
54  // Keep first element
55  Container elementContainer;
56  elementContainer.push_back(*begin);
57  combinations.push_back(elementContainer);
58 
59  // Recursion termination
60  if (kSeqSize == 1)
61  return combinations;
62 
63  // Build all permutations of the suffix - Recursion
64  auto subCombinations = Combinations<Container, IT>(begin + 1, end);
65 
66  // Put the letter into every possible position of the existing permutations.
67  for (auto it = subCombinations.begin(); it != subCombinations.end(); ++it)
68  {
69  combinations.push_back(*it);
70  it->push_back(*begin);
71  combinations.push_back(*it);
72  }
73 
74  return combinations;
75  }
76 }
77 
78 #endif // MODULE_COMBINATORY_COMBINATIONS_HXX
std::list< Container > Combinations(const IT &begin, const IT &end)