module HGeometry.Foldable.Sort
( sortBy
, sort
, sortOnCheap
) where
import Control.Monad.ST
import Data.Ord (comparing)
import qualified Data.Vector.Algorithms.Intro as Intro
import qualified Data.Vector.Generic as Vector
import qualified VectorBuilder.Builder as Builder
import qualified VectorBuilder.MVector as MVectorBuilder
sort :: forall vector f a. ( Foldable f
, Vector.Vector vector a
, Ord a
)
=> f a -> vector a
sort :: forall (vector :: * -> *) (f :: * -> *) a.
(Foldable f, Vector vector a, Ord a) =>
f a -> vector a
sort = (a -> a -> Ordering) -> f a -> vector a
forall (vector :: * -> *) (f :: * -> *) a.
(Foldable f, Vector vector a) =>
(a -> a -> Ordering) -> f a -> vector a
sortBy a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE sort #-}
sortOnCheap :: forall vector f a b. ( Foldable f
, Vector.Vector vector a
, Ord b
)
=> (a -> b) -> f a -> vector a
sortOnCheap :: forall (vector :: * -> *) (f :: * -> *) a b.
(Foldable f, Vector vector a, Ord b) =>
(a -> b) -> f a -> vector a
sortOnCheap a -> b
f = (a -> a -> Ordering) -> f a -> vector a
forall (vector :: * -> *) (f :: * -> *) a.
(Foldable f, Vector vector a) =>
(a -> a -> Ordering) -> f a -> vector a
sortBy ((a -> b) -> a -> a -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing a -> b
f)
{-# INLINE sortOnCheap #-}
sortBy :: forall vector f a. ( Foldable f
, Vector.Vector vector a)
=> (a -> a -> Ordering) -> f a -> vector a
sortBy :: forall (vector :: * -> *) (f :: * -> *) a.
(Foldable f, Vector vector a) =>
(a -> a -> Ordering) -> f a -> vector a
sortBy a -> a -> Ordering
cmp f a
xs = (forall s. ST s (vector a)) -> vector a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (vector a)) -> vector a)
-> (forall s. ST s (vector a)) -> vector a
forall a b. (a -> b) -> a -> b
$ do v <- Builder a -> ST s (Mutable vector s a)
forall (vector :: * -> * -> *) element s.
MVector vector element =>
Builder element -> ST s (vector s element)
MVectorBuilder.build (Builder a -> ST s (Mutable vector s a))
-> Builder a -> ST s (Mutable vector s a)
forall a b. (a -> b) -> a -> b
$ f a -> Builder a
forall (foldable :: * -> *) element.
Foldable foldable =>
foldable element -> Builder element
Builder.foldable f a
xs
Intro.sortBy cmp v
Vector.unsafeFreeze v
{-# INLINABLE sortBy #-}