mirror of
https://gitee.com/moluo-tech/CodeBrick.git
synced 2025-01-30 04:42:53 +08:00
118 lines
3.0 KiB
C
118 lines
3.0 KiB
C
/******************************************************************************
|
||
* @brief ͨ<>ö<EFBFBD><C3B6>й<EFBFBD><D0B9><EFBFBD>
|
||
*
|
||
* Copyright (c) 2017~2020, <morro_luo@163.com>
|
||
*
|
||
* SPDX-License-Identifier: Apache-2.0
|
||
*
|
||
* Change Logs:
|
||
* Date Author Notes
|
||
* 2017-03-19 Morro Initial version.
|
||
******************************************************************************/
|
||
#include "queue.h"
|
||
#include <stddef.h>
|
||
#include <string.h>
|
||
|
||
/* @brief <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
* @param[in] q - <20><><EFBFBD>й<EFBFBD><D0B9><EFBFBD><EFBFBD><EFBFBD>
|
||
* @param[in] base - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
||
* @param[in] size - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>
|
||
* @param[in] element_size - <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD>ش<EFBFBD>С(sizeof(type))
|
||
* @return true - <20><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>, false - <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
|
||
*/
|
||
bool queue_create(queue_t *q, void *base, int size, int element_size)
|
||
{
|
||
if (q == NULL || base == NULL || size== 0 || element_size == 0)
|
||
return false;
|
||
q->base = base;
|
||
q->size = size;
|
||
q->element_size = element_size;
|
||
q->front = q->rear = 0;
|
||
return true;
|
||
}
|
||
|
||
|
||
/* @brief <20>ж϶<D0B6><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>
|
||
* @return true - <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>, false - <20><><EFBFBD>зǿ<D0B7>
|
||
*/
|
||
bool queue_is_empty(queue_t *q)
|
||
{
|
||
return q->front == q->rear;
|
||
}
|
||
|
||
/* @brief <20>ж϶<D0B6><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>
|
||
* @return true - <20><><EFBFBD>з<EFBFBD><D0B7><EFBFBD>, false - <20><><EFBFBD><EFBFBD>δ<EFBFBD><CEB4>
|
||
*/
|
||
bool queue_is_full(queue_t *q)
|
||
{
|
||
return (q->rear + 1) % q->size == q->front;
|
||
}
|
||
|
||
/* @brief <20><><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β)
|
||
* @param[in] element - <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
|
||
* @return true - <20>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD>, false - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*/
|
||
bool queue_put(queue_t *q, void *element)
|
||
{
|
||
if (queue_is_full(q))
|
||
return false;
|
||
memcpy((unsigned char *)q->base + q->rear * q->element_size, element,
|
||
q->element_size);
|
||
q->rear = (q->rear + 1) % q->size;
|
||
return true;
|
||
}
|
||
|
||
/* @brief <20><><EFBFBD><EFBFBD>(<28>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>)
|
||
* @param[in] element - ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>صĵ<D8B5>ַ
|
||
* @return true - <20>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD>, false - <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>
|
||
*/
|
||
bool queue_get(queue_t *q, void *element)
|
||
{
|
||
if (queue_is_empty(q))
|
||
return false;
|
||
memcpy(element, (unsigned char *)q->base + q->front * q->element_size,
|
||
q->element_size);
|
||
q->front = (q->front + 1) % q->size;
|
||
return true;
|
||
}
|
||
|
||
/* @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>ַ(Ԥ<><D4A4>ȡ)
|
||
* @param[in] element - ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>صĵ<D8B5>ַ
|
||
* @return true - <20><>ȡ<EFBFBD>ɹ<EFBFBD>, false - <20><>ȡʧ<C8A1>ܶ<EFBFBD><DCB6><EFBFBD>Ϊ<EFBFBD><CEAA>
|
||
*/
|
||
bool queue_peek(queue_t *q, void **element)
|
||
{
|
||
if (queue_is_empty(q))
|
||
return false;
|
||
*element = (void *)((unsigned char *)q->base + q->front *
|
||
q->element_size);
|
||
return true;
|
||
}
|
||
|
||
/* @brief ɾ<><C9BE><EFBFBD><EFBFBD>βԪ<CEB2><D4AA>
|
||
* @return true - <20>ɹ<EFBFBD><C9B9>Ƴ<EFBFBD>, false - <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>,
|
||
*/
|
||
bool queue_del(queue_t *q)
|
||
{
|
||
if (queue_is_empty(q))
|
||
return false;
|
||
q->front = (q->front + 1) % q->size;
|
||
return true;
|
||
}
|
||
|
||
/* @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>
|
||
* @return <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>
|
||
*/
|
||
int queue_size(queue_t *q)
|
||
{
|
||
return (q->rear + q->size - q->front ) % q->size;
|
||
}
|
||
|
||
/* @brief <20><><EFBFBD>ն<EFBFBD><D5B6><EFBFBD>
|
||
* @return none
|
||
*/
|
||
void queue_clr(queue_t *q)
|
||
{
|
||
q->front = q->rear = 0;
|
||
}
|