2024-08-03 07:54:28 +01:00
|
|
|
#include <blue/core/queue.h>
|
|
|
|
|
|
2025-02-10 13:58:28 +00:00
|
|
|
size_t b_queue_length(const struct b_queue *q)
|
2024-08-03 07:54:28 +01:00
|
|
|
{
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
struct b_queue_entry *x = q->q_first;
|
|
|
|
|
while (x) {
|
|
|
|
|
i++;
|
|
|
|
|
x = x->qe_next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void b_queue_insert_before(
|
|
|
|
|
struct b_queue *q, struct b_queue_entry *entry, struct b_queue_entry *before)
|
|
|
|
|
{
|
|
|
|
|
struct b_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 b_queue_insert_after(
|
|
|
|
|
struct b_queue *q, struct b_queue_entry *entry, struct b_queue_entry *after)
|
|
|
|
|
{
|
|
|
|
|
struct b_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 b_queue_push_front(struct b_queue *q, struct b_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 b_queue_push_back(struct b_queue *q, struct b_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 b_queue_entry *b_queue_pop_front(struct b_queue *q)
|
|
|
|
|
{
|
|
|
|
|
struct b_queue_entry *x = q->q_first;
|
|
|
|
|
if (x) {
|
|
|
|
|
b_queue_delete(q, x);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct b_queue_entry *b_queue_pop_back(struct b_queue *q)
|
|
|
|
|
{
|
|
|
|
|
struct b_queue_entry *x = q->q_last;
|
|
|
|
|
if (x) {
|
|
|
|
|
b_queue_delete(q, x);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void b_queue_delete(struct b_queue *q, struct b_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 b_queue_delete_all(struct b_queue *q)
|
|
|
|
|
{
|
|
|
|
|
struct b_queue_entry *x = q->q_first;
|
|
|
|
|
while (x) {
|
|
|
|
|
struct b_queue_entry *next = x->qe_next;
|
|
|
|
|
x->qe_next = x->qe_prev = NULL;
|
|
|
|
|
x = next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
q->q_first = q->q_last = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool queue_iterator_next(struct b_iterator *it)
|
|
|
|
|
{
|
|
|
|
|
return b_queue_iterator_next((struct b_queue_iterator *)it);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static b_status queue_iterator_erase(struct b_iterator *it)
|
|
|
|
|
{
|
|
|
|
|
return b_queue_iterator_erase((struct b_queue_iterator *)it);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool queue_iterator_is_valid(const struct b_iterator *it)
|
|
|
|
|
{
|
|
|
|
|
return b_queue_iterator_is_valid((const struct b_queue_iterator *)it);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const b_iterator_ops queue_iterator_ops = {
|
|
|
|
|
.it_next = queue_iterator_next,
|
|
|
|
|
.it_erase = queue_iterator_erase,
|
|
|
|
|
.it_close = NULL,
|
|
|
|
|
.it_is_valid = queue_iterator_is_valid,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int b_queue_iterator_begin(const struct b_queue *q, struct b_queue_iterator *it)
|
|
|
|
|
{
|
|
|
|
|
it->_q = (struct b_queue *)q;
|
|
|
|
|
it->_base.it_ops = &queue_iterator_ops;
|
|
|
|
|
it->entry = q->q_first;
|
|
|
|
|
it->i = 0;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool b_queue_iterator_next(struct b_queue_iterator *it)
|
|
|
|
|
{
|
|
|
|
|
if (!it->entry) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
it->entry = it->entry->qe_next;
|
|
|
|
|
it->i++;
|
|
|
|
|
|
|
|
|
|
return it->entry != NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b_status b_queue_iterator_erase(struct b_queue_iterator *it)
|
|
|
|
|
{
|
|
|
|
|
if (!it->entry) {
|
|
|
|
|
return B_ERR_OUT_OF_BOUNDS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct b_queue_entry *next = it->entry->qe_next;
|
|
|
|
|
b_queue_delete(it->_q, it->entry);
|
|
|
|
|
it->entry = next;
|
|
|
|
|
|
|
|
|
|
return B_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool b_queue_iterator_is_valid(const struct b_queue_iterator *it)
|
|
|
|
|
{
|
|
|
|
|
return it->entry != NULL;
|
|
|
|
|
}
|