The ROme OpTimistic Simulator  3.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
heap.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <datatypes/array.h>
14 
15 #define binary_heap(type) dyn_array(type)
16 
17 #define heap_items(self) array_items(self)
18 #define heap_count(self) array_count(self)
19 
24 #define heap_init(self) array_init(self)
25 
32 #define heap_fini(self) array_fini(self)
33 
34 #define heap_is_empty(self) array_is_empty(self)
35 #define heap_min(self) ((__typeof(*array_items(self)) const)(array_items(self)[0]))
36 
47 #define heap_insert(self, cmp_f, elem) \
48 __extension__({ \
49  __typeof(array_count(self)) __i_h = array_count(self); \
50  array_push(self, elem); \
51  while( \
52  __i_h && \
53  cmp_f(elem, array_items(self)[(__i_h - 1U) / 2U]) \
54  ){ \
55  array_items(self)[__i_h] = \
56  array_items(self)[(__i_h - 1U) / 2U]; \
57  __i_h = (__i_h - 1U) / 2U; \
58  } \
59  array_items(self)[__i_h] = elem; \
60  __i_h; \
61 })
62 
72 #define heap_extract(self, cmp_f) \
73 __extension__({ \
74  __typeof(*array_items(self)) __ret_h = array_items(self)[0]; \
75  __typeof(*array_items(self)) __last_h = array_pop(self); \
76  __typeof(array_count(self)) __i_h = 1U; \
77  while (__i_h < array_count(self)) { \
78  __i_h += __i_h + 1 < array_count(self) && \
79  cmp_f( \
80  array_items(self)[__i_h + 1U], \
81  array_items(self)[__i_h] \
82  ); \
83  if (!cmp_f(array_items(self)[__i_h], __last_h)) { \
84  break; \
85  } \
86  array_items(self)[(__i_h - 1U) / 2U] = \
87  array_items(self)[__i_h]; \
88  __i_h = __i_h * 2U + 1U; \
89  } \
90  array_items(self)[(__i_h - 1U) / 2U] = __last_h; \
91  __ret_h; \
92 })
array.h
Dynamic array datatype.