O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
Loading...
Searching...
No Matches
pop.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* ____ _ _____ ____ _ _ _ _____ _ _ ____ ____ */
3/* / ___| / \ | ___| _ \ / \ | \ | | |_ _| || | | _ \/ ___| */
4/* \___ \ / _ \ | |_ | |_) | / _ \ | \| | | | | || |_| | | \___ \ */
5/* ___) / ___ \| _| | _ < / ___ \| |\ | | | |__ _| |_| |___) | */
6/* |____/_/ \_|_| |_| \_/_/ \_|_| \_| |_| |_| |____/|____/ */
7/* */
15/* ************************************************************************** */
16
17#include "private.h"
18
19#include "o2s/deque.h"
20#include "o2s/preprocessing.h" // min
21
22#include <string.h> // memcpy
23
29bool deque_pop_front(deque_t* self, void* destination)
30{
31 if (deque_is_empty(self))
32 return false;
33 if (destination != NULL)
34 memcpy(destination, self->front, deque_offset(self, 1));
35 self->front = deque_pointer_after(self, self->front);
36 self->count -= 1;
37 return true;
38}
39
45bool deque_pop_front_n(deque_t* self, void* destination, size_t count)
46{
47 size_t first_pass;
48 size_t first_pass_size;
49
50 if (count == 0)
51 return true;
52 if (deque_count(self) < count)
53 return false;
54
55 first_pass = min(count, deque_distance(self, self->front, deque_end(self)));
56 first_pass_size = deque_offset(self, first_pass);
57 if (destination != NULL)
58 {
59 memcpy(destination, self->front, first_pass_size);
60 destination += first_pass_size;
61 }
62 self->front += first_pass_size;
63 self->count -= first_pass;
64
65 if (self->front == deque_end(self))
66 self->front = deque_begin(self);
67 if (first_pass < count)
68 return deque_pop_front_n(self, destination, count - first_pass);
69 return true;
70}
71
76bool deque_pop_back(deque_t* self, void* destination)
77{
78 if (deque_is_empty(self))
79 return false;
80 self->back = deque_pointer_before(self, self->back);
81 if (destination != NULL)
82 memcpy(destination, self->back, deque_offset(self, 1));
83 self->count -= 1;
84 return true;
85}
86
91bool deque_pop_back_n(deque_t* self, void* destination, size_t count)
92{
93 if (deque_count(self) < count)
94 return false;
95 while (count-- > 0)
96 {
97 deque_pop_back(self, destination);
98 destination += deque_offset(self, 1);
99 }
100 return true;
101}
bool deque_is_empty(const deque_t *self)
True if no elements are currently stored.
Definition getters.c:57
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(deque_t *self, void *destination)
Pops the last element in the queue.
Definition pop.c:76
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
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
Internal deque state.
Realtime double-ended queues.
size_t deque_distance(const deque_t *self, const void *one, const void *two)
Number of elements in the range delimited by the two provided elements.
Definition internals.c:62
size_t deque_offset(const deque_t *self, size_t count)
The size in memory of count elements.
Definition internals.c:21
void * deque_pointer_before(const deque_t *self, void *pointer)
The previous slot.
Definition internals.c:54
void * deque_begin(const deque_t *self)
Pointer to the start of the underlying storage.
Definition internals.c:33
void * deque_end(const deque_t *self)
Pointer one-past-the-end of the underlying storage.
Definition internals.c:39
void * deque_pointer_after(const deque_t *self, void *pointer)
The next slot.
Definition internals.c:45
Compile-time utilities.
#define min(a, b)
Evaluates as the smallest of the two parameters.
Dynamic string implementation.
Double-ended queue.
Definition deque.h:27
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 count
Number of elements currently stored.
Definition deque.h:31