Skip to content
Ilya Kashnitskiy edited this page Jan 22, 2020 · 3 revisions

My own implementation of a dynamic cyclical deque on C.

This is the header with API: ft_deq.h and this is the implementation folder: sources/ft_deq/

  • Structure

typedef struct		s_deq
{
	void *restrict	mem;
	size_t		item_size;
	size_t		curlen;
	size_t		max_len;
	size_t		front;
	size_t		back;
}			t_deq;
  • mem : pointer to memory with items
  • item_size : size of each item in bytes
  • cur_len : current deque length (number of elements)
  • max_len : size of reserved memory (number of elements)
  • front : position of the first element
  • back : position of the last element

This is a dynamic structure! If the reserved memory is over, reallocation will be made with a 2-fold increase in size!

  • Functions

Definition Description
void deq_init(t_deq *restrict self, size_t item_size, size_t init_len) Initializes the deque self. init_len - initial size of deque (number of elements), must be > 0.
void deq_del(t_deq *restrict self) Free memory and fill self by zero.
void deq_extend(t_deq *restrict self, size_t n) Extends the deque to fit self->curlen + n elements.
void deq_shrink(t_deq *restrict self, size_t reserve) Shrink the deque to fit no more then self->curlen + reserve elements.
void deq_align_front(t_deq *restrict self) Rearranges the items in the deque so that the first item is at the beginning of mem
void *deq(t_deq *restrict self, size_t i) Returns pointer to thei-th element of a deque.
void *deq_front(t_deq *restrict self) Returns the first element of a deque.
void *deq_back(t_deq *restrict self) Returns the last element of a deque.
void *deq_(t_deq *restrict self, long long int i) Accepts both positive and negative i. Returns pointer to thei-th element of a deque, where positive i calc like i-th element and negative i calc like self->curlen - i-th element.
void *deq_eq(t_deq *restrict self, size_t i, void *data) Reads self->item_size bytes from data and writes it in i-th element of deque.
void *deq_push_front(t_deq *restrict self, void *data) Adds an element from data to the front of the deque. Extends deque if necessary.
void *deq_push_back(t_deq *restrict self, void *data) Adds an element from data to the end of the deque. Extends deque if necessary.
void *deq_pop_front(t_deq *restrict self) Removes and returns pointer to the item at the front of the deque. Save the data from this pointer, otherwise it will be lost.
void *deq_pop_back(t_deq *restrict self) Removes and returns pointer to the item at the end of the deque. Save the data from this pointer, otherwise it will be lost.
void deq_rotate(t_deq *restrict self) Rearranges the first item at the end of the deque.
void deq_rev_rotate(t_deq *restrict self) Rearranges the last item at the front of the deque.
void *deq_find_front(t_deq *restrict self, void *data, int (*cmp)(const void *, const void *)); Iterates over the entire deque from the beginning, comparing each element with data using cmp. Returns the item on the first matching item. NULL otherwise.

GO TO NEXT PAGE --->