ROOT-Sim core  3.0.0-rc.2
A General-Purpose Multi-threaded Parallel/Distributed Simulation Library
array.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <mm/mm.h>
14 
15 #include <stdint.h>
16 #include <string.h>
17 
19 #define INIT_SIZE_ARRAY 8U
21 typedef uint_least32_t array_count_t;
22 
27 #define dyn_array(type) \
28  struct { \
29  type *items; \
30  array_count_t count; \
31  array_count_t capacity; \
32  }
33 
41 #define array_items(self) ((self).items)
42 
48 #define array_count(self) ((self).count)
49 
54 #define array_capacity(self) ((self).capacity)
55 
63 #define array_peek(self) (array_items(self)[array_count(self) - 1])
64 
72 #define array_get_at(self, i) (array_items(self)[(i)])
73 
78 #define array_is_empty(self) (array_count(self) == 0)
79 
84 #define array_init(self) \
85  __extension__({ \
86  array_capacity(self) = INIT_SIZE_ARRAY; \
87  array_items(self) = mm_alloc(array_capacity(self) * sizeof(*array_items(self))); \
88  array_count(self) = 0; \
89  })
90 
95 #define array_fini(self) __extension__({ mm_free(array_items(self)); })
96 
102 #define array_push(self, elem) \
103  __extension__({ \
104  array_expand(self); \
105  array_items(self)[array_count(self)] = (elem); \
106  array_count(self)++; \
107  })
108 
113 #define array_pop(self) \
114  __extension__({ \
115  array_count(self)--; \
116  array_items(self)[array_count(self)]; \
117  })
118 
125 #define array_add_at(self, i, elem) \
126  __extension__({ \
127  array_expand(self); \
128  memmove(&(array_items(self)[(i) + 1]), &(array_items(self)[(i)]), \
129  sizeof(*array_items(self)) * (array_count(self) - (i))); \
130  array_items(self)[(i)] = (elem); \
131  array_count(self)++; \
132  })
133 
139 #define array_remove_at(self, i) \
140  __extension__({ \
141  __typeof__(*array_items(self)) __rmval; \
142  array_count(self)--; \
143  __rmval = array_items(self)[(i)]; \
144  memmove(&(array_items(self)[(i)]), &(array_items(self)[(i) + 1]), \
145  sizeof(*array_items(self)) * (array_count(self) - (i))); \
146  array_shrink(self); \
147  __rmval; \
148  })
149 
155 #define array_truncate_first(self, n) \
156  __extension__({ \
157  array_count(self) -= (n); \
158  memmove(array_items(self), &(array_items(self)[n]), sizeof(*array_items(self)) * (array_count(self))); \
159  })
160 
167 #define array_shrink(self) \
168  __extension__({ \
169  if(unlikely(array_count(self) > INIT_SIZE_ARRAY && array_count(self) * 3 <= array_capacity(self))) { \
170  array_capacity(self) /= 2; \
171  array_items(self) = \
172  mm_realloc(array_items(self), array_capacity(self) * sizeof(*array_items(self))); \
173  } \
174  })
175 
181 #define array_reserve(self, n) \
182  __extension__({ \
183  __typeof__(array_count(self)) tcnt = array_count(self) + (n); \
184  if(unlikely(tcnt >= array_capacity(self))) { \
185  do { \
186  array_capacity(self) *= 2; \
187  } while(unlikely(tcnt >= array_capacity(self))); \
188  array_items(self) = \
189  mm_realloc(array_items(self), array_capacity(self) * sizeof(*array_items(self))); \
190  } \
191  })
192 
197 #define array_expand(self) \
198  __extension__({ \
199  if(unlikely(array_count(self) >= array_capacity(self))) { \
200  array_capacity(self) *= 2; \
201  array_items(self) = \
202  mm_realloc(array_items(self), array_capacity(self) * sizeof(*array_items(self))); \
203  } \
204  })
205 
211 #define array_lazy_remove_at(self, i) \
212  __extension__({ array_items(self)[(i)] = array_items(self)[--array_count(self)]; })
uint_least32_t array_count_t
The type used to handle dynamic arrays count of elements and capacity.
Definition: array.h:21
Memory Manager main header.