CMagic  0.5.0
Portable C library of utilities and data structures
Macros
vector.h File Reference

Implementation of a vector container. More...

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include "cmagic/utils.h"
#include "cmagic/memory.h"

Go to the source code of this file.

Macros

#define CMAGIC_VECTOR(type)   type**
 Convenient alias for type**. Returned type of CMAGIC_VECTOR_NEW. More...
 
#define CMAGIC_VECTOR_DATA(cmagic_vector)   (*(cmagic_vector))
 Gets an address to the beginning of the vector data. More...
 
#define CMAGIC_VECTOR_BACK(cmagic_vector)
 Gets an address of the last element in the vector. More...
 
#define CMAGIC_VECTOR_NEW(type, alloc_packet)   ((CMAGIC_VECTOR(type))cmagic_vector_new(sizeof(type), (alloc_packet)))
 Allocates and returns an address of a newly created empty vector. More...
 
#define CMAGIC_VECTOR_FREE(cmagic_vector)   cmagic_vector_free((void**)(cmagic_vector))
 Frees the resources allocated by the vector before. More...
 
#define CMAGIC_VECTOR_ALLOCATE_BACK(cmagic_vector)   cmagic_vector_allocate_back((void**)(cmagic_vector))
 Allocates space for a new element but does not initialize it. More...
 
#define CMAGIC_VECTOR_PUSH_BACK(cmagic_vector, new_element_ptr)
 Allocates space for a new element and initializes it with data under new_element_ptr. More...
 
#define CMAGIC_VECTOR_POP_BACK(cmagic_vector)   cmagic_vector_pop_back((void**)(cmagic_vector))
 Deallocates the last element in the vector. More...
 
#define CMAGIC_VECTOR_SIZE(cmagic_vector)   cmagic_vector_size((void**)(cmagic_vector))
 Deallocates the last element in the vector. More...
 
#define CMAGIC_VECTOR_GET_ALLOC_PACKET(cmagic_vector)   cmagic_vector_get_alloc_packet((void**)(cmagic_vector))
 Extracts cmagic_memory_alloc_packet_t which was used as an argument of CMAGIC_VECTOR_NEW. More...
 

Detailed Description

Implementation of a vector container.

Please use provided macros instead of raw functions to gain additional type checks.

Macro Definition Documentation

◆ CMAGIC_VECTOR

#define CMAGIC_VECTOR (   type)    type**

Convenient alias for type**. Returned type of CMAGIC_VECTOR_NEW.

Parameters
typetype of vector elements

◆ CMAGIC_VECTOR_ALLOCATE_BACK

#define CMAGIC_VECTOR_ALLOCATE_BACK (   cmagic_vector)    cmagic_vector_allocate_back((void**)(cmagic_vector))

Allocates space for a new element but does not initialize it.

The new element can be accessed by CMAGIC_VECTOR_BACK

Parameters
cmagic_vectora vector allocated before with CMAGIC_VECTOR_NEW
Returns
true if allocation of the a new element was successful, false if there's not sufficient memory space and the vector was not modified

◆ CMAGIC_VECTOR_BACK

#define CMAGIC_VECTOR_BACK (   cmagic_vector)
Value:
(assert(CMAGIC_VECTOR_SIZE(cmagic_vector) > 0), \
CMAGIC_VECTOR_DATA(cmagic_vector) + CMAGIC_VECTOR_SIZE(cmagic_vector) - 1)
#define CMAGIC_VECTOR_SIZE(cmagic_vector)
Deallocates the last element in the vector.
Definition: vector.h:115

Gets an address of the last element in the vector.

Warning
Do not use this without ensuring the vector is not empty.
Parameters
cmagic_vectora vector allocated before with CMAGIC_VECTOR_NEW
Returns
address of the last element in the vector

◆ CMAGIC_VECTOR_DATA

#define CMAGIC_VECTOR_DATA (   cmagic_vector)    (*(cmagic_vector))

Gets an address to the beginning of the vector data.

Parameters
cmagic_vectora vector allocated before with CMAGIC_VECTOR_NEW
Returns
address of the first element in the vector

◆ CMAGIC_VECTOR_FREE

#define CMAGIC_VECTOR_FREE (   cmagic_vector)    cmagic_vector_free((void**)(cmagic_vector))

Frees the resources allocated by the vector before.

Must not use cmagic_vector after free.

Parameters
cmagic_vectora vector allocated before with CMAGIC_VECTOR_NEW

◆ CMAGIC_VECTOR_GET_ALLOC_PACKET

#define CMAGIC_VECTOR_GET_ALLOC_PACKET (   cmagic_vector)    cmagic_vector_get_alloc_packet((void**)(cmagic_vector))

Extracts cmagic_memory_alloc_packet_t which was used as an argument of CMAGIC_VECTOR_NEW.

Parameters
cmagic_vectora vector allocated before with CMAGIC_VECTOR_NEW
Returns
suite of dynamic memory managing functions associated with the vector

◆ CMAGIC_VECTOR_NEW

#define CMAGIC_VECTOR_NEW (   type,
  alloc_packet 
)    ((CMAGIC_VECTOR(type))cmagic_vector_new(sizeof(type), (alloc_packet)))

Allocates and returns an address of a newly created empty vector.

Parameters
typetype of vector elements
alloc_packetcmagic_memory_alloc_packet_t suite of dynamic memory managing functions
Returns
a new empty vector

◆ CMAGIC_VECTOR_POP_BACK

#define CMAGIC_VECTOR_POP_BACK (   cmagic_vector)    cmagic_vector_pop_back((void**)(cmagic_vector))

Deallocates the last element in the vector.

Warning
Do not use this without ensuring the vector is not empty.
Parameters
cmagic_vectora vector allocated before with CMAGIC_VECTOR_NEW

◆ CMAGIC_VECTOR_PUSH_BACK

#define CMAGIC_VECTOR_PUSH_BACK (   cmagic_vector,
  new_element_ptr 
)
Value:
(CMAGIC_UTILS_ASSERT_SAME_TYPE(**(cmagic_vector), *(new_element_ptr)), \
cmagic_vector_push_back((void**)(cmagic_vector), (new_element_ptr)))
#define CMAGIC_UTILS_ASSERT_SAME_TYPE(expr1, expr2)
Checks if two L-value expressions have the same type.
Definition: utils.h:33

Allocates space for a new element and initializes it with data under new_element_ptr.

The new element can be accessed by CMAGIC_VECTOR_BACK

Parameters
cmagic_vectora vector allocated before with CMAGIC_VECTOR_NEW
new_element_ptrpointer to a value to be copied into the new element
Returns
true if allocation and initialization of the a new element was successful, false if there's not sufficient memory space and the vector was not modified

◆ CMAGIC_VECTOR_SIZE

#define CMAGIC_VECTOR_SIZE (   cmagic_vector)    cmagic_vector_size((void**)(cmagic_vector))

Deallocates the last element in the vector.

Warning
Do not use this without ensuring the vector is not empty.
Parameters
cmagic_vectora vector allocated before with CMAGIC_VECTOR_NEW