代码拉取完成,页面将自动刷新
/*********************************************************************************************************
* ------------------------------------------------------------------------------------------------------
* file description
* ------------------------------------------------------------------------------------------------------
* \file queue.c
* \brief This is a C language queue
* \author Lamdonn
* \version 1.1.0
********************************************************************************************************/
/*********************************************************************************************************
MIT License
Copyright (C) 2023 Lamdonn.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
********************************************************************************************************/
#include "queue.h"
#define at(i) (((char *)(queue->base))+(i)*(queue->dsize)) /* Address of void array */
#define assign(d, s) memcpy((d), (s), queue->dsize) /* Assign the value of array index s to array index d */
#define qa(i) at((queue->head + queue->capacity + (i)) % (queue->capacity)) /* Queue index address */
static void* memcpy(void* dest, const void* src, int n)
{
char* dst = dest;
const char* s = src;
while (n--) *dst++ = *s++;
return dest;
}
static void move_forward(queue queue, int index, int size)
{
for (int i = index; i < index + size; i++) assign(qa(i - 1), qa(i));
}
static void move_backward(queue queue, int index, int size)
{
for (int i = index + size; i > index; i--) assign(qa(i), qa(i - 1));
}
int queue_push(queue queue, void* data)
{
if (!queue) return 0;
if (queue->size == queue->capacity) return 0;
if (data) assign(at(queue->tail), data);
queue->tail = (queue->tail + 1) % queue->capacity;
queue->size++;
return 1;
}
int queue_push2(queue queue, void* data)
{
if (!queue) return 0;
if (queue->size < queue->capacity) return queue_push(queue, data);
if (data) assign(at(queue->tail), data);
queue->tail = (queue->tail + 1) % queue->capacity;
queue->head = (queue->head + 1) % queue->capacity;
return 1;
}
int queue_pop(queue queue, void* data)
{
if (!queue) return 0;
if (queue->size == 0) return 0;
if (data) assign(data, at(queue->head));
queue->head = (queue->head + 1) % queue->capacity;
queue->size--;
return 1;
}
int queue_insert(queue queue, int index, void* data)
{
if (!queue) return 0;
if (index < 0 || index > queue->size) return 0;
if (queue->size == queue->capacity) return 0;
if (index <= queue->size / 2) { move_forward(queue, 0, index); queue->head = (queue->head + queue->capacity - 1) % queue->capacity; }
else { move_backward(queue, index, queue->size - index); queue->tail = (queue->tail + 1) % queue->capacity; }
if (data) assign(at((queue->head + index) % (queue->capacity)), data);
queue->size++;
return 1;
}
int queue_erase(queue queue, int index, void* data)
{
if (!queue) return 0;
if (index < 0 || index >= queue->size) return 0;
if (queue->size == 0) return 0;
if (data) assign(data, at((queue->head + index) % (queue->capacity)));
if (index <= queue->size / 2) { move_backward(queue, 0, index); queue->head = (queue->head + 1) % queue->capacity; }
else { move_forward(queue, index + 1, queue->size - index - 1); queue->tail = (queue->tail + queue->capacity - 1) % queue->capacity; }
queue->size--;
return 1;
}
void queue_clear(queue queue)
{
if (!queue) return;
queue->tail = 0;
queue->head = 0;
queue->size = 0;
}
void* queue_data(queue queue, int index)
{
if (!queue) return 0;
if (index < 0 || index >= queue->size) return 0;
return (void*)at((queue->head + index) % (queue->capacity));
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。