O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
Loading...
Searching...
No Matches
deque.h
Go to the documentation of this file.
1#pragma once
2
3/* ************************************************************************** */
4/* ____ _ _____ ____ _ _ _ _____ _ _ ____ ____ */
5/* / ___| / \ | ___| _ \ / \ | \ | | |_ _| || | | _ \/ ___| */
6/* \___ \ / _ \ | |_ | |_) | / _ \ | \| | | | | || |_| | | \___ \ */
7/* ___) / ___ \| _| | _ < / ___ \| |\ | | | |__ _| |_| |___) | */
8/* |____/_/ \_|_| |_| \_/_/ \_|_| \_| |_| |_| |____/|____/ */
9/* */
17/* ************************************************************************** */
18
19#include <stdbool.h> // bool
20#include <stddef.h> // size_t
21
22/* Forward declaration */
23struct array;
24
26typedef struct deque
27{
28 void* storage;
29 void* front;
30 void* back;
31 size_t count;
32 size_t capacity;
33 size_t type_size;
34} deque_t;
35
38deque_t deque_new(void* storage, size_t capacity, size_t type_size);
39void deque_clear(deque_t* self);
40//void deque_clear_f(deque_t* self, void (*cleanup)());
41
43#define DequeNew(Storage, Capacity, Type) \
44 deque_new(Storage, Capacity, sizeof(Type))
45
46
49deque_t deque_allocate(size_t capacity, size_t type_size);
50void deque_free(deque_t* self);
51//void deque_free_f(deque_t* self, void (*cleanup)());
52
54#define DequeAllocate(Capacity, Type) deque_allocate(Capacity, sizeof(Type))
55
71#define Deque __attribute__((cleanup(deque_free))) deque_t
73
76void* deque_first(const deque_t* self);
77void* deque_last(const deque_t* self);
78void* deque_get(const deque_t* self, size_t index);
80
83bool deque_is_empty(const deque_t* self);
84bool deque_is_full(const deque_t* self);
85
86size_t deque_count(const deque_t* self);
87size_t deque_capacity(const deque_t* self);
88size_t deque_room(const deque_t* self);
89
90void deque_reserve(deque_t* self, size_t count);
92
95bool deque_push_front(deque_t* self, const void* element);
96bool deque_push_front_n(deque_t* self, const void* elements, size_t count);
97
98bool deque_push_back(deque_t* self, const void* element);
99bool deque_push_back_n(deque_t* self, const void* elements, size_t count);
101
104bool deque_pop_front(deque_t* self, void* destination);
105bool deque_pop_front_n(deque_t* self, void* destination, size_t count);
106
107bool deque_pop_back(deque_t* self, void* destination);
108bool deque_pop_back_n(deque_t* self, void* destination, size_t count);
109
110bool deque_pop_front_into_array(deque_t* self, struct array* destination, size_t count);
112
115//void deque_iter(const deque_t* self, void (*f)());
116//void deque_iter1(const deque_t* self, void (*f)(), void* ext);
117//void deque_iter2(const deque_t* self, void (*f)(), void* ext1, void* ext2);
118
123#define deque_foreach(TYPE, DEQUE, ELEMENT) \
124 for (unsigned _i = 0; _i < deque_count(DEQUE) \
125 && ((*(ELEMENT) = *(TYPE*)deque_get(DEQUE, _i)) || true); \
126 _i++)
127
132#define deque_enumerate(TYPE, DEQUE, ELEMENT, INDEX) \
133 for (*(INDEX) = 0; *(INDEX) < deque_count(DEQUE) \
134 && ((*(ELEMENT) = *(TYPE*)deque_get(DEQUE, *(INDEX))) || true); \
135 (*(INDEX))++)
136
void deque_free(deque_t *self)
Frees properly the deque.
void * deque_last(const deque_t *self)
The current last element in the queue.
Definition getters.c:48
bool deque_push_front(deque_t *self, const void *element)
Inserts an element to the front of the queue.
Definition push.c:29
bool deque_pop_back(deque_t *self, void *destination)
Pops the last element in the queue.
Definition pop.c:76
size_t deque_capacity(const deque_t *self)
The maximum number of elements that can be stored.
Definition getters.c:28
bool deque_push_back(deque_t *self, const void *element)
Inserts an element to the back of the queue.
Definition push.c:59
bool deque_pop_front_into_array(deque_t *self, struct array *destination, size_t count)
Move the count first elements at the back of destination.
Definition into_array.c:24
bool deque_push_front_n(deque_t *self, const void *elements, size_t count)
Inserts count elements to the front of the queue.
Definition push.c:43
void deque_clear(deque_t *self)
Clears properly the deque.
void * deque_first(const deque_t *self)
The current first element in the queue.
Definition getters.c:37
bool deque_is_empty(const deque_t *self)
True if no elements are currently stored.
Definition getters.c:57
deque_t deque_new(void *storage, size_t capacity, size_t type_size)
Contructs an deque, using externally owned memory.
size_t deque_count(const deque_t *self)
The number of elements currently held in the queue.
Definition getters.c:22
bool deque_pop_back_n(deque_t *self, void *destination, size_t count)
Pops the count last elements in the queue.
Definition pop.c:91
void deque_reserve(deque_t *self, size_t count)
Reallocates the deque if count elements cannot fit in the current allocation.
Definition reserve.c:25
bool deque_push_back_n(deque_t *self, const void *elements, size_t count)
Inserts count elements to the back of the queue.
Definition push.c:68
bool deque_pop_front(deque_t *self, void *destination)
Pops the front-most element of the queue, copying it to destination.
Definition pop.c:29
bool deque_pop_front_n(deque_t *self, void *destination, size_t count)
Pops the count first elements of the queue.
Definition pop.c:45
bool deque_is_full(const deque_t *self)
True if the queue reached its maximum capacity.
Definition getters.c:63
deque_t deque_allocate(size_t capacity, size_t type_size)
Constructs a deque, allocating the needed memory.
size_t deque_room(const deque_t *self)
The remaining number of elements that can be added.
Definition internals.c:27
void * deque_get(const deque_t *self, size_t index)
The element of the element at an arbitrary index.
Definition getters.c:72
Double-ended queue.
Definition deque.h:27
size_t type_size
Size in bytes of a single element.
Definition deque.h:33
void * back
Pointer past-the-end of the queue.
Definition deque.h:30
void * front
Pointer to the front of the queue.
Definition deque.h:29
size_t capacity
Maximum number of elements that can be stored.
Definition deque.h:32
size_t count
Number of elements currently stored.
Definition deque.h:31
void * storage
Memory area.
Definition deque.h:28