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

/*! \brief  Contains public interface to various functions related
 *          to a wrapped linked-list
 */

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

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

#ifdef __cplusplus
extern "C" {
#endif

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

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

/*! \brief  Declaration of LIST object
 */
typedef struct _ListNode_
{
    int32_t nextIndex; /*!< next index value */
    void   *pElement;  /*!< the pointer to the element */
} ListNode;

/*! \brief  Declaration of LIST object
 */
typedef struct _ListType_
{
    ListNode *nodeArray;     /*!< the pointer to the nodes, which must be an 1-D array */
    uint32_t  containerSize; /*!< the size of the container */
    uint32_t  length;        /*!< the length of list */
    ListNode  head;          /*!< the head node */
} ListType;

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

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

/*! \brief Initialize the LIST object.
 *
 * This function initialize the LIST instance
 *
 * \param[in] obj : pointer to List instance
 * \param[in] pNodes : pointer to node
 * \param[in] pContainer : pointer to container
 * \param[in] elemSize : element size in byte
 * \param[in] containerSize : the size of the container
 */
static inline void List_Init(ListType *obj, ListNode *pNodes, void *pContainer, uint32_t elemSize, uint32_t containerSize)
{
    obj->nodeArray     = pNodes;
    obj->containerSize = containerSize;
    uint32_t i         = 0;
    for(i = 0; i < obj->containerSize; ++i)
    {
        obj->nodeArray[i].pElement  = (void *)((unsigned char *)pContainer + i * elemSize);
        obj->nodeArray[i].nextIndex = -1; /* Next index is -1 representing not occupied */
    }
    obj->head.nextIndex = obj->containerSize; /* next index = containerSize Mark the list */
    obj->head.pElement  = NULL;
    obj->length         = 0;
}

/*! \brief Get the length of the list
 *
 * This function get the length of the list
 *
 * \param[in] obj : pointer to List instance
 * \return The length
 */
static inline uint32_t List_Length(const ListType *obj)
{
    return obj->length;
}

/*! \brief Insert the element at the given index
 *
 * This function insert the element at the given index.
 *
 * \note If the given index is larger than the last element index+1,
 *       or the vector is full, the function will do nothing
 *
 * \param[in] obj : pointer to List instance
 * \param[in] pElem : the pointer to the element
 * \param[in] elemSize : the element size
 * \param[in] index : the index to insert
 * \return the result of the operation
 *         - true : success
 *         - false : fail
 */
static inline bool List_Insert(ListType *obj, const void *pElem, uint32_t elemSize, uint32_t index)
{
    /* If full, we cannot insert more elements */
    if(obj->length == obj->containerSize)
    {
        return false;
    }

    /* If no enough elements*/
    if(index > obj->length)
    {
        return false;
    }

    /* Find first node not occupied*/
    uint32_t actualIndex      = 0;
    bool     actualIndexFound = false;
    uint32_t i                = 0;
    for(i = 0; i < obj->containerSize; ++i)
    {
        if(obj->nodeArray[i].nextIndex == -1)
        {
            actualIndexFound = true;
            actualIndex      = i;
            break;
        }
    }
    if(actualIndexFound == false)
    {
        return false; /* Must found */
    }

    /* Assign the value */
    unsigned char *src  = (unsigned char *)pElem;
    unsigned char *dest = (unsigned char *)(obj->nodeArray[actualIndex].pElement);
    uint32_t       byte;
    for(byte = 0; byte < elemSize; byte++)
    {
        *dest++ = *src++;
    }

    /* Find the previous node */
    if(index == 0)
    {
        /* Insert at the first node */
        obj->nodeArray[actualIndex].nextIndex = obj->head.nextIndex; /* Replace the first valid data node */
        obj->head.nextIndex                   = actualIndex;         /* Mark the new first node */
    }
    else
    {
        uint32_t prevIndex = obj->head.nextIndex;
        for(i = 0; i < index - 1; ++i)
        {
            prevIndex = obj->nodeArray[prevIndex].nextIndex;
        }
        obj->nodeArray[actualIndex].nextIndex = obj->nodeArray[prevIndex].nextIndex;
        obj->nodeArray[prevIndex].nextIndex   = actualIndex;
    }

    /* Increase the length */
    obj->length++;

    return true;
}

/*! \brief Get the element at the given index
 *
 * This function gets the element at the given index
 *
 * \note index must be in the range of 0-size
 *
 * \param[in] obj : pointer to List instance
 * \param[out] pElem : pointer to the element to get
 * \param[in] elemSize : size of element
 * \param[in] index : index of the element to get
 * \return result of the operation
 *         - true : success
 *         - false : failed
 */
static inline bool List_GetElement(const ListType *obj, void *pElem, uint32_t elemSize, uint32_t index)
{
    if(index >= obj->length)
    {
        return false;
    }

    uint32_t i           = 0;
    uint32_t actualIndex = obj->head.nextIndex;
    for(i = 0; i < index; ++i)
    {
        actualIndex = obj->nodeArray[actualIndex].nextIndex;
    }

    unsigned char *dest = (unsigned char *)pElem;
    unsigned char *src  = (unsigned char *)(obj->nodeArray[actualIndex].pElement);

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

    return true;
}

/*! \brief Get the element at the given index
 *
 * This function gets the element at the given index
 *
 * \note index must be in the range of 0-size-1
 *
 * \param[in] obj : pointer to List instance
 * \param[in] elemSize : the element size
 * \param[in] index : the index to get
 * \return the pointer to the element
 */
static inline const void *List_At(const ListType *obj, uint32_t elemSize, uint32_t index)
{
    if(index >= obj->length)
    {
        return ((void *)0);
    }

    uint32_t i           = 0;
    uint32_t actualIndex = obj->head.nextIndex;
    for(i = 0; i < index; ++i)
    {
        actualIndex = obj->nodeArray[actualIndex].nextIndex;
    }

    return obj->nodeArray[actualIndex].pElement;
}

/*! \brief Clear the whole vector
 *
 * This function clears the whole vector
 *
 * \param[in] obj : pointer to List instance
 */
static inline void List_Clear(ListType *obj)
{
    uint32_t i = 0;
    for(i = 0; i < obj->containerSize; ++i)
    {
        obj->nodeArray[i].nextIndex = -1; /* Next index is -1 representing not occupied */
    }
    obj->head.nextIndex = obj->containerSize;
    obj->head.pElement  = NULL;
    obj->length         = 0;
}

/*! \brief Remove the element at the given index
 *
 * This function removes the element at the given index
 *
 * \param[in] obj : pointer to List instance
 */
static inline bool List_Remove(ListType *obj, uint32_t elemSize, uint32_t index)
{
    /* If empty, we cannot remove any elements */
    if(obj->length == 0)
    {
        return false;
    }

    /* If no enough elements */
    if(index >= obj->length)
    {
        return false;
    }

    /* Find the previous node */
    if(index == 0)
    {
        /* Remove the first node */
        uint32_t currentIndex                  = obj->head.nextIndex;
        obj->head.nextIndex                    = obj->nodeArray[obj->head.nextIndex].nextIndex;
        obj->nodeArray[currentIndex].nextIndex = -1; /* Mark unoccupied */
    }
    else
    {
        uint32_t prevIndex = obj->head.nextIndex;
        uint32_t i         = 0;
        for(i = 0; i < index - 1; ++i)
        {
            prevIndex = obj->nodeArray[prevIndex].nextIndex;
        }
        uint32_t currentIndex                  = obj->nodeArray[prevIndex].nextIndex;
        obj->nodeArray[prevIndex].nextIndex    = obj->nodeArray[currentIndex].nextIndex;
        obj->nodeArray[currentIndex].nextIndex = -1; /* Mark unoccupied */
    }

    /* Decrease the length */
    obj->length--;

    return true;
}

/*! \brief  Whether the list is full
 *
 * This function returns whether the list is full.
 *
 * \param[in] obj : pointer to List instance
 */
static inline bool List_IsFull(const ListType *obj)
{
    return obj->length == obj->containerSize;
}

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

#endif /* _LIST_H_ */
