# cython: language_level=3

"""C-API for zen_fronts.pareto_core._pareto_rating_cy.

This .pxd allows other Cython extension modules (e.g. Monte-Carlo selection)
to call the Pareto ranking kernel without going through Python.

Contract
--------
`rate_population_inplace`:
  - performs no allocations;
  - is safe to call under `nogil`;
  - expects all objectives are *maximized*;
  - fills caller-provided buffers.
"""

cimport numpy as cnp

ctypedef cnp.float64_t F64
ctypedef cnp.int64_t I64
ctypedef cnp.int32_t I32
ctypedef unsigned char U8


cdef struct Pair:
    double val
    int idx


cdef struct OrderKey:
    long front
    double neg_score
    long idx


cdef int rate_population_inplace(
    double[:, ::1] X,            # (N,M) objectives, maximize
    int mode_i,                  # 0=values, 1=ranks
    int within_mode_i,           # 0=crowding
    I64[::1] front_id,           # (N,)
    double[::1] within_score,    # (N,)
    I64[::1] order,              # (N,) global order (front asc, within desc, idx asc)
    I64[::1] offsets,            # (N+1,) packed fronts offsets
    I64[::1] items,              # (N,) packed fronts items
    U8[:, ::1] dom,              # (N,N) domination matrix scratch/output
    I32[::1] n_dom,              # (N,) scratch
    I32[::1] cur,                # (N,) scratch
    I32[::1] nxt,                # (N,) scratch
    Pair* pairs,                 # scratch, len>=N
    OrderKey* keys,              # scratch, len>=N
    double[:, ::1] R,            # (N,M) scratch for ranks when mode_i==1
) noexcept nogil
