Portability | Non-portable (type operators) |
---|---|

Stability | Experimental |

Maintainer | Dan Doel <dan.doel@gmail.com> |

This module implements operations for working with a trinary heap stored in an unboxed array. Most heapsorts are defined in terms of a binary heap, in which each internal node has at most two children. By contrast, a trinary heap has internal nodes with up to three children. This reduces the number of comparisons in a heapsort slightly, and improves locality (again, slightly) by flattening out the heap.

- sort :: (UA e, Ord e) => MUArr e s -> ST s ()
- sortBy :: UA e => Comparison e -> MUArr e s -> ST s ()
- sortByBounds :: UA e => Comparison e -> MUArr e s -> Int -> Int -> ST s ()
- select :: (UA e, Ord e) => MUArr e s -> Int -> ST s ()
- selectBy :: UA e => Comparison e -> MUArr e s -> Int -> ST s ()
- selectByBounds :: UA e => Comparison e -> MUArr e s -> Int -> Int -> Int -> ST s ()
- partialSort :: (UA e, Ord e) => MUArr e s -> Int -> ST s ()
- partialSortBy :: UA e => Comparison e -> MUArr e s -> Int -> ST s ()
- partialSortByBounds :: UA e => Comparison e -> MUArr e s -> Int -> Int -> Int -> ST s ()
- heapify :: UA e => Comparison e -> MUArr e s -> Int -> Int -> ST s ()
- pop :: UA e => Comparison e -> MUArr e s -> Int -> Int -> ST s ()
- popTo :: UA e => Comparison e -> MUArr e s -> Int -> Int -> Int -> ST s ()
- sortHeap :: UA e => Comparison e -> MUArr e s -> Int -> Int -> Int -> ST s ()
- type Comparison e = e -> e -> Ordering

# Sorting

sort :: (UA e, Ord e) => MUArr e s -> ST s ()Source

Sorts an entire array using the default ordering.

sortBy :: UA e => Comparison e -> MUArr e s -> ST s ()Source

Sorts an entire array using a custom ordering.

sortByBounds :: UA e => Comparison e -> MUArr e s -> Int -> Int -> ST s ()Source

Sorts a portion of an array [l,u) using a custom ordering

# Selection

select :: (UA e, Ord e) => MUArr e s -> Int -> ST s ()Source

Moves the lowest k elements to the front of the array. The elements will be in no particular order.

selectBy :: UA e => Comparison e -> MUArr e s -> Int -> ST s ()Source

Moves the `lowest`

(as defined by the comparison) k elements
to the front of the array. The elements will be in no particular
order.

selectByBounds :: UA e => Comparison e -> MUArr e s -> Int -> Int -> Int -> ST s ()Source

Moves the `lowest`

k elements in the portion [l,u) of the
array into the positions [l,k+l). The elements will be in
no particular order.

# Partial sorts

partialSort :: (UA e, Ord e) => MUArr e s -> Int -> ST s ()Source

Moves the lowest k elements to the front of the array, sorted.

partialSortBy :: UA e => Comparison e -> MUArr e s -> Int -> ST s ()Source

Moves the lowest k elements (as defined by the comparison) to the front of the array, sorted.

partialSortByBounds :: UA e => Comparison e -> MUArr e s -> Int -> Int -> Int -> ST s ()Source

Moves the lowest k elements in the portion [l,u) of the array into positions [l,k+l), sorted.

# Heap operations

heapify :: UA e => Comparison e -> MUArr e s -> Int -> Int -> ST s ()Source

Constructs a heap in a portion of an array [l, u)

pop :: UA e => Comparison e -> MUArr e s -> Int -> Int -> ST s ()Source

Given a heap stored in a portion of an array [l,u), swaps the top of the heap with the element at u and rebuilds the heap.

popTo :: UA e => Comparison e -> MUArr e s -> Int -> Int -> Int -> ST s ()Source

Given a heap stored in a portion of an array [l,u) swaps the top of the heap with the element at position t, and rebuilds the heap.

sortHeap :: UA e => Comparison e -> MUArr e s -> Int -> Int -> Int -> ST s ()Source

Given a heap stored in a portion of an array [l,u), sorts the highest values into [m,u). The elements in [l,m) are not in any particular order.

type Comparison e = e -> e -> OrderingSource

A type of comparisons between two values of a given type.