// SPDX-License-Identifier: CC0-1.0
// 生成版: Clang用。
// 生成元: Geometry/Nearest_Two_points.cpp
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/nearest_two_points.md
//
// API一覧
// [最近点対]
// P: pair<int32_t,int32_t>の別名。NearestTwoPointsResultはdistance_squared:uint64_tとfirst_index/second_index:size_tを所有。
// NearestTwoPointsResult::distance_squaredは最短距離の二乗、添字は元入力の0始まり（first_index < second_index）。
// nearest_two_points(first,last): [first,last)の整数pair-like列からoptional<NearestTwoPointsResult>を返す。int32座標・未整列可・入力不変、O(N log N)時間/O(N)領域。
// 2点未満はnullopt。同距離の対は任意。最小二乗距離がuint64_tを超える場合は常時overflow_error。
// nearest_two_points(first,count): firstからptrdiff_t型のcount個を処理する。同じ戻り値・計算量。count>=0で、指定個数を参照できること。
// nearest_two_points(points): vector全体を処理する。同じ戻り値・計算量。元入力の点番号を結果に保持する。
// closest_pair(first,n): firstからn個の最短距離二乗をuint64_tで返す。n<2はINT32_MAX（負数は検査時に例外）。
// int32座標・未整列可・入力不変、O(N log N)時間/O(N)領域。uint64_t超過は常時overflow_error。
// nearest_two_points_sorted(first,last): 整列済み範囲版。(x,y)辞書順の非減少を前提に初回sortを省く。検査時はコピー中に順序を確認。
// 戻り値はnearest_two_pointsと同じで、添字は渡した列内。O(N log N)時間/O(N)領域、入力不変。
// nearest_two_points_sorted(first,count): 整列済みfirstからcount個の版。count>=0かつ指定個数を参照可能。戻り値・計算量は範囲版と同じ。
// nearest_two_points_sorted(points): 整列済みvector全体版。戻り値・前提・計算量は範囲版と同じ。


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


// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_NEAREST_TWO_POINTS_TYPES_DEFINED
#define SIRKIT_NEAREST_TWO_POINTS_TYPES_DEFINED
using P = std::pair<std::int32_t, std::int32_t>;

struct NearestTwoPointsResult {
    std::uint64_t distance_squared;
    std::size_t first_index;
    std::size_t second_index;
};
#endif

