dynamic_array.h v0.3.1
Reference-counted dynamic arrays for C
Loading...
Searching...
No Matches
Macros
Type-Safe Array Macros

Convenient type-safe macros for array operations. More...

Macros

#define DA_CREATE(T, cap, retain_fn, release_fn)   da_create(sizeof(T), cap, retain_fn, release_fn)
 Type-safe array creation.
 
#define DA_PUSH_TYPED(arr, val, T)   do { T _temp = (val); da_push(arr, (void*)&_temp); } while(0)
 Type-safe element append with explicit type parameter.
 
#define DA_PUT_TYPED(arr, i, val, T)   do { T _temp = (val); da_set(arr, i, (void*)&_temp); } while(0)
 Type-safe element assignment with explicit type parameter.
 
#define DA_INSERT_TYPED(arr, i, val, T)   do { T _temp = (val); da_insert(arr, i, (void*)&_temp); } while(0)
 Type-safe element insert with explicit type parameter.
 
#define DA_PUSH(arr, val, T)   DA_PUSH_TYPED(arr, val, T)
 Type-safe element append (adaptive macro)
 
#define DA_PUT(arr, i, val, T)   DA_PUT_TYPED(arr, i, val, T)
 Type-safe element assignment (adaptive macro)
 
#define DA_INSERT(arr, i, val, T)   DA_INSERT_TYPED(arr, i, val, T)
 Type-safe element insert (adaptive macro)
 
#define DA_AT(arr, i, T)   (*(T*)da_get(arr, i))
 Type-safe element access by value.
 
#define DA_LENGTH(arr)   da_length(arr)
 Get array length (shorthand for da_length)
 
#define DA_CAPACITY(arr)   da_capacity(arr)
 Get array capacity (shorthand for da_capacity)
 
#define DA_POP(arr, out_ptr)   da_pop(arr, out_ptr)
 Pop last element (shorthand for da_pop)
 
#define DA_CLEAR(arr)   da_clear(arr)
 Clear array (shorthand for da_clear)
 
#define DA_RESERVE(arr, cap)   da_reserve(arr, cap)
 Reserve capacity (shorthand for da_reserve)
 
#define DA_RESIZE(arr, len)   da_resize(arr, len)
 Resize array (shorthand for da_resize)
 
#define DA_REMOVE(arr, i, out_ptr)   da_remove(arr, i, out_ptr)
 Remove element at index (shorthand for da_remove)
 

Detailed Description

Convenient type-safe macros for array operations.

Macro Definition Documentation

◆ DA_AT

#define DA_AT (   arr,
  i,
 
)    (*(T*)da_get(arr, i))

Type-safe element access by value.

Parameters
arrArray to access
iIndex to get
TElement type
Returns
Element value at index i
int value = DA_AT(arr, 0, int);
#define DA_AT(arr, i, T)
Type-safe element access by value.
Definition dynamic_array.h:1269

◆ DA_CREATE

#define DA_CREATE (   T,
  cap,
  retain_fn,
  release_fn 
)    da_create(sizeof(T), cap, retain_fn, release_fn)

Type-safe array creation.

Parameters
TElement type (e.g., int, float, struct mytype)
capInitial capacity
Returns
New da_array sized for type T
da_array arr = DA_CREATE(int, 10);
#define DA_CREATE(T, cap, retain_fn, release_fn)
Type-safe array creation.
Definition dynamic_array.h:1245
Reference-counted dynamic array structure.
Definition dynamic_array.h:192

◆ DA_INSERT

#define DA_INSERT (   arr,
  i,
  val,
 
)    DA_INSERT_TYPED(arr, i, val, T)

Type-safe element insert (adaptive macro)

Type-safe element insert (with typeof support)

Parameters
arrArray to modify
iIndex to insert at
valValue to insert
TType parameter (only when DA_SUPPORT_TYPE_INFERENCE=0 or DA_NOT_USE_TYPE_GENERIC is defined)
Note
When DA_SUPPORT_TYPE_INFERENCE=1 and DA_NOT_USE_TYPE_GENERIC is not defined: DA_INSERT(arr, 0, 42)
When DA_SUPPORT_TYPE_INFERENCE=0 or DA_NOT_USE_TYPE_GENERIC is defined: DA_INSERT(arr, 0, 42, int)
Automatically chooses between inferred and typed versions
// With type inference support:
DA_INSERT(arr, 0, 42);
// Without type inference or with DA_NOT_USE_TYPE_GENERIC:
DA_INSERT(arr, 0, 42, int);
#define DA_INSERT(arr, i, val, T)
Type-safe element insert (adaptive macro)
Definition dynamic_array.h:1264
Parameters
arrArray to modify
iIndex to insert at
valValue to insert
Note
With typeof support: DA_INSERT(arr, 0, 42)
Without typeof: DA_INSERT(arr, 0, 42, int)

