The ROme OpTimistic Simulator  3.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
array.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <mm/mm.h>
14 
15 #include <memory.h>
16 #include <stdint.h>
17 
19 #define INIT_SIZE_ARRAY 8U
20 typedef uint_fast32_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 
47 #define array_count(self) ((self).count)
48 
53 #define array_capacity(self) ((self).capacity)
54 
59 #define array_peek(self) (array_items(self)[array_count(self) - 1])
60 //this isn't checked CARE!
61 #define array_get_at(self, i) (array_items(self)[(i)])
62 
63 #define array_is_empty(self) (array_count(self) == 0)
64 
65 #define array_init(self) \
66 __extension__({ \
67  array_capacity(self) = INIT_SIZE_ARRAY; \
68  array_items(self) = mm_alloc(array_capacity(self) * \
69  sizeof(*array_items(self))); \
70  array_count(self) = 0; \
71 })
72 
73 #define array_fini(self) \
74 __extension__({ \
75  mm_free(array_items(self)); \
76 })
77 
78 #define array_push(self, elem) \
79 __extension__({ \
80  array_expand(self); \
81  array_items(self)[array_count(self)] = (elem); \
82  array_count(self)++; \
83 })
84 
85 #define array_pop(self) \
86 __extension__({ \
87  __typeof__(*array_items(self)) __popval; \
88  array_count(self)--; \
89  __popval = array_items(self)[array_count(self)]; \
90  array_shrink(self); \
91  __popval; \
92 })
93 
94 #define array_add_at(self, i, elem) \
95 __extension__({ \
96  array_expand(self); \
97  memmove(&(array_items(self)[(i)+1]), &(array_items(self)[(i)]), \
98  sizeof(*array_items(self)) * (array_count(self)-(i))); \
99  array_items(self)[(i)] = (elem); \
100  array_count(self)++; \
101 })
102 
103 #define array_remove_at_lazy(self, i) \
104 __extension__({ \
105  __typeof__(*array_items(self)) __rmval; \
106  array_count(self)--; \
107  __rmval = array_items(self)[(i)]; \
108  array_items(self)[(i)] = array_items(self)[array_count(self)]; \
109  array_shrink(self); \
110  __rmval; \
111 })
112 
113 #define array_remove_at(self, i) \
114 __extension__({ \
115  __typeof__(*array_items(self)) __rmval; \
116  array_count(self)--; \
117  __rmval = array_items(self)[(i)]; \
118  memmove( \
119  &(array_items(self)[(i)]), \
120  &(array_items(self)[(i)+1]), \
121  sizeof(*array_items(self)) * (array_count(self)-(i)) \
122  ); \
123  array_shrink(self); \
124  __rmval; \
125 })
126 
127 #define array_truncate_first(self, n) \
128 __extension__({ \
129  array_count(self) -= n; \
130  memmove( \
131  array_items(self), \
132  &(array_items(self)[n]), \
133  sizeof(*array_items(self)) * (array_count(self)) \
134  ); \
135 })
136 
137 #define array_shrink(self) \
138 __extension__({ \
139  if (unlikely( \
140  array_count(self) > INIT_SIZE_ARRAY && \
141  array_count(self) * 3 <= array_capacity(self))) { \
142  array_capacity(self) /= 2; \
143  array_items(self) = mm_realloc( \
144  array_items(self), \
145  array_capacity(self) * \
146  sizeof(*array_items(self)) \
147  ); \
148  } \
149 })
150 
151 #define array_expand(self) \
152 __extension__({ \
153  if (unlikely(array_count(self) >= array_capacity(self))) { \
154  array_capacity(self) *= 2; \
155  array_items(self) = mm_realloc( \
156  array_items(self), \
157  array_capacity(self) * \
158  sizeof(*array_items(self)) \
159  ); \
160  } \
161 })
array_count_t
uint_fast32_t array_count_t
The type used to handle dynamic arrays count of elements and capacity.
Definition: array.h:21
mm.h
Memory Manager main header.