O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
Loading...
Searching...
No Matches
push_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/array.h"
20
21#include <iso646.h> // not
22#include <string.h> // memcpy
23
28bool array_push_back_n(array_t* self, const void* elements, size_t count)
29{
30 if (count == 0)
31 return true;
32 if (not array_reserve(self, count))
33 return false;
34 memcpy(array_end(self), elements, array_offset(self, count));
35 self->count += count;
36 return true;
37}
38
43bool array_push_back(array_t* self, const void* element)
44{
45 return array_push_back_n(self, element, 1);
46}
47
53bool array_pop_back_n(array_t* self, void* destination, size_t count)
54{
55 if (count > self->count)
56 return false;
57 const void* source = array_get(self, self->count - count);
58 if (destination != NULL)
59 memcpy(destination, source, array_offset(self, count));
60 self->count -= count;
61 return true;
62}
63
68bool array_pop_back(array_t* self, void* destination)
69{
70 return array_pop_back_n(self, destination, 1);
71}
72
77bool array_pop_front_n(array_t* self, void* destination, size_t count)
78{
79 if (count > self->count)
80 return false;
81 size_t remaining = self->count - count;
82 if (destination)
83 memcpy(destination, self->start, array_offset(self, count));
84 memmove(self->start, array_get(self, count), array_offset(self, remaining));
85 self->count -= count;
86 return true;
87}
88
93bool array_pop_front(array_t* self, void* destination)
94{
95 return array_pop_front_n(self, destination, 1);
96}
void * array_end(const array_t *self)
One-past-the-end pointer of the array.
Definition getters.c:25
void * array_get(const array_t *self, size_t index)
Pointer of the element at position index of the array.
Definition getters.c:56
Internal state of a dynamic array.
Dynamic arrays.
size_t array_offset(const array_t *self, size_t count)
The size in memory of count elements.
Definition memory.c:23
bool array_reserve(array_t *self, size_t count)
Ensures the array has enough capacity to fit count new elements, reallocating if needed.
Definition memory.c:47
bool array_pop_back_n(array_t *self, void *destination, size_t count)
Moves to destination the n last elements of the array.
Definition push_pop.c:53
bool array_push_back_n(array_t *self, const void *elements, size_t count)
Adds count elements at the end of the array.
Definition push_pop.c:28
bool array_push_back(array_t *self, const void *element)
Adds one element at the end of the array.
Definition push_pop.c:43
bool array_pop_back(array_t *self, void *destination)
Moves to destination the last element of the array.
Definition push_pop.c:68
bool array_pop_front(array_t *self, void *destination)
Moves to destination the first element of self.
Definition push_pop.c:93
bool array_pop_front_n(array_t *self, void *destination, size_t count)
Moves to destination the count first elements of self.
Definition push_pop.c:77
Dynamic string implementation.
O2S array implementation.
Definition array.h:24
void * start
Underlying storage.
Definition array.h:25
size_t count
Number of elements currently stored.
Definition array.h:27