telodendria/src/Queue.c

177 lines
2.9 KiB
C
Raw Normal View History

2022-08-24 13:06:27 +00:00
/*
2022-12-26 15:52:52 +00:00
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
2022-08-24 13:06:27 +00:00
*
* 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 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>
#include <Memory.h>
2022-08-24 14:06:14 +00:00
2022-08-24 13:06:27 +00:00
struct Queue
{
void **items;
size_t size;
2022-08-24 14:06:14 +00:00
size_t front;
size_t rear;
2022-08-24 13:06:27 +00:00
};
Queue *
QueueCreate(size_t size)
{
Queue *q;
if (!size)
{
/* Can't have a queue of length zero */
return NULL;
}
q = Malloc(sizeof(Queue));
2022-08-24 13:06:27 +00:00
if (!q)
{
return NULL;
}
q->items = Malloc(size * sizeof(void *));
2022-08-24 13:06:27 +00:00
if (!q->items)
{
Free(q);
2022-08-24 13:06:27 +00:00
return NULL;
}
q->size = size;
2022-08-24 14:06:14 +00:00
q->front = size + 1;
q->rear = size + 1;
return q;
2022-08-24 13:06:27 +00:00
}
void
QueueFree(Queue * q)
{
if (q)
{
Free(q->items);
2022-08-24 13:06:27 +00:00
}
Free(q);
2022-08-24 13:06:27 +00:00
}
int
2022-08-24 14:06:14 +00:00
QueueFull(Queue * q)
2022-08-24 13:06:27 +00:00
{
if (!q)
{
return 0;
}
return ((q->front == q->rear + 1) || (q->front == 0 && q->rear == q->size - 1));
}
int
2022-08-24 14:06:14 +00:00
QueueEmpty(Queue * q)
2022-08-24 13:06:27 +00:00
{
if (!q)
{
return 0;
}
2022-08-24 14:06:14 +00:00
return q->front == q->size + 1;
2022-08-24 13:06:27 +00:00
}
int
QueuePush(Queue * q, void *element)
{
if (!q || !element)
{
return 0;
}
if (QueueFull(q))
{
return 0;
}
2022-08-24 14:06:14 +00:00
if (q->front == q->size + 1)
2022-08-24 13:06:27 +00:00
{
q->front = 0;
}
if (q->rear == q->size + 1)
{
q->rear = 0;
}
2022-08-24 20:29:00 +00:00
else
{
q->rear = (q->rear + 1) % q->size;
}
2022-08-24 13:06:27 +00:00
q->items[q->rear] = element;
return 1;
}
void *
QueuePop(Queue * q)
{
void *element;
if (!q)
{
return NULL;
}
if (QueueEmpty(q))
{
return NULL;
}
element = q->items[q->front];
if (q->front == q->rear)
{
2022-09-17 19:59:47 +00:00
q->front = q->size + 1;
q->rear = q->size + 1;
2022-08-24 13:06:27 +00:00
}
else
{
q->front = (q->front + 1) % q->size;
}
return element;
}
void *
QueuePeek(Queue * q)
{
if (!q)
{
return NULL;
}
if (QueueEmpty(q))
{
return NULL;
}
return q->items[q->front];
}