// SPDX-License-Identifier: CC0-1.0
// 生成版: MSVC・速度優先（入力検査を除外）。 所有・解放の省略は各APIの前提を参照。
// 生成元: Algorithm/next_combination.cpp
// 参考: https://github.com/drken1215/algorithm/blob/master/Search/next_combination.cpp
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/combination_masks.md
//
// API一覧
// [次のマスク]
// next_combination(sub): 同じ1の個数で次に大きいマスク。0・型内に次がなければ0。O(1)。
// boolを除くunsigned整数型に対応。既存int版は非負が前提で、正のintに収まらない次も0。
//
// [幅指定・列挙]
// first_combination<UInt=uint64_t>(k): 下位kビットが1の最小マスク。0<=k<=型幅。O(1)。
// last_combination<UInt=uint64_t>(n,k): nビット内で1がk個の最大マスク。0<=k<=n<=型幅。O(1)。
// next_combination(sub, n): 下位nビット内の次へsubを進める。次がなければfalseでsub不変。O(1)。
// for_each_combination<UInt=uint64_t>(n,k,visit): nビット内のk個の1を持つ全マスクを昇順にvisit(mask)へ渡す。k=0も1回。
// for_eachはC(n,k)回の呼出し・O(1)追加領域（visitの処理を除く）。入力検査・終端の計算は最初だけ行う。
// n,k,maskの検査はSKIPで省く。全幅のシフト・0除算を避ける処理は常に維持する。


// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <bit>
#include <concepts>
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <type_traits>


// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_NEXT_COMBINATION_DEFINED
#define SIRKIT_NEXT_COMBINATION_DEFINED
template <std::unsigned_integral UInt> requires (!std::same_as<UInt, bool>)
constexpr UInt next_combination(UInt sub) noexcept {
    if (sub == 0) return 0;
    const UInt low = static_cast<UInt>(sub & static_cast<UInt>(UInt{0} - sub));
    const UInt high = static_cast<UInt>(sub + low);
    if (high == 0) return 0;
    const UInt tail = static_cast<UInt>(static_cast<UInt>((sub ^ high) >> 2) >> std::countr_zero(sub));
    return static_cast<UInt>(high | tail);
}
inline constexpr int next_combination(int sub) {
    const auto next = next_combination(static_cast<unsigned int>(sub));
    return next > static_cast<unsigned int>(std::numeric_limits<int>::max()) ? 0 : static_cast<int>(next);
}
#endif

#ifndef SIRKIT_COMBINATION_MASK_UTILITIES_DEFINED
#define SIRKIT_COMBINATION_MASK_UTILITIES_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ {
template <std::unsigned_integral UInt> requires (!std::same_as<UInt, bool>)
constexpr bool NextCombinationBounded(UInt& sub, int n) noexcept {
    constexpr int width = std::numeric_limits<UInt>::digits;
    const auto next = next_combination(sub);
    if (next == 0) return false;
    if (n <= 0) return false;
    if (n < width && (next >> n) != 0) return false;
    sub = next;
    return true;
}
} // namespace reinsirk::detail
template <std::unsigned_integral UInt = std::uint64_t> requires (!std::same_as<UInt, bool>)
constexpr UInt first_combination(int k) {
    constexpr int width = std::numeric_limits<UInt>::digits;
    if (k <= 0) return 0;
    if (k >= width) return std::numeric_limits<UInt>::max();
    return static_cast<UInt>((UInt{1} << k) - UInt{1});
}
template <std::unsigned_integral UInt = std::uint64_t> requires (!std::same_as<UInt, bool>)
constexpr UInt last_combination(int n, int k) {
    if (k <= 0) return 0;
    return static_cast<UInt>(first_combination<UInt>(k) << (n - k));
}
template <std::unsigned_integral UInt> requires (!std::same_as<UInt, bool>)
constexpr bool next_combination(UInt& sub, int n) {
    return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::NextCombinationBounded(sub, n);
}
template <std::unsigned_integral UInt = std::uint64_t, class Visitor> requires (!std::same_as<UInt, bool>)
constexpr void for_each_combination(int n, int k, Visitor&& visit) {
    if (n < 0 || k < 0 || k > n) return;
    UInt mask = first_combination<UInt>(k);
    const UInt last = k == 0 ? UInt{0} : static_cast<UInt>(mask << (n - k));
    for (;;) {
        visit(UInt{mask});
        if (mask == last) break;
        if (k == 1) mask = static_cast<UInt>(mask << 1);
        else {
            // 終端以外は次が型内に存在し、k>=2なのでシフト量も型幅未満。
            const UInt low = static_cast<UInt>(mask & static_cast<UInt>(UInt{0} - mask));
            const UInt high = static_cast<UInt>(mask + low);
            const UInt tail = static_cast<UInt>((mask ^ high) >> (std::countr_zero(mask) + 2));
            mask = static_cast<UInt>(high | tail);
        }
    }
}
#endif

} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
