The ROme OpTimistic Simulator  3.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
array.h File Reference

Dynamic array datatype. More...

#include <mm/mm.h>
#include <memory.h>
#include <stdint.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 current capacity of a dynamic array. More...
 
#define array_get_at(self, i)   (array_items(self)[(i)])
 
#define array_is_empty(self)   (array_count(self) == 0)
 
#define array_init(self)
 
#define array_fini(self)
 
#define array_push(self, elem)
 
#define array_pop(self)
 
#define array_add_at(self, i, elem)
 
#define array_remove_at_lazy(self, i)
 
#define array_remove_at(self, i)
 
#define array_truncate_first(self, n)
 
#define array_shrink(self)
 
#define array_expand(self)
 

Typedefs

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

Detailed Description

Dynamic array datatype.

Dynamic array datatype

Definition in file array.h.

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)++; \
})

Definition at line 94 of file array.h.

◆ array_capacity

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

Gets the current capacity of a dynamic array.

Parameters
selfThe target dynamic array

Definition at line 53 of file array.h.

◆ array_count

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

Gets the count of contained element in a dynamic array.

Parameters
selfThe target dynamic array

Definition at line 47 of file array.h.

◆ 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)) \
); \
} \
})

Definition at line 151 of file array.h.

◆ array_fini

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

Definition at line 73 of file array.h.

◆ 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; \
})

Definition at line 65 of file array.h.

◆ 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!

Definition at line 41 of file array.h.

◆ array_peek

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

Gets the current capacity of a dynamic array.

Parameters
selfThe target dynamic array

Definition at line 59 of file array.h.

◆ array_pop

#define array_pop (   self)
Value:
__extension__({ \
__typeof__(*array_items(self)) __popval; \
array_count(self)--; \
__popval = array_items(self)[array_count(self)]; \
array_shrink(self); \
__popval; \
})

Definition at line 85 of file array.h.

◆ array_push

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

Definition at line 78 of file array.h.

◆ 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; \
})

Definition at line 113 of file array.h.

◆ array_remove_at_lazy

#define array_remove_at_lazy (   self,
 
)
Value:
__extension__({ \
__typeof__(*array_items(self)) __rmval; \
array_count(self)--; \
__rmval = array_items(self)[(i)]; \
array_items(self)[(i)] = array_items(self)[array_count(self)]; \
array_shrink(self); \
__rmval; \
})

Definition at line 103 of file array.h.

◆ array_shrink

#define array_shrink (   self)
Value:
__extension__({ \
if (unlikely( \
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)) \
); \
} \
})

Definition at line 137 of file array.h.

◆ 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)) \
); \
})

Definition at line 127 of file array.h.

◆ 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

Definition at line 27 of file array.h.

INIT_SIZE_ARRAY
#define INIT_SIZE_ARRAY
The initial size of dynamic arrays expressed in the number of elements.
Definition: array.h:19
array_items
#define array_items(self)
Gets the underlying actual array of elements of a dynamic array.
Definition: array.h:41
mm_alloc
void * mm_alloc(size_t mem_size)
A version of the stdlib malloc() used internally.
Definition: mm.h:26
array_capacity
#define array_capacity(self)
Gets the current capacity of a dynamic array.
Definition: array.h:53
array_count
#define array_count(self)
Gets the count of contained element in a dynamic array.
Definition: array.h:47
unlikely
#define unlikely(exp)
Optimize the branch as likely not taken.
Definition: core.h:59
mm_realloc
void * mm_realloc(void *ptr, size_t mem_size)
A version of the stdlib realloc() used internally.
Definition: mm.h:46