160 lines
2.5 KiB
C
160 lines
2.5 KiB
C
#include <fx/queue.h>
|
|
|
|
size_t fx_queue_length(const struct fx_queue *q)
|
|
{
|
|
size_t i = 0;
|
|
struct fx_queue_entry *x = q->q_first;
|
|
while (x) {
|
|
i++;
|
|
x = x->qe_next;
|
|
}
|
|
|
|
return i;
|
|
}
|
|
|
|
void fx_queue_insert_before(
|
|
struct fx_queue *q,
|
|
struct fx_queue_entry *entry,
|
|
struct fx_queue_entry *before)
|
|
{
|
|
struct fx_queue_entry *x = before->qe_prev;
|
|
if (x) {
|
|
x->qe_next = entry;
|
|
} else {
|
|
q->q_first = entry;
|
|
}
|
|
|
|
entry->qe_prev = x;
|
|
|
|
before->qe_prev = entry;
|
|
entry->qe_next = before;
|
|
}
|
|
|
|
void fx_queue_insert_after(
|
|
struct fx_queue *q,
|
|
struct fx_queue_entry *entry,
|
|
struct fx_queue_entry *after)
|
|
{
|
|
struct fx_queue_entry *x = after->qe_next;
|
|
if (x) {
|
|
x->qe_prev = entry;
|
|
} else {
|
|
q->q_last = entry;
|
|
}
|
|
|
|
entry->qe_next = x;
|
|
|
|
after->qe_next = entry;
|
|
entry->qe_prev = after;
|
|
}
|
|
|
|
void fx_queue_push_front(struct fx_queue *q, struct fx_queue_entry *entry)
|
|
{
|
|
if (q->q_first) {
|
|
q->q_first->qe_prev = entry;
|
|
}
|
|
|
|
entry->qe_next = q->q_first;
|
|
entry->qe_prev = NULL;
|
|
|
|
q->q_first = entry;
|
|
|
|
if (!q->q_last) {
|
|
q->q_last = entry;
|
|
}
|
|
}
|
|
|
|
void fx_queue_push_back(struct fx_queue *q, struct fx_queue_entry *entry)
|
|
{
|
|
if (q->q_last) {
|
|
q->q_last->qe_next = entry;
|
|
}
|
|
|
|
entry->qe_prev = q->q_last;
|
|
entry->qe_next = NULL;
|
|
|
|
q->q_last = entry;
|
|
|
|
if (!q->q_first) {
|
|
q->q_first = entry;
|
|
}
|
|
}
|
|
|
|
struct fx_queue_entry *fx_queue_pop_front(struct fx_queue *q)
|
|
{
|
|
struct fx_queue_entry *x = q->q_first;
|
|
if (x) {
|
|
fx_queue_delete(q, x);
|
|
}
|
|
|
|
return x;
|
|
}
|
|
|
|
struct fx_queue_entry *fx_queue_pop_back(struct fx_queue *q)
|
|
{
|
|
struct fx_queue_entry *x = q->q_last;
|
|
if (x) {
|
|
fx_queue_delete(q, x);
|
|
}
|
|
|
|
return x;
|
|
}
|
|
|
|
void fx_queue_move(fx_queue *q, fx_queue_entry *dest, fx_queue_entry *src)
|
|
{
|
|
if (src->qe_next) {
|
|
src->qe_next->qe_prev = dest;
|
|
}
|
|
|
|
if (src->qe_prev) {
|
|
src->qe_prev->qe_next = dest;
|
|
}
|
|
|
|
if (q->q_first == src) {
|
|
q->q_first = dest;
|
|
}
|
|
|
|
if (q->q_last == src) {
|
|
q->q_last = dest;
|
|
}
|
|
|
|
memmove(dest, src, sizeof *src);
|
|
}
|
|
|
|
void fx_queue_delete(struct fx_queue *q, struct fx_queue_entry *entry)
|
|
{
|
|
if (!entry) {
|
|
return;
|
|
}
|
|
|
|
if (entry == q->q_first) {
|
|
q->q_first = q->q_first->qe_next;
|
|
}
|
|
|
|
if (entry == q->q_last) {
|
|
q->q_last = q->q_last->qe_prev;
|
|
}
|
|
|
|
if (entry->qe_next) {
|
|
entry->qe_next->qe_prev = entry->qe_prev;
|
|
}
|
|
|
|
if (entry->qe_prev) {
|
|
entry->qe_prev->qe_next = entry->qe_next;
|
|
}
|
|
|
|
entry->qe_next = entry->qe_prev = NULL;
|
|
}
|
|
|
|
void fx_queue_delete_all(struct fx_queue *q)
|
|
{
|
|
struct fx_queue_entry *x = q->q_first;
|
|
while (x) {
|
|
struct fx_queue_entry *next = x->qe_next;
|
|
x->qe_next = x->qe_prev = NULL;
|
|
x = next;
|
|
}
|
|
|
|
q->q_first = q->q_last = NULL;
|
|
}
|