O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
Loading...
Searching...
No Matches
push.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 <iso646.h> // not
23#include <string.h> // memcpy
24
29bool deque_push_front(deque_t* self, const void* element)
30{
31 if (deque_is_full(self))
32 return false;
33 self->front = deque_pointer_before(self, self->front);
34 memcpy(self->front, element, deque_offset(self, 1));
35 self->count += 1;
36 return true;
37}
38
43bool deque_push_front_n(deque_t* self, const void* elements, size_t count)
44{
45 if (count > deque_room(self))
46 return false;
47 while (count-- > 0)
48 {
49 deque_push_front(self, elements);
50 elements += deque_offset(self, 1);
51 }
52 return true;
53}
54
59bool deque_push_back(deque_t* self, const void* element)
60{
61 return deque_push_back_n(self, element, 1);
62}
63
68bool deque_push_back_n(deque_t* self, const void* elements, size_t count)
69{
70 size_t first_pass;
71 size_t first_pass_size;
72
73 if (count > deque_room(self))
74 return false;
75 first_pass = min(count, deque_distance(self, self->back, deque_end(self)));
76 first_pass_size = deque_offset(self, first_pass);
77 memcpy(self->back, elements, first_pass_size);
78 self->back += first_pass_size;
79 self->count += first_pass;
80 if (first_pass < count)
81 {
82 self->back = deque_begin(self);
83 deque_push_back_n(self, elements + first_pass_size, count - first_pass);
84 }
85 return true;
86}
bool deque_is_full(const deque_t *self)
True if the queue reached its maximum capacity.
Definition getters.c:63
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
size_t deque_room(const deque_t *self)
The remaining number of elements that can be added.
Definition internals.c:27
Compile-time utilities.
#define min(a, b)
Evaluates as the smallest of the two parameters.
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_push_back(deque_t *self, const void *element)
Inserts an element to the back of the queue.
Definition push.c:59
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
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
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