#ifndef SIRKIT_INTERNAL_NEAREST_TWO_POINTS_DEFINED
#define SIRKIT_INTERNAL_NEAREST_TWO_POINTS_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ {
template<class Index>
class NearestTwoPointsSolver {
    struct Node {
        std::int32_t x;
        std::int32_t y;
        Index index;
        // 作業領域は各整列パスで全要素を代入してから読む。
        Node() noexcept {}
        Node(std::int32_t a, std::int32_t b, Index i) noexcept : x(a), y(b), index(i) {}
    };
#if (defined(__SIZEOF_INT128__)) && (!defined(SIRKIT_FORCE_STANDARD_ARITHMETIC))
    // GCC/Clangの整数拡張。標準C++の型ではない。
    __extension__ using Distance = unsigned __int128;
    // 差はUINT32_MAX以下なので、単独の二乗は64bitに収まる。
    static Distance square(std::uint64_t d) { return Distance(d * d); }
    static Distance add(Distance a, Distance b) { return a + b; }
    static std::uint64_t narrow(Distance d) {
        if (d > std::numeric_limits<std::uint64_t>::max())
            throw std::overflow_error("nearest_two_points: squared distance exceeds uint64_t");
        return static_cast<std::uint64_t>(d);
    }
#else
    // 座標差は32ビット以内。二乗和だけ65ビット必要になる。
    struct Distance {
        std::uint64_t low;
        bool high;
        friend bool operator<(Distance a, Distance b) {
            return a.high != b.high ? !a.high : a.low < b.low;
        }
    };
    static Distance square(std::uint64_t d) { return {d * d, false}; }
    static Distance add(Distance a, Distance b) {
        const auto low = a.low + b.low;
        return {low, low < a.low};
    }
    static std::uint64_t narrow(Distance d) {
        if (d.high)
            throw std::overflow_error("nearest_two_points: squared distance exceeds uint64_t");
        return d.low;
    }
#endif
    // 外接矩形内の全距離が64bitに収まる経路で使う。
    static std::uint64_t low(const Distance& value) {
#if (defined(__SIZEOF_INT128__)) && (!defined(SIRKIT_FORCE_STANDARD_ARITHMETIC))
        return static_cast<std::uint64_t>(value);
#else
        return value.low;
#endif
    }
    static Distance from_low(std::uint64_t value) {
#if (defined(__SIZEOF_INT128__)) && (!defined(SIRKIT_FORCE_STANDARD_ARITHMETIC))
        return Distance(value);
#else
        return {value, false};
#endif
    }
    static bool fits_low(const Distance& value) {
#if (defined(__SIZEOF_INT128__)) && (!defined(SIRKIT_FORCE_STANDARD_ARITHMETIC))
        return value <= std::numeric_limits<std::uint64_t>::max();
#else
        return !value.high;
#endif
    }
    std::vector<Node> nodes_;
    std::vector<Node> buffer_;
    Distance best_{};
    std::size_t best_first_ = 0;
    std::size_t best_second_ = 1;
    bool zero_ = false;

#ifndef SKIP_RUNTIME_CHECKS
    template<std::integral Integer>
    static bool valid_coordinate(Integer value) {
        if constexpr (std::numeric_limits<Integer>::digits <= 31) {
            return true;
        } else if constexpr (std::is_signed_v<Integer>) {
            return value >= static_cast<Integer>(std::numeric_limits<std::int32_t>::min()) &&
                   value <= static_cast<Integer>(std::numeric_limits<std::int32_t>::max());
        } else {
            return value <= static_cast<Integer>(std::numeric_limits<std::int32_t>::max());
        }
    }
#endif
    static std::uint64_t difference(std::int32_t a, std::int32_t b) {
        const std::int64_t d = std::int64_t(a) - b;
        return static_cast<std::uint64_t>(d < 0 ? -d : d);
    }
    static Distance distance(const Node& a, const Node& b) {
        return add(square(difference(a.x, b.x)), square(difference(a.y, b.y)));
    }
    static bool by_y(const Node& a, const Node& b) {
        return a.y != b.y ? a.y < b.y : a.x < b.x;
    }
    void update(const Node& a, const Node& b) {
        const auto d = distance(a, b);
        if (d < best_) {
            best_ = d;
            best_first_ = std::min(a.index, b.index);
            best_second_ = std::max(a.index, b.index);
            zero_ = a.x == b.x && a.y == b.y;
        }
    }
    // 呼出側で二乗和がUINT64_MAX以下であることを保証する。
    void update64(const Node& a, const Node& b) {
        const auto dx = difference(a.x, b.x), dy = difference(a.y, b.y);
        const auto d = dx * dx + dy * dy;
        if (d < low(best_)) {
            best_ = from_low(d);
            best_first_ = std::min(a.index, b.index);
            best_second_ = std::max(a.index, b.index);
            zero_ = a.x == b.x && a.y == b.y;
        }
    }
    // 符号bitを反転したxを10/11/11bitの3パスで基数整列する。
    template<bool ByY = false>
    unsigned sort_by_coordinate() {
        [[maybe_unused]] std::int32_t min_y = std::numeric_limits<std::int32_t>::max();
        [[maybe_unused]] std::int32_t max_y = std::numeric_limits<std::int32_t>::min();
        // Indexの範囲内の点数を扱うので、合計と最終offsetも同じ型に収まる。
        std::array<std::array<Index, 2048>, 3> counts{};
        for (const auto& node : nodes_) {
            if constexpr (!ByY) {
                min_y = std::min(min_y, node.y);
                max_y = std::max(max_y, node.y);
            }
            const auto key = static_cast<std::uint32_t>(ByY ? node.y : node.x) ^ UINT32_C(0x80000000);
            ++counts[0][key & 1023U];
            ++counts[1][(key >> 10) & 2047U];
            ++counts[2][key >> 21];
        }
        buffer_.resize(nodes_.size());
        Node* input = nodes_.data();
        Node* output = buffer_.data();
        for (unsigned pass = 0; pass < 3; ++pass) {
            auto& offsets = counts[pass];
            std::size_t active = 0;
            for (const auto count : offsets) active += count != 0;
            if (active <= 1) continue;
            Index sum = 0;
            for (auto& count : offsets) { const auto n = count; count = sum; sum += n; }
            for (std::size_t i = 0; i < nodes_.size(); ++i) {
                const auto key = static_cast<std::uint32_t>(ByY ? input[i].y : input[i].x) ^ UINT32_C(0x80000000);
                output[offsets[(key >> (pass == 0 ? 0 : pass == 1 ? 10 : 21)) & (pass == 0 ? 1023U : 2047U)]++] = input[i];
            }
            std::swap(input, output);
        }
        if (input != nodes_.data()) nodes_.swap(buffer_);
        // bit0は64bit距離、bit1は全点が同じy座標。
        if constexpr (!ByY) {
            return unsigned(fits_low(add(square(difference(nodes_.back().x, nodes_.front().x)),
                                         square(difference(max_y, min_y)))))
                 | (unsigned(min_y == max_y) << 1);
        } else return 0;
    }
    // 同区間の両配列をx順で開始し、outputをy順で返す。inputは作業用。
    void solve(Node* input, Node* output, std::size_t left, std::size_t right) {
        if (zero_) return;
        if (right - left <= 16) {
            for (auto i = left; i < right; ++i)
                for (auto j = i + 1; j < right; ++j) update(output[i], output[j]);
            std::sort(output + left, output + right, by_y);
            return;
        }
        const auto middle = left + (right - left) / 2;
        const auto split_x = input[middle].x;
        solve(output, input, left, middle);
        solve(output, input, middle, right);
        if (zero_) return;
        // 左半分のxは右半分以下。同じyでは左を先にすればよい。
        std::merge(input + left, input + middle, input + middle, input + right,
                   output + left, [](const Node& a, const Node& b) { return a.y < b.y; });
        auto used = left;
        for (auto i = left; i < right; ++i) {
            const Node current = output[i];
            if (!(square(difference(current.x, split_x)) < best_)) continue;
            for (auto j = used; j > left;) {
                const Node& previous = input[--j];
                if (!(square(difference(current.y, previous.y)) < best_)) break;
                update(current, previous);
                if (zero_) return;
            }
            input[used++] = current;
        }
    }
    // 座標順なので、1軸だけで現在の最短距離以上なら以降も候補にならない。
    template<bool ByY>
    void scan_leaf64(const Node* points, std::size_t left, std::size_t right) {
        for (auto i = left; i < right; ++i)
            for (auto j = i + 1; j < right; ++j) {
                const auto delta = ByY ? difference(points[i].y, points[j].y)
                                      : difference(points[i].x, points[j].x);
                if (!(delta * delta < low(best_))) break;
                update64(points[i], points[j]);
                if (zero_) return;
            }
    }
    void solve64(Node* input, Node* output, std::size_t left, std::size_t right) {
        if (zero_) return;
        if (right - left <= 16) {
            const auto span_x = difference(output[left].x, output[right - 1].x);
            std::sort(output + left, output + right, by_y);
            const auto span_y = difference(output[left].y, output[right - 1].y);
            // inputはx順、outputはy順。広がりの大きい軸で打ち切る。
            if (span_y >= span_x) scan_leaf64<true>(output, left, right);
            else scan_leaf64<false>(input, left, right);
            return;
        }
        const auto middle = left + (right - left) / 2;
        const auto split_x = input[middle].x;
        solve64(output, input, left, middle);
        solve64(output, input, middle, right);
        if (zero_) return;
        // 左半分のxは右半分以下。同じyでは左を先にすればよい。
        std::merge(input + left, input + middle, input + middle, input + right,
                   output + left, [](const Node& a, const Node& b) { return a.y < b.y; });
        auto used = left;
        for (auto i = left; i < right; ++i) {
            const Node current = output[i];
            const auto dx = difference(current.x, split_x);
            if (!(dx * dx < low(best_))) continue;
            for (auto j = used; j > left;) {
                const Node& previous = input[--j];
                const auto dy = difference(current.y, previous.y);
                if (!(dy * dy < low(best_))) break;
                update64(current, previous);
                if (zero_) return;
            }
            input[used++] = current;
        }
    }

public:
    template<bool Sorted = false, std::size_t kNodeLimit = std::numeric_limits<std::size_t>::max(),
             std::input_iterator Iterator, std::sentinel_for<Iterator> Sentinel>
    static std::optional<NearestTwoPointsResult> run(Iterator first, Sentinel last) {
        NearestTwoPointsSolver solver;
        const auto capacity = std::min(kNodeLimit, solver.nodes_.max_size());
        if constexpr (std::sized_sentinel_for<Sentinel, Iterator>) {
            const auto count = last - first;
#ifndef SKIP_RUNTIME_CHECKS
            if (count < 0) throw std::invalid_argument("nearest_two_points: reversed range");
#endif
            if (std::cmp_greater(count, capacity))
                throw std::length_error("nearest_two_points: too many points");
            solver.nodes_.reserve(static_cast<std::size_t>(count));
        }
        for (; first != last; ++first) {
            const auto& point = *first;
            static_assert(std::integral<std::remove_cvref_t<decltype(point.first)>> &&
                          std::integral<std::remove_cvref_t<decltype(point.second)>>,
                          "nearest_two_points requires integral pair coordinates");
#ifndef SKIP_RUNTIME_CHECKS
            if (!valid_coordinate(point.first) || !valid_coordinate(point.second))
                throw std::out_of_range("nearest_two_points: coordinates must fit int32_t");
#endif
            if (solver.nodes_.size() == capacity)
                throw std::length_error("nearest_two_points: too many points");
#ifndef SKIP_RUNTIME_CHECKS
            if constexpr (Sorted) {
                if (!solver.nodes_.empty()) {
                    const auto& previous = solver.nodes_.back();
                    if (static_cast<std::int32_t>(point.first) < previous.x ||
                        (static_cast<std::int32_t>(point.first) == previous.x &&
                         static_cast<std::int32_t>(point.second) < previous.y))
                        throw std::invalid_argument("nearest_two_points_sorted: unordered input");
                }
            }
#endif
            solver.nodes_.push_back({static_cast<std::int32_t>(point.first),
                                     static_cast<std::int32_t>(point.second),
                                     static_cast<Index>(solver.nodes_.size())});
        }
        if (solver.nodes_.size() < 2) return std::nullopt;
        if constexpr (!Sorted) {
            if (solver.nodes_.size() == 2)
                return NearestTwoPointsResult{narrow(distance(solver.nodes_[0], solver.nodes_[1])), 0, 1};
        }
        [[maybe_unused]] unsigned coordinate_flags = 0;
        if constexpr (!Sorted) {

            if (solver.nodes_.size() >= 2048) {
                if (solver.nodes_[0].x == solver.nodes_[1].x &&
                    solver.nodes_[0].y == solver.nodes_[1].y)
                    return NearestTwoPointsResult{0, 0, 1};
                coordinate_flags = solver.sort_by_coordinate();
            }
            else std::sort(solver.nodes_.begin(), solver.nodes_.end(), [](const Node& a, const Node& b) {
                return a.x != b.x ? a.x < b.x : a.y < b.y;
            });
        }
        if constexpr (!Sorted) {
            if (solver.nodes_.front().x == solver.nodes_.back().x) {
                if (solver.nodes_.size() >= 2048) solver.template sort_by_coordinate<true>();
                coordinate_flags |= 4U; // bit2は全点が同じx座標。
            }
        }
        solver.best_ = distance(solver.nodes_[0], solver.nodes_[1]);
        solver.best_first_ = std::min(solver.nodes_[0].index, solver.nodes_[1].index);
        solver.best_second_ = std::max(solver.nodes_[0].index, solver.nodes_[1].index);
        solver.zero_ = solver.nodes_[0].x == solver.nodes_[1].x &&
                       solver.nodes_[0].y == solver.nodes_[1].y;
        if constexpr (!Sorted) {
            if (!solver.zero_ && (coordinate_flags & 6U)) {
                for (std::size_t i = 2; i < solver.nodes_.size(); ++i) {
                    solver.update64(solver.nodes_[i - 1], solver.nodes_[i]);
                    if (solver.zero_) break;
                }
                return NearestTwoPointsResult{narrow(solver.best_), solver.best_first_, solver.best_second_};
            }
        }
        if (!solver.zero_) {
            // 最初だけ両配列のx順を揃える。
            solver.buffer_ = solver.nodes_;
            if constexpr (!Sorted) {
                if (coordinate_flags & 1U) solver.solve64(solver.nodes_.data(), solver.buffer_.data(), 0, solver.nodes_.size());
                else solver.solve(solver.nodes_.data(), solver.buffer_.data(), 0, solver.nodes_.size());
            } else solver.solve(solver.nodes_.data(), solver.buffer_.data(), 0, solver.nodes_.size());
        }
        return NearestTwoPointsResult{narrow(solver.best_), solver.best_first_, solver.best_second_};
    }
};
} // namespace reinsirk::detail
#endif

