Binary heap of (integer(int64) key, integer(int32) payload) pairs
with stable FIFO tie-breaking on equal keys.
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer(kind=int64), | private, | allocatable | :: | insertion(:) |
Insertion index of each entry, used to break key ties FIFO |
||
| integer(kind=int64), | private, | allocatable | :: | keys(:) |
Heap-ordered keys, valid in |
||
| logical, | private | :: | max_heap | = | .false. |
|
|
| integer(kind=default_int), | private | :: | n_entries | = | 0 |
Number of entries currently stored |
|
| integer(kind=int64), | private | :: | n_pushed | = | 0 |
Monotonic counter handing out insertion indices |
|
| integer(kind=int32), | private, | allocatable | :: | payloads(:) |
Payload travelling with |
Replace the heap contents with keys/payloads, heapified in O(n).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(heap_t), | intent(inout) | :: | self | |||
| integer(kind=int64), | intent(in) | :: | keys(:) | |||
| integer(kind=int32), | intent(in) | :: | payloads(:) | |||
| type(error_t), | intent(out), | optional | :: | err |
Number of entries the heap can hold before it has to grow.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(heap_t), | intent(in) | :: | self |
Remove every entry but keep the allocated storage for reuse.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(heap_t), | intent(inout) | :: | self |
Release the heap storage and reset it to empty.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(heap_t), | intent(inout) | :: | self |
Initialise the heap, discarding any previous contents.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(heap_t), | intent(inout) | :: | self | |||
| integer(kind=default_int), | intent(in), | optional | :: | cap | ||
| logical, | intent(in), | optional | :: | max_heap |
.true. when the heap holds no entries.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(heap_t), | intent(in) | :: | self |
Return the root entry without removing it.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(heap_t), | intent(in) | :: | self | |||
| integer(kind=int64), | intent(out) | :: | key | |||
| integer(kind=int32), | intent(out) | :: | payload | |||
| type(error_t), | intent(out), | optional | :: | err |
Remove and return the root entry (minimum, or maximum for a max-heap).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(heap_t), | intent(inout) | :: | self | |||
| integer(kind=int64), | intent(out) | :: | key | |||
| integer(kind=int32), | intent(out) | :: | payload | |||
| type(error_t), | intent(out), | optional | :: | err |
Insert one (key, payload) entry in O(log n).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(heap_t), | intent(inout) | :: | self | |||
| integer(kind=int64), | intent(in) | :: | key | |||
| integer(kind=int32), | intent(in) | :: | payload |
Ensure room for at least n entries without further reallocation.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(heap_t), | intent(inout) | :: | self | |||
| integer(kind=default_int), | intent(in) | :: | n |
type :: heap_t !! Binary heap of (`integer(int64)` key, `integer(int32)` payload) pairs !! with stable FIFO tie-breaking on equal keys. integer(int64), allocatable, private :: keys(:) !! Heap-ordered keys, valid in `1:n_entries` integer(int32), allocatable, private :: payloads(:) !! Payload travelling with `keys`, same index integer(int64), allocatable, private :: insertion(:) !! Insertion index of each entry, used to break key ties FIFO integer(default_int), private :: n_entries = 0 !! Number of entries currently stored integer(int64), private :: n_pushed = 0 !! Monotonic counter handing out insertion indices logical, private :: max_heap = .false. !! `.true.` selects max-heap ordering, `.false.` min-heap contains procedure :: init => heap_init procedure :: clear => heap_clear procedure :: destroy => heap_destroy procedure :: size => heap_size procedure :: is_empty => heap_is_empty procedure :: capacity => heap_capacity procedure :: reserve => heap_reserve procedure :: push => heap_push procedure :: pop => heap_pop procedure :: peek => heap_peek procedure :: build_from => heap_build_from end type heap_t