◆ DA_INSERT_TYPED

#define DA_INSERT_TYPED (   arr,
  i,
  val,
 
)    do { T _temp = (val); da_insert(arr, i, (void*)&_temp); } while(0)

Type-safe element insert with explicit type parameter.

Parameters
arrArray to modify
iIndex to insert at
valValue to insert
TExplicit type of the value
Note
Always requires explicit type parameter
DA_INSERT_TYPED(arr, 0, 42, int);
#define DA_INSERT_TYPED(arr, i, val, T)
Type-safe element insert with explicit type parameter.
Definition dynamic_array.h:1249

◆ DA_PUSH

#define DA_PUSH (   arr,
  val,
 
)    DA_PUSH_TYPED(arr, val, T)

Type-safe element append (adaptive macro)

Parameters
arrArray to modify
valValue to append
TType parameter (only when DA_SUPPORT_TYPE_INFERENCE=0 or DA_NOT_USE_TYPE_GENERIC is defined)
Note
When DA_SUPPORT_TYPE_INFERENCE=1 and DA_NOT_USE_TYPE_GENERIC is not defined: DA_PUSH(arr, 42)
When DA_SUPPORT_TYPE_INFERENCE=0 or DA_NOT_USE_TYPE_GENERIC is defined: DA_PUSH(arr, 42, int)
Automatically chooses between inferred and typed versions
// With type inference support:
DA_PUSH(arr, 42);
// Without type inference or with DA_NOT_USE_TYPE_GENERIC:
DA_PUSH(arr, 42, int);
#define DA_PUSH(arr, val, T)
Type-safe element append (adaptive macro)
Definition dynamic_array.h:1262

◆ DA_PUSH_TYPED

#define DA_PUSH_TYPED (   arr,
  val,
 
)    do { T _temp = (val); da_push(arr, (void*)&_temp); } while(0)

Type-safe element append with explicit type parameter.

Parameters
arrArray to modify
valValue to append
TExplicit type of the value
Note
Always requires explicit type parameter
DA_PUSH_TYPED(arr, 42, int);
#define DA_PUSH_TYPED(arr, val, T)
Type-safe element append with explicit type parameter.
Definition dynamic_array.h:1247

◆ DA_PUT

#define DA_PUT (   arr,
  i,
  val,
 
)    DA_PUT_TYPED(arr, i, val, T)

Type-safe element assignment (adaptive macro)

Parameters
arrArray to modify
iIndex to set
valValue to assign
TType parameter (only when DA_SUPPORT_TYPE_INFERENCE=0 or DA_NOT_USE_TYPE_GENERIC is defined)
Note
When DA_SUPPORT_TYPE_INFERENCE=1 and DA_NOT_USE_TYPE_GENERIC is not defined: DA_PUT(arr, 0, 42)
When DA_SUPPORT_TYPE_INFERENCE=0 or DA_NOT_USE_TYPE_GENERIC is defined: DA_PUT(arr, 0, 42, int)
Automatically chooses between inferred and typed versions
// With type inference support:
DA_PUT(arr, 0, 42);
// Without type inference or with DA_NOT_USE_TYPE_GENERIC:
DA_PUT(arr, 0, 42, int);
#define DA_PUT(arr, i, val, T)
Type-safe element assignment (adaptive macro)
Definition dynamic_array.h:1263

◆ DA_PUT_TYPED

#define DA_PUT_TYPED (   arr,
  i,
  val,
 
)    do { T _temp = (val); da_set(arr, i, (void*)&_temp); } while(0)

Type-safe element assignment with explicit type parameter.

Parameters
arrArray to modify
iIndex to set
valValue to assign
TExplicit type of the value
Note
Always requires explicit type parameter
DA_PUT_TYPED(arr, 0, 42, int);
#define DA_PUT_TYPED(arr, i, val, T)
Type-safe element assignment with explicit type parameter.
Definition dynamic_array.h:1248

◆ DA_REMOVE

#define DA_REMOVE (   arr,
  i,
  out_ptr 
)    da_remove(arr, i, out_ptr)

Remove element at index (shorthand for da_remove)

Parameters
arrArray to modify
iIndex to remove from
out_ptrOptional pointer to store removed element (can be NULL)