Sobes.tech
Junior — Senior

Optimization of the cell search function in an array

livecode

Task condition

It is necessary to improve the readability and structure of the function that searches for an element in an array by its identifier and returns a pointer to the found control block. Rewrite the code using more understandable variable names and simplify the logic without changing the behavior.

/**
 * @fn ueUtlFndCell
 *
 * @brief Fill cell control block from cellArr by \p cellId
 *
 * @param[in]  targetCellId   identifier of the cell to find
 * @param[out] outCell        pointer to store the found control block.
 * @return     ROK on success
 *             RFAILED if cellId not found.
 */
PUBLIC S16 ueUtlFndCell(U16 targetCellId, CellCb **outCell)
{
    TRC2(ueUtlFndCell)

    for (U16 idx = 0; idx < UE_NUM_CELLS; idx++) {
        if (targetCellId == cellArr[idx].cellId) {
            *outCell = &cellArr[idx];
            RETVALUE(ROK);
        }
    }

    UE_LOG(UE_APP, UE_LOG_ERROR, "ueUtlFndCell: CellCb not found for cell id: %d\n", targetCellId);
    *outCell = NULLP;
    RETVALUE(RFAILED);
}