vector-algorithms-0.8.0.1: Efficient algorithms for vector arrays

Copyright(c) 2011 Dan Doel
MaintainerDan Doel <dan.doel@gmail.com>
StabilityExperimental
PortabilityNon-portable (FlexibleContexts, ScopedTypeVariables)
Safe HaskellNone
LanguageHaskell98

Data.Vector.Algorithms.AmericanFlag

Description

This module implements American flag sort: an in-place, unstable, bucket sort. Also in contrast to radix sort, the values are inspected in a big endian order, and buckets are sorted via recursive splitting. This, however, makes it sensible for sorting strings in lexicographic order (provided indexing is fast).

The algorithm works as follows: at each stage, the array is looped over, counting the number of elements for each bucket. Then, starting at the beginning of the array, elements are permuted in place to reside in the proper bucket, following chains until they reach back to the current base index. Finally, each bucket is sorted recursively. This lends itself well to the aforementioned variable-length strings, and so the algorithm takes a stopping predicate, which is given a representative of the stripe, rather than running for a set number of iterations.

Synopsis

Documentation

sort :: forall e m v. (PrimMonad m, MVector v e, Lexicographic e, Ord e) => v (PrimState m) e -> m () #

Sorts an array using the default ordering. Both Lexicographic and Ord are necessary because the algorithm falls back to insertion sort for sufficiently small arrays.

sortBy #

Arguments

:: (PrimMonad m, MVector v e) 
=> Comparison e

a comparison for the insertion sort flalback

-> (e -> Int -> Bool)

determines whether a stripe is complete

-> Int

the number of buckets necessary

-> (Int -> e -> Int)

the big-endian radix function

-> v (PrimState m) e

the array to be sorted

-> m () 

A fully parameterized version of the sorting algorithm. Again, this function takes both radix information and a comparison, because the algorithms falls back to insertion sort for small arrays.

class Lexicographic e where #

The methods of this class specify the information necessary to sort arrays using the default ordering. The name Lexicographic is meant to convey that index should return results in a similar way to indexing into a string.

Minimal complete definition

extent, size, index

Methods

extent :: e -> Int #

Computes the length of a representative of a stripe. It should take n passes to sort values of extent n. The extent may not be uniform across all values of the type.

size :: Proxy e -> Int #

The size of the bucket array necessary for sorting es

index :: Int -> e -> Int #

Determines which bucket a given element should inhabit for a particular iteration.

Instances

Lexicographic Int # 

Methods

extent :: Int -> Int #

size :: Proxy * Int -> Int #

index :: Int -> Int -> Int #

Lexicographic Int8 # 

Methods

extent :: Int8 -> Int #

size :: Proxy * Int8 -> Int #

index :: Int -> Int8 -> Int #

Lexicographic Int16 # 

Methods

extent :: Int16 -> Int #

size :: Proxy * Int16 -> Int #

index :: Int -> Int16 -> Int #

Lexicographic Int32 # 

Methods

extent :: Int32 -> Int #

size :: Proxy * Int32 -> Int #

index :: Int -> Int32 -> Int #

Lexicographic Int64 # 

Methods

extent :: Int64 -> Int #

size :: Proxy * Int64 -> Int #

index :: Int -> Int64 -> Int #

Lexicographic Word # 

Methods

extent :: Word -> Int #

size :: Proxy * Word -> Int #

index :: Int -> Word -> Int #

Lexicographic Word8 # 

Methods

extent :: Word8 -> Int #

size :: Proxy * Word8 -> Int #

index :: Int -> Word8 -> Int #

Lexicographic Word16 # 

Methods

extent :: Word16 -> Int #

size :: Proxy * Word16 -> Int #

index :: Int -> Word16 -> Int #

Lexicographic Word32 # 

Methods

extent :: Word32 -> Int #

size :: Proxy * Word32 -> Int #

index :: Int -> Word32 -> Int #

Lexicographic Word64 # 

Methods

extent :: Word64 -> Int #

size :: Proxy * Word64 -> Int #

index :: Int -> Word64 -> Int #

Lexicographic ByteString # 
(Lexicographic a, Lexicographic b) => Lexicographic (Either a b) # 

Methods

extent :: Either a b -> Int #

size :: Proxy * (Either a b) -> Int #

index :: Int -> Either a b -> Int #

(Lexicographic a, Lexicographic b) => Lexicographic (a, b) # 

Methods

extent :: (a, b) -> Int #

size :: Proxy * (a, b) -> Int #

index :: Int -> (a, b) -> Int #