libamxc  1.10.3
C Generic Data Containers
Array Queue

A queue implementation based on Array. More...

Typedefs

typedef amxc_array_t amxc_aqueue_t
 The array queue structure. More...
 
typedef amxc_array_it_t amxc_aqueue_it_t
 The array queue iterator structure. More...
 
typedef amxc_array_it_delete_t amxc_aqueue_it_delete_t
 Definition of the item delete function. More...
 

Functions

AMXC_INLINE int amxc_aqueue_new (amxc_aqueue_t **aqueue)
 Allocates an array queue. More...
 
AMXC_INLINE void amxc_aqueue_delete (amxc_aqueue_t **aqueue, amxc_aqueue_it_delete_t func)
 Frees the previously allocated array queue. More...
 
AMXC_INLINE int amxc_aqueue_init (amxc_aqueue_t *const aqueue)
 Initializes an array queue. More...
 
AMXC_INLINE void amxc_aqueue_clean (amxc_aqueue_t *const aqueue, amxc_aqueue_it_delete_t func)
 Removes all items from the array queue. More...
 
AMXC_INLINE amxc_aqueue_it_tamxc_aqueue_add (amxc_aqueue_t *const aqueue, void *data)
 Adds data to the array queue. More...
 
AMXC_INLINE void * amxc_aqueue_remove (amxc_aqueue_t *const aqueue)
 Removes the first added data from the queue. More...
 
AMXC_INLINE size_t amxc_aqueue_size (const amxc_aqueue_t *const aqueue)
 Calculates the number of items in the queue. More...
 
AMXC_INLINE size_t amxc_aqueue_is_empty (const amxc_aqueue_t *const aqueue)
 Checks that the array queue is empty. More...
 

Detailed Description

A queue implementation based on Array.

The basic operators on a queue are add and remove.

Using the Array a queue can be created which has a initial size and can grow when needed.

When adding data to the queue, the data will be put in the first empty bucket, if no empty bucket is available, the queue (bucket array) will grow.

When removing data, the first non empty bucket is returned and all items are shifted left.

Typedef Documentation

◆ amxc_aqueue_it_delete_t

Definition of the item delete function.

A pointer to a delete function is used in the following functions amxc_aqueue_delete, amxc_aqueue_clean

Definition at line 114 of file amxc_aqueue.h.

◆ amxc_aqueue_it_t

The array queue iterator structure.

Definition at line 104 of file amxc_aqueue.h.

◆ amxc_aqueue_t

The array queue structure.

Definition at line 97 of file amxc_aqueue.h.

Function Documentation

◆ amxc_aqueue_add()

AMXC_INLINE amxc_aqueue_it_t* amxc_aqueue_add ( amxc_aqueue_t *const  aqueue,
void *  data 
)

Adds data to the array queue.

Parameters
aqueuea pointer to the array queue structure
dataa pointer to the data
Returns
returns the iterator where the data is added, or NULL if adding the data failed

Definition at line 223 of file amxc_aqueue.h.

223  {
224  return amxc_array_append_data(aqueue, data);
225 }
amxc_array_it_t * amxc_array_append_data(amxc_array_t *const array, void *data)
Adds an item after the last used item in the array.
Definition: amxc_array.c:429
char data[]

◆ amxc_aqueue_clean()

AMXC_INLINE void amxc_aqueue_clean ( amxc_aqueue_t *const  aqueue,
amxc_aqueue_it_delete_t  func 
)

Removes all items from the array queue.

Removes all items from the array queue. If a delete function is provided, it is called for each item in the queue.

Parameters
aqueuea pointer to the array queue structure
funca pointer to a function that is called to free each item in the array queue

Definition at line 205 of file amxc_aqueue.h.

206  {
207  amxc_array_clean(aqueue, func);
208 }
void amxc_array_clean(amxc_array_t *const array, amxc_array_it_delete_t func)
Removes all items from the array.
Definition: amxc_array.c:261

◆ amxc_aqueue_delete()

AMXC_INLINE void amxc_aqueue_delete ( amxc_aqueue_t **  aqueue,
amxc_aqueue_it_delete_t  func 
)

Frees the previously allocated array queue.

Removes all items from the array queue. If a delete function is provided, it is called for each item in the queue.