#ifndef SIRKIT_NEAREST_TWO_POINTS_DEFINED
#define SIRKIT_NEAREST_TWO_POINTS_DEFINED
template<std::input_iterator Iterator, std::sentinel_for<Iterator> Sentinel>
std::optional<NearestTwoPointsResult> nearest_two_points(Iterator first, Sentinel last) {
    // 小入力はsize_t経路を保ち、大入力だけ点ごとの添字を圧縮する。
    if constexpr (std::sized_sentinel_for<Sentinel, Iterator>) {
        const auto n = last - first;
        if (n >= 2048 && std::cmp_less_equal(n, std::numeric_limits<std::uint32_t>::max()))
            return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::NearestTwoPointsSolver<std::uint32_t>::run(std::move(first), std::move(last));
    }
    return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::NearestTwoPointsSolver<std::size_t>::run(std::move(first), std::move(last));
}

template<std::input_iterator Iterator>
std::optional<NearestTwoPointsResult> nearest_two_points(Iterator first, std::ptrdiff_t count) {
#ifndef SKIP_RUNTIME_CHECKS
    if (count < 0) throw std::invalid_argument("nearest_two_points: negative count");
#endif
    return nearest_two_points(std::counted_iterator(std::move(first), count), std::default_sentinel);
}

template<class Point, class Allocator>
std::optional<NearestTwoPointsResult> nearest_two_points(const std::vector<Point, Allocator>& points) {
    return nearest_two_points(points.begin(), points.end());
}
#endif

