O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
Loading...
Searching...
No Matches
deque.h File Reference

Realtime double-ended queues. More...

#include <stdbool.h>
#include <stddef.h>

Go to the source code of this file.

Data Structures

struct  deque_t
 Double-ended queue. More...

Macros

Iterators
#define deque_foreach(TYPE, DEQUE, ELEMENT)
 Iterate over the elements of the queue.
#define deque_enumerate(TYPE, DEQUE, ELEMENT, INDEX)
 Iterate over the elements of the queue, with the index.

Functions

Accessing elements
void * deque_first (const deque_t *self)
 The current first element in the queue.
void * deque_last (const deque_t *self)
 The current last element in the queue.
void * deque_get (const deque_t *self, size_t index)
 The element of the element at an arbitrary index.
Capacity
bool deque_is_empty (const deque_t *self)
 True if no elements are currently stored.
bool deque_is_full (const deque_t *self)
 True if the queue reached its maximum capacity.
size_t deque_count (const deque_t *self)
 The number of elements currently held in the queue.
size_t deque_capacity (const deque_t *self)
 The maximum number of elements that can be stored.
size_t deque_room (const deque_t *self)
 The remaining number of elements that can be added.
void deque_reserve (deque_t *self, size_t count)
 Reallocates the deque if count elements cannot fit in the current allocation.
Adding elements
bool deque_push_front (deque_t *self, const void *element)
 Inserts an element to the front of the queue.
bool deque_push_front_n (deque_t *self, const void *elements, size_t count)
 Inserts count elements to the front of the queue.
bool deque_push_back (deque_t *self, const void *element)
 Inserts an element to the back of the queue.
bool deque_push_back_n (deque_t *self, const void *elements, size_t count)
 Inserts count elements to the back of the queue.
Removing elements
bool deque_pop_front (deque_t *self, void *destination)
 Pops the front-most element of the queue, copying it to destination.
bool deque_pop_front_n (deque_t *self, void *destination, size_t count)
 Pops the count first elements of the queue.
bool deque_pop_back (deque_t *self, void *destination)
 Pops the last element in the queue.
bool deque_pop_back_n (deque_t *self, void *destination, size_t count)
 Pops the count last elements in the queue.
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.

Using externally owned memory

#define DequeNew(Storage, Capacity, Type)
 Wrapper arround the non-owning constructor.
deque_t deque_new (void *storage, size_t capacity, size_t type_size)
 Contructs an deque, using externally owned memory.
void deque_clear (deque_t *self)
 Clears properly the deque.

Managing the memory

#define DequeAllocate(Capacity, Type)
 Wrapper around the owning constructor.
#define Deque
 Automatically free the alloacted storage when going out of scope.
deque_t deque_allocate (size_t capacity, size_t type_size)
 Constructs a deque, allocating the needed memory.
void deque_free (deque_t *self)
 Frees properly the deque.

Detailed Description

Realtime double-ended queues.

Author
Hugo FOLCHER
Antoine GAGNIERE

Definition in file deque.h.

Macro Definition Documentation

◆ Deque

#define Deque

Automatically free the alloacted storage when going out of scope.

In a situation where one wants to declare a Deque in a local scope, this "typedef" can be used for that Deque to release the allocated memory automatically when the variable goes out of scope.

It means this "typedef" can only be used like this:

{
Deque my_deque = DequeAllocate(512, float);
...
} // <- the underlying storage will be freed at that point
#define DequeAllocate(Capacity, Type)
Wrapper around the owning constructor.
Definition deque.h:54
#define Deque
Automatically free the alloacted storage when going out of scope.
Definition deque.h:71

Definition at line 71 of file deque.h.

◆ deque_enumerate

#define deque_enumerate ( TYPE,
DEQUE,
ELEMENT,
INDEX )

Iterate over the elements of the queue, with the index.

To be used like a for loop

Definition at line 132 of file deque.h.

◆ deque_foreach

#define deque_foreach ( TYPE,
DEQUE,
ELEMENT )

Iterate over the elements of the queue.

To be used like a for loop

Definition at line 123 of file deque.h.

Function Documentation

◆ deque_allocate()

deque_t deque_allocate ( size_t capacity,
size_t type_size )

Constructs a deque, allocating the needed memory.

If the allocation failed, the capacity will be set to zero.

Definition at line 36 of file constructors_destructors.c.

◆ deque_first()

void * deque_first ( const deque_t * self)

The current first element in the queue.

Returns
NULL if the queue is empty

Definition at line 37 of file getters.c.

◆ deque_get()

void * deque_get ( const deque_t * self,
size_t index )

The element of the element at an arbitrary index.

Returns
NULL if the index is not a valid one

Definition at line 72 of file getters.c.

◆ deque_last()

void * deque_last ( const deque_t * self)

The current last element in the queue.

Returns
NULL if the queue is empty

Definition at line 48 of file getters.c.

◆ deque_pop_back()

bool deque_pop_back ( deque_t * self,
void * destination )

Pops the last element in the queue.

Returns
false if the queue is empty

Definition at line 76 of file pop.c.

◆ deque_pop_back_n()

bool deque_pop_back_n ( deque_t * self,
void * destination,
size_t count )

Pops the count last elements in the queue.

Returns
false if the queue contains less than count elements

Definition at line 91 of file pop.c.

◆ deque_pop_front()

bool deque_pop_front ( deque_t * self,
void * destination )

Pops the front-most element of the queue, copying it to destination.

If destination is NULL, it will be discarded

Returns
false if the queue is empty

Definition at line 29 of file pop.c.

◆ deque_pop_front_n()

bool deque_pop_front_n ( deque_t * self,
void * destination,
size_t count )

Pops the count first elements of the queue.

If destination is NULL, they will be discarded

Returns
false if there is less than count elements stored

Definition at line 45 of file pop.c.

◆ deque_push_back()

bool deque_push_back ( deque_t * self,
const void * element )

Inserts an element to the back of the queue.

Returns
false if the queue is already full

Definition at line 59 of file push.c.

◆ deque_push_back_n()

bool deque_push_back_n ( deque_t * self,
const void * elements,
size_t count )

Inserts count elements to the back of the queue.

Returns
false if there isn't enough capacity to fit count more elements

Definition at line 68 of file push.c.

◆ deque_push_front()

bool deque_push_front ( deque_t * self,
const void * element )

Inserts an element to the front of the queue.

Returns
false if the queue is already full

Definition at line 29 of file push.c.

◆ deque_push_front_n()

bool deque_push_front_n ( deque_t * self,
const void * elements,
size_t count )

Inserts count elements to the front of the queue.

Returns
false if there isn't enough capacity to fit count more elements

Definition at line 43 of file push.c.

◆ deque_reserve()

void deque_reserve ( deque_t * self,
size_t count )

Reallocates the deque if count elements cannot fit in the current allocation.

If the reallocation failed, the capacity will be set to zero.

Todo
: Handle present elements when split !

Definition at line 25 of file reserve.c.