1 Star 9 Fork 2

Lamdonn/queue

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
queue.c 4.78 KB
一键复制 编辑 原始数据 按行查看 历史
Lamdonn 提交于 2024-11-27 18:27 +08:00 . Add insert and erase functions
/*********************************************************************************************************
* ------------------------------------------------------------------------------------------------------
* 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));
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/Lamdonn/queue.git
git@gitee.com:Lamdonn/queue.git
Lamdonn
queue
queue
master

搜索帮助