Data structure for heaps.
This package exposes a data structure to implement heaps/priority queues/fast in-place sorting.
The heap is implemented as a pairing heap, as it is simple but fast, and has been shown to work well in real-world situations.
An empty heap for any comparable type (ints, floats, chars, strings, lists, or tuples).
A heap containing one comparable value (ints, floats, chars, strings, lists, or tuples).
Heap.singleton (3, 4)True if the Heap is empty, otherwise False.
>>> Heap.isEmpty Heap.empty
True
>>> Heap.isEmpty (Heap.singleton 3)
FalseAdd a value to a heap.
>>> Heap.fromList [ 1, 6, 7 ]
... |> Heap.push 4
... |> Heap.peek
Just 1
>>> Heap.fromList [ 5, 6, 7 ]
... |> Heap.push 4
... |> Heap.peek
Just 4Merge the second heap into the first heap.
Note This function assumes that both heaps are sorted using the same method. Strictly speaking, the merged heap has the same sorting method as the first argument.
>>> Heap.isEmpty (Heap.merge Heap.empty Heap.empty)
True
>>> Heap.merge (Heap.fromList [ 2, 4, 6, 7 ]) (Heap.fromList [ 5, 7, 9, 3 ])
... |> Heap.size
8True if the Heap is empty, otherwise False.
>>> Heap.isEmpty Heap.empty
True
>>> Heap.isEmpty (Heap.singleton 3)
FalseNumber of elements in heap.
>>> Heap.size Heap.empty
0
>>> Heap.size (Heap.fromList [ 1, 2, 3, 4, 5, 6, 7, 8 ])
8Look at smallest value in heap without applying any transformations.
>>> Heap.peek Heap.empty
Nothing
>>> Heap.peek (Heap.fromList [ 3, 56, 8, 367, 0, 4 ])
Just 0Try to remove the smallest value from the heap, returning the value and the new heap. If the heap is empty, return Nothing.
>>> Heap.pop Heap.empty
Nothing
>>> Heap.fromList [ 3, 5, 7, 7, 2, 9 ]
... |> Heap.pop
... |> Maybe.map (Tuple.mapSecond Heap.size)
Just (2, 5)Try to remove the smallest value from the heap, returning just the new heap. If the heap is empty, return Nothing.
>>> Heap.popBlind Heap.empty
Nothing
>>> Heap.singleton 3
... |> Heap.popBlind
... |> Maybe.map Heap.size
Just 0Compare two heaps (using compare method of first heap):
An empty heap is less than a non-empty heap.
If one heap's min-value is less than another, the heap is less than the other
If two heaps share the same min-value, remove the min-values are compare the resulting heaps
Heap.compareHeaps Heap.empty Heap.empty EQ
Heap.compareHeaps Heap.empty (Heap.singleton 3) LT
Heap.compareHeaps (Heap.fromList [ 1, 2, 3, 4 ]) (Heap.fromList [ 1, 2, 3 ]) GT
A heap containing all values in the list of comparable types.
Heap.fromList []
Heap.fromList [ 8, 3, 8, 3, 6, 67, 23 ]Get all values from the heap, in order.
>>> Heap.toList (Heap.fromList [ 9, 3, 6, 4, 1, 2, 8, 5, 7 ])
[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]Get all values from the heap, in reverse order.
>>> Heap.toListReverse (Heap.fromList [ 9, 3, 6, 4, 1, 2, 8, 5, 7 ])
[ 9, 8, 7, 6, 5, 4, 3, 2, 1 ]An empty heap for any type, given a function to make a comprable value.
Heap.emptySortedBy .surnameA heap containing one value, given a function to make a comparable value.
Heap.singletonSortedBy .surname { firstname = "Buzz", surname = "Aldrin" }An empty heap for any type, given a function to compare values.
Heap.emptySortedWith (\a b -> compare (List.maximum a) (List.maximum b))A heap containing one value, given a function to compare values.
Heap.SingletonSortedWith [ 1, 2, 3, 56 ]A heap containing all values in the list, given a function to make a comparable value.
Heap.fromListSortedBy .surname
[ { firstname = "Buzz", surname = "Aldrin" }
, { firstname = "Norman", surname = "Bates" }
, { firstname = "Bruce", surname = "Campbell" }
]A heap containing all values in the list, given a function to compare values.
Heap.fromListSortedWith (\a b -> compare (List.maximum a) (List.maximum b))
[ [ 1, 999 ]
, [ 6, 4, 3, 8, 9, 347, 34, 132, 546 ]
]
Heaps are exposed as an opaque union type.