Heap datatype.
More...
Go to the source code of this file.
Heap datatype.
A very simple binary heap implemented on top of our dynamic array
- Copyright
- Copyright (C) 2008-2022 HPDCS Group https://hpdcs.github.io
◆ heap_count
Gets the count of contained element in a heap.
- Parameters
-
- Returns
- the count of contained elements
◆ heap_declare
Declares a heap.
- Parameters
-
type | the type of the contained elements |
◆ heap_extract
#define heap_extract |
( |
|
self, |
|
|
|
cmp_f |
|
) |
| |
Value: __extension__({ \
while(i < cnt) { \
i += i + 1 < cnt && cmp_f(items[i + 1U], items[i]); \
if(!cmp_f(items[i], last)) \
break; \
items[j] = items[i]; \
j = i; \
i = i * 2U + 1U; \
} \
items[j] = last; \
ret; \
})
#define array_items(self)
Gets the underlying actual array of elements of a dynamic array.
Definition: array.h:41
#define array_count(self)
Gets the count of contained element in a dynamic array.
Definition: array.h:48
#define array_pop(self)
Pop an element from the end of a dynamic array.
Definition: array.h:113
Extract an element from the heap.
- Parameters
-
self | the heap from where to extract the element |
cmp_f | a comparing function f(a, b) which returns true iff a < b |
- Returns
- the extracted element
For correct operation of the heap you need to always pass the same cmp_f both for insertion and extraction
◆ heap_fini
Finalize a heap.
- Parameters
-
The user is responsible for cleaning up the possibly contained items.
◆ heap_init
Initialize an empty heap.
- Parameters
-
self | the heap to initialize |
◆ heap_insert
#define heap_insert |
( |
|
self, |
|
|
|
cmp_f, |
|
|
|
elem |
|
) |
| |
Value: __extension__({ \
array_reserve(self, 1); \
while(i && cmp_f(elem, items[(i - 1U) / 2U])) { \
items[i] = items[(i - 1U) / 2U]; \
i = (i - 1U) / 2U; \
} \
items[i] = elem; \
i; \
})
Insert an element into the heap.
- Parameters
-
self | the heap target of the insertion |
cmp_f | a comparing function f(a, b) which returns true iff a < b |
elem | the element to insert |
- Returns
- the position of the inserted element in the underlying array
For correct operation of the heap you need to always pass the same cmp_f, both for insertion and extraction
◆ heap_insert_n
#define heap_insert_n |
( |
|
self, |
|
|
|
cmp_f, |
|
|
|
ins, |
|
|
|
n |
|
) |
| |
Value: __extension__({ \
array_reserve(self, n); \
while(j--) { \
while(i && cmp_f((ins)[j], items[(i - 1U) / 2U])) { \
items[i] = items[(i - 1U) / 2U]; \
i = (i - 1U) / 2U; \
} \
items[i] = (ins)[j]; \
} \
})
Insert n elements into the heap.
- Parameters
-
self | the heap target of the insertion |
cmp_f | a comparing function f(a, b) which returns true iff a < b |
ins | the set of elements to insert |
n | the number of elements in the set |
- Returns
- the position of the inserted element in the underlying array
For correct operation of the heap you need to always pass the same cmp_f, both for insertion and extraction
◆ heap_is_empty
Check if a heap is empty.
- Parameters
-
- Returns
- true if
self
heap is empty, false otherwise
◆ heap_items
Gets the underlying actual array of elements of a binary heap.
- Parameters
-
- Returns
- a pointer to the underlying array of elements
You can use the returned array to directly index items, but do it at your own risk!
◆ heap_min
Get the highest priority element.
- Parameters
-
- Returns
- the highest priority element, cast to const