The ROme OpTimistic Simulator  2.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
heap.h
1 /*
2  * heap.h
3  *
4  * Created on: 30 giu 2018
5  * Author: andrea
6  */
7 
8 #ifndef __HEAP_H_
9 #define __HEAP_H_
10 
11 #include <datatypes/array.h>
12 
13 #define rootsim_heap(type) rootsim_array(type)
14 
15 #define heap_init(self) array_init(self)
16 
17 #define heap_empty(self) array_empty(self)
18 
19 #define heap_insert(self, elem, cmp_f) ({\
20  __typeof(array_count(self)) __i_h = array_count(self);\
21  array_push(self, elem);\
22  while(__i_h && cmp_f(elem, array_items(self)[(__i_h - 1)/2]) < 0){\
23  array_items(self)[__i_h] = array_items(self)[(__i_h - 1)/2];\
24  __i_h = (__i_h - 1)/2;\
25  }\
26  array_items(self)[__i_h] = elem;\
27  })
28 
29 #define heap_min(self) ((const)(array_items(self)[0]))
30 
31 #define heap_extract(self, cmp_f) ({\
32  __typeof(*array_items(self)) __ret_h = array_items(self)[0]; \
33  __typeof(*array_items(self)) __last_h = array_pop(self); \
34  __typeof(array_count(self)) __i_h = 0; \
35  do{ \
36  __i_h <<= 1; \
37  ++__i_h; \
38  if(__i_h >= array_count(self)){\
39  break; \
40  } \
41  if(__i_h + 1 < array_count(self)){ \
42  if(cmp_f(array_items(self)[__i_h + 1], array_items(self)[__i_h]) < 0) {\
43  ++__i_h;\
44  } \
45  }\
46  if(cmp_f(array_items(self)[__i_h], __last_h) < 0) {\
47  array_items(self)[(__i_h-1) >> 1] = array_items(self)[__i_h]; \
48  } \
49  else{ \
50  break; \
51  }\
52  } while(1); \
53  array_items(self)[(__i_h-1) >> 1] = __last_h; \
54  __ret_h; \
55  })
56 
57 #endif /* __HEAP_H_ */
This is the implementation of a dynamic array, used for managing various data structures.