//------------------------------------------------------------------------------
// GxB_BinaryOp_new_IndexOp_arena: create a new user-defined binary op
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//------------------------------------------------------------------------------

#include "GB.h"

#define GB_FREE_ALL ;

// GxB_BinaryOp_new_IndexOp: create a new binary op from an index binary op
GrB_Info GxB_BinaryOp_new_IndexOp_arena
(
    GrB_BinaryOp *binop_handle,     // handle of binary op to create
    GxB_IndexBinaryOp idxbinop,     // based on this index binary op
    GrB_Scalar theta,               // theta value to bind to the new binary op
    const int header_arena
)
{

    //--------------------------------------------------------------------------
    // check inputs
    //--------------------------------------------------------------------------

    GrB_Info info ;
    GB_CHECK_INIT ;
    GB_RETURN_IF_NULL (binop_handle) ;
    (*binop_handle) = NULL ;
    GB_RETURN_IF_NULL_OR_FAULTY (idxbinop) ;
    GB_RETURN_IF_NULL_OR_INVALID (theta) ;
    GB_OK (GB_check_arena (header_arena)) ;

    if (!GB_Type_compatible (idxbinop->theta_type, theta->type))
    { 
        return (GrB_DOMAIN_MISMATCH) ;
    }

    //--------------------------------------------------------------------------
    // allocate the binary op
    //--------------------------------------------------------------------------

    uint64_t mem = GB_mem (header_arena, 0) ;
    uint64_t header_mem = mem ;
    GrB_BinaryOp
        binop = GB_CALLOC_MEMORY (1, sizeof (struct GB_BinaryOp_opaque),
            &header_mem) ;
    if (binop == NULL)
    { 
        // out of memory
        return (GrB_OUT_OF_MEMORY) ;
    }
    binop->header_mem = header_mem ;

    //--------------------------------------------------------------------------
    // create the binary op
    //--------------------------------------------------------------------------

    // copy the index binary op contents into the binary op
    memcpy (binop, idxbinop, sizeof (struct GB_BinaryOp_opaque)) ;

    // remove the components owned by the index binary op
    binop->user_name = NULL ; binop->user_name_mem = 0 ;
    binop->defn = NULL ; binop->defn_mem = 0 ;

    bool jitable = (idxbinop->hash != UINT64_MAX) ;

    info = GB_op_name_and_defn (
        // output:
        binop->name, &(binop->name_len), &(binop->hash),
        &(binop->defn), &(binop->defn_mem),
        // input:
        idxbinop->name, idxbinop->defn, true, jitable, header_arena) ;
    if (info != GrB_SUCCESS)
    { 
        // out of memory
        GB_FREE_MEMORY (&binop, header_mem) ;
        return (info) ;
    }

    //--------------------------------------------------------------------------
    // copy theta into the new binary op
    //--------------------------------------------------------------------------

    binop->theta = GB_MALLOC_MEMORY (1, binop->theta_type->size,
        &(binop->theta_mem)) ;
    if (binop->theta == NULL)
    { 
        // out of memory
        GB_Op_free ((GB_Operator *) (&binop)) ;
        return (GrB_OUT_OF_MEMORY) ;
    }

    GB_cast_scalar (binop->theta, binop->theta_type->code,
        theta->x, theta->type->code, theta->type->size) ;

    //--------------------------------------------------------------------------
    // return result
    //--------------------------------------------------------------------------

    ASSERT_BINARYOP_OK (binop, "new user-defined binary op (based on idxbinop)",
        GB0) ;
    (*binop_handle) = binop ;
    return (GrB_SUCCESS) ;
}

