// SPDX-License-Identifier: CC0-1.0
// 生成版: MSVC・速度優先（入力検査を除外）。 所有・解放の省略は各APIの前提を参照。
// 生成元: Geometry/angle_sort.cpp
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/angle_sort.md
//
// API一覧
// [偏角ソート]
// pair<int64_t,int64_t>の偏角は+xから反時計回りの[0,2π)。零は昇順先頭・降順末尾、同方向の長さ順は任意。
// angle_cross_sign(a,b): int64_t全域で厳密な外積の符号を-1/0/1で返す。O(1)。
// same_angle(a,b): 同方向ならtrue。零は零とのみ同方向、長さを問わない。O(1)。
// arccmp_less(a,b)/arccmp_g(a,b): 偏角の厳密なa<b/a>b。std::sortに渡せる。O(1)。
// arccmp_less_eq(a,b)/arccmp_g_eq(a,b): 偏角の非厳密なa<=b/a>=b。std::sortの比較器には使わない。O(1)。
// AngleLess/AngleGreater: 昇順/降順の比較器オブジェクト。O(1)。
// angle_sort(p,descending=false): 配列をその場で昇順にソート。trueなら降順、同角の順序は任意。O(N log N)上界、大きい整列済み列はO(N)。
// angle_sort_indices(p,descending=false): 入力不変で昇順の添字列。trueなら降順。O(N log N)時間/O(N)追加領域。
// いずれもint64_t全域で有効なため入力検査は不要。SKIP_RUNTIME_CHECKSで動作は変わらない。


// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <numeric>
#include <utility>
#include <vector>



// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_ANGLE_ARITHMETIC_DEFINED
#define SIRKIT_ANGLE_ARITHMETIC_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ {
struct AngleArithmetic {
    static constexpr std::uint64_t magnitude(std::int64_t x) noexcept {
        const auto u = static_cast<std::uint64_t>(x);
        return x < 0 ? std::uint64_t{0} - u : u;
    }

    struct Product {
        std::uint64_t hi, lo;
    };

    static constexpr Product multiply(std::uint64_t x, std::uint64_t y) noexcept {
        constexpr std::uint64_t mask = 0xffffffffu;
        const std::uint64_t x0 = x & mask, x1 = x >> 32;
        const std::uint64_t y0 = y & mask, y1 = y >> 32;
        const std::uint64_t low = x0 * y0;
        const std::uint64_t middle = x1 * y0 + (low >> 32);
        const std::uint64_t carry = (middle & mask) + x0 * y1;
        return {x1 * y1 + (middle >> 32) + (carry >> 32),
                (carry << 32) | (low & mask)};
    }

    static constexpr int compare_product(Product x, Product y) noexcept {
        if (x.hi != y.hi) return x.hi > y.hi ? 1 : -1;
        return (x.lo > y.lo) - (x.lo < y.lo);
    }

    static constexpr bool lower_half(const std::pair<std::int64_t, std::int64_t>& p) noexcept {
        return p.second < 0 || (p.second == 0 && p.first < 0);
    }

    static constexpr bool zero(const std::pair<std::int64_t, std::int64_t>& p) noexcept {
        return p.first == 0 && p.second == 0;
    }

};
} // namespace reinsirk::detail
#endif

#ifndef SIRKIT_ANGLE_CROSS_SIGN_DEFINED
#define SIRKIT_ANGLE_CROSS_SIGN_DEFINED
inline constexpr int angle_cross_sign(
    const std::pair<std::int64_t, std::int64_t>& a,
    const std::pair<std::int64_t, std::int64_t>& b) noexcept {
    const bool p_zero = a.first == 0 || b.second == 0;
    const bool q_zero = a.second == 0 || b.first == 0;
    const bool p_negative = !p_zero && ((a.first < 0) != (b.second < 0));
    const bool q_negative = !q_zero && ((a.second < 0) != (b.first < 0));
    if (p_negative != q_negative) return p_negative ? -1 : 1;
    const auto x = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleArithmetic::magnitude(a.first);
    const auto y = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleArithmetic::magnitude(b.second);
    const auto z = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleArithmetic::magnitude(a.second);
    const auto w = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleArithmetic::magnitude(b.first);
    int cmp;
    if ((x | y | z | w) <= std::uint64_t{0xffffffffu}) {
        const auto p = x * y, q = z * w;
        cmp = (p > q) - (p < q);
    } else {
        cmp = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleArithmetic::compare_product(
            /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleArithmetic::multiply(x, y),
            /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleArithmetic::multiply(z, w));
    }
    return p_negative ? -cmp : cmp;
}
#endif