Frees the allocated memory and sets the pointer to NULL.

Note
Only call this function for array queues that are allocated on the heap using amxc_aqueue_new
Parameters
aqueuea pointer to the location where the pointer to the array queue is stored
funca pointer to a function that is called to free each item in the array queue

Definition at line 161 of file amxc_aqueue.h.

161  {
162  amxc_array_delete(aqueue, func);
163 }
void amxc_array_delete(amxc_array_t **array, const amxc_array_it_delete_t func)
Frees the previously allocated array.
Definition: amxc_array.c:213

◆ amxc_aqueue_init()

AMXC_INLINE int amxc_aqueue_init ( amxc_aqueue_t *const  aqueue)

Initializes an array queue.

Initializes the array queue structure. All pointers are reset to NULL. This function is typically called for array queues that are on the stack. Allocating and initializing an array queue on the heap can be done using amxc_aqueue_new

Note
When calling this function on an already initialized array queue, that contains items, the array queue is reset and all items in the queue are lost (Could potentially lead to memory leaks). Use amxc_aqueue_clean to remove all items from the queue.
Parameters
aqueuea pointer to the array queue structure.
Returns
0 on success. -1 if a NULL pointer is given.

Definition at line 188 of file amxc_aqueue.h.

188  {
189  return amxc_array_init(aqueue, 10);
190 }
int amxc_array_init(amxc_array_t *const array, const size_t items)
Initializes an array.
Definition: amxc_array.c:231

◆ amxc_aqueue_is_empty()

AMXC_INLINE size_t amxc_aqueue_is_empty ( const amxc_aqueue_t *const  aqueue)

Checks that the array queue is empty.

Parameters
aqueuea pointer to the array queue structure
Returns
returns true when the array queue contains no items, false when there is at least one item in the queue.

Definition at line 271 of file amxc_aqueue.h.

271  {
272  return amxc_array_is_empty(aqueue);
273 }
bool amxc_array_is_empty(const amxc_array_t *const array)
Checks that the array is empty.
Definition: amxc_array.c:399

◆ amxc_aqueue_new()

AMXC_INLINE int amxc_aqueue_new ( amxc_aqueue_t **  aqueue)

Allocates an array queue.

Allocates and initializes memory to store an array queue. This function allocates memory from the heap. If an array queue is on the stack, it can be initialized using function amxc_aqueue_init

Note
The allocated memory must be freed when not used anymore. Use amxc_aqueue_delete to free the memory
Parameters
aqueuea pointer to the location where the pointer to the new array queue can be stored
Returns
-1 if an error occurred. 0 on success

Definition at line 137 of file amxc_aqueue.h.

137  {
138  return amxc_array_new(aqueue, 10);
139 }
int8_t amxc_array_new(amxc_array_t **array, const size_t items)
Allocates an array.
Definition: amxc_array.c:179

◆ amxc_aqueue_remove()

AMXC_INLINE void* amxc_aqueue_remove ( amxc_aqueue_t *const  aqueue)

Removes the first added data from the queue.

Parameters
aqueuea pointer to the array queue structure
Returns
Pointer to the first added data or NULL if no more items on the queue

Definition at line 238 of file amxc_aqueue.h.

238  {
239  void* rv = amxc_array_take_first_data(aqueue);
240  amxc_array_shift_left(aqueue, 1, NULL);
241  return rv;
242 }
void * amxc_array_take_first_data(amxc_array_t *const array)
Takes the data pointer from the first used item in the array.
Definition: amxc_array.c:576
int amxc_array_shift_left(amxc_array_t *const array, const size_t items, amxc_array_it_delete_t func)
Shift all items to the left in the array.
Definition: amxc_array.c:361

◆ amxc_aqueue_size()

AMXC_INLINE size_t amxc_aqueue_size ( const amxc_aqueue_t *const  aqueue)

Calculates the number of items in the queue.

Parameters
aqueuea pointer to the array queue structure
Returns
The number of items in the array queue.

Definition at line 255 of file amxc_aqueue.h.

255  {
256  return amxc_array_size(aqueue);
257 }
size_t amxc_array_size(const amxc_array_t *const array)
Calculates the number of used items in the array.
Definition: amxc_array.c:415