ROOT-Sim core  3.0.0-rc.2
A General-Purpose Multi-threaded Parallel/Distributed Simulation Library
Macros | Typedefs
array.h File Reference

Dynamic array datatype. More...

#include <mm/mm.h>
#include <stdint.h>
#include <string.h>
Include dependency graph for array.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define INIT_SIZE_ARRAY   8U
 The initial size of dynamic arrays expressed in the number of elements.
 
#define dyn_array(type)
 Declares a dynamic array. More...
 
#define array_items(self)   ((self).items)
 Gets the underlying actual array of elements of a dynamic array. More...
 
#define array_count(self)   ((self).count)
 Gets the count of contained element in a dynamic array. More...
 
#define array_capacity(self)   ((self).capacity)
 Gets the current capacity of a dynamic array. More...
 
#define array_peek(self)   (array_items(self)[array_count(self) - 1])
 Gets the last element of a dynamic array. More...
 
#define array_get_at(self, i)   (array_items(self)[(i)])
 Gets the i-th element of a dynamic array. More...
 
#define array_is_empty(self)   (array_count(self) == 0)
 Check if the dynamic array is empty. More...
 
#define array_init(self)
 Initializes a dynamic array. More...
 
#define array_fini(self)   __extension__({ mm_free(array_items(self)); })
 Releases the memory used by a dynamic array. More...
 
#define array_push(self, elem)
 Push an element to the end of a dynamic array. More...
 
#define array_pop(self)
 Pop an element from the end of a dynamic array. More...
 
#define array_add_at(self, i, elem)
 Insert an element at the given index. More...
 
#define array_remove_at(self, i)
 Remove an element at the given index from the dynamic array. More...
 
#define array_truncate_first(self, n)
 Remove first n elements from the dynamic array. More...
 
#define array_shrink(self)
 Reduce the size of the dynamic array. More...
 
#define array_reserve(self, n)
 Expand the size of the dynamic array to have at least the requested capacity. More...
 
#define array_expand(self)
 Double the size of the dynamic array if the number of elements is greater than the capacity. More...
 
#define array_lazy_remove_at(self, i)    __extension__({ array_items(self)[(i)] = array_items(self)[--array_count(self)]; })
 Remove an element at the given index from the dynamic array, last array element will take its place. More...
 

Typedefs

typedef uint_least32_t array_count_t
 The type used to handle dynamic arrays count of elements and capacity.
 

Detailed Description

Dynamic array datatype.

Dynamic array datatype

Macro Definition Documentation

◆ array_add_at

#define array_add_at (   self,
  i,
  elem 
)
Value:
__extension__({ \
array_expand(self); \
memmove(&(array_items(self)[(i) + 1]), &(array_items(self)[(i)]), \
sizeof(*array_items(self)) * (array_count(self) - (i))); \
array_items(self)[(i)] = (elem); \
array_count(self)++; \
})
#define array_items(self)
Gets the underlying actual array of elements of a dynamic array.
Definition: array.h:41
#define array_count(self)
Gets the count of contained element in a dynamic array.
Definition: array.h:48

Insert an element at the given index.

Parameters
selfThe target dynamic array
iThe index at which to insert the element
elemThe element to insert

◆ array_capacity

#define array_capacity (   self)    ((self).capacity)

Gets the current capacity of a dynamic array.

Parameters
selfThe target dynamic array

◆ array_count

#define array_count (   self)    ((self).count)

Gets the count of contained element in a dynamic array.

Parameters
selfThe target dynamic array
Returns
the count of contained elements

◆ array_expand

#define array_expand (   self)
Value:
__extension__({ \
if(unlikely(array_count(self) >= array_capacity(self))) { \
array_capacity(self) *= 2; \
array_items(self) = \
mm_realloc(array_items(self), array_capacity(self) * sizeof(*array_items(self))); \
} \
})
#define array_capacity(self)
Gets the current capacity of a dynamic array.
Definition: array.h:54
#define unlikely(exp)
Optimize the branch as likely not taken.
Definition: core.h:51

Double the size of the dynamic array if the number of elements is greater than the capacity.

Parameters
selfThe target dynamic array

◆ array_fini

#define array_fini (   self)    __extension__({ mm_free(array_items(self)); })

Releases the memory used by a dynamic array.

Parameters
selfThe target dynamic array

◆ array_get_at

#define array_get_at (   self,
 
)    (array_items(self)[(i)])

Gets the i-th element of a dynamic array.

