89 lines
2.3 KiB
C
89 lines
2.3 KiB
C
#ifndef BLUELIB_CORE_QUEUE_H_
|
|
#define BLUELIB_CORE_QUEUE_H_
|
|
|
|
#include <blue/core/iterator.h>
|
|
#include <blue/core/status.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#define B_QUEUE_INIT ((b_queue) {.q_first = NULL, .q_last = NULL})
|
|
#define B_QUEUE_ENTRY_INIT ((b_queue_entry) {.qe_next = NULL, .qe_prev = NULL})
|
|
|
|
#define b_queue_foreach(it, q) \
|
|
for (int z__b_unique_name() = b_queue_iterator_begin(q, it); \
|
|
(it)->entry != NULL; b_queue_iterator_next(it))
|
|
|
|
typedef struct b_queue_entry {
|
|
struct b_queue_entry *qe_next;
|
|
struct b_queue_entry *qe_prev;
|
|
} b_queue_entry;
|
|
|
|
typedef struct b_queue {
|
|
b_queue_entry *q_first;
|
|
b_queue_entry *q_last;
|
|
} b_queue;
|
|
|
|
typedef struct b_queue_iterator {
|
|
b_iterator _base;
|
|
size_t i;
|
|
b_queue_entry *entry;
|
|
b_queue *_q;
|
|
} b_queue_iterator;
|
|
|
|
static inline void b_queue_init(b_queue *q)
|
|
{
|
|
memset(q, 0x00, sizeof *q);
|
|
}
|
|
static inline bool b_queue_empty(const b_queue *q)
|
|
{
|
|
return q->q_first == NULL;
|
|
}
|
|
|
|
static inline b_queue_entry *b_queue_first(const b_queue *q)
|
|
{
|
|
return q->q_first;
|
|
}
|
|
static inline b_queue_entry *b_queue_last(const b_queue *q)
|
|
{
|
|
return q->q_last;
|
|
}
|
|
static inline b_queue_entry *b_queue_next(const b_queue_entry *entry)
|
|
{
|
|
return entry->qe_next;
|
|
}
|
|
static inline b_queue_entry *b_queue_prev(const b_queue_entry *entry)
|
|
{
|
|
return entry->qe_prev;
|
|
}
|
|
|
|
BLUE_API size_t b_queue_length(const b_queue *q);
|
|
|
|
BLUE_API void b_queue_insert_before(
|
|
b_queue *q, b_queue_entry *entry, b_queue_entry *before);
|
|
BLUE_API void b_queue_insert_after(
|
|
b_queue *q, b_queue_entry *entry, b_queue_entry *after);
|
|
|
|
BLUE_API void b_queue_push_front(b_queue *q, b_queue_entry *entry);
|
|
BLUE_API void b_queue_push_back(b_queue *q, b_queue_entry *entry);
|
|
|
|
BLUE_API b_queue_entry *b_queue_pop_front(b_queue *q);
|
|
BLUE_API b_queue_entry *b_queue_pop_back(b_queue *q);
|
|
|
|
BLUE_API void b_queue_delete(b_queue *q, b_queue_entry *entry);
|
|
BLUE_API void b_queue_delete_all(b_queue *q);
|
|
|
|
BLUE_API int b_queue_iterator_begin(const b_queue *q, b_queue_iterator *it);
|
|
BLUE_API bool b_queue_iterator_next(b_queue_iterator *it);
|
|
BLUE_API b_status b_queue_iterator_erase(b_queue_iterator *it);
|
|
BLUE_API bool b_queue_iterator_is_valid(const b_queue_iterator *it);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|