GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Functions
gp_cbuffer.h File Reference

Circular buffer indexing and iterators. More...

#include <stddef.h>

Go to the source code of this file.

Data Structures

struct  gp_cbuffer
 A circular buffer indexes. More...
 
struct  gp_cbuffer_iter
 An interator for loops over the circular buffer. More...
 

Macros

#define GP_CBUFFER_FOREACH(self, iter)
 Loops over all elements in the circular buffer.
 
#define GP_CBUFFER_FORRANGE(self, iter, skip, count)
 Loops over a range of elements in the circular buffer.
 
#define GP_CBUFFER_FOREACH_REV(self, iter)
 Loops backwards over all elements in the circular buffer.
 

Typedefs

typedef struct gp_cbuffer gp_cbuffer
 A circular buffer indexes.
 
typedef struct gp_cbuffer_iter gp_cbuffer_iter
 An interator for loops over the circular buffer.
 

Functions

static void gp_cbuffer_init (gp_cbuffer *self, size_t size)
 Initializes circular buffer.
 
static size_t gp_cbuffer_append (gp_cbuffer *self)
 Appends into circular buffer.
 
static size_t gp_cbuffer_next (gp_cbuffer *self, size_t idx)
 Returns next position in the circular buffer.
 
static size_t gp_cbuffer_prev (gp_cbuffer *self, size_t idx)
 Returns previous position in the circular buffer.
 
static size_t gp_cbuffer_used (gp_cbuffer *self)
 Returns number of used positions in the circular buffer.
 
static size_t gp_cbuffer_first (gp_cbuffer *self)
 Returns index to the first element in buffer.
 
static size_t gp_cbuffer_last (gp_cbuffer *self)
 Returns index to the last element in buffer.
 

Detailed Description

Circular buffer indexing and iterators.

Definition in file gp_cbuffer.h.

Macro Definition Documentation

◆ GP_CBUFFER_FOREACH

#define GP_CBUFFER_FOREACH (   self,
  iter 
)
Value:
for ((iter)->idx = gp_cbuffer_first(self), (iter)->cnt = 0; \
(iter)->cnt < (self)->used; \
(iter)->idx = gp_cbuffer_next(self, (iter)->idx), (iter)->cnt++)
static size_t gp_cbuffer_next(gp_cbuffer *self, size_t idx)
Returns next position in the circular buffer.
Definition gp_cbuffer.h:69
static size_t gp_cbuffer_first(gp_cbuffer *self)
Returns index to the first element in buffer.
Definition gp_cbuffer.h:109

Loops over all elements in the circular buffer.

Parameters
selfA circular buffer.
iterA circular buffer iterator variable.

Definition at line 151 of file gp_cbuffer.h.

◆ GP_CBUFFER_FOREACH_REV

#define GP_CBUFFER_FOREACH_REV (   self,
  iter 
)
Value:
for ((iter)->idx = gp_cbuffer_last(self), (iter)->cnt = 0; \
(iter)->cnt < (self)->used; \
(iter)->idx = gp_cbuffer_prev(self, (iter)->idx), (iter)->cnt++)
static size_t gp_cbuffer_prev(gp_cbuffer *self, size_t idx)
Returns previous position in the circular buffer.
Definition gp_cbuffer.h:81
static size_t gp_cbuffer_last(gp_cbuffer *self)
Returns index to the last element in buffer.
Definition gp_cbuffer.h:130

Loops backwards over all elements in the circular buffer.

Parameters
selfA circular buffer.
iterA circular buffer iterator variable.

Definition at line 176 of file gp_cbuffer.h.

◆ GP_CBUFFER_FORRANGE

#define GP_CBUFFER_FORRANGE (   self,
  iter,
  skip,
  count 
)
Value:
if ((self)->used > skip) \
for ((iter)->idx = (gp_cbuffer_first(self) + skip) % (self)->size, (iter)->cnt = 0; \
(iter)->cnt < GP_MIN((self)->used - skip, count); \
(iter)->idx = gp_cbuffer_next(self, (iter)->idx), (iter)->cnt++)
#define GP_MIN(a, b)
Returns a minimum of the two numbers.
Definition gp_common.h:31

Loops over a range of elements in the circular buffer.

Parameters
selfA circular buffer.
iterA circular buffer iterator variable.
skipHow many element should we skip at the start.
countHow many element should we iterater over.

Definition at line 164 of file gp_cbuffer.h.

Function Documentation

◆ gp_cbuffer_append()

static size_t gp_cbuffer_append ( gp_cbuffer self)
inlinestatic

Appends into circular buffer.

Only index into the buffer array is returned, any old data has to be freed by the user of this API.

Parameters
selfA circular buffer.
Returns
An index for the buffer to append.

Definition at line 50 of file gp_cbuffer.h.

References gp_cbuffer::last, gp_cbuffer::size, and gp_cbuffer::used.

◆ gp_cbuffer_first()

static size_t gp_cbuffer_first ( gp_cbuffer self)
inlinestatic

Returns index to the first element in buffer.

Returns invalid data for empty buffer, make sure buffer is not empty with gp_buffer_used() before using this function.

Parameters
selfA circular buffer.
Returns
First used position in the buffer.

Definition at line 109 of file gp_cbuffer.h.

References gp_cbuffer::last, gp_cbuffer::size, and gp_cbuffer::used.

◆ gp_cbuffer_init()

static void gp_cbuffer_init ( gp_cbuffer self,
size_t  size 
)
inlinestatic

Initializes circular buffer.

Parameters
selfA circular buffer to be initialized.
sizeSize of the buffer.

Definition at line 34 of file gp_cbuffer.h.

References gp_cbuffer::last, gp_cbuffer::size, and gp_cbuffer::used.

◆ gp_cbuffer_last()

static size_t gp_cbuffer_last ( gp_cbuffer self)
inlinestatic

Returns index to the last element in buffer.

Returns invalid data for empty buffer, make sure buffer is not empty with gp_buffer_used() before using this function.

Parameters
selfA circular buffer.
Returns
Last used position in the buffer.

Definition at line 130 of file gp_cbuffer.h.

References gp_cbuffer_prev(), and gp_cbuffer::last.

◆ gp_cbuffer_next()

static size_t gp_cbuffer_next ( gp_cbuffer self,
size_t  idx 
)
inlinestatic

Returns next position in the circular buffer.

Parameters
selfA circular buffer.
idxA current position in the buffer.
Returns
Next position in the buffer.

Definition at line 69 of file gp_cbuffer.h.

References gp_cbuffer::size.

◆ gp_cbuffer_prev()

static size_t gp_cbuffer_prev ( gp_cbuffer self,
size_t  idx 
)
inlinestatic

Returns previous position in the circular buffer.

Parameters
selfA circular buffer.
idxA current position in the buffer.
Returns
Previous position in the buffer.

Definition at line 81 of file gp_cbuffer.h.

References gp_cbuffer::size.

Referenced by gp_cbuffer_last().

◆ gp_cbuffer_used()

static size_t gp_cbuffer_used ( gp_cbuffer self)
inlinestatic

Returns number of used positions in the circular buffer.

Parameters
selfA circular buffer.
Returns
Number of used postions in the buffer.

Definition at line 95 of file gp_cbuffer.h.

References gp_cbuffer::used.