Parameters
selfThe target dynamic array
iThe index of the element to get
Warning
This function is unsafe: It is not checked if the index is valid!

◆ array_init

#define array_init (   self)
Value:
__extension__({ \
array_capacity(self) = INIT_SIZE_ARRAY; \
array_items(self) = mm_alloc(array_capacity(self) * sizeof(*array_items(self))); \
array_count(self) = 0; \
})
#define INIT_SIZE_ARRAY
The initial size of dynamic arrays expressed in the number of elements.
Definition: array.h:19
static void * mm_alloc(size_t mem_size)
A version of the stdlib malloc() used internally.
Definition: mm.h:62

Initializes a dynamic array.

Parameters
selfThe target dynamic array

◆ array_is_empty

#define array_is_empty (   self)    (array_count(self) == 0)

Check if the dynamic array is empty.

Parameters
selfThe target dynamic array

◆ array_items

#define array_items (   self)    ((self).items)

Gets the underlying actual array of elements of a dynamic array.

Parameters
selfThe target dynamic array
Returns
a pointer to the underlying array of elements

You can use the array to directly index items, but do it at your own risk!

◆ array_lazy_remove_at

#define array_lazy_remove_at (   self,
 
)     __extension__({ array_items(self)[(i)] = array_items(self)[--array_count(self)]; })

Remove an element at the given index from the dynamic array, last array element will take its place.

Parameters
selfThe target dynamic array
iThe index of the element to remove

◆ array_peek

#define array_peek (   self)    (array_items(self)[array_count(self) - 1])

Gets the last element of a dynamic array.

Parameters
selfthe target dynamic array
Returns
the last element of the array

You have to check for the array emptiness before safely calling this macro

◆ array_pop

#define array_pop (   self)
Value:
__extension__({ \
array_count(self)--; \
array_items(self)[array_count(self)]; \
})

Pop an element from the end of a dynamic array.

Parameters
selfThe target dynamic array

◆ array_push

#define array_push (   self,
  elem 
)
Value:
__extension__({ \
array_expand(self); \
array_items(self)[array_count(self)] = (elem); \
array_count(self)++; \
})

Push an element to the end of a dynamic array.

Parameters
selfThe target dynamic array
elemThe element to push

◆ array_remove_at

#define array_remove_at (   self,
 
)
Value:
__extension__({ \
__typeof__(*array_items(self)) __rmval; \
array_count(self)--; \
__rmval = array_items(self)[(i)]; \
memmove(&(array_items(self)[(i)]), &(array_items(self)[(i) + 1]), \
sizeof(*array_items(self)) * (array_count(self) - (i))); \
array_shrink(self); \
__rmval; \
})
#define array_shrink(self)
Reduce the size of the dynamic array.
Definition: array.h:167

Remove an element at the given index from the dynamic array.

Parameters
selfThe target dynamic array
iThe index of the element to remove

◆ array_reserve

#define array_reserve (   self,
 
)
Value:
__extension__({ \
__typeof__(array_count(self)) tcnt = array_count(self) + (n); \
if(unlikely(tcnt >= array_capacity(self))) { \
do { \
array_capacity(self) *= 2; \
} while(unlikely(tcnt >= array_capacity(self))); \
array_items(self) = \
mm_realloc(array_items(self), array_capacity(self) * sizeof(*array_items(self))); \
} \
})

Expand the size of the dynamic array to have at least the requested capacity.

Parameters
selfThe target dynamic array
nThe requested capacity

◆ array_shrink

#define array_shrink (   self)
Value:
__extension__({ \
if(unlikely(array_count(self) > INIT_SIZE_ARRAY && array_count(self) * 3 <= array_capacity(self))) { \
array_capacity(self) /= 2; \
array_items(self) = \
mm_realloc(array_items(self), array_capacity(self) * sizeof(*array_items(self))); \
} \
})

Reduce the size of the dynamic array.

Parameters
selfThe target dynamic array

The size of the dinamic array is halved if the number of elements is less than a third of the capacity.

◆ array_truncate_first

#define array_truncate_first (   self,
 
)
Value:
__extension__({ \
array_count(self) -= (n); \
memmove(array_items(self), &(array_items(self)[n]), sizeof(*array_items(self)) * (array_count(self))); \
})

Remove first n elements from the dynamic array.

Parameters
selfThe target dynamic array
nThe number of elements to remove

◆ dyn_array

#define dyn_array (   type)
Value:
struct { \
type *items; \
array_count_t count; \
array_count_t capacity; \
}

Declares a dynamic array.

Parameters
typeThe type of the contained elements