EcGen 1.1.8
Loading...
Searching...
No Matches
perm.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8#include <py2cpp/gen.hpp>
9#include <type_traits> // for integral_constant
10
11namespace ecgen {
44 extern auto sjt_gen(int n) -> py::Generator<int>;
45
62 extern auto ehr_gen(int n) -> py::Generator<int>;
63
83 template <int N> constexpr auto Factorial() {
84 if constexpr (N <= 1) {
85 return std::integral_constant<size_t, 1U>{};
86 } else {
87 return std::integral_constant<size_t, N * Factorial<N - 1>()>{};
88 }
89 }
90
105 template <typename Container> inline auto sjt(Container& perm) -> py::Generator<Container&> {
106 const auto n = static_cast<int>(perm.size());
107 for (const int idx : ecgen::sjt_gen(n)) {
108 co_yield perm;
109 auto temp = perm[static_cast<typename Container::size_type>(idx)]; // swap
110 perm[static_cast<typename Container::size_type>(idx)]
111 = perm[static_cast<typename Container::size_type>(idx + 1)];
112 perm[static_cast<typename Container::size_type>(idx + 1)] = temp;
113 }
114 }
115
116} // namespace ecgen
Definition combin.hpp:13
constexpr auto Factorial()
Compute factorial N! at compile time.
Definition perm.hpp:83
auto ehr_gen(int n) -> py::Generator< int >
Generate permutation indices using Eades-Hickey-Read (EHR) algorithm.
auto sjt(Container &perm) -> py::Generator< Container & >
Generate all permutations via adjacent transpositions (SJT algorithm).
Definition perm.hpp:105
auto sjt_gen(int n) -> py::Generator< int >
Generate permutation indices using Steinhaus-Johnson-Trotter algorithm.