ROOT-Sim core  3.0.0-rc.2
A General-Purpose Multi-threaded Parallel/Distributed Simulation Library
heap.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <datatypes/array.h>
14 
19 #define heap_declare(type) dyn_array(type)
20 
28 #define heap_items(self) array_items(self)
29 
35 #define heap_count(self) array_count(self)
36 
41 #define heap_init(self) array_init(self)
42 
49 #define heap_fini(self) array_fini(self)
50 
56 #define heap_is_empty(self) array_is_empty(self)
57 
63 #define heap_min(self) (*(__typeof(*array_items(self)) *const)array_items(self))
64 
74 #define heap_insert(self, cmp_f, elem) \
75  __extension__({ \
76  array_reserve(self, 1); \
77  __typeof(array_count(self)) i = array_count(self)++; \
78  __typeof__(array_items(self)) items = array_items(self); \
79  while(i && cmp_f(elem, items[(i - 1U) / 2U])) { \
80  items[i] = items[(i - 1U) / 2U]; \
81  i = (i - 1U) / 2U; \
82  } \
83  items[i] = elem; \
84  i; \
85  })
86 
97 #define heap_insert_n(self, cmp_f, ins, n) \
98  __extension__({ \
99  array_reserve(self, n); \
100  __typeof(array_count(self)) j = n; \
101  __typeof__(array_items(self)) items = array_items(self); \
102  while(j--) { \
103  __typeof(array_count(self)) i = array_count(self)++; \
104  while(i && cmp_f((ins)[j], items[(i - 1U) / 2U])) { \
105  items[i] = items[(i - 1U) / 2U]; \
106  i = (i - 1U) / 2U; \
107  } \
108  items[i] = (ins)[j]; \
109  } \
110  })
111 
120 #define heap_extract(self, cmp_f) \
121  __extension__({ \
122  __typeof__(array_items(self)) items = array_items(self); \
123  __typeof(*array_items(self)) ret = array_items(self)[0]; \
124  __typeof(*array_items(self)) last = array_pop(self); \
125  __typeof(array_count(self)) cnt = array_count(self); \
126  __typeof(array_count(self)) i = 1U; \
127  __typeof(array_count(self)) j = 0U; \
128  while(i < cnt) { \
129  i += i + 1 < cnt && cmp_f(items[i + 1U], items[i]); \
130  if(!cmp_f(items[i], last)) \
131  break; \
132  items[j] = items[i]; \
133  j = i; \
134  i = i * 2U + 1U; \
135  } \
136  items[j] = last; \
137  ret; \
138  })
Dynamic array datatype.