/*
 * Copyright (c) 2022, Shenzhen CVA Innovation CO.,LTD
 * All rights reserved.
 *
 * Shenzhen CVA Innovation CO.,LTD (CVA chip) is supplying this file for use
 * exclusively with CVA's microcontroller products. This file can be freely
 * distributed within development tools that are supporting such microcontroller
 * products.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
 * CVA SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
 * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 */

#ifndef _SORT_H_
#define _SORT_H_

/*! \brief  Contains public interface to various functions related
 *          to the sort algorithms
 */

/*******************************************************************************
 * the includes
 ******************************************************************************/

#include <stdint.h>
#include "common/iqmath/iqmath.h"

#ifdef __cplusplus
extern "C" {
#endif

/*******************************************************************************
 * the defines
 ******************************************************************************/

/*******************************************************************************
 * the typedefs
 ******************************************************************************/

/*******************************************************************************
 * the globals
 ******************************************************************************/

/*******************************************************************************
 * the function prototypes
 ******************************************************************************/

/*! \brief Return the max value in an array
 *
 * This function returns the max value in an array
 *
 * \param[in] array[] : the array to search
 * \param[in] len : the length of the array
 */
static inline _iq MaxInArray(const _iq array[], uint16_t len)
{
    _iq      retval = array[0];
    uint16_t i      = 0;
    for(i = 0; i < len; i++)
    {
        if(array[i] > retval)
        {
            retval = array[i];
        }
    }
    return retval;
}

/*! \brief  Return the minimum value in an array
 *
 * This function returns the minimun value in an array
 *
 * \param[in] array[] : the array to search
 * \param[in] len : the length of the array
 */
static inline _iq MinInArray(const _iq array[], uint16_t len)
{
    _iq      retval = array[0];
    uint16_t i      = 0;
    for(i = 0; i < len; i++)
    {
        if(array[i] < retval)
        {
            retval = array[i];
        }
    }
    return retval;
}

/*! \brief  Return the max absolute value in an array
 *
 * This function returns the max absolute value in an array
 *
 * \param[in] array[] : the array to search
 * \param[in] len : the length of the array
 */
static inline _iq MaxAbsInArray(const _iq array[], uint16_t len)
{
    _iq      retval = _IQabs(array[0]);
    uint16_t i      = 0;
    for(i = 0; i < len; i++)
    {
        if(_IQabs(array[i]) > retval)
        {
            retval = _IQabs(array[i]);
        }
    }
    return retval;
}

/*! \brief  Return the minimum absolute value in an array
 *
 * This function returns the minimum absolute value in an array
 *
 * \param[in] array[] : the array to search
 * \param[in] len : the length of the array
 */
static inline _iq MinAbsInArray(const _iq array[], uint16_t len)
{
    _iq      retval = _IQabs(array[0]);
    uint16_t i      = 0;
    for(i = 0; i < len; i++)
    {
        if(_IQabs(array[i]) < retval)
        {
            retval = _IQabs(array[i]);
        }
    }
    return retval;
}

#ifdef __cplusplus
}
#endif /* extern "C" */

#endif /* _SORT_H_ */
