module Statistics.Function
(
minMax
, sort
, sortBy
, partialSort
, indexed
, indices
, nextHighestPowerOfTwo
, within
) where
#include "MachDeps.h"
import Data.Bits ((.|.), shiftR)
import qualified Data.Vector.Algorithms.Intro as I
import qualified Data.Vector.Generic as G
import Statistics.Function.Comparison (within)
sort :: (Ord e, G.Vector v e) => v e -> v e
sort = G.modify I.sort
sortBy :: (G.Vector v e) => I.Comparison e -> v e -> v e
sortBy f = G.modify $ I.sortBy f
partialSort :: (G.Vector v e, Ord e) =>
Int
-> v e
-> v e
partialSort k = G.modify (`I.partialSort` k)
indices :: (G.Vector v a, G.Vector v Int) => v a -> v Int
indices a = G.enumFromTo 0 (G.length a 1)
indexed :: (G.Vector v e, G.Vector v Int, G.Vector v (Int,e)) => v e -> v (Int,e)
indexed a = G.zip (indices a) a
data MM = MM !Double !Double
minMax :: (G.Vector v Double) => v Double -> (Double, Double)
minMax = fini . G.foldl' go (MM (1/0) (1/0))
where
go (MM lo hi) k = MM (min lo k) (max hi k)
fini (MM lo hi) = (lo, hi)
nextHighestPowerOfTwo :: Int -> Int
nextHighestPowerOfTwo n = o + 1
where m = n 1
o = m
.|. (m `shiftR` 1)
.|. (m `shiftR` 2)
.|. (m `shiftR` 4)
.|. (m `shiftR` 8)
.|. (m `shiftR` 16)
#if WORD_SIZE_IN_BITS == 64
.|. (m `shiftR` 32)
#endif