#ifndef SIRKIT_CLOSEST_PAIR_DEFINED
#define SIRKIT_CLOSEST_PAIR_DEFINED
template<std::input_iterator Iterator>
std::uint64_t closest_pair(Iterator first, int n) {
    const auto result = nearest_two_points(std::move(first), static_cast<std::ptrdiff_t>(n));
    return result ? result->distance_squared : std::numeric_limits<std::int32_t>::max();
}
#endif

#ifndef SIRKIT_NEAREST_TWO_POINTS_SORTED_DEFINED
#define SIRKIT_NEAREST_TWO_POINTS_SORTED_DEFINED
template<std::input_iterator Iterator, std::sentinel_for<Iterator> Sentinel>
std::optional<NearestTwoPointsResult> nearest_two_points_sorted(Iterator first, Sentinel last) {
    // 小入力はsize_t経路を保ち、大入力だけ点ごとの添字を圧縮する。
    if constexpr (std::sized_sentinel_for<Sentinel, Iterator>) {
        const auto n = last - first;
        if (n >= 2048 && std::cmp_less_equal(n, std::numeric_limits<std::uint32_t>::max()))
            return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::NearestTwoPointsSolver<std::uint32_t>::run<true>(std::move(first), std::move(last));
    }
    return /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::NearestTwoPointsSolver<std::size_t>::run<true>(std::move(first), std::move(last));
}
template<std::input_iterator Iterator>
std::optional<NearestTwoPointsResult> nearest_two_points_sorted(Iterator first, std::ptrdiff_t count) {
#ifndef SKIP_RUNTIME_CHECKS
    if (count < 0) throw std::invalid_argument("nearest_two_points_sorted: negative count");
#endif
    return nearest_two_points_sorted(std::counted_iterator(std::move(first), count), std::default_sentinel);
}
template<class Point, class Allocator>
std::optional<NearestTwoPointsResult> nearest_two_points_sorted(const std::vector<Point, Allocator>& points) {
    return nearest_two_points_sorted(points.begin(), points.end());
}
#endif

} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