#ifndef SIRKIT_ANGLE_STANDARD_LESS_WIDE_DEFINED
#define SIRKIT_ANGLE_STANDARD_LESS_WIDE_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ {
inline constexpr bool AngleStandardLessWide(
    const std::pair<std::int64_t, std::int64_t>& a,
    const std::pair<std::int64_t, std::int64_t>& b) noexcept {
    return angle_cross_sign(a, b) > 0;
}
} // namespace reinsirk::detail
#endif

#ifndef SIRKIT_ARCCMP_LESS_DEFINED
#define SIRKIT_ARCCMP_LESS_DEFINED
inline constexpr bool arccmp_less(
    const std::pair<std::int64_t, std::int64_t>& a,
    const std::pair<std::int64_t, std::int64_t>& b) noexcept {
    // 水平軸を符号だけで処理し、通常の比較から零判定を除く。
    if ((a.second == 0) | (b.second == 0)) {
        if (a.second == 0) {
            if (b.second == 0)
                return a.first == 0 ? b.first != 0 : a.first > 0 && b.first < 0;
            return a.first >= 0 || b.second < 0;
        }
        return b.first < 0 && a.second > 0;
    }
    const bool ah = a.second < 0;
    const bool bh = b.second < 0;
    if (ah != bh) return ah < bh;
    constexpr std::uint64_t bias = std::uint64_t{1} << 31;
    const std::uint64_t bits = (static_cast<std::uint64_t>(a.first) + bias)
        | (static_cast<std::uint64_t>(a.second) + bias)
        | (static_cast<std::uint64_t>(b.first) + bias)
        | (static_cast<std::uint64_t>(b.second) + bias);
    if (bits <= std::uint64_t{0xffffffffu}) [[likely]] {
        const std::int64_t p = a.first * b.second;
        const std::int64_t q = a.second * b.first;
        return p > q;
    }
    return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleStandardLessWide(a, b);
}
#endif

#ifndef SIRKIT_ARCCMP_G_DEFINED
#define SIRKIT_ARCCMP_G_DEFINED
inline constexpr bool arccmp_g(
    const std::pair<std::int64_t, std::int64_t>& a,
    const std::pair<std::int64_t, std::int64_t>& b) noexcept {
    return arccmp_less(b, a);
}
#endif

#ifndef SIRKIT_ARCCMP_LESS_EQ_DEFINED
#define SIRKIT_ARCCMP_LESS_EQ_DEFINED
inline constexpr bool arccmp_less_eq(
    const std::pair<std::int64_t, std::int64_t>& a,
    const std::pair<std::int64_t, std::int64_t>& b) noexcept {
    return !arccmp_less(b, a);
}
#endif

#ifndef SIRKIT_ARCCMP_G_EQ_DEFINED
#define SIRKIT_ARCCMP_G_EQ_DEFINED
inline constexpr bool arccmp_g_eq(
    const std::pair<std::int64_t, std::int64_t>& a,
    const std::pair<std::int64_t, std::int64_t>& b) noexcept {
    return !arccmp_less(a, b);
}
#endif

