/*
 * 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 _TABLE3D_H_
#define _TABLE3D_H_

/*! \brief Contains public interface to various functions related
 *         to the 3-D table template
 */

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

#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

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

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

/*! \brief Declaration of 3-D table prototype
 * \note The container should be an array in the form of buffer[depthNum][rowNum][columnNum]
 */
typedef struct _Table3DType_
{
    void    *pContainer; /*!< The pointer to the container, which must be a 3-D array  */
    uint16_t rowNum;     /*!< Total row number of the container */
    uint16_t columnNum;  /*!< Total column number of the container */
    uint16_t depthNum;   /*!< Total depth number of the container */
} Table3DType;

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

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

/*! \brief Initialize the TABLE 3D object.
 *
 * This function initializes a 3-D table instance
 *
 * \note The container should be the 3-D array
 *
 * \param[in] obj : pointer to 3-D Table instance
 * \param[in] pContainer : the container (3-D array) for this table
 * \param[in] rowNum : the total row count of the table
 * \param[in] columnNum : the total column count of the table
 * \param[in] depthNum : the total depth count of the table
 */
static inline void Table3D_Init(Table3DType *obj, void *pContainer, uint16_t rowNum, uint16_t columnNum, uint16_t depthNum)
{
    obj->pContainer = pContainer;
    obj->rowNum     = rowNum;
    obj->columnNum  = columnNum;
    obj->depthNum   = depthNum;
}

/*! \brief Get a certain element determined by row, column, depth from the table
 *
 * This function gets a certain element determined by row, column, depth from the table
 *
 * \param[in] obj : pointer to 3-D Table instance
 * \param[out] pElem : pointer to the element
 * \param[in] elemSize : the size of element in byte
 * \param[in] row : the row of the output element
 * \param[in] column : the column of the output element
 * \param[in] depth : the depth of the output element
 * \return the result of the get operation
 *         - true : succees
 *         - false : fail to get the element
 */
static inline bool Table3D_GetElement(const Table3DType *obj, void *pElem, uint16_t elemSize, uint16_t row, uint16_t column, uint16_t depth)
{
    if(row > obj->rowNum - 1 || column > obj->columnNum - 1 || depth > obj->depthNum - 1)
    {
        return false;
    }

    int            i = 0;
    unsigned char *src;
    unsigned char *dest;

    dest = (unsigned char *)pElem;
    src  = (unsigned char *)(obj->pContainer) + elemSize * (depth * obj->rowNum * obj->columnNum + row * obj->columnNum + column);

    for(i = 0; i < elemSize; i++)
    {
        *dest++ = *src++;
    }

    return true;
}

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

#endif /* _TABLE3D_H_ */
