EcGen 1.1.8
Loading...
Searching...
No Matches
combin.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8#include <py2cpp/gen.hpp>
9#include <py2cpp/recursive_gen.hpp>
10#include <type_traits> // for integral_constant
11#include <utility> // for pair
12
13namespace ecgen {
14
53 extern auto emk_comb_gen(int n, int k) -> py::RecursiveGenerator<std::pair<int, int>>;
54
75 template <typename Container> auto emk(int n, int k, Container lst)
76 -> py::Generator<Container&> {
77 co_yield lst;
78 for (const auto& [pos_x, pos_y] : emk_comb_gen(n, k)) {
79 auto temp = lst[static_cast<typename Container::size_type>(pos_x)]; // swap
80 lst[static_cast<typename Container::size_type>(pos_x)]
81 = lst[static_cast<typename Container::size_type>(pos_y)];
82 lst[static_cast<typename Container::size_type>(pos_y)] = temp;
83 co_yield lst;
84 }
85 }
86
106 template <int N, int K> constexpr auto Combination() {
107 if constexpr (K >= N || K == 0) {
108 return std::integral_constant<size_t, 1U>{};
109 } else {
110 return std::integral_constant<size_t,
111 Combination<N - 1, K - 1>() + Combination<N - 1, K>()>{};
112 }
113 }
114
115} // namespace ecgen
Definition combin.hpp:13
constexpr auto Combination()
Calculate binomial coefficient C(N, K) at compile time.
Definition combin.hpp:106
auto emk(int n, int k, Container lst) -> py::Generator< Container & >
Generate all k-combinations in reverse lexicographic order (revolving door).
Definition combin.hpp:75
auto emk_comb_gen(int n, int k) -> py::RecursiveGenerator< std::pair< int, int > >
Generate all k-combinations of n elements (revolving door algorithm).