#ifndef SIRKIT_SAME_ANGLE_DEFINED
#define SIRKIT_SAME_ANGLE_DEFINED
inline constexpr bool same_angle(
    const std::pair<std::int64_t, std::int64_t>& a,
    const std::pair<std::int64_t, std::int64_t>& b) noexcept {
    if (/*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleArithmetic::zero(a) || /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleArithmetic::zero(b))
        return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleArithmetic::zero(a) && /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleArithmetic::zero(b);
    return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleArithmetic::lower_half(a) == /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleArithmetic::lower_half(b)
        && angle_cross_sign(a, b) == 0;
}
#endif

#ifndef SIRKIT_ANGLE_LESS_DEFINED
#define SIRKIT_ANGLE_LESS_DEFINED
struct AngleLess {
    constexpr bool operator()(const std::pair<std::int64_t, std::int64_t>& a,
                              const std::pair<std::int64_t, std::int64_t>& b) const noexcept {
        return arccmp_less(a, b);
    }
};
#endif

#ifndef SIRKIT_ANGLE_GREATER_DEFINED
#define SIRKIT_ANGLE_GREATER_DEFINED
struct AngleGreater {
    constexpr bool operator()(const std::pair<std::int64_t, std::int64_t>& a,
                              const std::pair<std::int64_t, std::int64_t>& b) const noexcept {
        return arccmp_g(a, b);
    }
};
#endif

#ifndef SIRKIT_ANGLE_TRY_SORTED_DEFINED
#define SIRKIT_ANGLE_TRY_SORTED_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ {
template<class Compare>
// 2048点以上。確認・短い修復は定数領域で行い、失敗しても点の多重集合を保つ。
bool AngleTrySorted(std::vector<std::pair<std::int64_t, std::int64_t>>& points, Compare less) {
    if (less(points.back(), points.front())) return false;
    unsigned inversions = 0;
    for (std::size_t tail = points.size() - 1, count = 0; count < 16; --tail, ++count)
        if (less(points[tail], points[tail - 1]) && ++inversions == 2) return false;
    for (std::size_t sample = 1; sample <= 16; ++sample) {
        const auto at = points.size() - 1 - sample * (points.size() / 64);
        if (less(points[at], points[at - 1]) && ++inversions == 2) return false;
    }
    for (std::size_t sample = 1; sample <= 16; ++sample) {
        const auto at = sample * (points.size() / 17);
        if (less(points[at], points[at - 1]) && ++inversions == 2) return false;
    }
    auto next = std::is_sorted_until(points.begin(), points.end(), less);
    if (next == points.end()) return true;
    const auto remaining = static_cast<std::size_t>(points.end() - next);
    // 末尾16点に2回以上の逆転があれば、冒頭の検査で終了済み。
    if (remaining <= 16) {
        std::array<std::pair<std::int64_t, std::int64_t>, 16> tail;
        std::copy(next, points.end(), tail.begin());
        auto left = next, output = points.end();
        auto right = remaining;
        while (right != 0) {
            if (left != points.begin() && less(tail[right - 1], *(left - 1))) *--output = *--left;
            else *--output = tail[--right];
        }
        return true;
    }
    unsigned moves = 0;
    for (; next != points.end(); ++next) {
        const auto value = *next;
        auto hole = next;
        while (hole != points.begin() && less(value, *(hole - 1))) {
            *hole = *(hole - 1);
            --hole;
            if (++moves == 32) { *hole = value; return false; }
        }
        *hole = value;
    }
    return true;
}
} // namespace reinsirk::detail
#endif

#ifndef SIRKIT_ANGLE_SORT_LARGE_DEFINED
#define SIRKIT_ANGLE_SORT_LARGE_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ {
inline void AngleSortLarge(std::vector<std::pair<std::int64_t, std::int64_t>>& p,
                       bool descending) {
    if (p.size() >= 2048 && (descending ? AngleTrySorted(p, AngleGreater{})
                                      : AngleTrySorted(p, AngleLess{}))) return;
    // 半平面の振り分け中に、積をint64_tで比較できる範囲かも集計する。
    std::uint64_t bits = 0;
    constexpr std::uint64_t bias = std::uint64_t{1} << 31;
    const auto middle = std::partition(p.begin(), p.end(), [&](const auto& point) {
        bits |= (static_cast<std::uint64_t>(point.first) + bias)
              | (static_cast<std::uint64_t>(point.second) + bias);
        return AngleArithmetic::lower_half(point) == descending;
    });
    // 零を外積だけの比較から分離し、等値関係の推移性を保つ。
    const auto upper_begin = descending ? middle : p.begin();
    const auto upper_end = descending ? p.end() : middle;
    const auto zero_end = std::partition(upper_begin, upper_end, [descending](const auto& point) {
        return AngleArithmetic::zero(point) != descending;
    });
    const auto first = descending ? p.begin() : zero_end;
    const auto last = descending ? zero_end : p.end();
    const auto sort_parts = [&](auto less) {
        if (descending) {
            const auto greater = [&](const auto& a, const auto& b) { return less(b, a); };
            std::sort(first, middle, greater);
            std::sort(middle, last, greater);
        } else {
            std::sort(first, middle, less);
            std::sort(middle, last, less);
        }
    };
    if (bits <= std::uint64_t{0xffffffffu}) {
        sort_parts([](const auto& a, const auto& b) {
            return a.first * b.second > a.second * b.first;
        });
    } else {
        sort_parts([](const auto& a, const auto& b) {
            return angle_cross_sign(a, b) > 0;
        });
    }
}
} // namespace reinsirk::detail
#endif

#ifndef SIRKIT_ANGLE_SORT_DEFINED
#define SIRKIT_ANGLE_SORT_DEFINED
inline void angle_sort(std::vector<std::pair<std::int64_t, std::int64_t>>& p,
                       bool descending = false) {
    if (p.size() < 32) {
        if (descending) std::sort(p.begin(), p.end(), AngleGreater{});
        else std::sort(p.begin(), p.end(), AngleLess{});
    } else {
        /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleSortLarge(p, descending);
    }
}
#endif

#ifndef SIRKIT_ANGLE_SORT_INDICES_LARGE_DEFINED
#define SIRKIT_ANGLE_SORT_INDICES_LARGE_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ {
inline void AngleSortIndicesLarge(std::vector<std::size_t>& indices,
                                  const std::vector<std::pair<std::int64_t, std::int64_t>>& p,
                                  bool descending) {
    // 半平面の振り分け中に、積をint64_tで比較できる範囲かも集計する。
    std::uint64_t bits = 0;
    constexpr std::uint64_t bias = std::uint64_t{1} << 31;
    const auto middle = std::partition(indices.begin(), indices.end(), [&](std::size_t index) {
        const auto& point = p[index];
        bits |= (static_cast<std::uint64_t>(point.first) + bias)
              | (static_cast<std::uint64_t>(point.second) + bias);
        return AngleArithmetic::lower_half(point) == descending;
    });
    // 零を外積だけの比較から分離し、等値関係の推移性を保つ。
    const auto upper_begin = descending ? middle : indices.begin();
    const auto upper_end = descending ? indices.end() : middle;
    const auto zero_end = std::partition(upper_begin, upper_end, [&p, descending](std::size_t index) {
        return AngleArithmetic::zero(p[index]) != descending;
    });
    const auto first = descending ? indices.begin() : zero_end;
    const auto last = descending ? zero_end : indices.end();
    const auto sort_parts = [&](auto less) {
        if (descending) {
            const auto greater = [&](const auto& a, const auto& b) { return less(b, a); };
            std::sort(first, middle, greater);
            std::sort(middle, last, greater);
        } else {
            std::sort(first, middle, less);
            std::sort(middle, last, less);
        }
    };
    if (bits <= std::uint64_t{0xffffffffu}) {
        sort_parts([&p](std::size_t i, std::size_t j) {
            const auto& a = p[i]; const auto& b = p[j];
            return a.first * b.second > a.second * b.first;
        });
    } else {
        sort_parts([&p](std::size_t i, std::size_t j) {
            const auto& a = p[i]; const auto& b = p[j];
            return angle_cross_sign(a, b) > 0;
        });
    }
}
} // namespace reinsirk::detail
#endif

#ifndef SIRKIT_ANGLE_SORT_INDICES_DEFINED
#define SIRKIT_ANGLE_SORT_INDICES_DEFINED
inline std::vector<std::size_t> angle_sort_indices(
    const std::vector<std::pair<std::int64_t, std::int64_t>>& p,
    bool descending = false) {
    std::vector<std::size_t> indices(p.size());
    std::iota(indices.begin(), indices.end(), std::size_t{0});
    if (p.size() >= 32) {
        /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::AngleSortIndicesLarge(indices, p, descending);
        return indices;
    }
    if (descending) {
        std::sort(indices.begin(), indices.end(), [&p](std::size_t a, std::size_t b) {
            return arccmp_g(p[a], p[b]);
        });
    } else {
        std::sort(indices.begin(), indices.end(), [&p](std::size_t a, std::size_t b) {
            return arccmp_less(p[a], p[b]);
        });
    }
    return indices;
}
#endif

} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
